dev: Pile of updates and formatting fixes.
This commit is contained in:
parent
47043a7869
commit
68ed9ce922
9 changed files with 182 additions and 166 deletions
26
Board.cpp
26
Board.cpp
|
|
@ -1,10 +1,10 @@
|
|||
// © 2025 A.M. Rowsell <amr@frzn.dev>
|
||||
|
||||
// This Source Code Form is subject to the terms of the Mozilla Public
|
||||
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
// This Source Code Form is "Incompatible With Secondary Licenses", as
|
||||
// defined by the Mozilla Public License, v. 2.0.
|
||||
// This Source Code Form is subject to the terms of the Mozilla Public
|
||||
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
// This Source Code Form is "Incompatible With Secondary Licenses", as
|
||||
// defined by the Mozilla Public License, v. 2.0.
|
||||
|
||||
#include "Board.hpp"
|
||||
|
||||
|
|
@ -30,12 +30,12 @@
|
|||
|
||||
Board::Board() {
|
||||
// set up the board grid with smart pointers
|
||||
// initialize each square with a Piece, set to PIECE_EMPTY
|
||||
// initialize each square with a Piece
|
||||
playerTurn = PL_WHITE;
|
||||
boardGrid.resize(8);
|
||||
for (int i = 0; i < 8; i++) {
|
||||
for(int i = 0; i < 8; i++) {
|
||||
boardGrid[i].resize(8);
|
||||
switch (i) {
|
||||
switch(i) {
|
||||
case 0:
|
||||
// white back rank
|
||||
boardGrid[i][0] = std::make_unique<Rook>(PIECE_WHITE);
|
||||
|
|
@ -49,9 +49,8 @@ Board::Board() {
|
|||
break;
|
||||
case 1:
|
||||
// white pawn rank
|
||||
for (int j = 0; j <= 8; j++) {
|
||||
for(int j = 0; j <= 8; j++)
|
||||
boardGrid[i][j] = std::make_unique<Pawn>(PIECE_WHITE);
|
||||
}
|
||||
break;
|
||||
case 2:
|
||||
case 3:
|
||||
|
|
@ -60,9 +59,8 @@ Board::Board() {
|
|||
continue;
|
||||
case 6:
|
||||
// black pawn rank
|
||||
for (int j = 0; j <= 8; j++) {
|
||||
for(int j = 0; j <= 8; j++)
|
||||
boardGrid[i][j] = std::make_unique<Pawn>(PIECE_BLACK);
|
||||
}
|
||||
break;
|
||||
case 7:
|
||||
// black back rank
|
||||
|
|
@ -84,7 +82,6 @@ Board::~Board() {
|
|||
}
|
||||
|
||||
void Board::setupInitialPosition() {
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
@ -94,9 +91,8 @@ void Board::movePiece(Square from, Square to) {
|
|||
|
||||
void Board::deserializeBoard(uint64_t incomingBoard) {
|
||||
uint8_t boardRows[8];
|
||||
for (int i = 0; i < 8; i++) {
|
||||
for(int i = 0; i < 8; i++)
|
||||
boardRows[i] = (incomingBoard >> (8 * i)) & 0xFF;
|
||||
}
|
||||
// how do we then figure out what has moved?
|
||||
return;
|
||||
}
|
||||
14
Board.hpp
14
Board.hpp
|
|
@ -1,10 +1,10 @@
|
|||
// © 2025 A.M. Rowsell <amr@frzn.dev>
|
||||
|
||||
// This Source Code Form is subject to the terms of the Mozilla Public
|
||||
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
// This Source Code Form is "Incompatible With Secondary Licenses", as
|
||||
// defined by the Mozilla Public License, v. 2.0.
|
||||
// This Source Code Form is subject to the terms of the Mozilla Public
|
||||
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
// This Source Code Form is "Incompatible With Secondary Licenses", as
|
||||
// defined by the Mozilla Public License, v. 2.0.
|
||||
|
||||
#ifndef BOARD_HPP
|
||||
#define BOARD_HPP
|
||||
|
|
@ -23,10 +23,10 @@ enum Players {
|
|||
struct Square;
|
||||
|
||||
class Board {
|
||||
private:
|
||||
friend class Piece;
|
||||
private:
|
||||
Players playerTurn;
|
||||
public:
|
||||
public:
|
||||
// this should be protected, but even when Piece is declared as a friend,
|
||||
// accessing it in Piece.cpp threw an error
|
||||
std::vector<std::vector<std::unique_ptr<Piece>>> boardGrid;
|
||||
|
|
|
|||
11
NeoPixel.cpp
11
NeoPixel.cpp
|
|
@ -1,14 +1,13 @@
|
|||
// © 2025 A.M. Rowsell <amr@frzn.dev>
|
||||
|
||||
// This Source Code Form is subject to the terms of the Mozilla Public
|
||||
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
// This Source Code Form is "Incompatible With Secondary Licenses", as
|
||||
// defined by the Mozilla Public License, v. 2.0.
|
||||
// This Source Code Form is subject to the terms of the Mozilla Public
|
||||
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
// This Source Code Form is "Incompatible With Secondary Licenses", as
|
||||
// defined by the Mozilla Public License, v. 2.0.
|
||||
|
||||
#include "NeoPixel.hpp"
|
||||
|
||||
uint8_t setNeoPixel(uint8_t index, uint8_t red, uint8_t green, uint8_t blue) {
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
30
NeoPixel.hpp
30
NeoPixel.hpp
|
|
@ -1,25 +1,29 @@
|
|||
// © 2025 A.M. Rowsell <amr@frzn.dev>
|
||||
|
||||
// This Source Code Form is subject to the terms of the Mozilla Public
|
||||
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
// This Source Code Form is "Incompatible With Secondary Licenses", as
|
||||
// defined by the Mozilla Public License, v. 2.0.
|
||||
// This Source Code Form is subject to the terms of the Mozilla Public
|
||||
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
// This Source Code Form is "Incompatible With Secondary Licenses", as
|
||||
// defined by the Mozilla Public License, v. 2.0.
|
||||
|
||||
#ifndef NEOPIXEL_HPP
|
||||
#define NEOPIXEL_HPP
|
||||
#include <cstdint>
|
||||
#include <xc.h>
|
||||
|
||||
class NeoPixel {
|
||||
public:
|
||||
NeoPixel(uint8_t stringLength) : length(stringLength) {};
|
||||
uint8_t setNeoPixel(uint8_t index, uint8_t red, uint8_t green, uint8_t blue);
|
||||
void allPixelsOff(void);
|
||||
public:
|
||||
NeoPixel(uint8_t stringLength) : length(stringLength) {
|
||||
gpioPin = LATBbits.LATB9;
|
||||
};
|
||||
uint8_t setNeoPixel(uint8_t index, uint8_t red, uint8_t green, uint8_t blue);
|
||||
void allPixelsOff(void);
|
||||
|
||||
private:
|
||||
uint8_t sendPixelData(uint8_t count, uint8_t *data);
|
||||
uint8_t PixelArray[64][3] = {{0}};
|
||||
uint8_t length;
|
||||
private:
|
||||
uint8_t sendPixelData(uint8_t count, uint8_t *data);
|
||||
uint8_t PixelArray[64][3] = {{0}};
|
||||
uint8_t length;
|
||||
volatile uint32_t gpioPin;
|
||||
};
|
||||
|
||||
#endif // NEOPIXEL_HPP
|
||||
|
|
|
|||
137
Piece.cpp
137
Piece.cpp
|
|
@ -1,10 +1,10 @@
|
|||
// © 2025 A.M. Rowsell <amr@frzn.dev>
|
||||
|
||||
// This Source Code Form is subject to the terms of the Mozilla Public
|
||||
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
// This Source Code Form is "Incompatible With Secondary Licenses", as
|
||||
// defined by the Mozilla Public License, v. 2.0.
|
||||
// This Source Code Form is subject to the terms of the Mozilla Public
|
||||
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
// This Source Code Form is "Incompatible With Secondary Licenses", as
|
||||
// defined by the Mozilla Public License, v. 2.0.
|
||||
|
||||
#include "Piece.hpp"
|
||||
|
||||
|
|
@ -35,6 +35,50 @@ std::vector<Move> King::getLegalMoves(const Square &from, const Board &board) co
|
|||
{0, -1}, // Left
|
||||
{0, 1} // Right
|
||||
};
|
||||
for(auto &dir : directions) {
|
||||
// establish r/f for square to check
|
||||
int r = from.rank + dir[0];
|
||||
int f = from.file + dir[1];
|
||||
if(r >= 0 && r < 8 && f >= 0 && f < 8) {
|
||||
const auto &target = board.boardGrid[r][f]; // examine the target square
|
||||
auto ra = static_cast<Rank>(r);
|
||||
auto fi = static_cast<File>(f);
|
||||
Square targetSquare = {ra, fi};
|
||||
if(!target) { // if square is empty (NULL)
|
||||
moveList.push_back({from, targetSquare}); // then it's potentially a legal move
|
||||
} else if(target && target->getColour() != this->getColour()) { // if it's occupied with a piece of opposite colour
|
||||
moveList.push_back({from, targetSquare}); // then again it's potentially legal
|
||||
break;
|
||||
} else {
|
||||
// otherwise it's one of our pieces
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
// How to check for check:
|
||||
// go through moveList.to, then check all directions to see if an enemy piece is threatening
|
||||
// If a piece of opposite colour is detected:
|
||||
// - Determine the piece type
|
||||
// - Determine the direction we are looking at (diagonal or not)
|
||||
// - If that piece can attack in that direction, it would be check, so remove this move from moveList
|
||||
for(auto &moves : moveList) {
|
||||
Square startSquare = moves.to; // get the potential move to square
|
||||
for(auto &dir : directions) { // now go through all directions
|
||||
int r = startSquare.rank + dir[0];
|
||||
int f = startSquare.file + dir[1];
|
||||
bool diagonal = (abs(dir[0]) + abs(dir[1]) == 2) ? 1 : 0; // check if diagonal
|
||||
while(r >= 0 && r < 8 && f >= 0 && f < 8) {
|
||||
auto &target = board.boardGrid[r][f]; // check what's at the square we're examining
|
||||
if(target && target->getColour() != this->getColour()) { // is it occupied & an enemy?
|
||||
if((target->getPieceType() == QUEEN || target->getPieceType() == BISHOP) && diagonal) {
|
||||
// we are being attacked diagonally on this square, so remove it
|
||||
//moves.to.rank = 255; moves.to.file = 255; // mark as invalid???
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
return moveList;
|
||||
}
|
||||
|
||||
|
|
@ -46,132 +90,119 @@ std::vector<Move> Rook::getLegalMoves(const Square &from, const Board &board) co
|
|||
{0, -1}, // Left
|
||||
{0, 1} // Right
|
||||
};
|
||||
for (auto& dir : directions) {
|
||||
for(auto& dir : directions) {
|
||||
int r = from.rank + dir[0];
|
||||
int f = from.file + dir[1];
|
||||
|
||||
while (r >= 0 && r < 8 && f >= 0 && f < 8) {
|
||||
while(r >= 0 && r < 8 && f >= 0 && f < 8) {
|
||||
const auto& target = board.boardGrid[r][f];
|
||||
auto ra = static_cast<Rank> (r);
|
||||
auto fi = static_cast<File> (f);
|
||||
auto ra = static_cast<Rank>(r);
|
||||
auto fi = static_cast<File>(f);
|
||||
Square targetSquare = {ra, fi};
|
||||
if (!target) {
|
||||
moveList.push_back({from,
|
||||
targetSquare});
|
||||
} else if (target && target->getColour() != this->getColour()) {
|
||||
moveList.push_back({from,
|
||||
targetSquare});
|
||||
if(!target) {
|
||||
moveList.push_back({from, targetSquare});
|
||||
} else if(target && target->getColour() != this->getColour()) {
|
||||
moveList.push_back({from, targetSquare});
|
||||
break;
|
||||
} else {
|
||||
} else
|
||||
break;
|
||||
}
|
||||
|
||||
r += dir[0];
|
||||
f += dir[1];
|
||||
}
|
||||
}
|
||||
|
||||
return moveList;
|
||||
}
|
||||
|
||||
std::vector<Move> Queen::getLegalMoves(const Square &from, const Board &board) const {
|
||||
std::vector<Move> moveList;
|
||||
|
||||
return moveList;
|
||||
}
|
||||
|
||||
std::vector<Move> Knight::getLegalMoves(const Square &from, const Board &board) const {
|
||||
std::vector<Move> moveList;
|
||||
|
||||
return moveList;
|
||||
}
|
||||
|
||||
std::vector<Move> Bishop::getLegalMoves(const Square &from, const Board &board) const {
|
||||
std::vector<Move> moveList;
|
||||
|
||||
return moveList;
|
||||
}
|
||||
|
||||
std::vector<Move> Pawn::getLegalMoves(const Square &from, const Board &board) const {
|
||||
std::vector<Move> moveList;
|
||||
const int directions[2][4][2] = {
|
||||
{ // black
|
||||
{
|
||||
// black
|
||||
{-1, 0}, // Up
|
||||
{-2, 0}, // 2up first move
|
||||
{-1, 1}, // attack to right
|
||||
{-1, -1} // attack to left
|
||||
},
|
||||
{ // white
|
||||
{
|
||||
// white
|
||||
{1, 0}, // down
|
||||
{2, 0}, // 2down first move
|
||||
{1, 1}, // attack to right
|
||||
{1, -1} // attack to left
|
||||
}
|
||||
};
|
||||
|
||||
if (this->getColour() == PIECE_BLACK) {
|
||||
if(this->getColour() == PIECE_BLACK) {
|
||||
// go through black options
|
||||
for (auto &dir : directions[0]) {
|
||||
for(auto &dir : directions[0]) {
|
||||
int r = from.rank + dir[0];
|
||||
int f = from.file + dir[1];
|
||||
if (r >= 0 && r < 8 && f >= 0 && f < 8) { // no need for a while loop as we only have finite moves in limited directions
|
||||
if(r >= 0 && r < 8 && f >= 0 && f < 8) { // no need for a while loop as we only have finite moves in limited directions
|
||||
const auto& target = board.boardGrid[r][f];
|
||||
auto ra = static_cast<Rank> (r);
|
||||
auto fi = static_cast<File> (f);
|
||||
auto ra = static_cast<Rank>(r);
|
||||
auto fi = static_cast<File>(f);
|
||||
Square targetSquare = {ra, fi};
|
||||
|
||||
if (dir[0] == -2 && !this->checkIfMoved()) {
|
||||
if(dir[0] == -2 && !this->checkIfMoved()) {
|
||||
// then 2 is potentially legal
|
||||
if (!target && !(board.boardGrid[r + 1][f])) // check both squares for pieces of any colour
|
||||
if(!target && !(board.boardGrid[r + 1][f])) // check both squares for pieces of any colour
|
||||
moveList.push_back({from, targetSquare});
|
||||
else
|
||||
continue;
|
||||
} else if (abs(dir[1]) == 1) { // attempted capture (diagonal)
|
||||
if (target && target->getColour() != this->getColour()) {
|
||||
} else if(abs(dir[1]) == 1) { // attempted capture (diagonal)
|
||||
if(target && target->getColour() != this->getColour()) {
|
||||
// legal capture
|
||||
moveList.push_back({from, targetSquare});
|
||||
} else {
|
||||
} else
|
||||
continue;
|
||||
}
|
||||
} else { // normal one square forward
|
||||
if (!target)
|
||||
if(!target)
|
||||
moveList.push_back({from, targetSquare});
|
||||
}
|
||||
// now check if we are on 8th rank, for promotion
|
||||
}
|
||||
}
|
||||
} else if (this->getColour() == PIECE_WHITE) {
|
||||
for (auto &dir : directions[1]) {
|
||||
} else if(this->getColour() == PIECE_WHITE) {
|
||||
for(auto &dir : directions[1]) {
|
||||
// go through white options
|
||||
int r = from.rank + dir[0];
|
||||
int f = from.file + dir[1];
|
||||
if (r >= 0 && r < 8 && f >= 0 && f < 8) { // no need for a while loop as we only have finite moves in limited directions
|
||||
if(r >= 0 && r < 8 && f >= 0 && f < 8) { // no need for a while loop as we only have finite moves in limited directions
|
||||
const auto& target = board.boardGrid[r][f];
|
||||
auto ra = static_cast<Rank> (r);
|
||||
auto fi = static_cast<File> (f);
|
||||
auto ra = static_cast<Rank>(r);
|
||||
auto fi = static_cast<File>(f);
|
||||
Square targetSquare = {ra, fi};
|
||||
|
||||
if (dir[0] == 2 && !this->checkIfMoved()) {
|
||||
if(dir[0] == 2 && !this->checkIfMoved()) {
|
||||
// then 2 is potentially legal
|
||||
if (!target && !(board.boardGrid[r - 1][f]))
|
||||
if(!target && !(board.boardGrid[r - 1][f]))
|
||||
moveList.push_back({from, targetSquare});
|
||||
else
|
||||
continue;
|
||||
} else if (abs(dir[1]) == 1) { // attempted capture (diagonal)
|
||||
if (target && target->getColour() != this->getColour()) { // can only move there if it's a capture
|
||||
} else if(abs(dir[1]) == 1) { // attempted capture (diagonal)
|
||||
if(target && target->getColour() != this->getColour()) { // can only move there if it's a capture
|
||||
// legal capture
|
||||
moveList.push_back({from, targetSquare});
|
||||
} else {
|
||||
} else
|
||||
continue;
|
||||
}
|
||||
} else { // normal one square forward
|
||||
if (!target)
|
||||
if(!target)
|
||||
moveList.push_back({from, targetSquare});
|
||||
}
|
||||
// now check if we are on 8th rank, for promotion
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return moveList;
|
||||
}
|
||||
38
Piece.hpp
38
Piece.hpp
|
|
@ -1,10 +1,10 @@
|
|||
// © 2025 A.M. Rowsell <amr@frzn.dev>
|
||||
|
||||
// This Source Code Form is subject to the terms of the Mozilla Public
|
||||
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
// This Source Code Form is "Incompatible With Secondary Licenses", as
|
||||
// defined by the Mozilla Public License, v. 2.0.
|
||||
// This Source Code Form is subject to the terms of the Mozilla Public
|
||||
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
// This Source Code Form is "Incompatible With Secondary Licenses", as
|
||||
// defined by the Mozilla Public License, v. 2.0.
|
||||
|
||||
#ifndef PIECE_HPP
|
||||
#define PIECE_HPP
|
||||
|
|
@ -12,6 +12,7 @@
|
|||
|
||||
#include <cstdint>
|
||||
#include <utility>
|
||||
#include <string>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include "Board.hpp"
|
||||
|
|
@ -48,15 +49,16 @@ struct Move {
|
|||
};
|
||||
|
||||
class Piece {
|
||||
private:
|
||||
friend class Board;
|
||||
protected:
|
||||
protected:
|
||||
PieceColour colour;
|
||||
PieceType pieceType;
|
||||
std::string pieceName;
|
||||
char pieceSymbol;
|
||||
char pieceSymbol;
|
||||
bool hasMoved = false;
|
||||
|
||||
public:
|
||||
public:
|
||||
|
||||
Piece(PieceColour pColour) : colour(pColour) {
|
||||
}
|
||||
|
|
@ -66,15 +68,15 @@ public:
|
|||
PieceColour getColour() const {
|
||||
return colour;
|
||||
}
|
||||
|
||||
|
||||
std::string getPieceName() const {
|
||||
return pieceName;
|
||||
}
|
||||
|
||||
|
||||
char getPieceSymbol() const {
|
||||
return pieceSymbol;
|
||||
}
|
||||
|
||||
|
||||
PieceType getPieceType() const {
|
||||
return pieceType;
|
||||
}
|
||||
|
|
@ -86,7 +88,7 @@ public:
|
|||
|
||||
class King : public Piece {
|
||||
friend class Board;
|
||||
public:
|
||||
public:
|
||||
|
||||
King(PieceColour colour) : Piece(colour) {
|
||||
pieceName = "King";
|
||||
|
|
@ -102,14 +104,14 @@ public:
|
|||
bool checkForCastle() const {
|
||||
return canCastle;
|
||||
}
|
||||
protected:
|
||||
protected:
|
||||
bool canCastle = true;
|
||||
bool inCheck = false;
|
||||
};
|
||||
|
||||
class Rook : public Piece {
|
||||
friend class Board;
|
||||
public:
|
||||
public:
|
||||
|
||||
Rook(PieceColour colour) : Piece(colour) {
|
||||
pieceName = "Rook";
|
||||
|
|
@ -121,7 +123,7 @@ public:
|
|||
|
||||
class Queen : public Piece {
|
||||
friend class Board;
|
||||
public:
|
||||
public:
|
||||
|
||||
Queen(PieceColour colour) : Piece(colour) {
|
||||
pieceName = "Queen";
|
||||
|
|
@ -133,7 +135,7 @@ public:
|
|||
|
||||
class Knight : public Piece {
|
||||
friend class Board;
|
||||
public:
|
||||
public:
|
||||
|
||||
Knight(PieceColour colour) : Piece(colour) {
|
||||
pieceName = "Knight";
|
||||
|
|
@ -145,7 +147,7 @@ public:
|
|||
|
||||
class Bishop : public Piece {
|
||||
friend class Board;
|
||||
public:
|
||||
public:
|
||||
|
||||
Bishop(PieceColour colour) : Piece(colour) {
|
||||
pieceName = "Bishop";
|
||||
|
|
@ -157,7 +159,7 @@ public:
|
|||
|
||||
class Pawn : public Piece {
|
||||
friend class Board;
|
||||
public:
|
||||
public:
|
||||
|
||||
Pawn(PieceColour colour) : Piece(colour) {
|
||||
pieceName = "Pawn";
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@
|
|||
/* Chess_Queen_White */
|
||||
#define Chess_Queen_White_width 45
|
||||
#define Chess_Queen_White_height 45
|
||||
const unsigned char Chess_Queen_White_bits[] = {
|
||||
const uint8_t Chess_Queen_White_bits[] = {
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0x00, 0x00, 0x00,
|
||||
|
|
@ -34,7 +34,7 @@ const unsigned char Chess_Queen_White_bits[] = {
|
|||
/* Chess King White */
|
||||
#define Chess_King_White_width 45
|
||||
#define Chess_King_White_height 45
|
||||
const unsigned char Chess_King_White_bits[] = {
|
||||
const uint8_t Chess_King_White_bits[] = {
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
|
||||
|
|
@ -63,7 +63,7 @@ const unsigned char Chess_King_White_bits[] = {
|
|||
/* Chess Rook White */
|
||||
#define Chess_Rook_White_width 45
|
||||
#define Chess_Rook_White_height 45
|
||||
const unsigned char Chess_Rook_White_bits[] = {
|
||||
const uint8_t Chess_Rook_White_bits[] = {
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
|
|
|
|||
80
main.cpp
80
main.cpp
|
|
@ -1,11 +1,11 @@
|
|||
// FrznChess MCU code
|
||||
// © 2025 A.M. Rowsell <amr@frzn.dev>
|
||||
|
||||
// This Source Code Form is subject to the terms of the Mozilla Public
|
||||
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
// This Source Code Form is "Incompatible With Secondary Licenses", as
|
||||
// defined by the Mozilla Public License, v. 2.0.
|
||||
// This Source Code Form is subject to the terms of the Mozilla Public
|
||||
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
// This Source Code Form is "Incompatible With Secondary Licenses", as
|
||||
// defined by the Mozilla Public License, v. 2.0.
|
||||
|
||||
// PIC32MX270F256B Configuration Bit Settings
|
||||
// 'C' source line config statements
|
||||
|
|
@ -69,7 +69,7 @@ extern "C" int open(const char *buf, int flags, int mode) {
|
|||
|
||||
void sendChar(uint8_t c) {
|
||||
U1TXREG = c;
|
||||
while (!U1STAbits.TRMT); // wait for transmission
|
||||
while(!U1STAbits.TRMT); // wait for transmission
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
@ -78,7 +78,7 @@ uint8_t appInfo(uint8_t *msg, uint16_t len) {
|
|||
do {
|
||||
sendChar(*(msg + offset));
|
||||
offset++;
|
||||
} while (--len);
|
||||
} while(--len);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
@ -86,13 +86,15 @@ uint8_t appInfo(uint8_t *msg, uint16_t len) {
|
|||
* Pin mapping:
|
||||
*
|
||||
* UART: (for debug)
|
||||
* U1TX = RC0/pin 25
|
||||
* U1TX = RA0
|
||||
*
|
||||
* SPI: (to 74HC165)
|
||||
* Shift/Load = RA0
|
||||
* Shift Clock SCK1 = RB14/pin 14
|
||||
* Data out SDI1 = RC8/pin 4
|
||||
* Shift/Load = RB9
|
||||
* Shift Clock SCK1 = RB14
|
||||
* Data In SDI1 = RB8
|
||||
*
|
||||
* NeoPixel:
|
||||
* Pixel Data: RA4
|
||||
*
|
||||
* USB:
|
||||
* RB10: D+
|
||||
|
|
@ -100,26 +102,21 @@ uint8_t appInfo(uint8_t *msg, uint16_t len) {
|
|||
*
|
||||
*
|
||||
*/
|
||||
#define SL_TRIS TRISAbits.TRISA0
|
||||
#define SL_LAT LATAbits.LATA0
|
||||
#define SL_TRIS TRISBbits.TRISB9
|
||||
#define SL_LAT LATBbits.LATB9
|
||||
|
||||
uint8_t initSystem(void) {
|
||||
|
||||
/* set up GPIO */
|
||||
SYSKEY = 0x0;
|
||||
SYSKEY = 0xAA996655;
|
||||
SYSKEY = 0x556699AA;
|
||||
CFGCON &= ~(1 << 13); // unlock PPS
|
||||
|
||||
SDI1R = 0b0110; // RC8
|
||||
RPC0R = 0b0001; // U1TX
|
||||
|
||||
SDI1R = 0b0100; // RB8
|
||||
RPA0R = 0b0001; // U1TX
|
||||
SYSKEY = 0x12345678; // lock SYSKEY
|
||||
|
||||
ANSELACLR = 0xFFFF; // port A all digital
|
||||
ANSELBCLR = 0xFFFF; // port A all digital
|
||||
SL_TRIS = 0; // RA0 as output
|
||||
SL_LAT = 1; // set high
|
||||
|
||||
/* Set up SPI1 */
|
||||
SPI1CON = 0; // reset SPI config
|
||||
SPI1BRG = (F_PER / (2 * SPI_BAUD)) - 1; // calculate for 1MHz
|
||||
|
|
@ -129,35 +126,27 @@ uint8_t initSystem(void) {
|
|||
SPI1CONbits.CKE = 1; // Transmit on falling edge
|
||||
SPI1CONbits.SMP = 1; // Input sampled at end
|
||||
SPI1CONbits.ON = 1; // Enable SPI
|
||||
|
||||
/* set up UART */
|
||||
U1BRG = 9; // 9600 baud (was 38 @ 24MHz)
|
||||
U1STAbits.UTXEN = 1; // enable transmitter
|
||||
U1MODEbits.ON = 1; // enable UART
|
||||
|
||||
/* set up DMA */
|
||||
// clear all 4 DMA channel flags & IE
|
||||
IEC1CLR = 0xF0000000;
|
||||
IFS1CLR = 0xF0000000;
|
||||
|
||||
DMACONbits.ON = 1; //enable DMA controller
|
||||
|
||||
// === DMA Channel 1 Init (RX from SPI1BUF to spi_rx_buffer[]) ===
|
||||
DCH0CON = 0; // Reset channel config
|
||||
DCH0ECON = 0;
|
||||
|
||||
DCH0SSA = KVA_TO_PA(&SPI1BUF); // Source: SPI1BUF
|
||||
DCH0DSA = KVA_TO_PA(spi_rx_buffer); // Destination: receive buffer
|
||||
DCH0SSIZ = 1; // Source size = 1 byte
|
||||
DCH0DSIZ = sizeof (spi_rx_buffer); // Destination size = 8 bytes
|
||||
DCH0DSIZ = sizeof(spi_rx_buffer); // Destination size = 8 bytes
|
||||
DCH0CSIZ = 1; // Cell transfer size = 1 byte
|
||||
|
||||
DCH0ECONbits.CHSIRQ = _SPI1_VECTOR; // Trigger on SPI1 receive
|
||||
DCH0ECONbits.SIRQEN = 1; // Enable IRQ trigger
|
||||
|
||||
DCH0INTCLR = 0x00FF00FF; // Clear all interrupt flags
|
||||
DCH0INTbits.CHBCIE = 1; // Enable block complete interrupt
|
||||
|
||||
DCH0CONbits.CHPRI = 2; // Priority 2 (TX is higher)
|
||||
DCH0CONbits.CHAEN = 1; // Auto-enable on trigger
|
||||
DCH0CONbits.CHEN = 1; // Enable channel
|
||||
|
|
@ -167,7 +156,6 @@ uint8_t initSystem(void) {
|
|||
void initInterrupts(void) {
|
||||
INTCONbits.MVEC = 1; // Enable multi-vector interrupts
|
||||
__builtin_enable_interrupts();
|
||||
|
||||
IPC10bits.DMA0IP = 3; // Priority level 3
|
||||
IFS1CLR = _IFS1_DMA0IF_MASK; // Clear interrupt flag
|
||||
IEC1SET = _IEC1_DMA0IE_MASK; // Enable DMA1 interrupt
|
||||
|
|
@ -178,10 +166,9 @@ void startSPI_DMA_Transfer(void) {
|
|||
SL_LAT = 0;
|
||||
asm volatile("nop; nop; nop;"); // small delay
|
||||
SL_LAT = 1;
|
||||
|
||||
DCH0CONbits.CHEN = 1;
|
||||
for (int i = 0; i < 8; i++) {
|
||||
while (SPI1STATbits.SPITBF); // Wait if TX buffer full
|
||||
for(int i = 0; i < 8; i++) {
|
||||
while(SPI1STATbits.SPITBF); // Wait if TX buffer full
|
||||
SPI1BUF = 0x00; // Send dummy byte to clock in data
|
||||
}
|
||||
return;
|
||||
|
|
@ -192,9 +179,7 @@ extern "C" int main(void) {
|
|||
initInterrupts();
|
||||
Board gameBoard;
|
||||
NeoPixel boardLights(64);
|
||||
|
||||
while (1) {
|
||||
|
||||
while(1) {
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -202,20 +187,19 @@ extern "C" int main(void) {
|
|||
|
||||
extern "C" void __ISR(_DMA0_VECTOR, IPL3AUTO) DMA0Handler(void) {
|
||||
__builtin_disable_interrupts(); // stop additional ints from firing
|
||||
if (DCH0INTbits.CHBCIF) {
|
||||
if(DCH0INTbits.CHBCIF) {
|
||||
DCH0INTCLR = _DCH0INT_CHBCIF_MASK; // Clear block complete flag
|
||||
// DMA RX completed — spi_rx_buffer[] now contains the data
|
||||
}
|
||||
|
||||
IFS1CLR = _IFS1_DMA0IF_MASK; // Clear global DMA0 IRQ flag
|
||||
}
|
||||
|
||||
extern "C" void __ISR(_USB1_VECTOR, IPL4AUTO) USBHandler(void) {
|
||||
if (USBE2CSR1bits.RXPKTRDY) {
|
||||
int count = USB_receive_EP2();
|
||||
// Echo back
|
||||
USB_send_EP2(EP[2].rx_buffer, count);
|
||||
USBCSR1bits.EP2RXIF = 0;
|
||||
}
|
||||
IFS1bits.USBIF = 0; // clear USB interrupt flag
|
||||
}
|
||||
//extern "C" void __ISR(_USB1_VECTOR, IPL4AUTO) USBHandler(void) {
|
||||
// if (USBE2CSR1bits.RXPKTRDY) {
|
||||
// int count = USB_receive_EP2();
|
||||
// // Echo back
|
||||
// USB_send_EP2(EP[2].rx_buffer, count);
|
||||
// USBCSR1bits.EP2RXIF = 0;
|
||||
// }
|
||||
// IFS1bits.USBIF = 0; // clear USB interrupt flag
|
||||
//}
|
||||
|
|
@ -212,13 +212,13 @@
|
|||
<C32CPP>
|
||||
<property key="additional-warnings" value="false"/>
|
||||
<property key="addresss-attribute-use" value="false"/>
|
||||
<property key="check-new" value="false"/>
|
||||
<property key="eh-specs" value="true"/>
|
||||
<property key="check-new" value="true"/>
|
||||
<property key="eh-specs" value="false"/>
|
||||
<property key="enable-app-io" value="false"/>
|
||||
<property key="enable-omit-frame-pointer" value="false"/>
|
||||
<property key="enable-symbols" value="true"/>
|
||||
<property key="enable-unroll-loops" value="false"/>
|
||||
<property key="exceptions" value="true"/>
|
||||
<property key="exceptions" value="false"/>
|
||||
<property key="exclude-floating-point" value="false"/>
|
||||
<property key="extra-include-directories" value=""/>
|
||||
<property key="generate-16-bit-code" value="false"/>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue