#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 *BOTTOM_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<