This commit is contained in:
Mitchell Marino 2024-08-04 21:47:23 -05:00
parent 8b41f98659
commit ce54b8cda3
13 changed files with 319 additions and 147 deletions

View File

@ -2,6 +2,7 @@ set(SOURCES
"TM1640/TM16xx.cpp" "TM1640/TM16xx.cpp"
"TM1640/TM1640.cpp" "TM1640/TM1640.cpp"
"bottom_half.cpp" "bottom_half.cpp"
"wires.cpp"
) )
target_sources(${COMPONENT_LIB} PRIVATE ${SOURCES}) target_sources(${COMPONENT_LIB} PRIVATE ${SOURCES})

View File

@ -15,7 +15,7 @@ static uint16_t button_released;
static uint8_t touch_released; static uint8_t touch_released;
/// read buffer /// read buffer
static uint8_t read_buf[8]; static uint8_t buf[8];
void clear_all_pressed_released(void) { void clear_all_pressed_released(void) {
keypad_pressed = 0; keypad_pressed = 0;
@ -146,6 +146,20 @@ uint8_t get_switch_state(uint8_t* switch_flags) {
return (uint8_t)(button_state && 0xF); 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 poll_bottom_task(void *arg);
// static void IRAM_ATTR gpio_isr_handler(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) { static uint8_t receive_delta(void) {
uint8_t reg = 1; uint8_t reg = 1;
read_buf[0] = 0; buf[0] = 0;
ESP_ERROR_CHECK_WITHOUT_ABORT(i2c_master_write_read_device(BOTTOM_I2C_NUM, BOTTOM_I2C_ADDR, &reg, 1, read_buf, 1, (100 / portTICK_PERIOD_MS))); ESP_ERROR_CHECK_WITHOUT_ABORT(i2c_master_write_read_device(BOTTOM_I2C_NUM, BOTTOM_I2C_ADDR, &reg, 1, buf, 1, (100 / portTICK_PERIOD_MS)));
return read_buf[0]; return buf[0];
} }
static void receive_keypad(void) { static void receive_keypad(void) {
uint8_t reg = 2; uint8_t reg = 2;
read_buf[0] = 0; buf[0] = 0;
read_buf[1] = 0; buf[1] = 0;
ESP_ERROR_CHECK_WITHOUT_ABORT(i2c_master_write_read_device(BOTTOM_I2C_NUM, BOTTOM_I2C_ADDR, &reg, 1, read_buf, 2, (100 / portTICK_PERIOD_MS))); ESP_ERROR_CHECK_WITHOUT_ABORT(i2c_master_write_read_device(BOTTOM_I2C_NUM, BOTTOM_I2C_ADDR, &reg, 1, buf, 2, (100 / portTICK_PERIOD_MS)));
uint16_t new_keypad_state = read_buf[0] | (read_buf[1] << 8); uint16_t new_keypad_state = buf[0] | (buf[1] << 8);
uint16_t just_pressed = new_keypad_state & ~keypad_state; uint16_t just_pressed = new_keypad_state & ~keypad_state;
keypad_pressed |= just_pressed; keypad_pressed |= just_pressed;
@ -210,11 +224,10 @@ static void receive_keypad(void) {
static void receive_button(void) { static void receive_button(void) {
uint8_t reg = 3; uint8_t reg = 3;
read_buf[0] = 0; buf[0] = 0;
read_buf[1] = 0; buf[1] = 0;
ESP_ERROR_CHECK_WITHOUT_ABORT(i2c_master_write_read_device(BOTTOM_I2C_NUM, BOTTOM_I2C_ADDR, &reg, 1, read_buf, 2, (100 / portTICK_PERIOD_MS))); ESP_ERROR_CHECK_WITHOUT_ABORT(i2c_master_write_read_device(BOTTOM_I2C_NUM, BOTTOM_I2C_ADDR, &reg, 1, buf, 2, (100 / portTICK_PERIOD_MS)));
uint16_t new_button_state = read_buf[0] | (read_buf[1] << 8); uint16_t new_button_state = buf[0] | (buf[1] << 8);
ESP_LOGI("jjj", "New Button State: %d", new_button_state);
uint16_t just_pressed = new_button_state & ~button_state; uint16_t just_pressed = new_button_state & ~button_state;
button_pressed |= just_pressed; button_pressed |= just_pressed;
@ -225,6 +238,21 @@ static void receive_button(void) {
button_state = new_button_state; 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, &reg, 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) { static void poll_bottom_task(void *arg) {
while (1) { while (1) {
bool new_data = gpio_get_level(BOTTOM_PIN_INTERUPT) == 0; 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); // ESP_LOGI(_TAG, "delta: %d", delta);
if (delta == 0) ESP_LOGW(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_KP)) { if (delta & (1 << DELTA_BIT_KP)) receive_keypad();
receive_keypad(); if (delta & (1 << DELTA_BIT_BUTTON)) receive_button();
// ESP_LOGI(_TAG, "keypad: %d", keypad_state); if (delta & (1 << DELTA_BIT_TOUCH)) receive_touch();
}
if (delta & (1 << DELTA_BIT_BUTTON)) {
receive_button();
// ESP_LOGI(_TAG, "button: %d", button_state);
}
} }
vTaskDelay(pdMS_TO_TICKS(10)); vTaskDelay(pdMS_TO_TICKS(10));
} }

View File

@ -11,6 +11,7 @@
#define DELTA_BIT_KP 0 #define DELTA_BIT_KP 0
#define DELTA_BIT_BUTTON 1 #define DELTA_BIT_BUTTON 1
#define DELTA_BIT_TOUCH 2
typedef enum { typedef enum {
k1 = 0, k1 = 0,
@ -60,6 +61,10 @@ bool get_flipped_down_switch(SwitchKey* switch_);
bool get_flipped_switch(SwitchKey* switch_); bool get_flipped_switch(SwitchKey* switch_);
uint8_t get_switch_state(uint8_t* switch_flags); 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); static void poll_bottom_task(void *arg);
void init_bottom_half(); void init_bottom_half();

View File

@ -5,21 +5,30 @@
#include "sseg.hpp" #include "sseg.hpp"
static uint32_t _time_left; static bool playing;
static uint32_t time_left;
static void game_timer_task(void *arg); static void game_timer_task(void *arg);
uint32_t sat_sub(uint32_t x, uint32_t y); 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) { void init_game_timer(void) {
xTaskCreate(game_timer_task, "game_timer", 4096, NULL, 10, NULL); xTaskCreate(game_timer_task, "game_timer", 4096, NULL, 10, NULL);
} }
void set_game_time(uint32_t new_time) { void set_game_time(uint32_t new_time) {
_time_left = new_time; time_left = new_time;
} }
uint32_t get_game_time() { uint32_t get_game_time() {
return _time_left; return time_left;
} }
static void game_timer_task(void *arg) static void game_timer_task(void *arg)
@ -29,8 +38,10 @@ static void game_timer_task(void *arg)
const uint32_t frequency = 100; const uint32_t frequency = 100;
while (1) { while (1) {
vTaskDelayUntil( &lastWakeTime, pdMS_TO_TICKS(frequency)); vTaskDelayUntil( &lastWakeTime, pdMS_TO_TICKS(frequency));
_time_left = sat_sub(_time_left, frequency); if (playing) {
set_game_timer(_time_left / 100, 1); time_left = sat_sub(time_left, frequency);
set_game_timer(time_left / 100, 1);
}
} }
vTaskDelete(NULL); vTaskDelete(NULL);

View File

@ -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); 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); bytes_read = fread(read_buf, sizeof(uint8_t), AUDIO_BUFFER, fh);
for (int i = 0; i < bytes_read; i++) { 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); ESP_LOGV(SPEAKER_TAG, "Bytes read: %d", bytes_read);
} }

