big merge

This commit is contained in:
Philippe G
2021-12-18 21:04:23 -08:00
parent 955692f8ad
commit 898998efb0
583 changed files with 84472 additions and 1965 deletions

View File

@@ -21,16 +21,17 @@
#include "esp_task.h"
#include "driver/gpio.h"
#include "driver/rmt.h"
#include "gpio_exp.h"
#include "buttons.h"
#include "rotary_encoder.h"
#include "globdefs.h"
static const char * TAG = "buttons";
static int n_buttons = 0;
static EXT_RAM_ATTR int n_buttons;
#define BUTTON_STACK_SIZE 4096
#define MAX_BUTTONS 16
#define MAX_BUTTONS 32
#define DEBOUNCE 50
#define BUTTON_QUEUE_LEN 10
@@ -47,6 +48,7 @@ static EXT_RAM_ATTR struct button_s {
TimerHandle_t timer;
} buttons[MAX_BUTTONS];
// can't use EXT_RAM_ATTR for initialized structure
static struct {
int gpio, level;
struct button_s *button;
@@ -67,10 +69,11 @@ static EXT_RAM_ATTR struct {
infrared_handler handler;
} infrared;
static xQueueHandle button_evt_queue;
static QueueSetHandle_t common_queue_set;
static EXT_RAM_ATTR QueueHandle_t button_queue;
static EXT_RAM_ATTR QueueSetHandle_t common_queue_set;
static void buttons_task(void* arg);
static void buttons_handler(struct button_s *button, int level);
/****************************************************************************************
* Start task needed by button,s rotaty and infrared
@@ -86,40 +89,33 @@ static void common_task_init(void) {
}
/****************************************************************************************
* GPIO low-level handler
* GPIO low-level ISR handler
*/
static void IRAM_ATTR gpio_isr_handler(void* arg)
{
struct button_s *button = (struct button_s*) arg;
BaseType_t woken = pdFALSE;
if (xTimerGetPeriod(button->timer) > button->debounce / portTICK_RATE_MS) xTimerChangePeriodFromISR(button->timer, button->debounce / portTICK_RATE_MS, &woken); // does that restart the timer?
else xTimerResetFromISR(button->timer, &woken);
if (xTimerGetPeriod(button->timer) > pdMS_TO_TICKS(button->debounce)) {
if (button->gpio < GPIO_NUM_MAX) xTimerChangePeriodFromISR(button->timer, pdMS_TO_TICKS(button->debounce), &woken);
else xTimerChangePeriod(button->timer, pdMS_TO_TICKS(button->debounce), pdMS_TO_TICKS(10));
} else {
if (button->gpio < GPIO_NUM_MAX) xTimerResetFromISR(button->timer, &woken);
else xTimerReset(button->timer, portMAX_DELAY);
}
if (woken) portYIELD_FROM_ISR();
ESP_EARLY_LOGD(TAG, "INT gpio %u level %u", button->gpio, button->level);
}
/****************************************************************************************
* Buttons debounce/longpress timer
*/
static void buttons_timer( TimerHandle_t xTimer ) {
static void buttons_timer_handler( TimerHandle_t xTimer ) {
struct button_s *button = (struct button_s*) pvTimerGetTimerID (xTimer);
button->level = gpio_get_level(button->gpio);
if (button->shifter && button->shifter->type == button->shifter->level) button->shifter->shifting = true;
if (button->long_press && !button->long_timer && button->level == button->type) {
// detect a long press, so hold event generation
ESP_LOGD(TAG, "setting long timer gpio:%u level:%u", button->gpio, button->level);
xTimerChangePeriod(xTimer, button->long_press / portTICK_RATE_MS, 0);
button->long_timer = true;
} else {
// send a button pressed/released event (content is copied in queue)
ESP_LOGD(TAG, "sending event for gpio:%u level:%u", button->gpio, button->level);
// queue will have a copy of button's context
xQueueSend(button_evt_queue, button, 0);
button->long_timer = false;
}
// if this is an expanded GPIO, must give cache a chance
buttons_handler(button, gpio_exp_get_level(button->gpio, (button->debounce * 3) / 2, NULL));
}
/****************************************************************************************
@@ -133,11 +129,33 @@ static void buttons_polling( TimerHandle_t xTimer ) {
if (level != polled_gpio[i].level) {
polled_gpio[i].level = level;
buttons_timer(polled_gpio[i].button->timer);
buttons_handler(polled_gpio[i].button, level);
}
}
}
/****************************************************************************************
* Buttons timer handler for press/longpress
*/
static void buttons_handler(struct button_s *button, int level) {
button->level = level;
if (button->shifter && button->shifter->type == button->shifter->level) button->shifter->shifting = true;
if (button->long_press && !button->long_timer && button->level == button->type) {
// detect a long press, so hold event generation
ESP_LOGD(TAG, "setting long timer gpio:%u level:%u", button->gpio, button->level);
xTimerChangePeriod(button->timer, button->long_press / portTICK_RATE_MS, 0);
button->long_timer = true;
} else {
// send a button pressed/released event (content is copied in queue)
ESP_LOGD(TAG, "sending event for gpio:%u level:%u", button->gpio, button->level);
// queue will have a copy of button's context
xQueueSend(button_queue, button, 0);
button->long_timer = false;
}
}
/****************************************************************************************
* Tasks that calls the appropriate functions when buttons are pressed
*/
@@ -150,13 +168,13 @@ static void buttons_task(void* arg) {
// wait on button, rotary and infrared queues
if ((xActivatedMember = xQueueSelectFromSet( common_queue_set, portMAX_DELAY )) == NULL) continue;
if (xActivatedMember == button_evt_queue) {
if (xActivatedMember == button_queue) {
struct button_s button;
button_event_e event;
button_press_e press;
// received a button event
xQueueReceive(button_evt_queue, &button, 0);
xQueueReceive(button_queue, &button, 0);
event = (button.level == button.type) ? BUTTON_PRESSED : BUTTON_RELEASED;
@@ -175,18 +193,18 @@ static void buttons_task(void* arg) {
if (event == BUTTON_RELEASED) {
// early release of a long-press button, send press/release
if (!button.shifting) {
(*button.handler)(button.client, BUTTON_PRESSED, press, false);
(*button.handler)(button.client, BUTTON_RELEASED, press, false);
button.handler(button.client, BUTTON_PRESSED, press, false);
button.handler(button.client, BUTTON_RELEASED, press, false);
}
// button is a copy, so need to go to real context
button.self->shifting = false;
} else if (!button.shifting) {
// normal long press and not shifting so don't discard
(*button.handler)(button.client, BUTTON_PRESSED, press, true);
button.handler(button.client, BUTTON_PRESSED, press, true);
}
} else {
// normal press/release of a button or release of a long-press button
if (!button.shifting) (*button.handler)(button.client, event, press, button.long_press);
if (!button.shifting) button.handler(button.client, event, press, button.long_press);
// button is a copy, so need to go to real context
button.self->shifting = false;
}
@@ -195,12 +213,12 @@ static void buttons_task(void* arg) {
// received a rotary event
xQueueReceive(rotary.queue, &event, 0);
ESP_LOGD(TAG, "Event: position %d, direction %s", event.state.position,
event.state.direction ? (event.state.direction == ROTARY_ENCODER_DIRECTION_CLOCKWISE ? "CW" : "CCW") : "NOT_SET");
event.state.direction ? (event.state.direction == ROTARY_ENCODER_DIRECTION_CLOCKWISE ? "CW" : "CCW") : "NOT_SET");
rotary.handler(rotary.client, event.state.direction == ROTARY_ENCODER_DIRECTION_CLOCKWISE ?
ROTARY_RIGHT : ROTARY_LEFT, false);
ROTARY_RIGHT : ROTARY_LEFT, false);
} else {
// this is IR
infrared_receive(infrared.rb, infrared.handler);
@@ -224,9 +242,9 @@ void button_create(void *client, int gpio, int type, bool pull, int debounce, bu
ESP_LOGI(TAG, "Creating button using GPIO %u, type %u, pull-up/down %u, long press %u shifter %d", gpio, type, pull, long_press, shifter_gpio);
if (!n_buttons) {
button_evt_queue = xQueueCreate(BUTTON_QUEUE_LEN, sizeof(struct button_s));
button_queue = xQueueCreate(BUTTON_QUEUE_LEN, sizeof(struct button_s));
common_task_init();
xQueueAddToSet( button_evt_queue, common_queue_set );
xQueueAddToSet( button_queue, common_queue_set );
}
// just in case this structure is allocated in a future release
@@ -240,7 +258,7 @@ void button_create(void *client, int gpio, int type, bool pull, int debounce, bu
buttons[n_buttons].long_press = long_press;
buttons[n_buttons].shifter_gpio = shifter_gpio;
buttons[n_buttons].type = type;
buttons[n_buttons].timer = xTimerCreate("buttonTimer", buttons[n_buttons].debounce / portTICK_RATE_MS, pdFALSE, (void *) &buttons[n_buttons], buttons_timer);
buttons[n_buttons].timer = xTimerCreate("buttonTimer", buttons[n_buttons].debounce / portTICK_RATE_MS, pdFALSE, (void *) &buttons[n_buttons], buttons_timer_handler);
buttons[n_buttons].self = buttons + n_buttons;
for (int i = 0; i < n_buttons; i++) {
@@ -257,24 +275,21 @@ void button_create(void *client, int gpio, int type, bool pull, int debounce, bu
}
}
gpio_pad_select_gpio(gpio);
gpio_set_direction(gpio, GPIO_MODE_INPUT);
// we need any edge detection
gpio_set_intr_type(gpio, GPIO_INTR_ANYEDGE);
gpio_pad_select_gpio_x(gpio);
gpio_set_direction_x(gpio, GPIO_MODE_INPUT);
// do we need pullup or pulldown
if (pull) {
if (GPIO_IS_VALID_OUTPUT_GPIO(gpio)) {
if (type == BUTTON_LOW) gpio_set_pull_mode(gpio, GPIO_PULLUP_ONLY);
else gpio_set_pull_mode(gpio, GPIO_PULLDOWN_ONLY);
if (GPIO_IS_VALID_OUTPUT_GPIO(gpio) || gpio >= GPIO_NUM_MAX) {
if (type == BUTTON_LOW) gpio_set_pull_mode_x(gpio, GPIO_PULLUP_ONLY);
else gpio_set_pull_mode_x(gpio, GPIO_PULLDOWN_ONLY);
} else {
ESP_LOGW(TAG, "cannot set pull up/down for gpio %u", gpio);
}
}
// and initialize level ...
buttons[n_buttons].level = gpio_get_level(gpio);
buttons[n_buttons].level = gpio_get_level_x(gpio);
// nasty ESP32 bug: fire-up constantly INT on GPIO 36/39 if ADC1, AMP, Hall used which WiFi does when PS is activated
for (int i = 0; polled_gpio[i].gpio != -1; i++) if (polled_gpio[i].gpio == gpio) {
@@ -282,19 +297,21 @@ void button_create(void *client, int gpio, int type, bool pull, int debounce, bu
polled_timer = xTimerCreate("buttonsPolling", 100 / portTICK_RATE_MS, pdTRUE, polled_gpio, buttons_polling);
xTimerStart(polled_timer, portMAX_DELAY);
}
polled_gpio[i].button = buttons + n_buttons;
polled_gpio[i].level = gpio_get_level(gpio);
ESP_LOGW(TAG, "creating polled gpio %u, level %u", gpio, polled_gpio[i].level);
gpio = -1;
break;
}
// only create timers and ISR is this is not a polled gpio
// only create ISR if this is not a polled gpio
if (gpio != -1) {
gpio_isr_handler_add(gpio, gpio_isr_handler, (void*) &buttons[n_buttons]);
gpio_intr_enable(gpio);
// we need any edge detection
gpio_set_intr_type_x(gpio, GPIO_INTR_ANYEDGE);
gpio_isr_handler_add_x(gpio, gpio_isr_handler, buttons + n_buttons);
gpio_intr_enable_x(gpio);
}
n_buttons++;
@@ -362,7 +379,7 @@ void *button_remap(void *client, int gpio, button_handler handler, int long_pres
}
/****************************************************************************************
* Create rotary encoder
* Rotary encoder handler
*/
static void rotary_button_handler(void *id, button_event_e event, button_press_e mode, bool long_press) {
ESP_LOGI(TAG, "Rotary push-button %d", event);