buttons.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * a crude button press/long-press/shift management based on GPIO
  3. *
  4. * (c) Philippe G. 2019, philippe_44@outlook.com
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <unistd.h>
  23. #include <string.h>
  24. #include "freertos/FreeRTOS.h"
  25. #include "freertos/task.h"
  26. #include "freertos/timers.h"
  27. #include "freertos/queue.h"
  28. #include "esp_system.h"
  29. #include "esp_log.h"
  30. #include "esp_task.h"
  31. #include "driver/gpio.h"
  32. #include "buttons.h"
  33. static const char * TAG = "buttons";
  34. static int n_buttons = 0;
  35. #define BUTTON_STACK_SIZE 4096
  36. #define MAX_BUTTONS 16
  37. #define DEBOUNCE 50
  38. static EXT_RAM_ATTR struct button_s {
  39. void *id;
  40. int gpio, index;
  41. int debounce;
  42. button_handler handler;
  43. struct button_s *shifter;
  44. int long_press;
  45. bool long_timer, shifted, shifting;
  46. int type, level;
  47. TimerHandle_t timer;
  48. } buttons[MAX_BUTTONS];
  49. static xQueueHandle button_evt_queue = NULL;
  50. static void buttons_task(void* arg);
  51. /****************************************************************************************
  52. * GPIO low-level handler
  53. */
  54. static void IRAM_ATTR gpio_isr_handler(void* arg)
  55. {
  56. struct button_s *button = (struct button_s*) arg;
  57. BaseType_t woken = pdFALSE;
  58. if (xTimerGetPeriod(button->timer) > button->debounce / portTICK_RATE_MS) xTimerChangePeriodFromISR(button->timer, button->debounce / portTICK_RATE_MS, &woken); // does that restart the timer?
  59. else xTimerResetFromISR(button->timer, &woken);
  60. // ESP_EARLY_LOGI(TAG, "INT gpio %u level %u", button->gpio, button->level);
  61. }
  62. /****************************************************************************************
  63. * Buttons debounce/longpress timer
  64. */
  65. static void buttons_timer( TimerHandle_t xTimer ) {
  66. struct button_s *button = (struct button_s*) pvTimerGetTimerID (xTimer);
  67. button->level = gpio_get_level(button->gpio);
  68. if (button->shifter && button->shifter->type == button->shifter->level) button->shifter->shifting = true;
  69. if (button->long_press && !button->long_timer && button->level == button->type) {
  70. // detect a long press, so hold event generation
  71. ESP_LOGD(TAG, "setting long timer gpio:%u level:%u", button->gpio, button->level);
  72. xTimerChangePeriod(xTimer, button->long_press / portTICK_RATE_MS, 0);
  73. button->long_timer = true;
  74. } else {
  75. // send a button pressed/released event (content is copied in queue)
  76. ESP_LOGD(TAG, "sending event for gpio:%u level:%u", button->gpio, button->level);
  77. // queue will have a copy of button's context
  78. xQueueSend(button_evt_queue, button, 0);
  79. button->long_timer = false;
  80. }
  81. }
  82. /****************************************************************************************
  83. * Tasks that calls the appropriate functions when buttons are pressed
  84. */
  85. static void buttons_task(void* arg) {
  86. ESP_LOGI(TAG, "starting button tasks");
  87. while (1) {
  88. struct button_s button;
  89. button_event_e event;
  90. button_press_e press;
  91. if (!xQueueReceive(button_evt_queue, &button, portMAX_DELAY)) continue;
  92. event = (button.level == button.type) ? BUTTON_PRESSED : BUTTON_RELEASED;
  93. ESP_LOGD(TAG, "received event:%u from gpio:%u level:%u (timer %u shifting %u)", event, button.gpio, button.level, button.long_timer, button.shifting);
  94. // find if shifting is activated
  95. if (button.shifter && button.shifter->type == button.shifter->level) press = BUTTON_SHIFTED;
  96. else press = BUTTON_NORMAL;
  97. /*
  98. long_timer will be set either because we truly have a long press
  99. or we have a release before the long press timer elapsed, so two
  100. events shall be sent
  101. */
  102. if (button.long_timer) {
  103. if (event == BUTTON_RELEASED) {
  104. // early release of a long-press button, send press/release
  105. if (!button.shifting) {
  106. (*button.handler)(button.id, BUTTON_PRESSED, press, false);
  107. (*button.handler)(button.id, BUTTON_RELEASED, press, false);
  108. }
  109. // button is a copy, so need to go to real context
  110. buttons[button.index].shifting = false;
  111. } else if (!button.shifting) {
  112. // normal long press and not shifting so don't discard
  113. (*button.handler)(button.id, BUTTON_PRESSED, press, true);
  114. }
  115. } else {
  116. // normal press/release of a button or release of a long-press button
  117. if (!button.shifting) (*button.handler)(button.id, event, press, button.long_press);
  118. // button is a copy, so need to go to real context
  119. buttons[button.index].shifting = false;
  120. }
  121. }
  122. }
  123. /****************************************************************************************
  124. * dummy button handler
  125. */
  126. void dummy_handler(void *id, button_event_e event, button_press_e press) {
  127. ESP_LOGW(TAG, "should not be here");
  128. }
  129. /****************************************************************************************
  130. * Create buttons
  131. */
  132. void button_create(void *id, int gpio, int type, bool pull, int debounce, button_handler handler, int long_press, int shifter_gpio) {
  133. static DRAM_ATTR StaticTask_t xTaskBuffer __attribute__ ((aligned (4)));
  134. static EXT_RAM_ATTR StackType_t xStack[BUTTON_STACK_SIZE] __attribute__ ((aligned (4)));
  135. if (n_buttons >= MAX_BUTTONS) return;
  136. ESP_LOGI(TAG, "Creating button using GPIO %u, type %u, pull-up/down %u, long press %u shifter %u", gpio, type, pull, long_press, shifter_gpio);
  137. if (!n_buttons) {
  138. button_evt_queue = xQueueCreate(10, sizeof(struct button_s));
  139. xTaskCreateStatic( (TaskFunction_t) buttons_task, "buttons_thread", BUTTON_STACK_SIZE, NULL, ESP_TASK_PRIO_MIN + 1, xStack, &xTaskBuffer);
  140. }
  141. // just in case this structure is allocated in a future release
  142. memset(buttons + n_buttons, 0, sizeof(struct button_s));
  143. // set mandatory parameters
  144. buttons[n_buttons].id = id;
  145. buttons[n_buttons].gpio = gpio;
  146. buttons[n_buttons].debounce = debounce ? debounce: DEBOUNCE;
  147. buttons[n_buttons].handler = handler;
  148. buttons[n_buttons].long_press = long_press;
  149. buttons[n_buttons].level = -1;
  150. buttons[n_buttons].type = type;
  151. buttons[n_buttons].timer = xTimerCreate("buttonTimer", buttons[n_buttons].debounce / portTICK_RATE_MS, pdFALSE, (void *) &buttons[n_buttons], buttons_timer);
  152. // little trick to find ourselves from queued copy
  153. buttons[n_buttons].index = n_buttons;
  154. for (int i = 0; i < n_buttons; i++) {
  155. if (buttons[i].gpio == shifter_gpio) {
  156. buttons[n_buttons].shifter = buttons + i;
  157. // a shifter must have a long-press handler
  158. if (!buttons[i].long_press) buttons[i].long_press = -1;
  159. break;
  160. }
  161. }
  162. gpio_pad_select_gpio(gpio);
  163. gpio_set_direction(gpio, GPIO_MODE_INPUT);
  164. // we need any edge detection
  165. gpio_set_intr_type(gpio, GPIO_INTR_ANYEDGE);
  166. // do we need pullup or pulldown
  167. if (pull) {
  168. if (type == BUTTON_LOW) gpio_set_pull_mode(gpio, GPIO_PULLUP_ONLY);
  169. else gpio_set_pull_mode(gpio, GPIO_PULLDOWN_ONLY);
  170. }
  171. gpio_isr_handler_add(gpio, gpio_isr_handler, (void*) &buttons[n_buttons]);
  172. gpio_intr_enable(gpio);
  173. n_buttons++;
  174. }