working step0
This commit is contained in:
parent
2cbccd5599
commit
bd4548ec90
211
main/drivers/bottom_half.hpp
Normal file
211
main/drivers/bottom_half.hpp
Normal file
@ -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<<BOTTOM_PIN_INTERUPT);
|
||||
delta_pin_conf.pull_up_en = GPIO_PULLUP_ENABLE;
|
||||
gpio_config(&delta_pin_conf);
|
||||
|
||||
//install gpio isr service
|
||||
// gpio_install_isr_service(0);
|
||||
//hook isr handler for specific gpio pin
|
||||
// gpio_isr_handler_add(BOTTOM_INTERUPT_PIN, gpio_isr_handler, NULL);
|
||||
|
||||
xTaskCreate(poll_bottom_task, "poll_bottom", 4096, NULL, 10, NULL);
|
||||
}
|
||||
|
||||
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];
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
uint16_t just_pressed = new_keypad_state & ~keypad_state;
|
||||
keypad_pressed |= just_pressed;
|
||||
|
||||
uint16_t just_released = ~new_keypad_state & keypad_state;
|
||||
keypad_released |= just_released;
|
||||
|
||||
keypad_state = new_keypad_state;
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
uint16_t just_pressed = new_button_state & ~button_state;
|
||||
button_pressed |= just_pressed;
|
||||
|
||||
uint16_t just_released = ~new_button_state & button_state;
|
||||
button_released |= just_released;
|
||||
|
||||
button_state = new_button_state;
|
||||
}
|
||||
|
||||
static void poll_bottom_task(void *arg) {
|
||||
while (1) {
|
||||
bool new_data = gpio_get_level(BOTTOM_PIN_INTERUPT) == 0;
|
||||
if (new_data) {
|
||||
uint8_t delta = receive_delta();
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
vTaskDelay(pdMS_TO_TICKS(10));
|
||||
}
|
||||
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
|
||||
#endif /* BOTTOM_HALF_HPP */
|
||||
52
main/drivers/char_lcd.hpp
Normal file
52
main/drivers/char_lcd.hpp
Normal file
@ -0,0 +1,52 @@
|
||||
#ifndef CHAR_LCD_HPP
|
||||
#define CHAR_LCD_HPP
|
||||
|
||||
#include "i2c_lcd_pcf8574.h"
|
||||
|
||||
#define CHAR_LCD_I2C_NUM I2C_NUM_0
|
||||
|
||||
#define LCD_ADDR 0x27
|
||||
#define LCD_COLS 20
|
||||
#define LCD_ROWS 4
|
||||
|
||||
i2c_lcd_pcf8574_handle_t lcd;
|
||||
|
||||
static const char *CHAR_LCD_TAG = "char_lcd";
|
||||
|
||||
void init_char_lcd(void) {
|
||||
|
||||
lcd_init(&lcd, LCD_ADDR, CHAR_LCD_I2C_NUM);
|
||||
lcd_begin(&lcd, LCD_COLS, LCD_ROWS);
|
||||
|
||||
// Turn on the backlight
|
||||
lcd_set_backlight(&lcd, 255);
|
||||
|
||||
ESP_LOGI(CHAR_LCD_TAG, "LCD initialized");
|
||||
}
|
||||
|
||||
void example_lcd(void) {
|
||||
// Print a message
|
||||
lcd_set_cursor(&lcd, 0, 0);
|
||||
lcd_print(&lcd, "Hello, ESP32!");
|
||||
lcd_set_cursor(&lcd, 0, 1);
|
||||
lcd_print(&lcd, "LCD Test");
|
||||
|
||||
int counter = 0;
|
||||
while (1) {
|
||||
vTaskDelay(20 / portTICK_PERIOD_MS); // Wait for 1 second
|
||||
|
||||
// Update the counter on the LCD
|
||||
lcd_set_cursor(&lcd, 10, 1);
|
||||
char buffer[14];
|
||||
snprintf(buffer, sizeof(buffer), "%5d", counter);
|
||||
lcd_print(&lcd, buffer);
|
||||
|
||||
counter++;
|
||||
|
||||
// Reset counter to avoid display overflow
|
||||
if (counter > 99999) counter = 0;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif /* CHAR_LCD_HPP */
|
||||
46
main/drivers/game_timer.hpp
Normal file
46
main/drivers/game_timer.hpp
Normal file
@ -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 */
|
||||
12
main/drivers/wires.hpp
Normal file
12
main/drivers/wires.hpp
Normal file
@ -0,0 +1,12 @@
|
||||
#ifndef WIRES_HPP
|
||||
#define WIRES_HPP
|
||||
|
||||
void init_wires(void) {
|
||||
|
||||
}
|
||||
|
||||
void strike(char* reason) {
|
||||
|
||||
}
|
||||
|
||||
#endif /* WIRES_HPP */
|
||||
@ -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
|
||||
|
||||
@ -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};
|
||||
|
||||
71
main/steps/step0.hpp
Normal file
71
main/steps/step0.hpp
Normal file
@ -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 */
|
||||
0
main/steps/step1.hpp
Normal file
0
main/steps/step1.hpp
Normal file
Loading…
Reference in New Issue
Block a user