From bd4548ec9035334cdf8bd40da1c68834d5bc9975 Mon Sep 17 00:00:00 2001 From: Mitchell M Date: Fri, 2 Aug 2024 16:46:43 -0500 Subject: [PATCH] working step0 --- main/drivers/bottom_half.hpp | 211 +++++++++++++++++++++++++++++++++++ main/drivers/char_lcd.hpp | 52 +++++++++ main/drivers/game_timer.hpp | 46 ++++++++ main/drivers/wires.hpp | 12 ++ main/idf_component.yml | 1 + main/main.cpp | 46 ++++---- main/steps/step0.hpp | 71 ++++++++++++ main/steps/step1.hpp | 0 8 files changed, 417 insertions(+), 22 deletions(-) create mode 100644 main/drivers/bottom_half.hpp create mode 100644 main/drivers/char_lcd.hpp create mode 100644 main/drivers/game_timer.hpp create mode 100644 main/drivers/wires.hpp create mode 100644 main/steps/step0.hpp create mode 100644 main/steps/step1.hpp diff --git a/main/drivers/bottom_half.hpp b/main/drivers/bottom_half.hpp new file mode 100644 index 0000000..95e620e --- /dev/null +++ b/main/drivers/bottom_half.hpp @@ -0,0 +1,211 @@ +#ifndef BOTTOM_HALF_HPP +#define BOTTOM_HALF_HPP + +#include "driver/i2c.h" +#include "driver/gpio.h" + +#define BOTTOM_I2C_NUM I2C_NUM_0 +#define BOTTOM_I2C_ADDR 126 +#define BOTTOM_PIN_INTERUPT GPIO_NUM_0 + +#define DELTA_BIT_KP 0 +#define DELTA_BIT_BUTTON 1 + +typedef enum { + k1 = 0, + k4 = 1, + k7 = 2, + star = 3, + k2 = 4, + k5 = 5, + k8 = 6, + k0 = 7, + k3 = 8, + k6 = 9, + k9 = 10, + pound = 11, + ka = 12, + kb = 13, + kc = 14, + kd = 15, +} KeypadKey; + +static const char *_TAG = "bottom_half"; + +uint16_t keypad_state; +uint16_t button_state; +uint8_t touch_state; + +uint16_t keypad_pressed; +uint16_t button_pressed; +uint8_t touch_pressed; + +uint16_t keypad_released; +uint16_t button_released; +uint8_t touch_released; + +static bool _take_key(KeypadKey* kp, uint16_t* keypad_bitfield) { + for (int i = 0; i < 16; i++) { + int bit_selector = (1 << i); + if ((*keypad_bitfield) & bit_selector) { + *kp = (KeypadKey) i; + // clear bit + *keypad_bitfield &= ~bit_selector; + return true; + } + } + return false; +} + +bool get_pressed_keypad(KeypadKey* kp) { + return _take_key(kp, &keypad_pressed); +} +bool get_released_keypad(KeypadKey* kp) { + return _take_key(kp, &keypad_released); +} + +char char_of_keypad_key(KeypadKey kp) { + switch (kp) { + case KeypadKey::k1: + return '1'; + case KeypadKey::k2: + return '2'; + case KeypadKey::k3: + return '3'; + case KeypadKey::k4: + return '4'; + case KeypadKey::k5: + return '5'; + case KeypadKey::k6: + return '6'; + case KeypadKey::k7: + return '7'; + case KeypadKey::k8: + return '8'; + case KeypadKey::k9: + return '9'; + case KeypadKey::k0: + return '0'; + case KeypadKey::ka: + return 'A'; + case KeypadKey::kb: + return 'B'; + case KeypadKey::kc: + return 'C'; + case KeypadKey::kd: + return 'D'; + case KeypadKey::star: + return '*'; + case KeypadKey::pound: + return '#'; + default: + return ' '; + } +} + +/// read buffer +static uint8_t read_buf[8]; + +static void poll_bottom_task(void *arg); + +// static void IRAM_ATTR gpio_isr_handler(void* arg) +// { +// // TODO: do somthing +// ESP_LOGI("BOTTOM_HALF", "Hit"); +// } + +void init_bottom_half() { + i2c_config_t conf = { + .mode = I2C_MODE_MASTER, + .sda_io_num = GPIO_NUM_5, + .scl_io_num = GPIO_NUM_6, + .sda_pullup_en = GPIO_PULLUP_ENABLE, + .scl_pullup_en = GPIO_PULLUP_ENABLE, + .master = { + .clk_speed = 100000, + } + }; + + ESP_ERROR_CHECK(i2c_param_config(BOTTOM_I2C_NUM, &conf)); + ESP_ERROR_CHECK(i2c_driver_install(BOTTOM_I2C_NUM, conf.mode, 0, 0, 0)); + + gpio_config_t delta_pin_conf = {}; + // delta_pin_conf.intr_type = GPIO_INTR_LOW_LEVEL; + delta_pin_conf.mode = GPIO_MODE_INPUT; + // GPIO 0 + delta_pin_conf.pin_bit_mask = (1ULL< 99999) counter = 0; + + } +} + +#endif /* CHAR_LCD_HPP */ \ No newline at end of file diff --git a/main/drivers/game_timer.hpp b/main/drivers/game_timer.hpp new file mode 100644 index 0000000..03d25fb --- /dev/null +++ b/main/drivers/game_timer.hpp @@ -0,0 +1,46 @@ +#ifndef GAME_TIMER_HPP +#define GAME_TIMER_HPP + +#include "freertos/FreeRTOS.h" + +#include "sseg.hpp" + +static uint32_t _time_left; + +static void game_timer_task(void *arg); +uint32_t sat_sub(uint32_t x, uint32_t y); + +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; +} + +uint32_t get_game_time() { + return _time_left; +} + +static void game_timer_task(void *arg) +{ + TickType_t lastWakeTime = xTaskGetTickCount(); + + 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); + } + + vTaskDelete(NULL); +} + +uint32_t sat_sub(uint32_t x, uint32_t y) +{ + uint32_t res = x - y; + res &= -(res <= x); + return res; +} + +#endif /* GAME_TIMER_HPP */ \ No newline at end of file diff --git a/main/drivers/wires.hpp b/main/drivers/wires.hpp new file mode 100644 index 0000000..c078eec --- /dev/null +++ b/main/drivers/wires.hpp @@ -0,0 +1,12 @@ +#ifndef WIRES_HPP +#define WIRES_HPP + +void init_wires(void) { + +} + +void strike(char* reason) { + +} + +#endif /* WIRES_HPP */ \ No newline at end of file diff --git a/main/idf_component.yml b/main/idf_component.yml index 5d67497..f22ebd2 100644 --- a/main/idf_component.yml +++ b/main/idf_component.yml @@ -1,5 +1,6 @@ ## IDF Component Manager Manifest File dependencies: + iamflinks/i2c_lcd_pcf8574: "^1.0.1" lvgl/lvgl: "^8.1" atanisoft/esp_lcd_ili9488: "^1.0.9" ## Required IDF version diff --git a/main/main.cpp b/main/main.cpp index 2bd8ec7..2d3e9f2 100755 --- a/main/main.cpp +++ b/main/main.cpp @@ -5,34 +5,48 @@ #include "driver/uart.h" #include "driver/i2c.h" #include "drivers/tft.hpp" +#include "drivers/wires.hpp" +#include "drivers/bottom_half.hpp" #include "drivers/sd.hpp" -#include "drivers/sseg.hpp" +#include "drivers/game_timer.hpp" #include "drivers/speaker.hpp" +#include "drivers/char_lcd.hpp" #include "esp_rom_gpio.h" +#include "steps/step0.hpp" + #define WIRES_ADDR 125 #define WIRES_REG_WIRES - static const char *TAG = "main"; static void relay_task(void *arg); extern "C" void app_main(void) { printf("app_main\n"); - // init_sd(); + init_sd(); + init_tft(); + init_speaker(); + init_sseg(); + init_game_timer(); + init_wires(); + set_game_time(30000); + + init_bottom_half(); + init_char_lcd(); + + + step0(); + step1(); - // init_tft(); // create_demo_ui(); - // init_speaker(); // play_example(); // deinit_sd(); - init_sseg(); - set_game_timer(1234, 1); - set_module_timer(5678, 2); + // set_game_timer(1234, 1); + // set_module_timer(5678, 2); // while (1) { // vTaskDelay(pdMS_TO_TICKS(10)); @@ -42,19 +56,7 @@ extern "C" void app_main(void) { // UART relay xTaskCreate(relay_task, "relay_task", 4096, NULL, 10, NULL); - i2c_config_t conf = { - .mode = I2C_MODE_MASTER, - .sda_io_num = GPIO_NUM_5, - .scl_io_num = GPIO_NUM_6, - .sda_pullup_en = GPIO_PULLUP_ENABLE, - .scl_pullup_en = GPIO_PULLUP_ENABLE, - .master = { - .clk_speed = 100000, - } - }; - - ESP_ERROR_CHECK(i2c_param_config(I2C_NUM_0, &conf)); - ESP_ERROR_CHECK(i2c_driver_install(I2C_NUM_0, conf.mode, 0, 0, 0)); + gpio_reset_pin(GPIO_NUM_41); gpio_reset_pin(GPIO_NUM_42); @@ -75,7 +77,7 @@ extern "C" void app_main(void) { // uint8_t write[8] = {1, 2, 4, 8, 16, 32, 64, 128}; - + return; while (1) { vTaskDelay(pdMS_TO_TICKS(5000)); uint8_t read[8] = {0}; diff --git a/main/steps/step0.hpp b/main/steps/step0.hpp new file mode 100644 index 0000000..dc2a96f --- /dev/null +++ b/main/steps/step0.hpp @@ -0,0 +1,71 @@ +#ifndef STEP_0_HPP +#define STEP_0_HPP + +#include "../drivers/bottom_half.hpp" +#include "../drivers/char_lcd.hpp" +#include "i2c_lcd_pcf8574.h" + +static const char *STEP0_TAG = "step0"; + +#define STRING_MAX_LEN 8 + +/// Wait for "*9819" +void step0(void) { + KeypadKey key; + + int current_idx = 0; + char current[STRING_MAX_LEN+1] = {0}; + + while (1) { + while (get_pressed_keypad(&key)) { + if (key == KeypadKey::star) { + current[0] = '*'; + for (int i = 1; i < STRING_MAX_LEN; i++) { + current[i] = 0; + } + current_idx = 1; + } else if (key == KeypadKey::pound) { + // submit + if (strcmp(current, "*9819") == 0) { + lcd_set_cursor(&lcd, 1, 2); + lcd_print(&lcd, "Diffusal Initated"); + vTaskDelay(pdMS_TO_TICKS(2000)); + lcd_clear(&lcd); + return; + } else { + lcd_set_cursor(&lcd, 1, 2); + lcd_print(&lcd, "Invalid Star Code"); + } + + // clear + for (int i = 0; i < STRING_MAX_LEN; i++) { + current[i] = 0; + } + current_idx = 0; + + vTaskDelay(pdMS_TO_TICKS(2000)); + } else { + // out of room. skip + if (current_idx >= STRING_MAX_LEN) break; + // no code started. + if (current[0] != '*') continue; + + char c = char_of_keypad_key(key); + current[current_idx++] = c; + } + // ESP_LOGI(STEP0_TAG, "Pressed: %c", c); + + lcd_clear(&lcd); + lcd_set_cursor(&lcd, 1, 1); + lcd_print(&lcd, current); + } + while (get_released_keypad(&key)) { + char c = char_of_keypad_key(key); + // ESP_LOGI(STEP0_TAG, "Released: %c", c); + } + + vTaskDelay(pdMS_TO_TICKS(10)); + } +} + +#endif /* STEP_0_HPP */ \ No newline at end of file diff --git a/main/steps/step1.hpp b/main/steps/step1.hpp new file mode 100644 index 0000000..e69de29