2
0

buttons.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 = "audio_controls";
  34. static int n_buttons = 0;
  35. #define MAX_BUTTONS 16
  36. #define DEBOUNCE 50
  37. static struct button_s {
  38. int gpio, index;
  39. button_handler handler, long_handler, shift_handler;
  40. struct button_s *shifter;
  41. int long_press;
  42. bool long_timer, shifted, shifting;
  43. int type, level;
  44. TimerHandle_t timer;
  45. } buttons[MAX_BUTTONS];
  46. static xQueueHandle button_evt_queue = NULL;
  47. static void buttons_task(void* arg);
  48. /****************************************************************************************
  49. * GPIO low-level handler
  50. */
  51. static void IRAM_ATTR gpio_isr_handler(void* arg)
  52. {
  53. struct button_s *button = (struct button_s*) arg;
  54. BaseType_t woken = pdFALSE;
  55. if (xTimerGetPeriod(button->timer) > DEBOUNCE / portTICK_RATE_MS) xTimerChangePeriodFromISR(button->timer, DEBOUNCE / portTICK_RATE_MS, &woken); // does that restart the timer?
  56. else xTimerResetFromISR(button->timer, &woken);
  57. // ESP_EARLY_LOGD(TAG, "INT gpio %u level %u", button->gpio, button->level);
  58. }
  59. /****************************************************************************************
  60. * Buttons debounce/longpress timer
  61. */
  62. static void buttons_timer( TimerHandle_t xTimer ) {
  63. struct button_s *button = (struct button_s*) pvTimerGetTimerID (xTimer);
  64. button->level = gpio_get_level(button->gpio);
  65. if (button->long_press && !button->long_timer && button->level == button->type) {
  66. // detect a long press, so hold event generation
  67. ESP_LOGD(TAG, "setting long timer gpio:%u level:%u", button->gpio, button->level);
  68. xTimerChangePeriod(xTimer, button->long_press / portTICK_RATE_MS, 0);
  69. button->long_timer = true;
  70. } else {
  71. // send a button pressed/released event (content is copied in queue)
  72. ESP_LOGD(TAG, "sending event for gpio:%u level:%u", button->gpio, button->level);
  73. // queue will have a copy of button's context
  74. xQueueSend(button_evt_queue, button, 0);
  75. button->long_timer = false;
  76. }
  77. }
  78. /****************************************************************************************
  79. * Tasks that calls the appropriate functions when buttons are pressed
  80. */
  81. static void buttons_task(void* arg) {
  82. ESP_LOGI(TAG, "starting button tasks");
  83. while (1) {
  84. struct button_s button;
  85. button_event_e event;
  86. bool shifted = false;
  87. if (!xQueueReceive(button_evt_queue, &button, portMAX_DELAY)) continue;
  88. event = (button.level == button.type) ? BUTTON_PRESSED : BUTTON_RELEASED;
  89. 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);
  90. // find if shifting is activated
  91. if (button.shifter && button.shifter->type == button.shifter->level) {
  92. button.shifter->shifting = true;
  93. shifted = true;
  94. }
  95. /*
  96. long_timer will be set either because we truly have a long press
  97. or we have a release before the long press timer elapsed, so two
  98. events shall be sent
  99. */
  100. if (button.long_timer) {
  101. if (event == BUTTON_RELEASED) {
  102. // early release of a long-press button, send press/release
  103. if (shifted) {
  104. (*button.shift_handler)(BUTTON_PRESSED, BUTTON_SHIFTED);
  105. (*button.shift_handler)(BUTTON_RELEASED, BUTTON_SHIFTED);
  106. } else if (!button.shifting) {
  107. (*button.handler)(BUTTON_PRESSED, BUTTON_NORMAL);
  108. (*button.handler)(BUTTON_RELEASED, BUTTON_NORMAL);
  109. }
  110. // button is a copy, so need to go to real context
  111. buttons[button.index].shifting = false;
  112. } else if (!button.shifting) {
  113. /*
  114. normal long press and not shifting so don't discard but there
  115. is no long-press when shifted
  116. */
  117. if (shifted) (*button.shift_handler)(BUTTON_PRESSED, BUTTON_SHIFTED);
  118. else (*button.long_handler)(BUTTON_PRESSED, BUTTON_LONG);
  119. }
  120. } else if (shifted) {
  121. // we are shifted
  122. (*button.shift_handler)(event, BUTTON_SHIFTED);
  123. } else {
  124. // normal press/release of a button or release of a long-press button
  125. if (!button.shifting) {
  126. if (button.long_press) (*button.long_handler)(event, BUTTON_LONG);
  127. else (*button.handler)(event, BUTTON_NORMAL);
  128. }
  129. // button is a copy, so need to go to real context
  130. buttons[button.index].shifting = false;
  131. }
  132. }
  133. }
  134. /****************************************************************************************
  135. * dummy button handler
  136. */
  137. void dummy_handler(button_event_e event, button_press_e press) {
  138. ESP_LOGW(TAG, "should not be here");
  139. }
  140. /****************************************************************************************
  141. * Create buttons
  142. */
  143. void button_create(int gpio, int type, bool pull, button_handler handler, ...) {
  144. char *param;
  145. va_list args;
  146. if (n_buttons >= MAX_BUTTONS) return;
  147. ESP_LOGI(TAG, "creating button using GPIO %u, type %u, pull-up/down %u", gpio, type, pull);
  148. if (!n_buttons) {
  149. button_evt_queue = xQueueCreate(10, sizeof(struct button_s));
  150. gpio_install_isr_service(0);
  151. xTaskCreate(buttons_task, "buttons_task", 2048, NULL, ESP_TASK_PRIO_MIN + 1, NULL);
  152. }
  153. // just in case this structure is allocated later
  154. memset(buttons + n_buttons, 0, sizeof(struct button_s));
  155. // set mandatory parameters
  156. buttons[n_buttons].gpio = gpio;
  157. buttons[n_buttons].handler = handler;
  158. buttons[n_buttons].level = -1;
  159. buttons[n_buttons].type = type;
  160. buttons[n_buttons].timer = xTimerCreate("buttonTimer", DEBOUNCE / portTICK_RATE_MS, pdFALSE, (void *) &buttons[n_buttons], buttons_timer);
  161. // little trick to find ourselves from queued copy
  162. buttons[n_buttons].index = n_buttons;
  163. va_start(args, handler);
  164. // set optional parameters
  165. while ((param = va_arg(args, char*)) != NULL) {
  166. if (!strcasecmp(param, "long")) {
  167. buttons[n_buttons].long_handler = (button_handler) va_arg(args, void*);
  168. buttons[n_buttons].long_press = va_arg(args, int);
  169. ESP_LOGI(TAG, "adding long press %u (ms)", buttons[n_buttons].long_press);
  170. } else if (!strcasecmp(param, "shift")) {
  171. buttons[n_buttons].shift_handler = (button_handler) va_arg(args, void*);
  172. int shifter_gpio = va_arg(args, int);
  173. for (int i = 0; i < n_buttons; i++) {
  174. if (buttons[i].gpio == shifter_gpio) {
  175. buttons[n_buttons].shifter = buttons + i;
  176. // a shifter must have a long-press handler
  177. if (!buttons[i].long_press) {
  178. buttons[i].long_press = 60*60*1000;
  179. buttons[i].long_handler = dummy_handler;
  180. }
  181. break;
  182. }
  183. }
  184. ESP_LOGI(TAG, "adding shifter %u", buttons[n_buttons].shifter->gpio);
  185. }
  186. }
  187. va_end(args);
  188. gpio_set_direction(gpio, GPIO_MODE_INPUT);
  189. // we need any edge detection
  190. gpio_set_intr_type(gpio, GPIO_INTR_ANYEDGE);
  191. // do we need pullup or pulldown
  192. if (pull) {
  193. if (type == BUTTON_LOW) gpio_set_pull_mode(gpio, GPIO_PULLUP_ONLY);
  194. else gpio_set_pull_mode(gpio, GPIO_PULLDOWN_ONLY);
  195. }
  196. gpio_isr_handler_add(gpio, gpio_isr_handler, (void*) &buttons[n_buttons]);
  197. gpio_intr_enable(gpio);
  198. n_buttons++;
  199. }