From ce54b8cda3f750663f421d4869a3d05116c66e85 Mon Sep 17 00:00:00 2001 From: Mitchell M Date: Sun, 4 Aug 2024 21:47:23 -0500 Subject: [PATCH] step 6 --- main/drivers/CMakeLists.txt | 1 + main/drivers/bottom_half.cpp | 66 ++++++++++++------- main/drivers/bottom_half.h | 5 ++ main/drivers/game_timer.hpp | 21 ++++-- main/drivers/speaker.hpp | 2 +- main/drivers/{wires.hpp => wires.cpp} | 95 ++++++++++++++++++++------- main/drivers/wires.h | 28 ++++++++ main/main.cpp | 10 ++- main/steps/setup_wires.hpp | 78 +++++++++++----------- main/steps/step0.hpp | 2 +- main/steps/step6.hpp | 54 +++++++++++++++ main/steps/wires_puzzle.cpp | 86 ++++++++++++------------ main/steps/wires_puzzle.h | 18 ++--- 13 files changed, 319 insertions(+), 147 deletions(-) rename main/drivers/{wires.hpp => wires.cpp} (51%) create mode 100644 main/drivers/wires.h diff --git a/main/drivers/CMakeLists.txt b/main/drivers/CMakeLists.txt index 989d0e7..5b940b7 100644 --- a/main/drivers/CMakeLists.txt +++ b/main/drivers/CMakeLists.txt @@ -2,6 +2,7 @@ set(SOURCES "TM1640/TM16xx.cpp" "TM1640/TM1640.cpp" "bottom_half.cpp" + "wires.cpp" ) target_sources(${COMPONENT_LIB} PRIVATE ${SOURCES}) diff --git a/main/drivers/bottom_half.cpp b/main/drivers/bottom_half.cpp index f4dd878..b6f93d5 100644 --- a/main/drivers/bottom_half.cpp +++ b/main/drivers/bottom_half.cpp @@ -15,7 +15,7 @@ static uint16_t button_released; static uint8_t touch_released; /// read buffer -static uint8_t read_buf[8]; +static uint8_t buf[8]; void clear_all_pressed_released(void) { keypad_pressed = 0; @@ -146,6 +146,20 @@ uint8_t get_switch_state(uint8_t* switch_flags) { return (uint8_t)(button_state && 0xF); } +bool get_touch_state(void) { + return touch_state; +} +bool get_touch_pressed(void) { + bool return_ = touch_pressed; + touch_pressed = false; + return return_; +} +bool get_touch_released(void) { + bool return_ = touch_released; + touch_released = false; + return return_; +} + static void poll_bottom_task(void *arg); // static void IRAM_ATTR gpio_isr_handler(void* arg) @@ -187,17 +201,17 @@ void init_bottom_half() { static uint8_t receive_delta(void) { uint8_t reg = 1; - read_buf[0] = 0; - ESP_ERROR_CHECK_WITHOUT_ABORT(i2c_master_write_read_device(BOTTOM_I2C_NUM, BOTTOM_I2C_ADDR, ®, 1, read_buf, 1, (100 / portTICK_PERIOD_MS))); - return read_buf[0]; + buf[0] = 0; + ESP_ERROR_CHECK_WITHOUT_ABORT(i2c_master_write_read_device(BOTTOM_I2C_NUM, BOTTOM_I2C_ADDR, ®, 1, buf, 1, (100 / portTICK_PERIOD_MS))); + return buf[0]; } static void receive_keypad(void) { uint8_t reg = 2; - read_buf[0] = 0; - read_buf[1] = 0; - ESP_ERROR_CHECK_WITHOUT_ABORT(i2c_master_write_read_device(BOTTOM_I2C_NUM, BOTTOM_I2C_ADDR, ®, 1, read_buf, 2, (100 / portTICK_PERIOD_MS))); - uint16_t new_keypad_state = read_buf[0] | (read_buf[1] << 8); + buf[0] = 0; + buf[1] = 0; + ESP_ERROR_CHECK_WITHOUT_ABORT(i2c_master_write_read_device(BOTTOM_I2C_NUM, BOTTOM_I2C_ADDR, ®, 1, buf, 2, (100 / portTICK_PERIOD_MS))); + uint16_t new_keypad_state = buf[0] | (buf[1] << 8); uint16_t just_pressed = new_keypad_state & ~keypad_state; keypad_pressed |= just_pressed; @@ -210,11 +224,10 @@ static void receive_keypad(void) { static void receive_button(void) { uint8_t reg = 3; - read_buf[0] = 0; - read_buf[1] = 0; - ESP_ERROR_CHECK_WITHOUT_ABORT(i2c_master_write_read_device(BOTTOM_I2C_NUM, BOTTOM_I2C_ADDR, ®, 1, read_buf, 2, (100 / portTICK_PERIOD_MS))); - uint16_t new_button_state = read_buf[0] | (read_buf[1] << 8); - ESP_LOGI("jjj", "New Button State: %d", new_button_state); + buf[0] = 0; + buf[1] = 0; + ESP_ERROR_CHECK_WITHOUT_ABORT(i2c_master_write_read_device(BOTTOM_I2C_NUM, BOTTOM_I2C_ADDR, ®, 1, buf, 2, (100 / portTICK_PERIOD_MS))); + uint16_t new_button_state = buf[0] | (buf[1] << 8); uint16_t just_pressed = new_button_state & ~button_state; button_pressed |= just_pressed; @@ -225,6 +238,21 @@ static void receive_button(void) { button_state = new_button_state; } +static void receive_touch(void) { + uint8_t reg = 4; + buf[0] = 0; + ESP_ERROR_CHECK_WITHOUT_ABORT(i2c_master_write_read_device(BOTTOM_I2C_NUM, BOTTOM_I2C_ADDR, ®, 1, buf, 1, (100 / portTICK_PERIOD_MS))); + bool new_touch_state = buf[0] != 0; + + bool just_pressed = new_touch_state & !touch_state; + touch_pressed |= just_pressed; + + bool just_released = (!new_touch_state) & touch_state; + touch_released |= just_released; + + touch_state = new_touch_state; +} + static void poll_bottom_task(void *arg) { while (1) { bool new_data = gpio_get_level(BOTTOM_PIN_INTERUPT) == 0; @@ -233,15 +261,9 @@ static void poll_bottom_task(void *arg) { // ESP_LOGI(_TAG, "delta: %d", delta); if (delta == 0) ESP_LOGW(TAG, "delta pin was low, but delta register returned 0"); - if (delta & (1 << DELTA_BIT_KP)) { - receive_keypad(); - // ESP_LOGI(_TAG, "keypad: %d", keypad_state); - } - - if (delta & (1 << DELTA_BIT_BUTTON)) { - receive_button(); - // ESP_LOGI(_TAG, "button: %d", button_state); - } + if (delta & (1 << DELTA_BIT_KP)) receive_keypad(); + if (delta & (1 << DELTA_BIT_BUTTON)) receive_button(); + if (delta & (1 << DELTA_BIT_TOUCH)) receive_touch(); } vTaskDelay(pdMS_TO_TICKS(10)); } diff --git a/main/drivers/bottom_half.h b/main/drivers/bottom_half.h index 6d6360e..e84acf4 100644 --- a/main/drivers/bottom_half.h +++ b/main/drivers/bottom_half.h @@ -11,6 +11,7 @@ #define DELTA_BIT_KP 0 #define DELTA_BIT_BUTTON 1 +#define DELTA_BIT_TOUCH 2 typedef enum { k1 = 0, @@ -60,6 +61,10 @@ bool get_flipped_down_switch(SwitchKey* switch_); bool get_flipped_switch(SwitchKey* switch_); uint8_t get_switch_state(uint8_t* switch_flags); +bool get_touch_state(void); +bool get_touch_pressed(void); +bool get_touch_released(void); + static void poll_bottom_task(void *arg); void init_bottom_half(); diff --git a/main/drivers/game_timer.hpp b/main/drivers/game_timer.hpp index 03d25fb..1d7c36b 100644 --- a/main/drivers/game_timer.hpp +++ b/main/drivers/game_timer.hpp @@ -5,21 +5,30 @@ #include "sseg.hpp" -static uint32_t _time_left; +static bool playing; +static uint32_t time_left; static void game_timer_task(void *arg); uint32_t sat_sub(uint32_t x, uint32_t y); +void start_timer(void) { + playing = true; +} + +void stop_timer(void) { + playing = false; +} + void init_game_timer(void) { xTaskCreate(game_timer_task, "game_timer", 4096, NULL, 10, NULL); } void set_game_time(uint32_t new_time) { - _time_left = new_time; + time_left = new_time; } uint32_t get_game_time() { - return _time_left; + return time_left; } static void game_timer_task(void *arg) @@ -29,8 +38,10 @@ static void game_timer_task(void *arg) const uint32_t frequency = 100; while (1) { vTaskDelayUntil( &lastWakeTime, pdMS_TO_TICKS(frequency)); - _time_left = sat_sub(_time_left, frequency); - set_game_timer(_time_left / 100, 1); + if (playing) { + time_left = sat_sub(time_left, frequency); + set_game_timer(time_left / 100, 1); + } } vTaskDelete(NULL); diff --git a/main/drivers/speaker.hpp b/main/drivers/speaker.hpp index 9a27504..9db7301 100644 --- a/main/drivers/speaker.hpp +++ b/main/drivers/speaker.hpp @@ -54,7 +54,7 @@ esp_err_t play_raw(const char *fp) { i2s_channel_write(tx_chan, write_buf, bytes_read * sizeof(int16_t), &bytes_written, portMAX_DELAY); bytes_read = fread(read_buf, sizeof(uint8_t), AUDIO_BUFFER, fh); for (int i = 0; i < bytes_read; i++) { - write_buf[i] = read_buf[i] << 3; + write_buf[i] = read_buf[i] << 4; } ESP_LOGV(SPEAKER_TAG, "Bytes read: %d", bytes_read); } diff --git a/main/drivers/wires.hpp b/main/drivers/wires.cpp similarity index 51% rename from main/drivers/wires.hpp rename to main/drivers/wires.cpp index b555536..12b88a6 100644 --- a/main/drivers/wires.hpp +++ b/main/drivers/wires.cpp @@ -1,5 +1,4 @@ -#ifndef WIRES_HPP -#define WIRES_HPP +#include "wires.h" #define WIRES_PIN_DELTA GPIO_NUM_2 #define WIRES_I2C_NUM I2C_NUM_1 @@ -8,13 +7,20 @@ #define DELTA_BIT_WIRES 0 #define DELTA_BIT_BUTTON 1 -static const char *WIRES_TAG = "wires"; +static const char *TAG = "wires"; -uint8_t button_state; -uint8_t wires_states; -uint8_t wires_cut; +static bool button_state; +static bool button_pressed; +static bool button_released; + +static uint8_t wires_state; +static uint8_t wires_cut; + +static uint8_t buf[8]; static void poll_wires_task(void *arg); +static void receive_wires(void); +static void receive_button(void); void init_wires(void) { i2c_config_t wires_conf = { @@ -41,35 +47,77 @@ void init_wires(void) { int_pin_conf.pull_up_en = GPIO_PULLUP_ENABLE; gpio_config(&int_pin_conf); + receive_button(); + receive_wires(); + xTaskCreate(poll_wires_task, "poll_wires", 4096, NULL, 10, NULL); } -void strike(char* reason) { - uint8_t reg = 5; - ESP_ERROR_CHECK_WITHOUT_ABORT(i2c_master_write_to_device(WIRES_I2C_NUM, WIRES_I2C_ADDR, ®, 1, (1000 / portTICK_PERIOD_MS))); +uint8_t get_wires(void) { + return wires_state; +} +uint8_t get_cut_wires(void) { + uint8_t return_ = wires_cut; + wires_cut = 0; + return return_; } -/// read buffer -static uint8_t read_buf[8]; +bool get_button(void) { + return button_state; +} +bool get_button_pressed(void) { + bool return_ = button_pressed; + button_pressed = false; + return return_; +} +bool get_button_released(void) { + bool return_ = button_released; + button_released = false; + return return_; +} + +void strike(char* reason) { + uint8_t reg = 6; + ESP_ERROR_CHECK_WITHOUT_ABORT(i2c_master_write_to_device(WIRES_I2C_NUM, WIRES_I2C_ADDR, ®, 1, (100 / portTICK_PERIOD_MS))); +} + +void set_leds(uint8_t led_states) { + buf[0] = 5; // register 5 + buf[1] = led_states; + ESP_ERROR_CHECK_WITHOUT_ABORT(i2c_master_write_to_device(WIRES_I2C_NUM, WIRES_I2C_ADDR, buf, 2, (100 / portTICK_PERIOD_MS))); +} static uint8_t receive_delta(void) { uint8_t reg = 1; - read_buf[0] = 0; - ESP_ERROR_CHECK_WITHOUT_ABORT(i2c_master_write_read_device(WIRES_I2C_NUM, WIRES_I2C_ADDR, ®, 1, read_buf, 1, (100 / portTICK_PERIOD_MS))); - return read_buf[0]; + buf[0] = 0; + ESP_ERROR_CHECK_WITHOUT_ABORT(i2c_master_write_read_device(WIRES_I2C_NUM, WIRES_I2C_ADDR, ®, 1, buf, 1, (100 / portTICK_PERIOD_MS))); + return buf[0]; } static void receive_wires(void) { - uint8_t reg = 1; - read_buf[0] = 0; - ESP_ERROR_CHECK_WITHOUT_ABORT(i2c_master_write_read_device(WIRES_I2C_NUM, WIRES_I2C_ADDR, ®, 1, read_buf, 1, (100 / portTICK_PERIOD_MS))); + uint8_t reg = 2; + buf[0] = 0; + ESP_ERROR_CHECK_WITHOUT_ABORT(i2c_master_write_read_device(WIRES_I2C_NUM, WIRES_I2C_ADDR, ®, 1, buf, 1, (100 / portTICK_PERIOD_MS))); + uint8_t new_wires = buf[0]; + + uint8_t just_cut = ~new_wires & wires_state; + wires_cut |= just_cut; + + wires_state = new_wires; } static void receive_button(void) { - uint8_t reg = 2; - read_buf[0] = 0; - ESP_ERROR_CHECK_WITHOUT_ABORT(i2c_master_write_read_device(WIRES_I2C_NUM, WIRES_I2C_ADDR, ®, 1, read_buf, 1, (100 / portTICK_PERIOD_MS))); - uint8_t new_button = read_buf[0]; + uint8_t reg = 3; + buf[0] = 0; + ESP_ERROR_CHECK_WITHOUT_ABORT(i2c_master_write_read_device(WIRES_I2C_NUM, WIRES_I2C_ADDR, ®, 1, buf, 1, (100 / portTICK_PERIOD_MS))); + bool new_button = buf[0] != 0; + + bool just_pressed = new_button & !button_state; + button_pressed |= just_pressed; + + bool just_released = !new_button & button_state; + button_released |= just_released; + button_state = new_button; } @@ -78,8 +126,7 @@ static void poll_wires_task(void *arg) { bool new_data = gpio_get_level(WIRES_PIN_DELTA) == 0; if (new_data) { uint8_t delta = receive_delta(); - - if (delta == 0) ESP_LOGW(WIRES_TAG, "delta pin was low, but delta register returned 0"); + if (delta == 0) ESP_LOGW(TAG, "delta pin was low, but delta register returned 0"); if (delta & (1 << DELTA_BIT_WIRES)) receive_wires(); if (delta & (1 << DELTA_BIT_BUTTON)) receive_button(); @@ -89,5 +136,3 @@ static void poll_wires_task(void *arg) { vTaskDelete(NULL); } - -#endif /* WIRES_HPP */ \ No newline at end of file diff --git a/main/drivers/wires.h b/main/drivers/wires.h new file mode 100644 index 0000000..a1d6cbf --- /dev/null +++ b/main/drivers/wires.h @@ -0,0 +1,28 @@ +#ifndef WIRES_HPP +#define WIRES_HPP + +#include +#include +#include +#include + +#define WIRES_PIN_DELTA GPIO_NUM_2 +#define WIRES_I2C_NUM I2C_NUM_1 +#define WIRES_I2C_ADDR 125 + +#define DELTA_BIT_WIRES 0 +#define DELTA_BIT_BUTTON 1 + +void init_wires(void); +uint8_t get_wires(void); +uint8_t get_cut_wires(void); + +bool get_button(void); +bool get_button_pressed(void); +bool get_button_released(void); + +void set_leds(uint8_t led_states); + +void strike(char* reason); + +#endif /* WIRES_HPP */ \ No newline at end of file diff --git a/main/main.cpp b/main/main.cpp index 4955884..73d0aa4 100755 --- a/main/main.cpp +++ b/main/main.cpp @@ -5,7 +5,7 @@ #include "driver/uart.h" #include "driver/i2c.h" #include "drivers/tft.hpp" -#include "drivers/wires.hpp" +#include "drivers/wires.h" #include "drivers/bottom_half.h" #include "drivers/sd.hpp" #include "drivers/game_timer.hpp" @@ -36,12 +36,13 @@ extern "C" void app_main(void) { init_sseg(); init_game_timer(); init_wires(); - set_game_time(30000); init_bottom_half(); init_char_lcd(); step0(); + set_game_time(30000); + start_timer(); step1(); step2(); step3(); @@ -49,10 +50,15 @@ extern "C" void app_main(void) { step5(); step6(); + stop_timer(); + ESP_LOGI(TAG, "Bomb has been diffused. Counter-Terrorists win."); + ESP_ERROR_CHECK_WITHOUT_ABORT(play_raw("/sdcard/diffused.pcm")); + // create_demo_ui(); // play_example(); + // deinit_sd(); // set_game_timer(1234, 1); diff --git a/main/steps/setup_wires.hpp b/main/steps/setup_wires.hpp index 08e3591..e215431 100644 --- a/main/steps/setup_wires.hpp +++ b/main/steps/setup_wires.hpp @@ -6,27 +6,46 @@ #include "wires_puzzle.h" #include -void setup_wires(void) { - clear_all_pressed_released(); - lcd_clear(&lcd); - - WiresColor wires[NUM_WIRES]; - load_wires_from_sd_card(wires); +uint8_t wires_state = 0; +void print_wires(WireColor* wires, int editing_idx) { bool cut[NUM_WIRES]; solve_wires(wires, cut); lcd_set_cursor(&lcd, 1, 1); - char wires_string[NUM_WIRES+1] = {0}; - wires_to_string(wires, wires_string); - lcd_print(&lcd, wires_string); + char string_buf[NUM_WIRES+1] = {0}; + wires_to_string(wires, string_buf); + lcd_print(&lcd, string_buf); lcd_set_cursor(&lcd, 1, 2); - cut_to_string(cut, wires_string); - lcd_print(&lcd, wires_string); - lcd_set_cursor(&lcd, 1, 1); + cut_to_string(cut, string_buf); + lcd_print(&lcd, string_buf); + + lcd_set_cursor(&lcd, 1, 3); + wires_state = get_wires(); + for (int i = 0; i < NUM_WIRES; i++) { + if (wires_state & (1<= NUM_WIRES) editing_idx = NUM_WIRES-1; } else if (button == ButtonKey::b2) { // down - wires[editing_idx] = (WiresColor)(((int)(wires[editing_idx]) + (NUM_COLORS-1)) % NUM_COLORS); + wires[editing_idx] = (WireColor)(((int)(wires[editing_idx]) + (NUM_COLORS-1)) % NUM_COLORS); } else if (button == ButtonKey::b3) { // up - wires[editing_idx] = (WiresColor)(((int)(wires[editing_idx]) + 1) % NUM_COLORS); + wires[editing_idx] = (WireColor)(((int)(wires[editing_idx]) + 1) % NUM_COLORS); } save_wires_to_sd_card(wires); - lcd_set_cursor(&lcd, 1, 1); - char wires_string[NUM_WIRES+1] = {0}; - wires_to_string(wires, wires_string); - lcd_print(&lcd, wires_string); - - bool cut[NUM_WIRES]; - solve_wires(wires, cut); - lcd_set_cursor(&lcd, 1, 2); - cut_to_string(cut, wires_string); - lcd_print(&lcd, wires_string); - - lcd_set_cursor(&lcd, editing_idx+1, 1); + print_wires(wires, editing_idx); } + uint8_t new_wires_state = get_wires(); + if (new_wires_state != wires_state) { + print_wires(wires, editing_idx); + } vTaskDelay(pdMS_TO_TICKS(10)); } } diff --git a/main/steps/step0.hpp b/main/steps/step0.hpp index 4525b00..8ac1e9d 100644 --- a/main/steps/step0.hpp +++ b/main/steps/step0.hpp @@ -3,7 +3,7 @@ #include "../drivers/bottom_half.h" #include "../drivers/char_lcd.hpp" -#include "../drivers/wires.hpp" +#include "../drivers/wires.h" #include "setup_wires.hpp" static const char *STEP0_TAG = "step0"; diff --git a/main/steps/step6.hpp b/main/steps/step6.hpp index 92c46c6..6c9ea50 100644 --- a/main/steps/step6.hpp +++ b/main/steps/step6.hpp @@ -1,10 +1,64 @@ #ifndef STEP_6_HPP #define STEP_6_HPP +#include "wires_puzzle.h" +#include "drivers/wires.h" +#include "drivers/bottom_half.h" + static const char *STEP6_TAG = "step6"; +static uint8_t cut_wires = 0; + +static void correct_wire_task(void *arg); + void step6(void) { + get_cut_wires(); + clear_all_pressed_released(); + + WireColor wires[NUM_WIRES]; + load_wires_from_sd_card(wires); + bool solution[NUM_WIRES] = {0}; + solve_wires(wires, solution); + + while (1) { + uint8_t new_cut_wires = get_cut_wires(); + uint8_t just_cut_wires = new_cut_wires & ~cut_wires; + cut_wires |= new_cut_wires; + + for (int i = 0; i < NUM_WIRES; i++) { + if (just_cut_wires & (1<= 6 to be populated with random colors -void generate_new_wires(WiresColor* wires) { +void generate_new_wires(WireColor* wires) { for (int w = 0; w < NUM_WIRES; w++) { // roughly evenly distributed uint32_t rand = esp_random() % NUM_COLORS; - wires[w] = (WiresColor)(rand); + wires[w] = (WireColor)(rand); } bool cuts[NUM_WIRES] = {0}; @@ -121,7 +121,7 @@ void generate_new_wires(WiresColor* wires) { } } -void solve_wires(WiresColor* wires, bool* out_cut) { +void solve_wires(WireColor* wires, bool* out_cut) { // by default, don't cut any wires for (int i = 0; i < NUM_WIRES; i++) { out_cut[i] = false; @@ -142,17 +142,17 @@ void solve_wires(WiresColor* wires, bool* out_cut) { int white_pos_len = 0; for (int i = 0; i < NUM_WIRES; i++) { - if (wires[i] == WiresColor::red) { + if (wires[i] == WireColor::red) { red_pos[red_pos_len++] = i; - } else if (wires[i] == WiresColor::yellow) { + } else if (wires[i] == WireColor::yellow) { yellow_pos[yellow_pos_len++] = i; - } else if (wires[i] == WiresColor::green) { + } else if (wires[i] == WireColor::green) { green_pos[green_pos_len++] = i; - } else if (wires[i] == WiresColor::blue) { + } else if (wires[i] == WireColor::blue) { blue_pos[blue_pos_len++] = i; - } else if (wires[i] == WiresColor::black) { + } else if (wires[i] == WireColor::black) { black_pos[black_pos_len++] = i; - } else if (wires[i] == WiresColor::white) { + } else if (wires[i] == WireColor::white) { white_pos[white_pos_len++] = i; } } @@ -188,18 +188,18 @@ void solve_wires(WiresColor* wires, bool* out_cut) { } // cut the first wire if it is green or white - if (wires[0] == WiresColor::green || wires[0] == WiresColor::white) out_cut[0] = true; + if (wires[0] == WireColor::green || wires[0] == WireColor::white) out_cut[0] = true; // cut blue wires in even positions (odd indexes) for (int i = 1; i < NUM_WIRES; i += 2) { - if (wires[i] == WiresColor::blue) out_cut[i] = true; + if (wires[i] == WireColor::blue) out_cut[i] = true; } // cut black and yellow wires next to black and yellow wires for (int i = 0; i < NUM_WIRES-1; i++) { if ( - (wires[i] == WiresColor::yellow || wires[i] == WiresColor::black) && - (wires[i+1] == WiresColor::yellow || wires[i+1] == WiresColor::black) + (wires[i] == WireColor::yellow || wires[i] == WireColor::black) && + (wires[i+1] == WireColor::yellow || wires[i+1] == WireColor::black) ) { out_cut[i] = true; out_cut[1] = true; @@ -210,10 +210,10 @@ void solve_wires(WiresColor* wires, bool* out_cut) { for (int green_idx = green_pos_len-1; green_idx >= 0; green_idx--) { int pos = green_pos[green_idx]; if ( - wires[pos-1] == WiresColor::yellow || - wires[pos-1] == WiresColor::white || - wires[pos+1] == WiresColor::yellow || - wires[pos+1] == WiresColor::white + wires[pos-1] == WireColor::yellow || + wires[pos-1] == WireColor::white || + wires[pos+1] == WireColor::yellow || + wires[pos+1] == WireColor::white ) { out_cut[pos] = true; break; @@ -221,7 +221,7 @@ void solve_wires(WiresColor* wires, bool* out_cut) { } // cut all white wires if there is a red wire in the 5th position - if (wires[4] == WiresColor::red) { + if (wires[4] == WireColor::red) { for (int white_idx = 0; white_idx < white_pos_len; white_idx++) { out_cut[white_pos[white_idx]] = true; } @@ -268,8 +268,8 @@ void solve_wires(WiresColor* wires, bool* out_cut) { // cut any wire adjacent to both a yellow and blue wire for (int i = 0; i < NUM_WIRES-2; i++) { if ( - (wires[i] == WiresColor::yellow && wires[i+2] == WiresColor::blue) || - (wires[i] == WiresColor::blue && wires[i+2] == WiresColor::white) + (wires[i] == WireColor::yellow && wires[i+2] == WireColor::blue) || + (wires[i] == WireColor::blue && wires[i+2] == WireColor::white) ) { out_cut[i+1] = true; } @@ -281,10 +281,10 @@ void solve_wires(WiresColor* wires, bool* out_cut) { for (int i = 0; i < blue_pos_len; i++) { int pos = blue_pos[i]; if ( - wires[pos-1] == WiresColor::red || - wires[pos-1] == WiresColor::green || - wires[pos+1] == WiresColor::red || - wires[pos+1] == WiresColor::green + wires[pos-1] == WireColor::red || + wires[pos-1] == WireColor::green || + wires[pos+1] == WireColor::red || + wires[pos+1] == WireColor::green ) { out_cut[pos] = false; } @@ -298,10 +298,10 @@ void solve_wires(WiresColor* wires, bool* out_cut) { } // never cut red or black wires in the 4th or 7th positions - if (wires[3] == WiresColor::red || wires[3] == WiresColor::black) { + if (wires[3] == WireColor::red || wires[3] == WireColor::black) { out_cut[3] = false; } - if (wires[6] == WiresColor::red || wires[6] == WiresColor::black) { + if (wires[6] == WireColor::red || wires[6] == WireColor::black) { out_cut[6] = false; } @@ -324,7 +324,7 @@ void solve_wires(WiresColor* wires, bool* out_cut) { } // never cut a blue or green wire in the 8th postion - if (wires[7] == WiresColor::blue || wires[7] == WiresColor::green) { + if (wires[7] == WireColor::blue || wires[7] == WireColor::green) { out_cut[7] = false; } diff --git a/main/steps/wires_puzzle.h b/main/steps/wires_puzzle.h index 57b3fe2..836ab20 100644 --- a/main/steps/wires_puzzle.h +++ b/main/steps/wires_puzzle.h @@ -16,25 +16,25 @@ typedef enum { blue = 3, black = 4, white = 5, -} WiresColor; +} WireColor; -void solve_wires(WiresColor* wires, bool* out_cut); -void generate_new_wires(WiresColor* wires); +void solve_wires(WireColor* wires, bool* out_cut); +void generate_new_wires(WireColor* wires); void cut_to_string(bool* cut, char* out_cut_string); -void wires_to_string(WiresColor* wires, char* out_wires_string); -void string_to_wires(char* wires_string, WiresColor* out_wires); +void wires_to_string(WireColor* wires, char* out_wires_string); +void string_to_wires(char* wires_string, WireColor* out_wires); -void save_wires_to_sd_card(WiresColor* wires); -void load_wires_from_sd_card(WiresColor* out_wires); +void save_wires_to_sd_card(WireColor* wires); +void load_wires_from_sd_card(WireColor* out_wires); /// @brief Fills the array with 6 random WiresColors. /// @param wires the array of len >= 6 to be populated with random colors -void generate_new_wires(WiresColor* wires); +void generate_new_wires(WireColor* wires); /// @brief Solves the wires puzzle, setting `out_cut` according to which wires are cut. /// @param wires the array of len >= 6 as input to the solver /// @param out_cut the output array of len >= 6 for which wires to cut -void solve_wires(WiresColor* wires, bool* out_cut); +void solve_wires(WireColor* wires, bool* out_cut); #endif /* WIRES_PUZZLE_H */ \ No newline at end of file