wires impl

This commit is contained in:
2024-08-02 17:53:22 -05:00
parent bb20c66b85
commit be7c1bc20e
7 changed files with 86 additions and 164 deletions
@@ -1,48 +1,21 @@
#ifndef BOTTOM_HALF_HPP
#define BOTTOM_HALF_HPP
#include "bottom_half.h"
#include "driver/i2c.h"
#include "driver/gpio.h"
static const char *TAG = "bottom_half";
#define BOTTOM_I2C_NUM I2C_NUM_0
#define BOTTOM_I2C_ADDR 126
#define BOTTOM_PIN_INTERUPT GPIO_NUM_0
static uint16_t keypad_state;
static uint16_t button_state;
static uint8_t touch_state;
#define DELTA_BIT_KP 0
#define DELTA_BIT_BUTTON 1
static uint16_t keypad_pressed;
static uint16_t button_pressed;
static uint8_t touch_pressed;
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 uint16_t keypad_released;
static uint16_t button_released;
static uint8_t touch_released;
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;
/// read buffer
static uint8_t read_buf[8];
static bool _take_key(KeypadKey* kp, uint16_t* keypad_bitfield) {
for (int i = 0; i < 16; i++) {
@@ -103,9 +76,6 @@ char char_of_keypad_key(KeypadKey kp) {
}
}
/// read buffer
static uint8_t read_buf[8];
static void poll_bottom_task(void *arg);
// static void IRAM_ATTR gpio_isr_handler(void* arg)
@@ -190,7 +160,7 @@ static void poll_bottom_task(void *arg) {
if (new_data) {
uint8_t delta = receive_delta();
// ESP_LOGI(_TAG, "delta: %d", delta);
if (delta == 0) ESP_LOGW(BOTTOM_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_KP)) {
receive_keypad();
@@ -206,6 +176,4 @@ static void poll_bottom_task(void *arg) {
}
vTaskDelete(NULL);
}
#endif /* BOTTOM_HALF_HPP */
}
+42
View File
@@ -0,0 +1,42 @@
#ifndef BOTTOM_HALF_HPP
#define BOTTOM_HALF_HPP
#include "driver/i2c.h"
#include "driver/gpio.h"
#include "esp_log.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;
bool get_pressed_keypad(KeypadKey* kp);
bool get_released_keypad(KeypadKey* kp);
char char_of_keypad_key(KeypadKey kp);
static void poll_bottom_task(void *arg);
void init_bottom_half();
#endif /* BOTTOM_HALF_HPP */
+19 -17
View File
@@ -1,12 +1,16 @@
#ifndef WIRES_HPP
#define WIRES_HPP
#define WIRES_PIN_INTERUPT GPIO_NUM_2
#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
static const char *WIRES_TAG = "wires";
uint8_t button_state;
uint8_t wires_states;
uint8_t wires_cut;
@@ -24,17 +28,20 @@ void init_wires(void) {
}
};
gpio_reset_pin(GPIO_NUM_41);
gpio_reset_pin(GPIO_NUM_42);
ESP_ERROR_CHECK(i2c_param_config(WIRES_I2C_NUM, &wires_conf));
ESP_ERROR_CHECK(i2c_driver_install(WIRES_I2C_NUM, wires_conf.mode, 0, 0, 0));
gpio_config_t int_pin_conf = {};
// delta_pin_conf.intr_type = GPIO_INTR_LOW_LEVEL;
int_pin_conf.mode = GPIO_MODE_INPUT;
// GPIO 0
int_pin_conf.pin_bit_mask = (1ULL<<WIRES_PIN_INTERUPT);
int_pin_conf.pin_bit_mask = (1ULL<<WIRES_PIN_DELTA);
int_pin_conf.pull_up_en = GPIO_PULLUP_ENABLE;
gpio_config(&int_pin_conf);
xTaskCreate(poll_wires_task, "poll_wires", 4096, NULL, 10, NULL);
}
void strike(char* reason) {
@@ -48,39 +55,34 @@ static uint8_t read_buf[8];
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)));
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];
}
static void receive_wires(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];
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)));
}
static void receive_button(void) {
uint8_t reg = 2;
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)));
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];
button_state = new_button;
}
static void poll_wires_task(void *arg) {
while (1) {
bool new_data = gpio_get_level(WIRES_PIN_INTERUPT) == 0;
bool new_data = gpio_get_level(WIRES_PIN_DELTA) == 0;
if (new_data) {
if (delta == 0) ESP_LOGW(WIRES_TAG, "int pin was low, but int register returned 0");
uint8_t delta = receive_delta();
if (delta & (1 << DELTA_BIT_KP)) {
receive_keypad();
// ESP_LOGI(_TAG, "keypad: %d", keypad_state);
}
if (delta == 0) ESP_LOGW(WIRES_TAG, "delta pin was low, but delta register returned 0");
if (delta & (1 << DELTA_BIT_BUTTON)) {
receive_button();
// ESP_LOGI(_TAG, "button: %d", button_state);
}
if (delta & (1 << DELTA_BIT_WIRES)) receive_wires();
if (delta & (1 << DELTA_BIT_BUTTON)) receive_button();
}
vTaskDelay(pdMS_TO_TICKS(10));
}