step 6
This commit is contained in:
parent
8b41f98659
commit
ce54b8cda3
@ -2,6 +2,7 @@ set(SOURCES
|
||||
"TM1640/TM16xx.cpp"
|
||||
"TM1640/TM1640.cpp"
|
||||
"bottom_half.cpp"
|
||||
"wires.cpp"
|
||||
)
|
||||
|
||||
target_sources(${COMPONENT_LIB} PRIVATE ${SOURCES})
|
||||
|
||||
@ -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));
|
||||
}
|
||||
|
||||
@ -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();
|
||||
|
||||
@ -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);
|
||||
|
||||
@ -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);
|
||||
}
|
||||
|
||||
@ -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 */
|
||||
28
main/drivers/wires.h
Normal file
28
main/drivers/wires.h
Normal 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 */
|
||||
@ -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);
|
||||
|
||||
@ -6,27 +6,46 @@
|
||||
#include "wires_puzzle.h"
|
||||
#include <esp_log.h>
|
||||
|
||||
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<<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;
|
||||
print_wires(wires, editing_idx);
|
||||
|
||||
KeypadKey key;
|
||||
ButtonKey button;
|
||||
@ -36,18 +55,7 @@ void setup_wires(void) {
|
||||
if (key == KeypadKey::k1) {
|
||||
generate_new_wires(wires);
|
||||
save_wires_to_sd_card(wires);
|
||||
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);
|
||||
|
||||
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);
|
||||
} else if (key == KeypadKey::pound) {
|
||||
lcd_no_cursor(&lcd);
|
||||
return;
|
||||
@ -55,7 +63,6 @@ void setup_wires(void) {
|
||||
}
|
||||
|
||||
while (get_pressed_button(&button)) {
|
||||
ESP_LOGI("setup_wires", "Button press: %d", (int)button);
|
||||
if (button == ButtonKey::b1) {
|
||||
// left
|
||||
editing_idx = editing_idx - 1;
|
||||
@ -66,26 +73,19 @@ void setup_wires(void) {
|
||||
if (editing_idx >= 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));
|
||||
}
|
||||
}
|
||||
|
||||
@ -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";
|
||||
|
||||
@ -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";
|
||||
|
||||
void step6(void) {
|
||||
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<<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 */
|
||||
@ -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);
|
||||
|
||||
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++) {
|
||||
switch (wires[i]) {
|
||||
case WiresColor::red:
|
||||
case WireColor::red:
|
||||
out_wires_string[i] = 'r';
|
||||
break;
|
||||
case WiresColor::yellow:
|
||||
case WireColor::yellow:
|
||||
out_wires_string[i] = 'y';
|
||||
break;
|
||||
case WiresColor::green:
|
||||
case WireColor::green:
|
||||
out_wires_string[i] = 'g';
|
||||
break;
|
||||
case WiresColor::blue:
|
||||
case WireColor::blue:
|
||||
out_wires_string[i] = 'u';
|
||||
break;
|
||||
case WiresColor::black:
|
||||
case WireColor::black:
|
||||
out_wires_string[i] = 'b';
|
||||
break;
|
||||
case WiresColor::white:
|
||||
case WireColor::white:
|
||||
out_wires_string[i] = 'w';
|
||||
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++) {
|
||||
switch (wires_string[i]) {
|
||||
case 'r':
|
||||
out_wires[i] = WiresColor::red;
|
||||
out_wires[i] = WireColor::red;
|
||||
break;
|
||||
case 'y':
|
||||
out_wires[i] = WiresColor::yellow;
|
||||
out_wires[i] = WireColor::yellow;
|
||||
break;
|
||||
case 'g':
|
||||
out_wires[i] = WiresColor::green;
|
||||
out_wires[i] = WireColor::green;
|
||||
break;
|
||||
case 'u':
|
||||
out_wires[i] = WiresColor::blue;
|
||||
out_wires[i] = WireColor::blue;
|
||||
break;
|
||||
case 'b':
|
||||
out_wires[i] = WiresColor::black;
|
||||
out_wires[i] = WireColor::black;
|
||||
break;
|
||||
case 'w':
|
||||
out_wires[i] = WiresColor::white;
|
||||
out_wires[i] = WireColor::white;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void save_wires_to_sd_card(WiresColor* wires) {
|
||||
void save_wires_to_sd_card(WireColor* wires) {
|
||||
FILE* f = fopen(WIRES_FILE_PATH, "w");
|
||||
if (f == NULL) {
|
||||
ESP_LOGE(TAG, "Failed to open wires file to write");
|
||||
@ -84,7 +84,7 @@ void save_wires_to_sd_card(WiresColor* wires) {
|
||||
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");
|
||||
if (f == NULL) {
|
||||
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.
|
||||
/// @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++) {
|
||||
// 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;
|
||||
}
|
||||
|
||||
|
||||
@ -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 */
|
||||
Loading…
Reference in New Issue
Block a user