Mert Us Matthew Hannay Logan Starr
Dependencies: mbed 4DGL-uLCD-SE
Diff: main.cpp
- Revision:
- 13:3c6da0590428
- Parent:
- 12:7ef20deb9d5e
- Child:
- 15:cae96e8b62a3
--- a/main.cpp Fri Dec 02 22:47:38 2022 +0000 +++ b/main.cpp Sat Dec 03 00:06:21 2022 +0000 @@ -3,6 +3,7 @@ #include <vector> uLCD_4DGL uLCD(p28,p27,p30); // serial tx, serial rx, reset pin; +Serial Blue(p13, p14); enum Piece {e, wK, bK, wQ, bQ, wR, bR, wB, bB, wN, bN, w, b}; std::vector<Piece> whitePieces; @@ -39,7 +40,8 @@ //automatic read on RHS operator int (); //index to any switch array style - bool operator[](int index) { + bool operator[](int index) + { return _pins[index]; }; private: @@ -516,8 +518,7 @@ static const int HOVER_COLOR = 0x0000ff; static const int SELECTED_COLOR = 0xff8800; static const int MOVE_COLOR = 0xff00ff; - -<<<<<<< working copy + // gets the pixel coordinates of the top left of the square static pixelCoord getTopLeftOfSquare(boardPos pos) { @@ -572,22 +573,8 @@ }; pixelCoord tl = getTopLeftOfSquare(row, column); uLCD.BLIT(tl.x + 2, tl.y + 2, 12, 12, sprite); -======= - // gets the pixel coordinates of the top left of the square - static pixelCoord getTopLeftOfSquare(boardPos pos) - { - return getTopLeftOfSquare(pos.row, pos.column); - } - static pixelCoord getTopLeftOfSquare(int row, int column) - { - pixelCoord topLeft; - topLeft.x = 16 * column; - topLeft.y = 112 - 16 * row; - return topLeft; ->>>>>>> merge rev } -<<<<<<< working copy static void drawKnight(int row, int column, bool white, bool light) { int X = white ? 0xffffff : 0x000000; @@ -670,128 +657,6 @@ }; pixelCoord tl = getTopLeftOfSquare(row, column); uLCD.BLIT(tl.x + 2, tl.y + 2, 12, 12, sprite); -======= - // piece sprites (12 x 12) - static void drawPawn(int row, int column, bool white, bool light) - { - int X = white ? 0xffffff : 0x000000; - int _ = light ? BOARD_LIGHT_COLOR : BOARD_DARK_COLOR; - int sprite[144] = {_, _, _, _, _, _, _, _, _, _, _, _, - _, _, _, _, _, _, _, _, _, _, _, _, - _, _, _, _, _, _, _, _, _, _, _, _, - _, _, _, _, _, _, _, _, _, _, _, _, - _, _, _, _, _, X, X, _, _, _, _, _, - _, _, _, _, X, X, X, X, _, _, _, _, - _, _, _, _, X, X, X, X, _, _, _, _, - _, _, _, _, _, X, X, _, _, _, _, _, - _, _, _, _, _, X, X, _, _, _, _, _, - _, _, _, _, X, X, X, X, _, _, _, _, - _, _, X, X, X, X, X, X, X, X, _, _, - _, _, X, X, X, X, X, X, X, X, _, _ - }; - pixelCoord tl = getTopLeftOfSquare(row, column); - uLCD.BLIT(tl.x + 2, tl.y + 2, 12, 12, sprite); - } - static void drawRook(int row, int column, bool white, bool light) - { - int X = white ? 0xffffff : 0x000000; - int _ = light ? BOARD_LIGHT_COLOR : BOARD_DARK_COLOR; - int sprite[144] = {X, X, _, X, X, _, _, X, X, _, X, X, - X, X, _, X, X, _, _, X, X, _, X, X, - X, X, X, X, X, X, X, X, X, X, X, X, - X, X, X, X, X, X, X, X, X, X, X, X, - _, X, X, X, _, X, X, _, X, X, X, _, - _, X, X, X, _, X, X, _, X, X, X, _, - _, _, X, X, _, X, X, _, X, X, _, _, - _, _, X, X, _, X, X, _, X, X, _, _, - _, _, X, X, _, X, X, _, X, X, _, _, - _, X, X, X, X, X, X, X, X, X, X, _, - X, X, X, X, X, X, X, X, X, X, X, X, - X, X, X, X, X, X, X, X, X, X, X, X - }; - pixelCoord tl = getTopLeftOfSquare(row, column); - uLCD.BLIT(tl.x + 2, tl.y + 2, 12, 12, sprite); - } - static void drawKnight(int row, int column, bool white, bool light) - { - int X = white ? 0xffffff : 0x000000; - int _ = light ? BOARD_LIGHT_COLOR : BOARD_DARK_COLOR; - int sprite[144] = {_, _, _, _, _, _, _, _, _, _, _, _, - _, _, _, _, _, X, X, _, X, X, _, _, - _, _, _, _, _, X, X, _, X, X, _, _, - _, _, _, X, X, X, X, X, X, _, _, _, - _, _, X, X, X, X, X, _, X, _, _, _, - _, _, X, X, X, X, X, X, X, _, _, _, - _, _, _, _, _, X, X, X, X, _, _, _, - _, _, _, _, X, X, X, X, X, _, _, _, - _, _, _, X, X, X, X, X, X, X, _, _, - _, _, X, X, X, X, X, X, X, X, _, _, - _, X, X, X, X, X, X, X, X, X, X, _, - _, X, X, X, X, X, X, X, X, X, X, _ - }; - pixelCoord tl = getTopLeftOfSquare(row, column); - uLCD.BLIT(tl.x + 2, tl.y + 2, 12, 12, sprite); - } - static void drawBishop(int row, int column, bool white, bool light) - { - int X = white ? 0xffffff : 0x000000; - int _ = light ? BOARD_LIGHT_COLOR : BOARD_DARK_COLOR; - int sprite[144] = {_, _, _, _, _, X, X, _, _, _, _, _, - _, _, _, _, X, X, X, _, _, _, _, _, - _, _, _, X, X, X, _, _, X, _, _, _, - _, _, _, X, X, _, _, X, X, _, _, _, - _, _, _, X, X, X, X, X, X, _, _, _, - _, _, _, _, X, X, X, X, _, _, _, _, - _, _, _, _, _, X, X, _, _, _, _, _, - _, _, _, _, X, X, X, X, _, _, _, _, - _, _, _, X, X, X, X, X, X, _, _, _, - _, _, _, X, X, X, X, X, X, _, _, _, - _, _, X, X, X, X, X, X, X, X, _, _, - _, _, X, X, X, X, X, X, X, X, _, _ - }; - pixelCoord tl = getTopLeftOfSquare(row, column); - uLCD.BLIT(tl.x + 2, tl.y + 2, 12, 12, sprite); - } - static void drawQueen(int row, int column, bool white, bool light) - { - int X = white ? 0xffffff : 0x000000; - int _ = light ? BOARD_LIGHT_COLOR : BOARD_DARK_COLOR; - int sprite[144] = {_, _, _, _, _, X, X, _, _, _, _, _, - _, _, X, _, _, X, X, _, _, X, _, _, - X, _, X, X, _, X, X, _, X, X, _, X, - X, _, X, X, _, X, X, _, X, X, _, X, - X, _, X, X, _, X, X, _, X, X, _, X, - X, X, X, X, X, X, X, X, X, X, X, X, - X, X, X, X, X, X, X, X, X, X, X, X, - X, X, _, X, X, X, X, X, X, _, X, X, - X, X, X, X, _, X, X, _, X, X, X, X, - _, X, X, X, X, X, X, X, X, X, X, _, - _, _, X, X, X, X, X, X, X, X, _, _, - _, X, X, X, X, X, X, X, X, X, X, _ - }; - pixelCoord tl = getTopLeftOfSquare(row, column); - uLCD.BLIT(tl.x + 2, tl.y + 2, 12, 12, sprite); - } - static void drawKing(int row, int column, bool white, bool light) - { - int X = white ? 0xffffff : 0x000000; - int _ = light ? BOARD_LIGHT_COLOR : BOARD_DARK_COLOR; - int sprite[144] = {_, _, _, _, _, X, X, _, _, _, _, _, - _, _, _, _, _, X, X, _, _, _, _, _, - _, _, _, X, X, X, X, X, X, _, _, _, - _, _, _, X, X, X, X, X, X, _, _, _, - X, X, _, _, _, X, X, _, _, _, X, X, - X, X, X, X, _, X, X, _, X, X, X, X, - X, _, X, X, X, X, X, X, X, X, _, X, - X, X, X, X, X, X, X, X, X, X, X, X, - X, X, X, _, X, X, X, X, _, X, X, X, - _, X, X, X, X, X, X, X, X, X, X, _, - _, _, X, X, X, _, _, X, X, X, _, _, - _, X, X, X, X, X, X, X, X, X, X, _ - }; - pixelCoord tl = getTopLeftOfSquare(row, column); - uLCD.BLIT(tl.x + 2, tl.y + 2, 12, 12, sprite); ->>>>>>> merge rev } public: @@ -867,7 +732,6 @@ { boardState.placePiece(piece, row, column); pixelCoord tl = getTopLeftOfSquare(row, column); -<<<<<<< working copy switch(piece) { case wK: case bK: @@ -893,33 +757,7 @@ case b: drawPawn(row, column, piece==w, (row+column)%2); break; -======= - switch(piece) { - case wK: - case bK: - drawKing(row, column, piece==wK, (row+column)%2); - break; - case wQ: - case bQ: - drawQueen(row, column, piece==wQ, (row+column)%2); - break; - case wB: - case bB: - drawBishop(row, column, piece==wB, (row+column)%2); - break; - case wN: - case bN: - drawKnight(row, column, piece==wN, (row+column)%2); - break; - case wR: - case bR: - drawRook(row, column, piece==wR, (row+column)%2); - break; - case w: - case b: - drawPawn(row, column, piece==w, (row+column)%2); - break; ->>>>>>> merge rev + } } @@ -1098,8 +936,7 @@ // transition state state = state == whiteSelecting ? whitePickedUp : blackPickedUp; } else { - selectedPos = (boardPos) - { + selectedPos = (boardPos) { 10, 10 }; } @@ -1129,8 +966,7 @@ gameBoard.unselectSquare(selectedPos); gameBoard.hoverSquare(cursorPos); possibleMoves.clear(); - selectedPos = (boardPos) - { + selectedPos = (boardPos) { 10, 10 }; break; @@ -1142,11 +978,46 @@ } } +// bluetooth +volatile bool button_ready = 0; +volatile int bnum = 0; +volatile int bhit = 0; +enum statetype {start = 0, got_exclm, got_B, got_num, got_hit}; +statetype bluetooth_state = start; + +void parse_message() +{ + switch (bluetooth_state) { + case start: + if (Blue.getc() == '!') bluetooth_state = got_exclm; + else bluetooth_state = start; + break; + case got_exclm: + if (Blue.getc() == 'B') bluetooth_state = got_B; + else bluetooth_state = start; + break; + case got_B: + bnum = Blue.getc(); + bluetooth_state = got_num; + break; + case got_num: + bhit = Blue.getc(); + bluetooth_state = got_hit; + break; + case got_hit: + if (Blue.getc() == char(~('!' + ' B' + bnum + bhit))) button_ready = 1; + bluetooth_state = start; + break; + default: + Blue.getc(); + bluetooth_state = start; + } +} + Nav_Switch myNav(p9, p6, p7, p5, p8); //pin order on Sparkfun breakout int main() { - //gameBoard = GameBoard(); whitePieces.push_back(wK); whitePieces.push_back(wQ); whitePieces.push_back(wB); @@ -1159,7 +1030,10 @@ blackPieces.push_back(bN); blackPieces.push_back(bR); blackPieces.push_back(b); + moveCursor(0, 0); + Blue.attach(&parse_message,Serial::RxIrq); + while (1) { if (myNav.up()) { joyStickUp(); @@ -1171,6 +1045,26 @@ joyStickRight(); } else if (myNav.fire()) { joyStickPressed(); + } else if (button_ready && bhit == '1') { + switch(bnum) { + case '1': + joyStickPressed(); + break; + case '5': + joyStickUp(); + break; + case '6': + joyStickDown(); + break; + case '7': + joyStickLeft(); + break; + case '8': + joyStickRight(); + break; + } + button_ready = false; + } wait(0.25); }