View File

@ -1,5 +1,4 @@
#ifndef WIRES_HPP #include "wires.h"
#define WIRES_HPP
#define WIRES_PIN_DELTA GPIO_NUM_2 #define WIRES_PIN_DELTA GPIO_NUM_2
#define WIRES_I2C_NUM I2C_NUM_1 #define WIRES_I2C_NUM I2C_NUM_1
@ -8,13 +7,20 @@
#define DELTA_BIT_WIRES 0 #define DELTA_BIT_WIRES 0
#define DELTA_BIT_BUTTON 1 #define DELTA_BIT_BUTTON 1
static const char *WIRES_TAG = "wires"; static const char *TAG = "wires";
uint8_t button_state; static bool button_state;
uint8_t wires_states; static bool button_pressed;
uint8_t wires_cut; 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 poll_wires_task(void *arg);
static void receive_wires(void);
static void receive_button(void);
void init_wires(void) { void init_wires(void) {
i2c_config_t wires_conf = { i2c_config_t wires_conf = {
@ -41,35 +47,77 @@ void init_wires(void) {
int_pin_conf.pull_up_en = GPIO_PULLUP_ENABLE; int_pin_conf.pull_up_en = GPIO_PULLUP_ENABLE;
gpio_config(&int_pin_conf); gpio_config(&int_pin_conf);
receive_button();
receive_wires();
xTaskCreate(poll_wires_task, "poll_wires", 4096, NULL, 10, NULL); xTaskCreate(poll_wires_task, "poll_wires", 4096, NULL, 10, NULL);
} }
void strike(char* reason) { uint8_t get_wires(void) {
uint8_t reg = 5; return wires_state;
ESP_ERROR_CHECK_WITHOUT_ABORT(i2c_master_write_to_device(WIRES_I2C_NUM, WIRES_I2C_ADDR, &reg, 1, (1000 / portTICK_PERIOD_MS))); }
uint8_t get_cut_wires(void) {
uint8_t return_ = wires_cut;
wires_cut = 0;
return return_;
} }
/// read buffer bool get_button(void) {
static uint8_t read_buf[8]; 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, &reg, 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) { static uint8_t receive_delta(void) {
uint8_t reg = 1; uint8_t reg = 1;
read_buf[0] = 0; buf[0] = 0;
ESP_ERROR_CHECK_WITHOUT_ABORT(i2c_master_write_read_device(WIRES_I2C_NUM, WIRES_I2C_ADDR, &reg, 1, read_buf, 1, (100 / portTICK_PERIOD_MS))); ESP_ERROR_CHECK_WITHOUT_ABORT(i2c_master_write_read_device(WIRES_I2C_NUM, WIRES_I2C_ADDR, &reg, 1, buf, 1, (100 / portTICK_PERIOD_MS)));
return read_buf[0]; return buf[0];
} }
static void receive_wires(void) { static void receive_wires(void) {
uint8_t reg = 1; uint8_t reg = 2;
read_buf[0] = 0; buf[0] = 0;
ESP_ERROR_CHECK_WITHOUT_ABORT(i2c_master_write_read_device(WIRES_I2C_NUM, WIRES_I2C_ADDR, &reg, 1, read_buf, 1, (100 / portTICK_PERIOD_MS))); ESP_ERROR_CHECK_WITHOUT_ABORT(i2c_master_write_read_device(WIRES_I2C_NUM, WIRES_I2C_ADDR, &reg, 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) { static void receive_button(void) {
uint8_t reg = 2; uint8_t reg = 3;
read_buf[0] = 0; buf[0] = 0;
ESP_ERROR_CHECK_WITHOUT_ABORT(i2c_master_write_read_device(WIRES_I2C_NUM, WIRES_I2C_ADDR, &reg, 1, read_buf, 1, (100 / portTICK_PERIOD_MS))); ESP_ERROR_CHECK_WITHOUT_ABORT(i2c_master_write_read_device(WIRES_I2C_NUM, WIRES_I2C_ADDR, &reg, 1, buf, 1, (100 / portTICK_PERIOD_MS)));
uint8_t new_button = read_buf[0]; 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; 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; bool new_data = gpio_get_level(WIRES_PIN_DELTA) == 0;
if (new_data) { if (new_data) {
uint8_t delta = receive_delta(); uint8_t delta = receive_delta();
if (delta == 0) ESP_LOGW(TAG, "delta pin was low, but delta register returned 0");
if (delta == 0) ESP_LOGW(WIRES_TAG, "delta pin was low, but delta register returned 0");
if (delta & (1 << DELTA_BIT_WIRES)) receive_wires(); if (delta & (1 << DELTA_BIT_WIRES)) receive_wires();
if (delta & (1 << DELTA_BIT_BUTTON)) receive_button(); if (delta & (1 << DELTA_BIT_BUTTON)) receive_button();
@ -89,5 +136,3 @@ static void poll_wires_task(void *arg) {
vTaskDelete(NULL); vTaskDelete(NULL);
} }
#endif /* WIRES_HPP */

28
main/drivers/wires.h Normal file
View File

@ -0,0 +1,28 @@
#ifndef WIRES_HPP
#define WIRES_HPP
#include <stdint.h>
#include <driver/i2c.h>
#include <driver/gpio.h>
#include <esp_log.h>
#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 */

View File

@ -5,7 +5,7 @@
#include "driver/uart.h" #include "driver/uart.h"
#include "driver/i2c.h" #include "driver/i2c.h"
#include "drivers/tft.hpp" #include "drivers/tft.hpp"
#include "drivers/wires.hpp" #include "drivers/wires.h"
#include "drivers/bottom_half.h" #include "drivers/bottom_half.h"
#include "drivers/sd.hpp" #include "drivers/sd.hpp"
#include "drivers/game_timer.hpp" #include "drivers/game_timer.hpp"
@ -36,12 +36,13 @@ extern "C" void app_main(void) {
init_sseg(); init_sseg();
init_game_timer(); init_game_timer();
init_wires(); init_wires();
set_game_time(30000);
init_bottom_half(); init_bottom_half();
init_char_lcd(); init_char_lcd();
step0(); step0();
set_game_time(30000);
start_timer();
step1(); step1();
step2(); step2();
step3(); step3();
@ -49,10 +50,15 @@ extern "C" void app_main(void) {
step5(); step5();
step6(); 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(); // create_demo_ui();
// play_example(); // play_example();
// deinit_sd(); // deinit_sd();
// set_game_timer(1234, 1); // set_game_timer(1234, 1);

View File

@ -6,27 +6,46 @@
#include "wires_puzzle.h" #include "wires_puzzle.h"
#include <esp_log.h> #include <esp_log.h>
void setup_wires(void) { uint8_t wires_state = 0;
clear_all_pressed_released();
lcd_clear(&lcd);
WiresColor wires[NUM_WIRES];
load_wires_from_sd_card(wires);
void print_wires(WireColor* wires, int editing_idx) {
bool cut[NUM_WIRES]; bool cut[NUM_WIRES];
solve_wires(wires, cut); solve_wires(wires, cut);
lcd_set_cursor(&lcd, 1, 1); lcd_set_cursor(&lcd, 1, 1);
char wires_string[NUM_WIRES+1] = {0}; char string_buf[NUM_WIRES+1] = {0};
wires_to_string(wires, wires_string); wires_to_string(wires, string_buf);
lcd_print(&lcd, wires_string); lcd_print(&lcd, string_buf);
lcd_set_cursor(&lcd, 1, 2); lcd_set_cursor(&lcd, 1, 2);
cut_to_string(cut, wires_string); cut_to_string(cut, string_buf);
lcd_print(&lcd, wires_string); lcd_print(&lcd, string_buf);
lcd_set_cursor(&lcd, 1, 1);
lcd_set_cursor(&lcd, 1, 3);
wires_state = get_wires();
for (int i = 0; i < NUM_WIRES; i++) {
if (wires_state & (1<<i)) {
string_buf[i] = ' ';
} else {
string_buf[i] = '!';
}
}
lcd_print(&lcd, string_buf);
lcd_set_cursor(&lcd, editing_idx+1, 1);
}
void setup_wires(void) {
clear_all_pressed_released();
get_cut_wires();
lcd_clear(&lcd);
lcd_cursor(&lcd);
WireColor wires[NUM_WIRES];
load_wires_from_sd_card(wires);
int editing_idx = 0; int editing_idx = 0;
print_wires(wires, editing_idx);
KeypadKey key; KeypadKey key;
ButtonKey button; ButtonKey button;
@ -36,18 +55,7 @@ void setup_wires(void) {
if (key == KeypadKey::k1) { if (key == KeypadKey::k1) {
generate_new_wires(wires); generate_new_wires(wires);
save_wires_to_sd_card(wires); save_wires_to_sd_card(wires);
bool cut[NUM_WIRES]; print_wires(wires, editing_idx);
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);
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);
} else if (key == KeypadKey::pound) { } else if (key == KeypadKey::pound) {
lcd_no_cursor(&lcd); lcd_no_cursor(&lcd);
return; return;
@ -55,7 +63,6 @@ void setup_wires(void) {
} }
while (get_pressed_button(&button)) { while (get_pressed_button(&button)) {
ESP_LOGI("setup_wires", "Button press: %d", (int)button);
if (button == ButtonKey::b1) { if (button == ButtonKey::b1) {
// left // left
editing_idx = editing_idx - 1; editing_idx = editing_idx - 1;
@ -66,26 +73,19 @@ void setup_wires(void) {
if (editing_idx >= NUM_WIRES) editing_idx = NUM_WIRES-1; if (editing_idx >= NUM_WIRES) editing_idx = NUM_WIRES-1;
} else if (button == ButtonKey::b2) { } else if (button == ButtonKey::b2) {
// down // 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) { } else if (button == ButtonKey::b3) {
// up // 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); save_wires_to_sd_card(wires);
lcd_set_cursor(&lcd, 1, 1); print_wires(wires, editing_idx);
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);
} }
uint8_t new_wires_state = get_wires();
if (new_wires_state != wires_state) {
print_wires(wires, editing_idx);
}
vTaskDelay(pdMS_TO_TICKS(10)); vTaskDelay(pdMS_TO_TICKS(10));
} }
} }

View File

@ -3,7 +3,7 @@
#include "../drivers/bottom_half.h" #include "../drivers/bottom_half.h"
#include "../drivers/char_lcd.hpp" #include "../drivers/char_lcd.hpp"
#include "../drivers/wires.hpp" #include "../drivers/wires.h"
#include "setup_wires.hpp" #include "setup_wires.hpp"
static const char *STEP0_TAG = "step0"; static const char *STEP0_TAG = "step0";

View File

@ -1,10 +1,64 @@
#ifndef STEP_6_HPP #ifndef STEP_6_HPP
#define 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 const char *STEP6_TAG = "step6";
static uint8_t cut_wires = 0;
static void correct_wire_task(void *arg);
void step6(void) { 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<<i)) {
if (solution[i]) {
xTaskCreate(correct_wire_task, "correct_wire", 4096, NULL, 5, NULL);
} else {
strike("Cut wrong wire");
}
}
}
if (get_touch_pressed()) {
bool correct = true;
for (int i = 0; i < NUM_WIRES; i++) {
bool wire_cut = (cut_wires & (1<<i)) != 0;
if (solution[i] && !wire_cut) {
correct = false;
}
}
if (correct) {
return;
} else {
strike("Not all wires are cut!");
}
}
vTaskDelay(pdMS_TO_TICKS(10));
}
}
static void correct_wire_task(void *arg) {
ESP_LOGI(STEP6_TAG, "Correct!");
vTaskDelete(NULL);
} }
#endif /* STEP_6_HPP */ #endif /* STEP_6_HPP */

View File

@ -14,25 +14,25 @@ static int color_name_len[NUM_COLORS] = {
static int max(int n0, int n1, int n2, int n3, int n4, int n5); static int max(int n0, int n1, int n2, int n3, int n4, int n5);
void wires_to_string(WiresColor* wires, char* out_wires_string) { void wires_to_string(WireColor* wires, char* out_wires_string) {
for (int i = 0; i < NUM_WIRES; i++) { for (int i = 0; i < NUM_WIRES; i++) {
switch (wires[i]) { switch (wires[i]) {
case WiresColor::red: case WireColor::red:
out_wires_string[i] = 'r'; out_wires_string[i] = 'r';
break; break;
case WiresColor::yellow: case WireColor::yellow:
out_wires_string[i] = 'y'; out_wires_string[i] = 'y';
break; break;
case WiresColor::green: case WireColor::green:
out_wires_string[i] = 'g'; out_wires_string[i] = 'g';
break; break;
case WiresColor::blue: case WireColor::blue:
out_wires_string[i] = 'u'; out_wires_string[i] = 'u';
break; break;
case WiresColor::black: case WireColor::black:
out_wires_string[i] = 'b'; out_wires_string[i] = 'b';
break; break;
case WiresColor::white: case WireColor::white:
out_wires_string[i] = 'w'; out_wires_string[i] = 'w';
break; break;
} }
@ -48,32 +48,32 @@ void cut_to_string(bool* cut, char* out_cut_string) {
} }
} }
void string_to_wires(char* wires_string, WiresColor* out_wires) { void string_to_wires(char* wires_string, WireColor* out_wires) {
for (int i = 0; i < NUM_WIRES; i++) { for (int i = 0; i < NUM_WIRES; i++) {
switch (wires_string[i]) { switch (wires_string[i]) {
case 'r': case 'r':
out_wires[i] = WiresColor::red; out_wires[i] = WireColor::red;
break; break;
case 'y': case 'y':
out_wires[i] = WiresColor::yellow; out_wires[i] = WireColor::yellow;
break; break;
case 'g': case 'g':
out_wires[i] = WiresColor::green; out_wires[i] = WireColor::green;
break; break;
case 'u': case 'u':
out_wires[i] = WiresColor::blue; out_wires[i] = WireColor::blue;
break; break;
case 'b': case 'b':
out_wires[i] = WiresColor::black; out_wires[i] = WireColor::black;
break; break;
case 'w': case 'w':
out_wires[i] = WiresColor::white; out_wires[i] = WireColor::white;
break; break;
} }
} }
} }
void save_wires_to_sd_card(WiresColor* wires) { void save_wires_to_sd_card(WireColor* wires) {
FILE* f = fopen(WIRES_FILE_PATH, "w"); FILE* f = fopen(WIRES_FILE_PATH, "w");
if (f == NULL) { if (f == NULL) {
ESP_LOGE(TAG, "Failed to open wires file to write"); ESP_LOGE(TAG, "Failed to open wires file to write");
@ -84,7 +84,7 @@ void save_wires_to_sd_card(WiresColor* wires) {
fclose(f); fclose(f);
} }
void load_wires_from_sd_card(WiresColor* out_wires) { void load_wires_from_sd_card(WireColor* out_wires) {
FILE* f = fopen(WIRES_FILE_PATH, "r"); FILE* f = fopen(WIRES_FILE_PATH, "r");
if (f == NULL) { if (f == NULL) {
ESP_LOGW(TAG, "Failed to read wires file. Generating new wires"); ESP_LOGW(TAG, "Failed to read wires file. Generating new wires");
@ -101,11 +101,11 @@ void load_wires_from_sd_card(WiresColor* out_wires) {
/// @brief Fills the array with 6 random WiresColors. /// @brief Fills the array with 6 random WiresColors.
/// @param wires and array of len >= 6 to be populated with random colors /// @param wires and array of len >= 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++) { for (int w = 0; w < NUM_WIRES; w++) {
// roughly evenly distributed // roughly evenly distributed
uint32_t rand = esp_random() % NUM_COLORS; uint32_t rand = esp_random() % NUM_COLORS;
wires[w] = (WiresColor)(rand); wires[w] = (WireColor)(rand);
} }
bool cuts[NUM_WIRES] = {0}; 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 // by default, don't cut any wires
for (int i = 0; i < NUM_WIRES; i++) { for (int i = 0; i < NUM_WIRES; i++) {
out_cut[i] = false; out_cut[i] = false;
@ -142,17 +142,17 @@ void solve_wires(WiresColor* wires, bool* out_cut) {
int white_pos_len = 0; int white_pos_len = 0;
for (int i = 0; i < NUM_WIRES; i++) { for (int i = 0; i < NUM_WIRES; i++) {
if (wires[i] == WiresColor::red) { if (wires[i] == WireColor::red) {
red_pos[red_pos_len++] = i; red_pos[red_pos_len++] = i;
} else if (wires[i] == WiresColor::yellow) { } else if (wires[i] == WireColor::yellow) {
yellow_pos[yellow_pos_len++] = i; yellow_pos[yellow_pos_len++] = i;
} else if (wires[i] == WiresColor::green) { } else if (wires[i] == WireColor::green) {
green_pos[green_pos_len++] = i; green_pos[green_pos_len++] = i;
} else if (wires[i] == WiresColor::blue) { } else if (wires[i] == WireColor::blue) {
blue_pos[blue_pos_len++] = i; blue_pos[blue_pos_len++] = i;
} else if (wires[i] == WiresColor::black) { } else if (wires[i] == WireColor::black) {
black_pos[black_pos_len++] = i; black_pos[black_pos_len++] = i;
} else if (wires[i] == WiresColor::white) { } else if (wires[i] == WireColor::white) {
white_pos[white_pos_len++] = i; 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 // 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) // cut blue wires in even positions (odd indexes)
for (int i = 1; i < NUM_WIRES; i += 2) { 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 // cut black and yellow wires next to black and yellow wires
for (int i = 0; i < NUM_WIRES-1; i++) { for (int i = 0; i < NUM_WIRES-1; i++) {
if ( if (
(wires[i] == WiresColor::yellow || wires[i] == WiresColor::black) && (wires[i] == WireColor::yellow || wires[i] == WireColor::black) &&
(wires[i+1] == WiresColor::yellow || wires[i+1] == WiresColor::black) (wires[i+1] == WireColor::yellow || wires[i+1] == WireColor::black)
) { ) {
out_cut[i] = true; out_cut[i] = true;
out_cut[1] = 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--) { for (int green_idx = green_pos_len-1; green_idx >= 0; green_idx--) {
int pos = green_pos[green_idx]; int pos = green_pos[green_idx];
if ( if (
wires[pos-1] == WiresColor::yellow || wires[pos-1] == WireColor::yellow ||
wires[pos-1] == WiresColor::white || wires[pos-1] == WireColor::white ||
wires[pos+1] == WiresColor::yellow || wires[pos+1] == WireColor::yellow ||
wires[pos+1] == WiresColor::white wires[pos+1] == WireColor::white
) { ) {
out_cut[pos] = true; out_cut[pos] = true;
break; 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 // 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++) { for (int white_idx = 0; white_idx < white_pos_len; white_idx++) {
out_cut[white_pos[white_idx]] = true; 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 // cut any wire adjacent to both a yellow and blue wire
for (int i = 0; i < NUM_WIRES-2; i++) { for (int i = 0; i < NUM_WIRES-2; i++) {
if ( if (
(wires[i] == WiresColor::yellow && wires[i+2] == WiresColor::blue) || (wires[i] == WireColor::yellow && wires[i+2] == WireColor::blue) ||
(wires[i] == WiresColor::blue && wires[i+2] == WiresColor::white) (wires[i] == WireColor::blue && wires[i+2] == WireColor::white)
) { ) {
out_cut[i+1] = true; 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++) { for (int i = 0; i < blue_pos_len; i++) {
int pos = blue_pos[i]; int pos = blue_pos[i];
if ( if (
wires[pos-1] == WiresColor::red || wires[pos-1] == WireColor::red ||
wires[pos-1] == WiresColor::green || wires[pos-1] == WireColor::green ||
wires[pos+1] == WiresColor::red || wires[pos+1] == WireColor::red ||
wires[pos+1] == WiresColor::green wires[pos+1] == WireColor::green
) { ) {
out_cut[pos] = false; 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 // 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; 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; 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 // 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; out_cut[7] = false;
} }

View File

@ -16,25 +16,25 @@ typedef enum {
blue = 3, blue = 3,
black = 4, black = 4,
white = 5, white = 5,
} WiresColor; } WireColor;
void solve_wires(WiresColor* wires, bool* out_cut); void solve_wires(WireColor* wires, bool* out_cut);
void generate_new_wires(WiresColor* wires); void generate_new_wires(WireColor* wires);
void cut_to_string(bool* cut, char* out_cut_string); void cut_to_string(bool* cut, char* out_cut_string);
void wires_to_string(WiresColor* wires, char* out_wires_string); void wires_to_string(WireColor* wires, char* out_wires_string);
void string_to_wires(char* wires_string, WiresColor* out_wires); void string_to_wires(char* wires_string, WireColor* out_wires);
void save_wires_to_sd_card(WiresColor* wires); void save_wires_to_sd_card(WireColor* wires);
void load_wires_from_sd_card(WiresColor* out_wires); void load_wires_from_sd_card(WireColor* out_wires);
/// @brief Fills the array with 6 random WiresColors. /// @brief Fills the array with 6 random WiresColors.
/// @param wires the array of len >= 6 to be populated with random colors /// @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. /// @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 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 /// @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 */ #endif /* WIRES_PUZZLE_H */