2023-02-18 15:19:26 +08:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2023-02-24 21:29:15 +08:00
|
|
|
#include "config_key.h"
|
2023-02-18 15:19:26 +08:00
|
|
|
#include "driver/gpio.h"
|
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/queue.h"
|
|
|
|
#include "freertos/task.h"
|
2023-05-03 18:10:21 +08:00
|
|
|
#include "gui.c"
|
2023-02-18 15:19:26 +08:00
|
|
|
#include "light.c"
|
2023-03-05 21:47:20 +08:00
|
|
|
#include "pca9555.c"
|
|
|
|
|
|
|
|
#define ENCODER_0_CLK_PORT_IO CONFIG_ENCODER_0_CLK_PORT_IO
|
|
|
|
#define ENCODER_0_DT_PORT_IO CONFIG_ENCODER_0_DT_PORT_IO
|
|
|
|
#define ENCODER_0_SW_PORT_IO CONFIG_ENCODER_0_SW_PORT_IO
|
|
|
|
#define ENCODER_1_CLK_PORT_IO CONFIG_ENCODER_1_CLK_PORT_IO
|
|
|
|
#define ENCODER_1_DT_PORT_IO CONFIG_ENCODER_1_DT_PORT_IO
|
|
|
|
#define ENCODER_1_SW_PORT_IO CONFIG_ENCODER_1_SW_PORT_IO
|
|
|
|
#define ENCODER_INT_GPIO GPIO_NUM_3
|
2023-02-18 15:19:26 +08:00
|
|
|
#define ESP_INTR_FLAG_DEFAULT 0
|
|
|
|
|
2023-03-05 21:47:20 +08:00
|
|
|
#define RISING_EDGE_NOT_ROTATING 0b00000000
|
|
|
|
#define RISING_EDGE_CLOCKWISE 0b10000000
|
|
|
|
#define RISING_EDGE_COUNTER_CLOCKWISE 0b11000000
|
|
|
|
#define RISING_EDGE_ROTATING_MASK 0b11000000
|
|
|
|
|
|
|
|
#define FALLING_EDGE_NOT_ROTATING 0b00000000
|
|
|
|
#define FALLING_EDGE_CLOCKWISE 0b00100000
|
|
|
|
#define FALLING_EDGE_COUNTER_CLOCKWISE 0b00110000
|
|
|
|
#define FALLING_EDGE_ROTATING_MASK 0b00110000
|
2023-02-18 15:19:26 +08:00
|
|
|
|
|
|
|
static const char *UI_INPUT_TAG = "UiInput";
|
|
|
|
|
2023-04-24 17:47:37 +08:00
|
|
|
static QueueHandle_t ui_input_event = NULL;
|
|
|
|
static QueueHandle_t ui_input_raw_event = NULL;
|
2023-02-18 15:19:26 +08:00
|
|
|
|
2023-03-05 21:47:20 +08:00
|
|
|
typedef struct encoder_state {
|
|
|
|
e_ui_input_raw_key_t key;
|
|
|
|
uint8_t bits; // rising_edge_rotation_direction,
|
|
|
|
// falling_edge_rotation_direction, dt, clk, sw
|
|
|
|
uint8_t value; // dt, clk, sw
|
|
|
|
} encoder_state_t;
|
|
|
|
|
|
|
|
static encoder_state_t encoder_0_state = {.key = ui_input_raw_key_encoder_0,
|
|
|
|
.bits = 0};
|
|
|
|
static encoder_state_t encoder_1_state = {.key = ui_input_raw_key_encoder_1,
|
|
|
|
.bits = 0};
|
2023-02-18 15:19:26 +08:00
|
|
|
|
2023-03-05 21:47:20 +08:00
|
|
|
uint8_t level_byte;
|
|
|
|
int8_t delta = 0;
|
2023-05-07 21:40:11 +08:00
|
|
|
int64_t ec0_last_time = 0;
|
|
|
|
int64_t ec1_last_time = 0;
|
|
|
|
int64_t ec0_interval = 0;
|
|
|
|
int64_t ec1_interval = 0;
|
2023-02-18 15:19:26 +08:00
|
|
|
|
|
|
|
static void IRAM_ATTR gpio_isr_handler(void *arg) {
|
2023-03-05 21:47:20 +08:00
|
|
|
xQueueSendFromISR(ui_input_raw_event, NULL, NULL);
|
2023-02-18 15:19:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void ui_input_update_embedded_display(void *arg) {
|
|
|
|
s_ui_input_t input;
|
2023-04-24 17:47:37 +08:00
|
|
|
char changing_str[20] = "NC";
|
2023-02-18 15:19:26 +08:00
|
|
|
for (;;) {
|
|
|
|
if (xQueueReceive(ui_input_event, &input, portMAX_DELAY)) {
|
|
|
|
switch (input.key) {
|
|
|
|
case ui_input_key_display_0_brightness:
|
|
|
|
sprintf(changing_str, "Dis0: % 3d", input.value);
|
|
|
|
break;
|
|
|
|
case ui_input_key_display_1_brightness:
|
|
|
|
sprintf(changing_str, "Dis1: % 3d", input.value);
|
|
|
|
break;
|
|
|
|
case ui_input_key_computer_volume:
|
|
|
|
sprintf(changing_str, "CVol: % 3d", input.value);
|
|
|
|
break;
|
|
|
|
case ui_input_key_display_ambient_lighting_level:
|
|
|
|
break;
|
|
|
|
case ui_input_key_display_ambient_lighting_mode:
|
|
|
|
sprintf(changing_str, "ALMd: % 3d", input.value);
|
|
|
|
break;
|
|
|
|
case ui_input_key_display_0_mode:
|
|
|
|
sprintf(changing_str, "Dis0M: % 2d", input.value);
|
|
|
|
break;
|
|
|
|
case ui_input_key_display_1_mode:
|
|
|
|
sprintf(changing_str, "Dis1M: % 2d", input.value);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
strcpy(changing_str, "NC");
|
|
|
|
break;
|
|
|
|
}
|
2023-05-03 23:55:54 +08:00
|
|
|
|
|
|
|
ESP_LOGI(UI_INPUT_TAG, "%s", changing_str);
|
2023-02-18 15:19:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-05 21:47:20 +08:00
|
|
|
static void encoder_value_change(encoder_state_t *state) {
|
|
|
|
if ((state->value >> 1 & 1) == (state->bits >> 1 & 1)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
state->bits = (state->bits & 0b11111000) | (state->value & 0b111);
|
|
|
|
if (state->value >> 1 & 1) {
|
|
|
|
state->bits = (state->bits & (~RISING_EDGE_ROTATING_MASK)) | 0x80 |
|
|
|
|
~(state->value >> 2 & 1) << 6;
|
|
|
|
if (((state->bits & FALLING_EDGE_ROTATING_MASK) >> 4) ==
|
|
|
|
((state->bits & RISING_EDGE_ROTATING_MASK) >> 6)) {
|
|
|
|
if ((state->bits & RISING_EDGE_ROTATING_MASK) == RISING_EDGE_CLOCKWISE) {
|
|
|
|
delta = -1;
|
2023-02-18 15:19:26 +08:00
|
|
|
} else {
|
2023-03-05 21:47:20 +08:00
|
|
|
delta = 1;
|
2023-02-18 15:19:26 +08:00
|
|
|
}
|
2023-03-05 21:47:20 +08:00
|
|
|
state->bits = (state->bits & (~FALLING_EDGE_ROTATING_MASK)) |
|
|
|
|
FALLING_EDGE_NOT_ROTATING;
|
|
|
|
} else {
|
|
|
|
delta = 0;
|
|
|
|
if ((state->bits & FALLING_EDGE_ROTATING_MASK) !=
|
|
|
|
FALLING_EDGE_NOT_ROTATING) {
|
|
|
|
state->bits = (state->bits & (~FALLING_EDGE_ROTATING_MASK)) |
|
|
|
|
FALLING_EDGE_NOT_ROTATING;
|
2023-02-18 15:19:26 +08:00
|
|
|
}
|
2023-03-05 21:47:20 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
state->bits = (state->bits & (~FALLING_EDGE_ROTATING_MASK)) | 0x20 |
|
|
|
|
(state->value >> 2 & 1) << 4;
|
|
|
|
if (((state->bits & FALLING_EDGE_ROTATING_MASK) >> 4) ==
|
|
|
|
((state->bits & RISING_EDGE_ROTATING_MASK) >> 6)) {
|
|
|
|
if ((state->bits & FALLING_EDGE_ROTATING_MASK) ==
|
|
|
|
FALLING_EDGE_CLOCKWISE) {
|
|
|
|
delta = -1;
|
|
|
|
} else {
|
|
|
|
delta = 1;
|
|
|
|
}
|
|
|
|
state->bits = (state->bits & (~RISING_EDGE_ROTATING_MASK)) |
|
|
|
|
RISING_EDGE_NOT_ROTATING;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
delta = 0;
|
|
|
|
if ((state->bits & RISING_EDGE_ROTATING_MASK) !=
|
|
|
|
RISING_EDGE_NOT_ROTATING) {
|
|
|
|
state->bits = (state->bits & (~RISING_EDGE_ROTATING_MASK)) |
|
|
|
|
RISING_EDGE_NOT_ROTATING;
|
2023-02-18 15:19:26 +08:00
|
|
|
}
|
2023-03-05 21:47:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (delta == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
s_ui_input_t event = {.value = delta};
|
|
|
|
if (state->key == ui_input_raw_key_encoder_0) {
|
2023-05-07 21:40:11 +08:00
|
|
|
ec0_interval = esp_timer_get_time() - ec0_last_time;
|
|
|
|
if (ec0_interval < 10000) { // 100ms
|
|
|
|
event.value = event.value * 5;
|
|
|
|
} else if (ec0_interval < 20000) { // 100ms
|
|
|
|
event.value = event.value * 4;
|
|
|
|
} else if (ec0_interval < 50000) { // 100ms
|
|
|
|
event.value = event.value * 3;
|
|
|
|
} else if (ec0_interval < 100000) { // 100ms
|
|
|
|
event.value = event.value * 2;
|
|
|
|
}
|
|
|
|
ec0_last_time = esp_timer_get_time();
|
|
|
|
|
2023-03-05 21:47:20 +08:00
|
|
|
if (state->value & 1) {
|
|
|
|
event.key = ui_input_key_computer_volume;
|
|
|
|
} else {
|
2023-05-07 21:40:11 +08:00
|
|
|
event.key = ui_input_key_display_1_brightness;
|
2023-03-05 21:47:20 +08:00
|
|
|
}
|
|
|
|
} else if (state->key == ui_input_raw_key_encoder_1) {
|
|
|
|
if (state->value & 1) {
|
2023-05-07 21:40:11 +08:00
|
|
|
ec1_interval = esp_timer_get_time() - ec1_last_time;
|
|
|
|
if (ec1_interval < 20000) { // 100ms
|
|
|
|
event.value = event.value * 7;
|
|
|
|
} else if (ec1_interval < 30000) { // 100ms
|
|
|
|
event.value = event.value * 5;
|
|
|
|
} else if (ec1_interval < 40000) { // 100ms
|
|
|
|
event.value = event.value * 3;
|
|
|
|
} else if (ec1_interval < 50000) {
|
|
|
|
event.value = event.value * 2;
|
|
|
|
}
|
|
|
|
ec1_last_time = esp_timer_get_time();
|
|
|
|
|
2023-03-05 21:47:20 +08:00
|
|
|
event.key = ui_input_key_display_ambient_lighting_level;
|
2023-05-07 21:40:11 +08:00
|
|
|
led_strip_set_brightness(display_ambient_lighting_level + event.value);
|
2023-05-03 18:10:21 +08:00
|
|
|
gui_change_strip_level(display_ambient_lighting_level);
|
2023-03-05 21:47:20 +08:00
|
|
|
} else {
|
2023-05-07 21:40:11 +08:00
|
|
|
event.key = ui_input_key_display_0_brightness;
|
2023-03-05 21:47:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
xQueueSend(ui_input_event, &event, NULL);
|
2023-05-07 22:09:16 +08:00
|
|
|
ESP_LOGD(UI_INPUT_TAG, "key: %d, delta: %d. delay: %lld, %lld", state->key,
|
2023-05-07 21:40:11 +08:00
|
|
|
event.value, ec0_interval, ec1_interval);
|
2023-03-05 21:47:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void ui_input_raw_handler(void *arg) {
|
|
|
|
for (;;) {
|
|
|
|
if (xQueueReceive(ui_input_raw_event, NULL, portMAX_DELAY)) {
|
|
|
|
pca9555_read_one_input(PCA95555_CMD_INPUT_PORT_1, &level_byte);
|
|
|
|
|
|
|
|
encoder_0_state.value = level_byte & 0x7;
|
|
|
|
encoder_value_change(&encoder_0_state);
|
|
|
|
encoder_1_state.value = level_byte >> 3 & 0x7;
|
|
|
|
encoder_value_change(&encoder_1_state);
|
2023-02-18 15:19:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ui_input_init(void) {
|
|
|
|
// zero-initialize the config structure.
|
|
|
|
gpio_config_t io_conf = {};
|
|
|
|
|
|
|
|
io_conf.mode = GPIO_MODE_INPUT;
|
|
|
|
io_conf.pull_up_en = 1;
|
2023-02-26 23:43:17 +08:00
|
|
|
io_conf.pull_down_en = 0;
|
2023-02-18 15:19:26 +08:00
|
|
|
// interrupt of rising edge
|
2023-03-05 21:47:20 +08:00
|
|
|
io_conf.intr_type = GPIO_INTR_NEGEDGE;
|
|
|
|
io_conf.pin_bit_mask = 1ULL << ENCODER_INT_GPIO;
|
2023-02-18 15:19:26 +08:00
|
|
|
gpio_config(&io_conf);
|
|
|
|
|
|
|
|
// start encoder task
|
2023-05-07 21:40:11 +08:00
|
|
|
ui_input_event = xQueueCreate(5, sizeof(s_ui_input_t));
|
2023-03-05 21:47:20 +08:00
|
|
|
ui_input_raw_event = xQueueCreate(10, 0);
|
2023-02-18 15:19:26 +08:00
|
|
|
|
|
|
|
// hook isr handler for specific gpio pin
|
2023-03-05 21:47:20 +08:00
|
|
|
gpio_isr_handler_add(ENCODER_INT_GPIO, gpio_isr_handler, NULL);
|
2023-02-18 15:19:26 +08:00
|
|
|
|
2023-05-03 23:55:54 +08:00
|
|
|
// xTaskCreate(ui_input_update_embedded_display, "ui_input_event", 2048, NULL,
|
|
|
|
// 10, NULL);
|
2023-02-18 15:19:26 +08:00
|
|
|
xTaskCreate(ui_input_raw_handler, "ui_input_event", 2048, NULL, 10, NULL);
|
|
|
|
}
|