This commit is contained in:
2024-08-04 21:47:23 -05:00
parent 8b41f98659
commit ce54b8cda3
13 changed files with 319 additions and 147 deletions
+1
View File
@@ -2,6 +2,7 @@ set(SOURCES
"TM1640/TM16xx.cpp"
"TM1640/TM1640.cpp"
"bottom_half.cpp"
"wires.cpp"
)
target_sources(${COMPONENT_LIB} PRIVATE ${SOURCES})
+44 -22
View File
@@ -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, &reg, 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, &reg, 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, &reg, 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, &reg, 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, &reg, 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, &reg, 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, &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) {
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));
}
+5
View File
@@ -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();
+16 -5
View File
@@ -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);
+1 -1
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);
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, &reg, 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, &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) {
uint8_t reg = 1;
read_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)));
return read_buf[0];
buf[0] = 0;
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 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, &reg, 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, &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) {
uint8_t reg = 2;
read_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)));
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, &reg, 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
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 */