From 9c255363bb578ada6bb53d15e5ca12fa8b39986b Mon Sep 17 00:00:00 2001 From: Elwin Date: Sat, 22 Jun 2024 17:40:40 +0200 Subject: Freertos support --- puzzle/vault/makefile | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 puzzle/vault/makefile (limited to 'puzzle/vault/makefile') diff --git a/puzzle/vault/makefile b/puzzle/vault/makefile new file mode 100644 index 0000000..509d8e3 --- /dev/null +++ b/puzzle/vault/makefile @@ -0,0 +1,20 @@ +TARGET = $(BUILD_DIR)/main.elf + +include ../../lazy.mk + +export SERIAL_PORT ?= /dev/ttyACM0 +flash: upload-main; +upload-main: $(TARGET) + +test: test_a test_b; + +test_a: + $(MAKE) -C . clean + $(MAKE) -E CMFLAGS+=-D\ CMAKE_CXX_FLAGS=-DTEST_A -C . + $(MAKE) -E SERIAL_PORT=/dev/ttyACM0 -C . flash + +test_b: + $(MAKE) -C . clean + $(MAKE) -E CMFLAGS+=-D\ CMAKE_CXX_FLAGS=-DTEST_B -C . + $(MAKE) -E SERIAL_PORT=/dev/ttyACM1 -C . flash + -- cgit v1.2.3 From 77aba570a3a94cdcf78e82dc0ebbff0c3dde41d1 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Sun, 23 Jun 2024 14:16:41 +0200 Subject: clean up documentation for puzzle modules --- puzzle/dummy/CMakeLists.txt | 3 +-- puzzle/dummy/makefile | 12 ------------ puzzle/neo/CMakeLists.txt | 14 +++----------- puzzle/neo/index.dox | 8 ++++++++ puzzle/neo/makefile | 12 ------------ puzzle/neo/mod.c | 4 ++-- puzzle/readme.md | 17 +++++++---------- puzzle/vault/CMakeLists.txt | 12 ++---------- puzzle/vault/index.dox | 5 +++++ puzzle/vault/makefile | 12 ------------ puzzle/vault/mod.c | 4 ++-- 11 files changed, 30 insertions(+), 73 deletions(-) (limited to 'puzzle/vault/makefile') diff --git a/puzzle/dummy/CMakeLists.txt b/puzzle/dummy/CMakeLists.txt index 0d508ae..63c4449 100644 --- a/puzzle/dummy/CMakeLists.txt +++ b/puzzle/dummy/CMakeLists.txt @@ -17,8 +17,7 @@ set(ARDUINO_BOARD "Arduino Mega or Mega 2560 [avr.mega]") # freertos add_library(freertos_config INTERFACE) target_include_directories(freertos_config SYSTEM INTERFACE .) -# set(FREERTOS_PORT GCC_ATMEGA) # Arduino Uno -set(FREERTOS_PORT GCC_ATMEGA) # Arduino Uno +set(FREERTOS_PORT GCC_ATMEGA) set(FREERTOS_HEAP 4) # used for testing diff --git a/puzzle/dummy/makefile b/puzzle/dummy/makefile index 509d8e3..26e9157 100644 --- a/puzzle/dummy/makefile +++ b/puzzle/dummy/makefile @@ -6,15 +6,3 @@ export SERIAL_PORT ?= /dev/ttyACM0 flash: upload-main; upload-main: $(TARGET) -test: test_a test_b; - -test_a: - $(MAKE) -C . clean - $(MAKE) -E CMFLAGS+=-D\ CMAKE_CXX_FLAGS=-DTEST_A -C . - $(MAKE) -E SERIAL_PORT=/dev/ttyACM0 -C . flash - -test_b: - $(MAKE) -C . clean - $(MAKE) -E CMFLAGS+=-D\ CMAKE_CXX_FLAGS=-DTEST_B -C . - $(MAKE) -E SERIAL_PORT=/dev/ttyACM1 -C . flash - diff --git a/puzzle/neo/CMakeLists.txt b/puzzle/neo/CMakeLists.txt index db3aadc..6c45f13 100644 --- a/puzzle/neo/CMakeLists.txt +++ b/puzzle/neo/CMakeLists.txt @@ -7,26 +7,18 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS 1) # enable debug features set(CMAKE_BUILD_TYPE Debug) add_compile_definitions(DEBUG) -# add_compile_options(-O0) # no optimizations # arduino set(CMAKE_TOOLCHAIN_FILE ${CMAKE_SOURCE_DIR}/lib/Arduino-CMake-Toolchain/Arduino-toolchain.cmake) -# set(ARDUINO_BOARD "Arduino Uno [avr.uno]") set(ARDUINO_BOARD "Arduino Mega or Mega 2560 [avr.mega]") # freertos add_library(freertos_config INTERFACE) target_include_directories(freertos_config SYSTEM INTERFACE .) -# set(FREERTOS_PORT GCC_ATMEGA) # Arduino Uno -set(FREERTOS_PORT GCC_ATMEGA) # Arduino Uno +set(FREERTOS_PORT GCC_ATMEGA) set(FREERTOS_HEAP 4) -# used for testing -# set(ARDUINO_BOARD "Raspberry Pi Pico W [rp2040.rpipicow]") -# add_compile_definitions(USE_TINYUSB) -# include_directories(/home/loek/.arduino15/packages/rp2040/hardware/rp2040/3.9.2/libraries/Adafruit_TinyUSB_Arduino/src/arduino) - -project(pb_mod_dummy C CXX) +project(pb_mod_neo C CXX) add_subdirectory(lib/pbdrv) add_subdirectory(lib/FreeRTOS-Kernel) @@ -45,7 +37,7 @@ target_link_arduino_libraries(main PUBLIC Adafruit_seesaw ) -#Adafruit workarround +# fugly workaround target_link_arduino_libraries(_arduino_lib_Adafruit_seesaw_Library PUBLIC "Adafruit BusIO") target_enable_arduino_upload(main) diff --git a/puzzle/neo/index.dox b/puzzle/neo/index.dox index 14cefdc..87822e0 100644 --- a/puzzle/neo/index.dox +++ b/puzzle/neo/index.dox @@ -3,4 +3,12 @@ \ingroup puz \defgroup puz_neo Neo \brief NeoTrellis puzzle module + +\par Setup +- Use the Arduino IDE library manager to install the "Adafruit seesaw Library" + library and its dependencies + +\warning There is another library named "Adafruit NeoTrellis M4 Library", this +is not the right library. + */ diff --git a/puzzle/neo/makefile b/puzzle/neo/makefile index 509d8e3..26e9157 100644 --- a/puzzle/neo/makefile +++ b/puzzle/neo/makefile @@ -6,15 +6,3 @@ export SERIAL_PORT ?= /dev/ttyACM0 flash: upload-main; upload-main: $(TARGET) -test: test_a test_b; - -test_a: - $(MAKE) -C . clean - $(MAKE) -E CMFLAGS+=-D\ CMAKE_CXX_FLAGS=-DTEST_A -C . - $(MAKE) -E SERIAL_PORT=/dev/ttyACM0 -C . flash - -test_b: - $(MAKE) -C . clean - $(MAKE) -E CMFLAGS+=-D\ CMAKE_CXX_FLAGS=-DTEST_B -C . - $(MAKE) -E SERIAL_PORT=/dev/ttyACM1 -C . flash - diff --git a/puzzle/neo/mod.c b/puzzle/neo/mod.c index 058a585..7157d22 100644 --- a/puzzle/neo/mod.c +++ b/puzzle/neo/mod.c @@ -1,6 +1,6 @@ #include "pb.h" #include "pb-mod.h" -const char * PB_MOD_NAME = "dummy"; -const i2c_addr_t PB_MOD_ADDR = PB_ADDR_MOD_DUMMY; +const char * PB_MOD_NAME = "neotrellis"; +const i2c_addr_t PB_MOD_ADDR = PB_ADDR_MOD_NEOTRELLIS; diff --git a/puzzle/readme.md b/puzzle/readme.md index 21cddd3..1b572ba 100644 --- a/puzzle/readme.md +++ b/puzzle/readme.md @@ -1,29 +1,24 @@ \defgroup puz puzzle \brief Puzzle modules -# puzzles - -This folder contains the source code for all puzzle modules. - ## Arduino-based puzzle modules > [!NOTE] > Because of the poorly designed hardware (21-22) used during development > (23-24), all puzzle modules ended up being developed using Arduino boards. -All libraries in this repository use CMake for building (for consistency), -which also means the Arduino based puzzle modules use CMake. The CMakeLists.txt -of some puzzles uses the [Arduino-CMake-Toolchain][arduino-cmake]. To build any -of these subfolders, make sure you have done the following: +The Arduino based puzzle modules also use CMake with the +[Arduino-CMake-Toolchain][arduino-cmake]. To build any of these puzzles, make +sure you have done the following: - Install the official Arduino IDE - Open "Tools" > "Board" > "Board manager" - Install the "Arduino AVR Boards" package (1.8.6 works at the time of writing) -- Install the Adafruit seesaw library and dependicies (for the neotrellis puzzle) -- Install the TM1637 library from Avishay Orpaz [arduino-cmake]: https://github.com/a9183756-gh/Arduino-CMake-Toolchain + + diff --git a/puzzle/vault/CMakeLists.txt b/puzzle/vault/CMakeLists.txt index b0f05f2..bc13c8c 100644 --- a/puzzle/vault/CMakeLists.txt +++ b/puzzle/vault/CMakeLists.txt @@ -7,26 +7,18 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS 1) # enable debug features set(CMAKE_BUILD_TYPE Debug) add_compile_definitions(DEBUG) -# add_compile_options(-O0) # no optimizations # arduino set(CMAKE_TOOLCHAIN_FILE ${CMAKE_SOURCE_DIR}/lib/Arduino-CMake-Toolchain/Arduino-toolchain.cmake) -# set(ARDUINO_BOARD "Arduino Uno [avr.uno]") set(ARDUINO_BOARD "Arduino Mega or Mega 2560 [avr.mega]") # freertos add_library(freertos_config INTERFACE) target_include_directories(freertos_config SYSTEM INTERFACE .) -# set(FREERTOS_PORT GCC_ATMEGA) # Arduino Uno -set(FREERTOS_PORT GCC_ATMEGA) # Arduino Uno +set(FREERTOS_PORT GCC_ATMEGA) set(FREERTOS_HEAP 4) -# used for testing -# set(ARDUINO_BOARD "Raspberry Pi Pico W [rp2040.rpipicow]") -# add_compile_definitions(USE_TINYUSB) -# include_directories(/home/loek/.arduino15/packages/rp2040/hardware/rp2040/3.9.2/libraries/Adafruit_TinyUSB_Arduino/src/arduino) - -project(pb_mod_dummy C CXX) +project(pb_mod_vault C CXX) add_subdirectory(lib/pbdrv) add_subdirectory(lib/FreeRTOS-Kernel) diff --git a/puzzle/vault/index.dox b/puzzle/vault/index.dox index b14e616..15eae1f 100644 --- a/puzzle/vault/index.dox +++ b/puzzle/vault/index.dox @@ -3,4 +3,9 @@ \ingroup puz \defgroup puz_vault Vault \brief Vault puzzle module + +\par Setup +- Use the Arduino IDE library manager to install the "TM1637" library (by + Avishay Orpaz) and its dependencies + */ diff --git a/puzzle/vault/makefile b/puzzle/vault/makefile index 509d8e3..26e9157 100644 --- a/puzzle/vault/makefile +++ b/puzzle/vault/makefile @@ -6,15 +6,3 @@ export SERIAL_PORT ?= /dev/ttyACM0 flash: upload-main; upload-main: $(TARGET) -test: test_a test_b; - -test_a: - $(MAKE) -C . clean - $(MAKE) -E CMFLAGS+=-D\ CMAKE_CXX_FLAGS=-DTEST_A -C . - $(MAKE) -E SERIAL_PORT=/dev/ttyACM0 -C . flash - -test_b: - $(MAKE) -C . clean - $(MAKE) -E CMFLAGS+=-D\ CMAKE_CXX_FLAGS=-DTEST_B -C . - $(MAKE) -E SERIAL_PORT=/dev/ttyACM1 -C . flash - diff --git a/puzzle/vault/mod.c b/puzzle/vault/mod.c index 058a585..bae8a3d 100644 --- a/puzzle/vault/mod.c +++ b/puzzle/vault/mod.c @@ -1,6 +1,6 @@ #include "pb.h" #include "pb-mod.h" -const char * PB_MOD_NAME = "dummy"; -const i2c_addr_t PB_MOD_ADDR = PB_ADDR_MOD_DUMMY; +const char * PB_MOD_NAME = "vault"; +const i2c_addr_t PB_MOD_ADDR = PB_ADDR_MOD_VAULT; -- cgit v1.2.3 From ac40ddb49eb3a65cf928b1dd48e79802f2414c2e Mon Sep 17 00:00:00 2001 From: Elwin Date: Mon, 24 Jun 2024 11:33:05 +0200 Subject: cmake change and pbdrv implementation --- puzzle/vault/main.cpp | 79 ++++++++++++++------------------------------------- puzzle/vault/makefile | 2 +- 2 files changed, 23 insertions(+), 58 deletions(-) (limited to 'puzzle/vault/makefile') diff --git a/puzzle/vault/main.cpp b/puzzle/vault/main.cpp index 76e5f65..a553c97 100644 --- a/puzzle/vault/main.cpp +++ b/puzzle/vault/main.cpp @@ -1,6 +1,7 @@ #include -#include #include +#include "lib/pbdrv/pb-types.h" +#include "lib/pbdrv/pb-mod.h" #define TOTAL_LEVELS 5 #define ROWS 4 @@ -18,29 +19,16 @@ const int COL_PINS[COLS] = {10, 9, 8}; const char* validButtons[TOTAL_LEVELS] = {"A2", "B1", "D3", "C2", "C3"}; const char bombCode[] = "1234"; -TM1637Display display(CLK, DIO); -typedef enum { - STATE_UNINITIALIZED = 0x00, - STATE_RESET = 0x01, - STATE_PLAYING = 0x02, - STATE_SOLVED = 0x03, - STATE_ERROR = 0x04 -} PuzzleState; +// Puzzle state +pb_global_state_t puzzleState = PB_GS_NOINIT; -PuzzleState puzzleState = STATE_UNINITIALIZED; -int currentLevel = 0; +TM1637Display display(CLK, DIO); -void requestEvent() { - if (puzzleState == STATE_PLAYING) { - uint8_t responseData[] = HANDSHAKE_SEND; - Wire.write(responseData, sizeof(responseData)); - Serial.println("Handshake response sent."); - } -} +int currentLevel = 0; void blink_display(char num) { - while(puzzleState == STATE_UNINITIALIZED || puzzleState == STATE_ERROR) { + while(puzzleState == PB_GS_NOINIT) { display.showNumberDecEx(0, 0b11111111, true); // Display "0000" with all digits on delay(500); display.clear(); @@ -73,12 +61,12 @@ void check_button_press() { if (strcmp(keyPress, validButtons[currentLevel]) == 0) { currentLevel++; if (currentLevel >= TOTAL_LEVELS) { - puzzleState = STATE_SOLVED; + puzzleState = PB_GS_SOLVED; Serial.println("Puzzle solved!"); display.showNumberDec(currentLevel + 1, true); digitalWrite(SOLVED_PIN, HIGH); } else { - puzzleState = STATE_PLAYING; + puzzleState = PB_GS_PLAYING; } } else { currentLevel = 0; @@ -102,35 +90,6 @@ 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; - } - } - - if (match) { - Serial.println("Correct handshake data received."); - puzzleState = STATE_PLAYING; - initialize_system(); - } else { - Serial.println("Incorrect handshake data received."); - puzzleState = STATE_ERROR; - } - } else { - Serial.print("Received wrong number of bytes: "); - Serial.println(howMany); - puzzleState = STATE_ERROR; - } -} - void setup() { Serial.begin(115200); @@ -138,9 +97,12 @@ void setup() { digitalWrite(SOLVED_PIN, LOW); display.setBrightness(0x0f); - Wire.begin(I2C_MODULE_ADDRESS); - Wire.onRequest(requestEvent); - Wire.onReceive(receiveEvent); + //puzzelState = pb_hook_mod_state_read(); + + + ///// TEST + puzzleState = PB_GS_IDLE; + initialize_system(); // Initialize display with blinking zeros to indicate no connection or initialization state blink_display('0'); @@ -148,18 +110,21 @@ void setup() { void loop() { switch(puzzleState) { - case STATE_PLAYING: + case PB_GS_PLAYING: check_button_press(); delay(100); break; - case STATE_SOLVED: + case 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: + case PB_GS_NOINIT: blink_display('0'); break; + case PB_GS_IDLE: + blink_display('1'); + break; } + //puzzelState = pb_hook_mod_state_read(); } \ No newline at end of file diff --git a/puzzle/vault/makefile b/puzzle/vault/makefile index 26e9157..e22f546 100644 --- a/puzzle/vault/makefile +++ b/puzzle/vault/makefile @@ -3,6 +3,6 @@ TARGET = $(BUILD_DIR)/main.elf include ../../lazy.mk export SERIAL_PORT ?= /dev/ttyACM0 -flash: upload-main; +flash: upload; upload-main: $(TARGET) -- cgit v1.2.3 From 8f1fd935e69c5e303466b2be80bff77f556dead5 Mon Sep 17 00:00:00 2001 From: Elwin Date: Mon, 24 Jun 2024 13:17:55 +0200 Subject: Cmake fix and working vault code with pbdrv impl. --- puzzle/vault/main.cpp | 89 +++++++++++++++++++++++++++++++++++++++++---------- puzzle/vault/makefile | 4 +-- 2 files changed, 74 insertions(+), 19 deletions(-) (limited to 'puzzle/vault/makefile') diff --git a/puzzle/vault/main.cpp b/puzzle/vault/main.cpp index a553c97..1a70076 100644 --- a/puzzle/vault/main.cpp +++ b/puzzle/vault/main.cpp @@ -18,6 +18,28 @@ 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 @@ -27,15 +49,20 @@ TM1637Display display(CLK, DIO); int currentLevel = 0; -void blink_display(char num) { - while(puzzleState == PB_GS_NOINIT) { - display.showNumberDecEx(0, 0b11111111, true); // Display "0000" with all digits on - delay(500); - display.clear(); - delay(500); +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 display_final_code(const char* code) { uint8_t segs[4] = {0, 0, 0, 0}; int numDigits = strlen(code); @@ -90,6 +117,37 @@ void initialize_system() { Serial.println("GPIO and display initialized."); } +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 + + 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 { + // Handle unexpected characters or extend SEGMENT_MAP for more letters + segs[0] = 0; // Display nothing for undefined letters + } + } else { + // 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 +} + void setup() { Serial.begin(115200); @@ -97,34 +155,31 @@ void setup() { digitalWrite(SOLVED_PIN, LOW); display.setBrightness(0x0f); - //puzzelState = pb_hook_mod_state_read(); + puzzleState = pb_hook_mod_state_read(); - - ///// TEST - puzzleState = PB_GS_IDLE; initialize_system(); - - // Initialize display with blinking zeros to indicate no connection or initialization state - blink_display('0'); } void loop() { switch(puzzleState) { case PB_GS_PLAYING: + display_code_for_level(currentLevel); check_button_press(); delay(100); break; 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 PB_GS_NOINIT: - blink_display('0'); + Serial.println("STATE = PB_GS_NOINIT"); + blink_display(0); break; case PB_GS_IDLE: - blink_display('1'); + Serial.println("STATE = PB_GS_IDLE"); + blink_display(1); break; } - //puzzelState = pb_hook_mod_state_read(); + puzzleState = pb_hook_mod_state_read(); } \ No newline at end of file diff --git a/puzzle/vault/makefile b/puzzle/vault/makefile index e22f546..f33ca7e 100644 --- a/puzzle/vault/makefile +++ b/puzzle/vault/makefile @@ -2,7 +2,7 @@ TARGET = $(BUILD_DIR)/main.elf include ../../lazy.mk -export SERIAL_PORT ?= /dev/ttyACM0 +export SERIAL_PORT ?= /dev/ttyUSB0 flash: upload; -upload-main: $(TARGET) +upload: $(TARGET) -- cgit v1.2.3