forked from Concept-Bytes/Open-Chess
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathboard_driver.cpp
More file actions
385 lines (342 loc) · 12.7 KB
/
Copy pathboard_driver.cpp
File metadata and controls
385 lines (342 loc) · 12.7 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
#include "board_driver.h"
#include <math.h>
// ---------------------------
// BoardDriver Implementation
// ---------------------------
BoardDriver::BoardDriver() : strip(LED_COUNT, LED_PIN, NEO_GRBW + NEO_KHZ800) {
// Initialize column pins
int tempColPins[NUM_COLS] = COL_PINS;
for (int i = 0; i < NUM_COLS; i++) {
colPins[i] = tempColPins[i];
}
// Initialize row patterns (LSB-first for shift register)
rowPatterns[0] = 0x01; // row 0
rowPatterns[1] = 0x02; // row 1
rowPatterns[2] = 0x04; // row 2
rowPatterns[3] = 0x08; // row 3
rowPatterns[4] = 0x10; // row 4
rowPatterns[5] = 0x20; // row 5
rowPatterns[6] = 0x40; // row 6
rowPatterns[7] = 0x80; // row 7
}
void BoardDriver::begin() {
// Initialize NeoPixel strip
strip.begin();
strip.show(); // turn off all pixels
strip.setBrightness(BRIGHTNESS);
// Setup shift register control pins
pinMode(SER_PIN, OUTPUT);
pinMode(SRCLK_PIN, OUTPUT);
pinMode(RCLK_PIN, OUTPUT);
// Setup column input pins
for (int c = 0; c < NUM_COLS; c++) {
pinMode(colPins[c], INPUT);
}
// Initialize shift register to no row active
loadShiftRegister(0x00);
// Initialize sensor arrays
for (int row = 0; row < 8; row++) {
for (int col = 0; col < 8; col++) {
sensorState[row][col] = false;
sensorPrev[row][col] = false;
}
}
}
void BoardDriver::loadShiftRegister(byte data) {
digitalWrite(RCLK_PIN, LOW);
for (int i = 0; i < 8; i++) {
bool bitVal = (data & (1 << i)) != 0;
digitalWrite(SER_PIN, bitVal ? HIGH : LOW);
digitalWrite(SRCLK_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(SRCLK_PIN, LOW);
delayMicroseconds(10);
}
digitalWrite(RCLK_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(RCLK_PIN, LOW);
}
void BoardDriver::readSensors() {
for (int row = 0; row < 8; row++) {
loadShiftRegister(rowPatterns[row]);
delayMicroseconds(100);
for (int col = 0; col < NUM_COLS; col++) {
int sensorVal = digitalRead(colPins[col]);
// Calibration on this PCB found that physical file 'a' is wired
// to the rightmost shift-register column and file 'h' to the
// leftmost. Flip the column index so the logical (row, col)
// exposed to the engine matches standard chess notation:
// col 0 = file a, col 7 = file h.
sensorState[row][7 - col] = (sensorVal == LOW);
}
}
loadShiftRegister(0x00);
}
bool BoardDriver::getSensorState(int row, int col) {
return sensorState[row][col];
}
bool BoardDriver::getSensorPrev(int row, int col) {
return sensorPrev[row][col];
}
void BoardDriver::updateSensorPrev() {
for (int row = 0; row < 8; row++) {
for (int col = 0; col < 8; col++) {
sensorPrev[row][col] = sensorState[row][col];
}
}
}
int BoardDriver::getPixelIndex(int row, int col) {
// Mirror the col flip applied in readSensors() so LEDs and sensors
// share the same logical coordinate system. Without this, lighting
// (row 0, col 0) [which the engine treats as a1] would land on the
// physical h1 square because the LED strip is wired the same way as
// the sensor matrix.
return (7 - col) * NUM_COLS + (7 - row);
}
void BoardDriver::clearAllLEDs() {
for (int i = 0; i < LED_COUNT; i++) {
strip.setPixelColor(i, 0);
}
strip.show();
}
void BoardDriver::setSquareLED(int row, int col, uint32_t color) {
int pixelIndex = getPixelIndex(row, col);
strip.setPixelColor(pixelIndex, color);
}
void BoardDriver::setSquareLED(int row, int col, uint8_t r, uint8_t g, uint8_t b, uint8_t w) {
int pixelIndex = getPixelIndex(row, col);
strip.setPixelColor(pixelIndex, strip.Color(r, g, b, w));
}
void BoardDriver::showLEDs() {
strip.show();
}
void BoardDriver::highlightSquare(int row, int col, uint32_t color) {
setSquareLED(row, col, color);
showLEDs();
}
void BoardDriver::blinkSquare(int row, int col, int times) {
int pixelIndex = getPixelIndex(row, col);
for (int i = 0; i < times; i++) {
strip.setPixelColor(pixelIndex, strip.Color(0, 0, 0, 255));
strip.show();
delay(200);
strip.setPixelColor(pixelIndex, 0);
strip.show();
delay(200);
}
}
void BoardDriver::breatheRow(int row, uint8_t r, uint8_t g, uint8_t b, uint8_t w) {
// Triangle-wave breathing (no math.h needed). ~1.8 s period, 9 discrete
// brightness steps. Only repaint when the step or the row changes, so we
// avoid hammering the LED strip (each show() briefly disables interrupts).
const unsigned long periodMs = 1800;
float phase = (millis() % periodMs) / (float)periodMs; // 0..1
float d = 2.0f * phase - 1.0f; // -1..1
if (d < 0) d = -d; // 0..1
float tri = 1.0f - d; // 0..1..0
int step = (int)(tri * 8.0f); // 0..8
static int lastStep = -1;
static int lastRow = -1;
if (step == lastStep && row == lastRow) return;
lastStep = step;
lastRow = row;
float k = 0.15f + (step / 8.0f) * 0.85f; // 0.15..1.0
clearAllLEDs();
for (int c = 0; c < 8; c++) {
setSquareLED(row, c, (uint8_t)(r * k), (uint8_t)(g * k),
(uint8_t)(b * k), (uint8_t)(w * k));
}
showLEDs();
}
void BoardDriver::celebrateUntilLifted() {
// Snapshot which squares currently have a piece. We keep firing fireworks
// until any one of them is lifted (occupied -> empty), then stop.
readSensors();
bool occupied[8][8];
for (int r = 0; r < 8; r++)
for (int c = 0; c < 8; c++)
occupied[r][c] = getSensorState(r, c);
while (true) {
fireworkAnimation(); // one ~couple-second burst
// Between bursts, poll the sensors a few times so we react quickly
// to a lifted piece without it having to be lifted during a burst.
for (int i = 0; i < 10; i++) {
readSensors();
for (int r = 0; r < 8; r++)
for (int c = 0; c < 8; c++)
if (occupied[r][c] && !getSensorState(r, c)) {
clearAllLEDs();
showLEDs();
return; // a piece was lifted -> stop celebrating
}
delay(50);
}
}
}
void BoardDriver::fireworkAnimation() {
float centerX = 3.5;
float centerY = 3.5;
// Expansion phase:
for (float radius = 0; radius < 6; radius += 0.5) {
for (int row = 0; row < 8; row++) {
for (int col = 0; col < 8; col++) {
float dx = col - centerX;
float dy = row - centerY;
float dist = sqrt(dx * dx + dy * dy);
int pixelIndex = getPixelIndex(row, col);
if (fabs(dist - radius) < 0.5)
strip.setPixelColor(pixelIndex, strip.Color(0, 0, 0, 255));
else
strip.setPixelColor(pixelIndex, 0);
}
}
strip.show();
delay(100);
}
// Contraction phase:
for (float radius = 6; radius > 0; radius -= 0.5) {
for (int row = 0; row < 8; row++) {
for (int col = 0; col < 8; col++) {
float dx = col - centerX;
float dy = row - centerY;
float dist = sqrt(dx * dx + dy * dy);
int pixelIndex = getPixelIndex(row, col);
if (fabs(dist - radius) < 0.5)
strip.setPixelColor(pixelIndex, strip.Color(0, 0, 0, 255));
else
strip.setPixelColor(pixelIndex, 0);
}
}
strip.show();
delay(100);
}
// Second expansion phase:
for (float radius = 0; radius < 6; radius += 0.5) {
for (int row = 0; row < 8; row++) {
for (int col = 0; col < 8; col++) {
float dx = col - centerX;
float dy = row - centerY;
float dist = sqrt(dx * dx + dy * dy);
int pixelIndex = getPixelIndex(row, col);
if (fabs(dist - radius) < 0.5)
strip.setPixelColor(pixelIndex, strip.Color(0, 0, 0, 255));
else
strip.setPixelColor(pixelIndex, 0);
}
}
strip.show();
delay(100);
}
// Clear all LEDs
clearAllLEDs();
}
void BoardDriver::captureAnimation() {
float centerX = 3.5;
float centerY = 3.5;
// Pulsing outward animation
for (int pulse = 0; pulse < 3; pulse++) {
for (int row = 0; row < 8; row++) {
for (int col = 0; col < 8; col++) {
float dx = col - centerX;
float dy = row - centerY;
float dist = sqrt(dx * dx + dy * dy);
// Create a pulsing effect around the center
float pulseWidth = 1.5 + pulse;
int pixelIndex = getPixelIndex(row, col);
if (dist >= pulseWidth - 0.5 && dist <= pulseWidth + 0.5) {
// Alternate between red and orange for capture effect
uint32_t color = (pulse % 2 == 0)
? strip.Color(255, 0, 0, 0) // Red
: strip.Color(255, 165, 0, 0); // Orange
strip.setPixelColor(pixelIndex, color);
} else {
strip.setPixelColor(pixelIndex, 0);
}
}
}
strip.show();
delay(150);
}
// Clear LEDs
clearAllLEDs();
}
void BoardDriver::promotionAnimation(int col) {
const uint32_t PROMOTION_COLOR = strip.Color(255, 215, 0, 50); // Gold with white
// Column-based waterfall animation
for (int step = 0; step < 16; step++) {
for (int row = 0; row < 8; row++) {
int pixelIndex = getPixelIndex(row, col);
// Create a golden wave moving up and down the column
if ((step + row) % 8 < 4) {
strip.setPixelColor(pixelIndex, PROMOTION_COLOR);
} else {
strip.setPixelColor(pixelIndex, 0);
}
}
strip.show();
delay(100);
}
// Clear the animation
for (int row = 0; row < 8; row++) {
int pixelIndex = getPixelIndex(row, col);
strip.setPixelColor(pixelIndex, 0);
}
strip.show();
}
bool BoardDriver::checkInitialBoard(const char initialBoard[8][8]) {
readSensors();
bool allPresent = true;
for (int row = 0; row < 8; row++) {
for (int col = 0; col < 8; col++) {
if (initialBoard[row][col] != ' ' && !sensorState[row][col]) {
allPresent = false;
}
}
}
return allPresent;
}
void BoardDriver::updateSetupDisplay(const char initialBoard[8][8]) {
// Show squares that still NEED a piece. White-side squares (uppercase
// pieces, rows 0-1) glow white; black-side squares (rows 6-7) glow red.
// As the user places each piece the matching square goes dark, so the
// remaining lit squares form a clear "place piece here" map.
// When all pieces are in place the board goes fully dark, then the
// game-start animation kicks in.
for (int row = 0; row < 8; row++) {
for (int col = 0; col < 8; col++) {
int pixelIndex = getPixelIndex(row, col);
char piece = initialBoard[row][col];
if (piece == ' ' || sensorState[row][col]) {
// Empty starting square OR piece already placed -> off
strip.setPixelColor(pixelIndex, 0);
} else {
// Piece missing on this square -> show hint
bool isWhitePiece = (piece >= 'A' && piece <= 'Z');
if (isWhitePiece) {
strip.setPixelColor(pixelIndex, strip.Color(0, 0, 0, 200)); // white channel
} else {
strip.setPixelColor(pixelIndex, strip.Color(180, 0, 0)); // red
}
}
}
}
strip.show();
}
void BoardDriver::printBoardState(const char initialBoard[8][8]) {
Serial.println("Current Board:");
for (int row = 0; row < 8; row++) {
Serial.print("{ ");
for (int col = 0; col < 8; col++) {
char displayChar = ' ';
if (initialBoard[row][col] != ' ') {
displayChar = sensorState[row][col] ? initialBoard[row][col] : '-';
}
Serial.print("'");
Serial.print(displayChar);
Serial.print("'");
if (col < 7) Serial.print(", ");
}
Serial.println(" },");
}
Serial.println();
}