aboutsummaryrefslogtreecommitdiff
path: root/main/puzzle/neo/arduino-neopuzzle/arduino-neopuzzle.ino
diff options
context:
space:
mode:
authorElwin Hammer <elwinhamer@gmail.com>2024-05-18 23:19:58 +0200
committerElwin Hammer <elwinhamer@gmail.com>2024-05-18 23:19:58 +0200
commit885e34ef906d7d11e83b8fafa0b6f3b3653f44fd (patch)
tree6d07e35bc8191d0070c3a85719e90a948198f01d /main/puzzle/neo/arduino-neopuzzle/arduino-neopuzzle.ino
parent65f139edb2d3cfa055504e04cba266566fdfbf3b (diff)
Converted ESP code to Arduino
Diffstat (limited to 'main/puzzle/neo/arduino-neopuzzle/arduino-neopuzzle.ino')
-rw-r--r--main/puzzle/neo/arduino-neopuzzle/arduino-neopuzzle.ino84
1 files changed, 84 insertions, 0 deletions
diff --git a/main/puzzle/neo/arduino-neopuzzle/arduino-neopuzzle.ino b/main/puzzle/neo/arduino-neopuzzle/arduino-neopuzzle.ino
new file mode 100644
index 0000000..8febdea
--- /dev/null
+++ b/main/puzzle/neo/arduino-neopuzzle/arduino-neopuzzle.ino
@@ -0,0 +1,84 @@
+#include <Wire.h>
+#include <Adafruit_NeoTrellis.h>
+
+#define MATRIX_SIZE 8
+#define INT_PIN 5 // Interrupt pin for the NeoTrellis
+
+enum NeoState {
+ NEO_UNINITIALIZED,
+ NEO_PLAYING,
+ NEO_SOLVED
+};
+
+Adafruit_NeoTrellis trellis;
+NeoState neoState = NEO_UNINITIALIZED;
+
+// Initialize the NeoTrellis matrix
+void initializeNeoMatrix() {
+ if (!trellis.begin()) {
+ Serial.println("Failed to initialize NeoTrellis");
+ while (1); // Hold here if initialization fails
+ }
+
+ // Set all buttons to listen for presses and releases
+ for (int i = 0; i < MATRIX_SIZE; i++) {
+ for (int j = 0; j < MATRIX_SIZE; j++) {
+ trellis.activateKey(i * MATRIX_SIZE + j, SEESAW_KEYPAD_EDGE_RISING, true);
+ trellis.activateKey(i * MATRIX_SIZE + j, SEESAW_KEYPAD_EDGE_FALLING, true);
+ trellis.setPixelColor(i * MATRIX_SIZE + j, 0x000000); // Turn off LED
+ }
+ }
+ trellis.show();
+ neoState = NEO_PLAYING;
+}
+
+// Callback to handle button presses
+void buttonCallback(uint8_t x) {
+ uint8_t i = x / MATRIX_SIZE;
+ uint8_t j = x % MATRIX_SIZE;
+
+ // Toggle the central button and adjacent LEDs
+ toggleAdjacentLEDs(i, j);
+ if (isNeoPuzzleSolved()) {
+ neoState = NEO_SOLVED;
+ Serial.println("The NeoTrellis puzzle is solved!");
+ // Additional actions upon solving the puzzle can go here
+ }
+ trellis.show();
+}
+
+void toggleAdjacentLEDs(int x, int y) {
+ int idx = x * MATRIX_SIZE + y;
+ trellis.setPixelColor(idx, trellis.getPixelColor(idx) ^ 0xFFFFFF); // Toggle LED color
+
+ // Toggle adjacent LEDs
+ if (x > 0) trellis.setPixelColor((x-1) * MATRIX_SIZE + y, trellis.getPixelColor((x-1) * MATRIX_SIZE + y) ^ 0xFFFFFF);
+ if (x < MATRIX_SIZE - 1) trellis.setPixelColor((x+1) * MATRIX_SIZE + y, trellis.getPixelColor((x+1) * MATRIX_SIZE + y) ^ 0xFFFFFF);
+ if (y > 0) trellis.setPixelColor(x * MATRIX_SIZE + (y-1), trellis.getPixelColor(x * MATRIX_SIZE + (y-1)) ^ 0xFFFFFF);
+ if (y < MATRIX_SIZE - 1) trellis.setPixelColor(x * MATRIX_SIZE + (y+1), trellis.getPixelColor(x * MATRIX_SIZE + (y+1)) ^ 0xFFFFFF);
+}
+
+bool isNeoPuzzleSolved() {
+ for (int i = 0; i < MATRIX_SIZE; i++) {
+ for (int j = 0; j < MATRIX_SIZE; j++) {
+ if (trellis.getPixelColor(i * MATRIX_SIZE + j) != 0x000000) return false; // If any LED is on, puzzle is not solved
+ }
+ }
+ return true;
+}
+
+void setup() {
+ Serial.begin(115200);
+ trellis.begin(INT_PIN);
+ trellis.setBrightness(50); // Set brightness of LEDs (0-255)
+ initializeNeoMatrix();
+ trellis.registerCallback(buttonCallback);
+}
+
+void loop() {
+ if (neoState == NEO_PLAYING) {
+ if (trellis.read()) { // If there was a button event
+ trellis.show(); // Update the display
+ }
+ }
+}