diff options
author | Loek Le Blansch <loek@pipeframe.xyz> | 2024-06-24 15:40:53 +0200 |
---|---|---|
committer | Loek Le Blansch <loek@pipeframe.xyz> | 2024-06-24 15:40:53 +0200 |
commit | b4592a9108688610ef4558167194257324cf8b35 (patch) | |
tree | 4ec77c3d210ac81a9a63e6fabbc4afdb85acc6d4 /puzzle/neo | |
parent | 4f2efb94ee445b198040a72948a36b94fc7f0692 (diff) | |
parent | da2128b7957f0a387c5316bd99fc8d8cdaecbabc (diff) |
Merge branch 'prot/neo-puzzle' of github.com:lonkaars/puzzelbox
Diffstat (limited to 'puzzle/neo')
-rw-r--r-- | puzzle/neo/main.cpp | 120 | ||||
-rw-r--r-- | puzzle/neo/makefile | 4 |
2 files changed, 92 insertions, 32 deletions
diff --git a/puzzle/neo/main.cpp b/puzzle/neo/main.cpp index 13a6859..fe4c7e5 100644 --- a/puzzle/neo/main.cpp +++ b/puzzle/neo/main.cpp @@ -1,10 +1,14 @@ #include <Arduino.h> #include <Wire.h> #include <Adafruit_NeoTrellis.h> +#include "lib/pbdrv/pb-types.h" +#include "lib/pbdrv/pb-mod.h" #define MATRIX_SIZE 8 -#define LED_COLOR_ON 0xFFFFFF // Color of the LEDs in ON state +#define LED_COLOR_ON 0x0000FF // Color of the LEDs in ON state #define LED_COLOR_OFF 0x000000 // Color of the LEDs in OFF state +#define LED_COLOR_RED 0xFF0000 // Red color for UNINIT state +#define LED_COLOR_ORANGE 0xFFA500 // Orange color for IDLE state Adafruit_NeoTrellis t_array[MATRIX_SIZE / 4][MATRIX_SIZE / 4] = { {Adafruit_NeoTrellis(0x2E), Adafruit_NeoTrellis(0x2F)}, @@ -15,13 +19,15 @@ Adafruit_MultiTrellis trellis((Adafruit_NeoTrellis *)t_array, MATRIX_SIZE / 4, M bool neoMatrix[MATRIX_SIZE][MATRIX_SIZE]; // To track state of each pixel -enum NeoState { - NEO_UNINITIALIZED, - NEO_PLAYING, - NEO_SOLVED -}; -NeoState neoState = NEO_UNINITIALIZED; +unsigned long previousMillis = 0; +const long interval = 500; // Interval at which to blink (milliseconds) + +bool gamefield = false; +bool ledState = false; + +// Puzzle state +pb_global_state_t puzzleState = PB_GS_NOINIT; void toggleAdjacentLEDs(int x, int y) { for (int dx = -1; dx <= 1; ++dx) { @@ -54,7 +60,7 @@ TrellisCallback buttonCallback(keyEvent evt) { toggleAdjacentLEDs(x, y); trellis.show(); if (isNeoPuzzleSolved()) { - neoState = NEO_SOLVED; + pb_hook_mod_state_write(PB_GS_SOLVED); Serial.println("The NeoTrellis puzzle is solved!"); } } @@ -64,35 +70,89 @@ TrellisCallback buttonCallback(keyEvent evt) { void setup() { Serial.begin(115200); - while (!Serial); // Wait for Serial to be ready - + while (!Serial); // Wait for Serial to be read if (!trellis.begin()) { Serial.println("Failed to initialize NeoTrellis"); - while (1) delay(1); + pb_hook_mod_state_write(PB_GS_NOINIT); } +} - // Initialize the matrix with a checkerboard pattern - bool toggle = false; - for (int i = 0; i < MATRIX_SIZE; i++) { - for (int j = 0; j < MATRIX_SIZE; j++) { - neoMatrix[i][j] = toggle; - toggle = !toggle; - trellis.setPixelColor(i * MATRIX_SIZE + j, neoMatrix[i][j] ? LED_COLOR_ON : LED_COLOR_OFF); +void set_game_field(){ + if (gamefield == false){ + // Initialize the matrix with a checkerboard pattern + bool toggle = false; + for (int i = 0; i < MATRIX_SIZE; i++) { + for (int j = 0; j < MATRIX_SIZE; j++) { + neoMatrix[i][j] = toggle; + toggle = !toggle; + trellis.setPixelColor(i * MATRIX_SIZE + j, neoMatrix[i][j] ? LED_COLOR_ON : LED_COLOR_OFF); + } + toggle = !toggle; + } + trellis.show(); + + // Register the callback for each key + for (int i = 0; i < MATRIX_SIZE * MATRIX_SIZE; i++) { + trellis.activateKey(i, SEESAW_KEYPAD_EDGE_RISING, true); + trellis.activateKey(i, SEESAW_KEYPAD_EDGE_FALLING, true); + trellis.registerCallback(i, buttonCallback); + } + gamefield = true; + } +} + +pb_global_state_t pb_hook_mod_state_read() { + return puzzleState; +} + +void pb_hook_mod_state_write(pb_global_state_t state) { + puzzleState = state; +} + +void flashCorners(uint32_t color) { + unsigned long currentMillis = millis(); + + if (currentMillis - previousMillis >= interval) { + previousMillis = currentMillis; + ledState = !ledState; + + for (int i = 0; i < MATRIX_SIZE / 4; i++) { + for (int j = 0; j < MATRIX_SIZE / 4; j++) { + int baseIndex = (i * 4 * MATRIX_SIZE) + (j * 4); + if (ledState) { + trellis.setPixelColor(baseIndex, color); // Top-left corner + trellis.setPixelColor(baseIndex + 3, color); // Top-right corner + trellis.setPixelColor(baseIndex + 3 * MATRIX_SIZE, color); // Bottom-left corner + trellis.setPixelColor(baseIndex + 3 * MATRIX_SIZE + 3, color); // Bottom-right corner + } else { + trellis.setPixelColor(baseIndex, LED_COLOR_OFF); + trellis.setPixelColor(baseIndex + 3, LED_COLOR_OFF); + trellis.setPixelColor(baseIndex + 3 * MATRIX_SIZE, LED_COLOR_OFF); + trellis.setPixelColor(baseIndex + 3 * MATRIX_SIZE + 3, LED_COLOR_OFF); + } + } } - toggle = !toggle; - } - trellis.show(); - neoState = NEO_PLAYING; - - // Register the callback for each key - for (int i = 0; i < MATRIX_SIZE * MATRIX_SIZE; i++) { - trellis.activateKey(i, SEESAW_KEYPAD_EDGE_RISING, true); - trellis.activateKey(i, SEESAW_KEYPAD_EDGE_FALLING, true); - trellis.registerCallback(i, buttonCallback); + trellis.show(); } } void loop() { - trellis.read(); // Process button events - delay(20); + switch(puzzleState) { + case PB_GS_PLAYING: + set_game_field(); + trellis.read(); // Process button events + delay(20); + break; + case PB_GS_SOLVED: + Serial.println("STATE = PB_GS_SOLVED"); + break; + case PB_GS_NOINIT: + Serial.println("STATE = PB_GS_NOINIT"); + flashCorners(LED_COLOR_RED); + break; + case PB_GS_IDLE: + Serial.println("STATE = PB_GS_IDLE"); + flashCorners(LED_COLOR_ORANGE); + break; +} }
\ No newline at end of file diff --git a/puzzle/neo/makefile b/puzzle/neo/makefile index 26e9157..6b8341b 100644 --- a/puzzle/neo/makefile +++ b/puzzle/neo/makefile @@ -3,6 +3,6 @@ TARGET = $(BUILD_DIR)/main.elf include ../../lazy.mk export SERIAL_PORT ?= /dev/ttyACM0 -flash: upload-main; -upload-main: $(TARGET) +flash: upload; +upload: $(TARGET) |