Files

100 lines
2.2 KiB
C

#include "hal_input.h"
#include "driver/gpio.h"
#include "esp_log.h"
static const char *TAG = "input";
#define ROWS 5
#define COLS 4
static const gpio_num_t s_row_pins[ROWS] = {19, 18, 5, 17, 16};
static const gpio_num_t s_col_pins[COLS] = {15, 2, 0, 4};
static const char s_keys[ROWS][COLS] = {
{'F', 'E', '#', '*'},
{'1', '2', '3', 'U'},
{'4', '5', '6', 'D'},
{'7', '8', '9', 'X'},
{'L', '0', 'R', 'E'}};
static bool s_inited;
static char s_held;
static void cols_idle_high(void)
{
for (int c = 0; c < COLS; c++) {
gpio_set_level(s_col_pins[c], 1);
}
}
static char scan_once(void)
{
for (int c = 0; c < COLS; c++) {
cols_idle_high();
gpio_set_level(s_col_pins[c], 0);
for (int r = 0; r < ROWS; r++) {
if (gpio_get_level(s_row_pins[r]) == 0) {
cols_idle_high();
return s_keys[r][c];
}
}
}
cols_idle_high();
return 0;
}
void hal_input_init(void)
{
if (s_inited) {
return;
}
for (int r = 0; r < ROWS; r++) {
gpio_config_t io = {
.pin_bit_mask = 1ULL << s_row_pins[r],
.mode = GPIO_MODE_INPUT,
.pull_up_en = GPIO_PULLUP_ENABLE,
.pull_down_en = GPIO_PULLDOWN_DISABLE,
.intr_type = GPIO_INTR_DISABLE,
};
gpio_config(&io);
}
for (int c = 0; c < COLS; c++) {
gpio_config_t io = {
.pin_bit_mask = 1ULL << s_col_pins[c],
.mode = GPIO_MODE_OUTPUT,
.pull_up_en = GPIO_PULLUP_DISABLE,
.pull_down_en = GPIO_PULLDOWN_DISABLE,
.intr_type = GPIO_INTR_DISABLE,
};
gpio_config(&io);
gpio_set_level(s_col_pins[c], 1);
}
s_held = 0;
s_inited = true;
ESP_LOGI(TAG, "init 5x4 GPIO keypad map byte-identical to src/config.cpp");
}
bool hal_input_pop_key(ui_raw_key_t *out)
{
char k = scan_once();
if (k == 0) {
s_held = 0;
return false;
}
if (k == s_held) {
return false;
}
s_held = k;
if (out != NULL) {
out->key = k;
}
return true;
}
bool hal_input_pop_touch(ui_raw_touch_t *out)
{
(void)out;
return false;
}