diff options
Diffstat (limited to 'puzzle/vault/main.cpp')
-rw-r--r-- | puzzle/vault/main.cpp | 152 |
1 files changed, 89 insertions, 63 deletions
diff --git a/puzzle/vault/main.cpp b/puzzle/vault/main.cpp index 76e5f65..88d5b35 100644 --- a/puzzle/vault/main.cpp +++ b/puzzle/vault/main.cpp @@ -1,6 +1,7 @@ #include <Arduino.h> -#include <Wire.h> #include <TM1637Display.h> +#include "lib/pbdrv/pb-types.h" +#include "lib/pbdrv/pb-mod.h" #define TOTAL_LEVELS 5 #define ROWS 4 @@ -17,36 +18,50 @@ const int ROW_PINS[ROWS] = {7, 6, 5, 4}; const int COL_PINS[COLS] = {10, 9, 8}; const char* validButtons[TOTAL_LEVELS] = {"A2", "B1", "D3", "C2", "C3"}; const char bombCode[] = "1234"; +const uint8_t SEGMENT_MAP[] = { + 0b00111111, // 0 + 0b00000110, // 1 + 0b01011011, // 2 + 0b01001111, // 3 + 0b01100110, // 4 + 0b01101101, // 5 + 0b01111101, // 6 + 0b00000111, // 7 + 0b01111111, // 8 + 0b01101111, // 9 + 0b01110111, // A + 0b01111100, // B + 0b00111001, // C + 0b01011110, // D + 0b01111001, // E + 0b01110001 // F + // Add other letters if needed +}; + +// This array of level codes matches the codes you might display per level. +const char* levelCodes[TOTAL_LEVELS] = {"A1", "B2", "D1", "C3", "A2"}; + + +// Puzzle state +pb_global_state_t puzzleState = PB_GS_NOINIT; TM1637Display display(CLK, DIO); -typedef enum { - STATE_UNINITIALIZED = 0x00, - STATE_RESET = 0x01, - STATE_PLAYING = 0x02, - STATE_SOLVED = 0x03, - STATE_ERROR = 0x04 -} PuzzleState; - -PuzzleState puzzleState = STATE_UNINITIALIZED; int currentLevel = 0; -void requestEvent() { - if (puzzleState == STATE_PLAYING) { - uint8_t responseData[] = HANDSHAKE_SEND; - Wire.write(responseData, sizeof(responseData)); - Serial.println("Handshake response sent."); +void blink_display(int num) { + if (num == 1) { + // Display "1111" with leading zeros shown if necessary + display.showNumberDecEx(1111, 0b11111111, true); + } else if (num == 0) { + // Display "0000" with leading zeros shown if necessary + display.showNumberDecEx(0, 0b11111111, true); } + delay(500); + display.clear(); + delay(500); } -void blink_display(char num) { - while(puzzleState == STATE_UNINITIALIZED || puzzleState == STATE_ERROR) { - display.showNumberDecEx(0, 0b11111111, true); // Display "0000" with all digits on - delay(500); - display.clear(); - delay(500); - } -} void display_final_code(const char* code) { uint8_t segs[4] = {0, 0, 0, 0}; @@ -73,12 +88,10 @@ void check_button_press() { if (strcmp(keyPress, validButtons[currentLevel]) == 0) { currentLevel++; if (currentLevel >= TOTAL_LEVELS) { - puzzleState = STATE_SOLVED; + pb_hook_mod_state_write(PB_GS_SOLVED); Serial.println("Puzzle solved!"); display.showNumberDec(currentLevel + 1, true); digitalWrite(SOLVED_PIN, HIGH); - } else { - puzzleState = STATE_PLAYING; } } else { currentLevel = 0; @@ -102,64 +115,77 @@ void initialize_system() { Serial.println("GPIO and display initialized."); } -void receiveEvent(int howMany) { - if (howMany == 6) { - uint8_t expectedBytes[] = HANDSHAKE_RECEIVED; - uint8_t receivedBytes[6]; - bool match = true; - - for (int i = 0; i < 6; i++) { - receivedBytes[i] = Wire.read(); - if (receivedBytes[i] != expectedBytes[i]) { - match = false; - break; - } - } +void display_code_for_level(int level) { + char code[3] = {0}; // Temp storage for level code + strncpy(code, levelCodes[level], 2); // Copy the level-specific code - if (match) { - Serial.println("Correct handshake data received."); - puzzleState = STATE_PLAYING; - initialize_system(); + uint8_t segs[4] = {0}; // Segments to send to the display + + // Check if the first character is a letter and map it + if (isalpha(code[0])) { + if (code[0] >= 'A' && code[0] <= 'F') { + segs[0] = SEGMENT_MAP[code[0] - 'A' + 10]; // Maps A-F to their segment patterns } else { - Serial.println("Incorrect handshake data received."); - puzzleState = STATE_ERROR; + // Handle unexpected characters or extend SEGMENT_MAP for more letters + segs[0] = 0; // Display nothing for undefined letters } } else { - Serial.print("Received wrong number of bytes: "); - Serial.println(howMany); - puzzleState = STATE_ERROR; + // Assume it's a number and map directly + segs[0] = SEGMENT_MAP[code[0] - '0']; } + + // Check if the second character is a digit and map it + if (isdigit(code[1])) { + segs[1] = SEGMENT_MAP[code[1] - '0']; + } else { + // Handle unexpected characters + segs[1] = 0; // Display nothing for undefined digits + } + + // Set only the first two segments, leave others blank + display.setSegments(segs, 2, 0); // Display on leftmost two digits } +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 pb_hook_ev_main_state_update(pb_global_state_t state){ + Serial.println("WE IN BOISS"); +} + void setup() { Serial.begin(115200); pinMode(SOLVED_PIN, OUTPUT); digitalWrite(SOLVED_PIN, LOW); - display.setBrightness(0x0f); - - Wire.begin(I2C_MODULE_ADDRESS); - Wire.onRequest(requestEvent); - Wire.onReceive(receiveEvent); - - // Initialize display with blinking zeros to indicate no connection or initialization state - blink_display('0'); + display.setBrightness(0x0f); + initialize_system(); } void loop() { switch(puzzleState) { - case STATE_PLAYING: + case PB_GS_PLAYING: + display_code_for_level(currentLevel); check_button_press(); delay(100); break; - case STATE_SOLVED: + case PB_GS_SOLVED: + Serial.println("STATE = PB_GS_SOLVED"); display_final_code(bombCode); digitalWrite(SOLVED_PIN, HIGH); - Serial.println("Final display shown. Puzzle complete."); break; - case STATE_ERROR: - case STATE_UNINITIALIZED: - blink_display('0'); + case PB_GS_NOINIT: + Serial.println("STATE = PB_GS_NOINIT"); + blink_display(0); + break; + case PB_GS_IDLE: + Serial.println("STATE = PB_GS_IDLE"); + blink_display(1); break; } -}
\ No newline at end of file +} |