forked from Concept-Bytes/Open-Chess
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathOpenChess.ino
More file actions
399 lines (350 loc) · 12.4 KB
/
OpenChess.ino
File metadata and controls
399 lines (350 loc) · 12.4 KB
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
#include "board_driver.h"
#include "chess_engine.h"
#include "chess_moves.h"
#include "sensor_test.h"
#include "chess_bot.h"
// Uncomment the next line to enable WiFi features (requires compatible board)
#define ENABLE_WIFI // Currently disabled - RP2040 boards use local mode only
#ifdef ENABLE_WIFI
// Use different WiFi manager based on board type
#if defined(ESP32) || defined(ESP8266)
#include "wifi_manager_esp32.h" // Full WiFi implementation for ESP32/ESP8266
#define WiFiManager WiFiManagerESP32
#elif defined(ARDUINO_SAMD_MKRWIFI1010) || defined(ARDUINO_SAMD_NANO_33_IOT) || defined(ARDUINO_NANO_RP2040_CONNECT)
#include "wifi_manager.h" // Full WiFi implementation for boards with WiFiNINA
#else
#include "wifi_manager_rp2040.h" // Placeholder for RP2040 and other boards
#define WiFiManager WiFiManagerRP2040
#endif
#endif
// ---------------------------
// Game State and Configuration
// ---------------------------
// Game Mode Definitions
enum GameMode {
MODE_SELECTION = 0,
MODE_CHESS_MOVES = 1,
MODE_CHESS_BOT = 2, // Chess vs Bot mode (Medium difficulty)
MODE_GAME_3 = 3, // Black AI Stockfish (Medium difficulty)
MODE_SENSOR_TEST = 4
};
// Global instances
BoardDriver boardDriver;
ChessEngine chessEngine;
ChessMoves chessMoves(&boardDriver, &chessEngine);
SensorTest sensorTest(&boardDriver);
ChessBot chessBot(&boardDriver, &chessEngine, BOT_MEDIUM, true); // Mode 2: Player White, AI Black, Medium
ChessBot chessBot3(&boardDriver, &chessEngine, BOT_MEDIUM, false); // Mode 3: Player Black, AI White, Hard
#ifdef ENABLE_WIFI
WiFiManager wifiManager;
#endif
// Current game state
GameMode currentMode = MODE_SELECTION;
bool modeInitialized = false;
// ---------------------------
// Function Prototypes
// ---------------------------
void showGameSelection();
void handleGameSelection();
void initializeSelectedMode(GameMode mode);
// ---------------------------
// SETUP
// ---------------------------
void setup() {
// Initialize Serial with extended timeout
Serial.begin(9600);
// Wait for Serial to be ready (critical for RP2040)
unsigned long startTime = millis();
while (!Serial && (millis() - startTime < 10000)) {
// Wait up to 10 seconds for Serial connection
delay(100);
}
// Force a delay to ensure Serial is stable
delay(2000);
Serial.println();
Serial.println("================================================");
Serial.println(" OpenChess Starting Up");
Serial.println("================================================");
Serial.println("DEBUG: Serial communication established");
Serial.print("DEBUG: Millis since boot: ");
Serial.println(millis());
// Debug board type detection
Serial.println("DEBUG: Board type detection:");
#if defined(ESP32)
Serial.println(" - Detected: ESP32");
#elif defined(ESP8266)
Serial.println(" - Detected: ESP8266");
#elif defined(ARDUINO_SAMD_MKRWIFI1010)
Serial.println(" - Detected: ARDUINO_SAMD_MKRWIFI1010");
#elif defined(ARDUINO_SAMD_NANO_33_IOT)
Serial.println(" - Detected: ARDUINO_SAMD_NANO_33_IOT");
#elif defined(ARDUINO_NANO_RP2040_CONNECT)
Serial.println(" - Detected: ARDUINO_NANO_RP2040_CONNECT");
#else
Serial.println(" - Detected: Unknown/Other board type");
#endif
// Check which mode is compiled
#ifdef ENABLE_WIFI
Serial.println("DEBUG: Compiled with ENABLE_WIFI defined");
#else
Serial.println("DEBUG: Compiled without ENABLE_WIFI (local mode only)");
#endif
Serial.println("DEBUG: About to initialize board driver...");
// Initialize board driver
boardDriver.begin();
Serial.println("DEBUG: Board driver initialized successfully");
#ifdef ENABLE_WIFI
Serial.println();
Serial.println("=== WiFi Mode Enabled ===");
Serial.println("DEBUG: About to initialize WiFi Manager...");
Serial.println("DEBUG: This will attempt to create Access Point");
// Initialize WiFi Manager
wifiManager.begin();
Serial.println("DEBUG: WiFi Manager initialization completed");
Serial.println("If WiFi AP was created successfully, you should see:");
Serial.println("- Network name: OpenChessBoard");
Serial.println("- Password: chess123");
Serial.println("- Web interface: http://192.168.4.1");
Serial.println("Or place a piece on the board for local selection");
#else
Serial.println();
Serial.println("=== Local Mode Only ===");
Serial.println("WiFi features are disabled in this build");
Serial.println("To enable WiFi: Uncomment #define ENABLE_WIFI and recompile");
#endif
Serial.println();
Serial.println("=== Game Selection Mode ===");
Serial.println("DEBUG: About to show game selection LEDs...");
// Show game selection interface
showGameSelection();
Serial.println("DEBUG: Game selection LEDs should now be visible");
Serial.println("Four white LEDs should be lit in the center of the board:");
Serial.println("Position 1 (3,3): Chess Moves (Human vs Human)");
Serial.println("Position 2 (3,4): Chess Bot (Human vs AI)");
Serial.println("Position 3 (4,3): Black AI Stockfish (Hard)");
Serial.println("Position 4 (4,4): Sensor Test");
Serial.println();
Serial.println("Place any chess piece on a white LED to select that mode");
Serial.println("================================================");
Serial.println(" Setup Complete - Entering Main Loop");
Serial.println("================================================");
}
// ---------------------------
// MAIN LOOP
// ---------------------------
void loop() {
static unsigned long lastDebugPrint = 0;
static bool firstLoop = true;
if (firstLoop) {
Serial.println("DEBUG: Entered main loop - system is running");
firstLoop = false;
}
// Print periodic status every 10 seconds
if (millis() - lastDebugPrint > 10000) {
Serial.print("DEBUG: Loop running, uptime: ");
Serial.print(millis() / 1000);
Serial.println(" seconds");
lastDebugPrint = millis();
}
#ifdef ENABLE_WIFI
// Handle WiFi clients
wifiManager.handleClient();
// Check for pending board edits from WiFi
char editBoard[8][8];
if (wifiManager.getPendingBoardEdit(editBoard)) {
Serial.println("Applying board edit from WiFi interface...");
if (currentMode == MODE_CHESS_MOVES && modeInitialized) {
chessMoves.setBoardState(editBoard);
Serial.println("Board edit applied to Chess Moves mode");
} else if (currentMode == MODE_CHESS_BOT && modeInitialized) {
chessBot.setBoardState(editBoard);
Serial.println("Board edit applied to Chess Bot mode");
} else if (currentMode == MODE_GAME_3 && modeInitialized) {
chessBot3.setBoardState(editBoard);
Serial.println("Board edit applied to Black AI Stockfish mode");
} else {
Serial.println("Warning: Board edit received but no active game mode");
}
wifiManager.clearPendingEdit();
}
// Update board state for WiFi display
static unsigned long lastBoardUpdate = 0;
if (millis() - lastBoardUpdate > 500) { // Update every 500ms
char currentBoard[8][8];
bool boardUpdated = false;
float evaluation = 0.0;
if (currentMode == MODE_CHESS_MOVES && modeInitialized) {
chessMoves.getBoardState(currentBoard);
boardUpdated = true;
} else if (currentMode == MODE_CHESS_BOT && modeInitialized) {
chessBot.getBoardState(currentBoard);
evaluation = chessBot.getEvaluation();
boardUpdated = true;
} else if (currentMode == MODE_GAME_3 && modeInitialized) {
chessBot3.getBoardState(currentBoard);
evaluation = chessBot3.getEvaluation();
boardUpdated = true;
}
if (boardUpdated) {
wifiManager.updateBoardState(currentBoard, evaluation);
}
lastBoardUpdate = millis();
}
// Check for WiFi game selection
int selectedMode = wifiManager.getSelectedGameMode();
if (selectedMode > 0) {
Serial.print("DEBUG: WiFi game selection detected: ");
Serial.println(selectedMode);
switch (selectedMode) {
case 1:
currentMode = MODE_CHESS_MOVES;
break;
case 2:
currentMode = MODE_CHESS_BOT;
break;
case 3:
currentMode = MODE_GAME_3;
break;
case 4:
currentMode = MODE_SENSOR_TEST;
break;
default:
Serial.println("Invalid game mode selected via WiFi");
selectedMode = 0;
break;
}
if (selectedMode > 0) {
modeInitialized = false;
boardDriver.clearAllLEDs();
wifiManager.resetGameSelection();
// Brief confirmation animation
for (int i = 0; i < 3; i++) {
boardDriver.setSquareLED(3, 3, 0, 255, 0, 0); // Green flash
boardDriver.setSquareLED(3, 4, 0, 255, 0, 0);
boardDriver.setSquareLED(4, 3, 0, 255, 0, 0);
boardDriver.setSquareLED(4, 4, 0, 255, 0, 0);
boardDriver.showLEDs();
delay(200);
boardDriver.clearAllLEDs();
delay(200);
}
}
}
#endif
if (currentMode == MODE_SELECTION) {
handleGameSelection();
} else {
static bool modeChangeLogged = false;
if (!modeChangeLogged) {
Serial.print("DEBUG: Mode changed to: ");
Serial.println(currentMode);
modeChangeLogged = true;
}
// Initialize the selected mode if not already done
if (!modeInitialized) {
initializeSelectedMode(currentMode);
modeInitialized = true;
}
// Run the current game mode
switch (currentMode) {
case MODE_CHESS_MOVES:
chessMoves.update();
break;
case MODE_CHESS_BOT:
chessBot.update();
break;
case MODE_GAME_3:
chessBot3.update();
break;
case MODE_SENSOR_TEST:
sensorTest.update();
break;
default:
currentMode = MODE_SELECTION;
modeInitialized = false;
showGameSelection();
break;
}
}
delay(50); // Small delay to prevent overwhelming the system
}
// ---------------------------
// GAME SELECTION FUNCTIONS
// ---------------------------
void showGameSelection() {
// Clear all LEDs first
boardDriver.clearAllLEDs();
// Light up the 4 selector positions in the middle of the board
// Each mode has a different color for easy identification
// Position 1: Chess Moves (row 3, col 3) - Orange
boardDriver.setSquareLED(3, 3, 255, 165, 0);
// Position 2: Chess Bot (row 3, col 4) - White
boardDriver.setSquareLED(3, 4, 0, 0, 0, 255);
// Position 3: Black AI Stockfish (row 4, col 3) - Blue
boardDriver.setSquareLED(4, 3, 0, 0, 255);
// Position 4: Sensor Test (row 4, col 4) - Red
boardDriver.setSquareLED(4, 4, 255, 0, 0);
boardDriver.showLEDs();
}
void handleGameSelection() {
boardDriver.readSensors();
// Check for piece placement on selector squares
if (boardDriver.getSensorState(3, 3)) {
// Chess Moves selected
Serial.println("Chess Moves mode selected!");
currentMode = MODE_CHESS_MOVES;
modeInitialized = false;
boardDriver.clearAllLEDs();
delay(500); // Debounce delay
}
else if (boardDriver.getSensorState(3, 4)) {
// Chess Bot selected
Serial.println("Chess Bot mode selected (Human vs AI)!");
currentMode = MODE_CHESS_BOT;
modeInitialized = false;
boardDriver.clearAllLEDs();
delay(500);
}
else if (boardDriver.getSensorState(4, 3)) {
// Game Mode 3 selected - Black AI Stockfish (Hard)
Serial.println("Game Mode 3 selected (Black AI Stockfish - Hard)!");
currentMode = MODE_GAME_3;
modeInitialized = false;
boardDriver.clearAllLEDs();
delay(500);
}
else if (boardDriver.getSensorState(4, 4)) {
// Sensor Test selected
Serial.println("Sensor Test mode selected!");
currentMode = MODE_SENSOR_TEST;
modeInitialized = false;
boardDriver.clearAllLEDs();
delay(500);
}
delay(100);
}
void initializeSelectedMode(GameMode mode) {
switch (mode) {
case MODE_CHESS_MOVES:
Serial.println("Starting Chess Moves (Human vs Human)...");
chessMoves.begin();
break;
case MODE_CHESS_BOT:
Serial.println("Starting Chess Bot (Player White vs AI Black - Medium)...");
chessBot.begin();
break;
case MODE_SENSOR_TEST:
Serial.println("Starting Sensor Test...");
sensorTest.begin();
break;
case MODE_GAME_3:
Serial.println("Starting Black AI Stockfish (Player Black vs AI White - Hard)...");
chessBot3.begin();
break;
default:
currentMode = MODE_SELECTION;
modeInitialized = false;
showGameSelection();
break;
}
}