blk_box_tc/main/drivers/wires.hpp
2024-08-02 17:18:06 -05:00

91 lines
2.6 KiB
C++

#ifndef WIRES_HPP
#define WIRES_HPP
#define WIRES_PIN_INTERUPT GPIO_NUM_2
#define WIRES_I2C_NUM I2C_NUM_1
#define WIRES_I2C_ADDR 125
static const char *WIRES_TAG = "wires";
uint8_t wires_states;
uint8_t wires_cut;
static void poll_wires_task(void *arg);
void init_wires(void) {
i2c_config_t wires_conf = {
.mode = I2C_MODE_MASTER,
.sda_io_num = GPIO_NUM_41,
.scl_io_num = GPIO_NUM_42,
.sda_pullup_en = GPIO_PULLUP_ENABLE,
.scl_pullup_en = GPIO_PULLUP_ENABLE,
.master = {
.clk_speed = 100000,
}
};
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.pull_up_en = GPIO_PULLUP_ENABLE;
gpio_config(&int_pin_conf);
}
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)));
}
/// read buffer
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)));
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];
}
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)));
uint8_t new_button = read_buf[0];
}
static void poll_wires_task(void *arg) {
while (1) {
bool new_data = gpio_get_level(WIRES_PIN_INTERUPT) == 0;
if (new_data) {
if (delta == 0) ESP_LOGW(WIRES_TAG, "int pin was low, but int 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 /* WIRES_HPP */