-
Notifications
You must be signed in to change notification settings - Fork 0
/
ESP32-Chess.ino
251 lines (210 loc) · 6.13 KB
/
ESP32-Chess.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#include "SPI.h"
#include "TFT_eSPI.h"
#include "pieceDefs.h"
#include "Menu.h"
#include "Board.h"
#include <esp_now.h>
#include "WiFi.h"
#include "Connection.h"
#include "states.h"
#include "Buttons.h"
#include "ButtonMap.h"
#include "Engine.h"
#define CONNECT_INTERVAL 1000
#define DRAW_INTERVAL 15 // ~60 FPS
// #define HEARTBEAT_INTERVAL 1000
TFT_eSPI tft = TFT_eSPI();
TFT_eSprite sprite = TFT_eSprite(&tft);
TFT_eSprite tmpSpr = TFT_eSprite(NULL);
TaskHandle_t task_game;
TaskHandle_t task_con;
extern struct_message receive_data;
Board board;
Menu menu;
Connection connection;
Engine engine;
uint8_t color;
uint8_t state = STATE_START;
uint32_t lastDrawTime = 0;
uint32_t connectTime = 0;
// uint32_t heartBeatTime = 0;
// uint8_t heartBeatMisses = 0;
bool yourTurn;
uint8_t cur_x = 3;
uint8_t cur_y = 6;
uint8_t prev_x;
uint8_t prev_y;
bool selected = false;
uint8_t selectedPiece;
uint8_t prevPiece;
bool cannotSelect = false;
// Opponents' last move
uint8_t enemyLastSrc_x;
uint8_t enemyLastSrc_y;
uint8_t enemyLastDest_x;
uint8_t enemyLastDest_y;
uint32_t cannotSelectTime;
void setup() {
Serial.begin(115200);
tft.init();
tft.fillScreen(TFT_BLACK);
// Color depth 8 bit otherwise too much ram is being used
sprite.setColorDepth(8);
sprite.createSprite(240, 240);
tmpSpr.createSprite(30, 30);
tmpSpr.setSwapBytes(true);
xTaskCreatePinnedToCore(
core0_task,
"con",
50000, /* Stack size in words */
NULL, /* Task input parameter */
0, /* Priority of the task */
&task_con, /* Task handle. */
0); /* Core where the task should run */
xTaskCreatePinnedToCore(
core1_task,
"game",
50000, /* Stack size in words */
NULL, /* Task input parameter */
1, /* Priority of the task (HIGHER)*/
&task_game, /* Task handle. */
1); /* Core where the task should run */
pinMode(BUTTON_UP, INPUT_PULLDOWN);
pinMode(BUTTON_DOWN, INPUT_PULLDOWN);
pinMode(BUTTON_RIGHT, INPUT_PULLDOWN);
pinMode(BUTTON_LEFT, INPUT_PULLDOWN);
pinMode(BUTTON_MENU, INPUT_PULLDOWN);
attachInterrupt(BUTTON_DOWN, down_handler, RISING);
attachInterrupt(BUTTON_MENU, menu_handler, RISING);
attachInterrupt(BUTTON_LEFT, left_handler, RISING);
attachInterrupt(BUTTON_RIGHT, right_handler, RISING);
attachInterrupt(BUTTON_UP, up_handler, RISING);
}
void loop() {
}
void core0_task(void *pvParameters) {
(void)pvParameters;
while (1) {
if (millis() - lastDrawTime > DRAW_INTERVAL) {
switch (state) {
case STATE_START:
menu.drawStartMenu();
break;
case STATE_CONNECT:
menu.drawConnectMenu();
break;
case STATE_INGAME:
board.drawBoard();
board.drawTurn();
if (selected) {
board.drawPiece(selectedPiece, cur_x, cur_y);
board.drawCursor(cur_x, cur_y, TFT_GREEN);
} else {
board.drawCursor(cur_x, cur_y, TFT_WHITE);
}
if (cannotSelect) {
board.drawCursor(cur_x, cur_y, TFT_RED);
if (millis() - cannotSelectTime > 400) {
cannotSelect = false;
}
}
board.draw();
break;
}
lastDrawTime = millis();
}
// vTaskDelay(pdMS_TO_TICKS(1));
}
}
void core1_task(void *pvParameters) {
(void)pvParameters;
while (1) {
handleButtons();
switch (state) {
case STATE_CONNECT:
if (!connection.connected) {
connection.init();
break;
}
if (connection.received) {
// Serial.println("Connected!, color is: " + String(color));
connection.received = false;
initGame(receive_data.dest_y == 0 ? 1 : 0);
state = STATE_INGAME;
break;
}
if (connection.delivered) {
// Serial.println("Connected!, color is: " + String(color));
initGame(color);
state = STATE_INGAME;
break;
}
if (millis() - connectTime > CONNECT_INTERVAL) {
// Serial.println("Trying to Connect...");
color = random(0, 2);
connection.sendData(255, 255, 255, color);
connectTime = millis();
}
break;
case STATE_INGAME:
if (connection.received) {
// if other user started a game due to disconnection etc.
if (receive_data.src_x == 255 && receive_data.src_y == 255 && receive_data.dest_x == 255) {
initGame(receive_data.dest_y == 0 ? 1 : 0);
} else {
Serial.println("received");
// TODO: when moved a piece, reset the prevpiece etc **Not necessary anymore I think**
enemyLastSrc_x = 7 - receive_data.src_x;
enemyLastSrc_y = 7 - receive_data.src_y;
enemyLastDest_x = 7 - receive_data.dest_x;
enemyLastDest_y = 7 - receive_data.dest_y;
board.moveOpponentPiece(receive_data.src_x, receive_data.src_y, receive_data.dest_x, receive_data.dest_y);
yourTurn = !yourTurn;
}
connection.received = false; // clear flag
}
break;
}
// if (state == STATE_INGAME && millis() - heartBeatTime > HEARTBEAT_INTERVAL && connection.connected)
// {
// if (!connection.heartBeat())
// {
// heartBeatMisses++;
// if (heartBeatMisses > 5)
// {
// Serial.println("Connection lost");
// connection.connected = false;
// esp_now_deinit();
// WiFi.mode(WIFI_OFF);
// board.resetBoard();
// state = STATE_START;
// cur_x = 3;
// cur_y = 6;
// prev_x = 0;
// prev_y = 0;
// prevPiece = 0;
// selectedPiece = 0;
// selected = false;
// }
// }
// else {
// heartBeatMisses = 0;
// }
// heartBeatTime = millis();
// }
// vTaskDelay(pdMS_TO_TICKS(1));
}
}
void initGame(uint8_t color_) {
color = color_;
board.resetBoard();
board.init(color);
cur_x = 3;
cur_y = 6;
prev_x = 0;
prev_y = 0;
prevPiece = 0;
selectedPiece = 0;
selected = false;
yourTurn = color == 0 ? false : true;
}