buttons.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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 software is released under the MIT License.
  7. * https://opensource.org/licenses/MIT
  8. *
  9. */
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <unistd.h>
  13. #include <string.h>
  14. #include "freertos/FreeRTOS.h"
  15. #include "freertos/task.h"
  16. #include "freertos/timers.h"
  17. #include "freertos/queue.h"
  18. #include "esp_system.h"
  19. #include "esp_log.h"
  20. #include "esp_task.h"
  21. #include "driver/gpio.h"
  22. #include "buttons.h"
  23. #include "rotary_encoder.h"
  24. #include "globdefs.h"
  25. bool gpio36_39_used;
  26. static const char * TAG = "buttons";
  27. static int n_buttons = 0;
  28. #define BUTTON_STACK_SIZE 4096
  29. #define MAX_BUTTONS 16
  30. #define DEBOUNCE 50
  31. #define BUTTON_QUEUE_LEN 10
  32. static EXT_RAM_ATTR struct button_s {
  33. void *client;
  34. int gpio;
  35. int debounce;
  36. button_handler handler;
  37. struct button_s *self, *shifter;
  38. int shifter_gpio; // this one is just for post-creation
  39. int long_press;
  40. bool long_timer, shifted, shifting;
  41. int type, level;
  42. TimerHandle_t timer;
  43. } buttons[MAX_BUTTONS];
  44. static struct {
  45. QueueHandle_t queue;
  46. void *client;
  47. rotary_encoder_info_t info;
  48. int A, B, SW;
  49. rotary_handler handler;
  50. } rotary;
  51. static xQueueHandle button_evt_queue;
  52. static QueueSetHandle_t button_queue_set;
  53. static void buttons_task(void* arg);
  54. /****************************************************************************************
  55. * GPIO low-level handler
  56. */
  57. static void IRAM_ATTR gpio_isr_handler(void* arg)
  58. {
  59. struct button_s *button = (struct button_s*) arg;
  60. BaseType_t woken = pdFALSE;
  61. if (xTimerGetPeriod(button->timer) > button->debounce / portTICK_RATE_MS) xTimerChangePeriodFromISR(button->timer, button->debounce / portTICK_RATE_MS, &woken); // does that restart the timer?
  62. else xTimerResetFromISR(button->timer, &woken);
  63. if (woken) portYIELD_FROM_ISR();
  64. ESP_EARLY_LOGD(TAG, "INT gpio %u level %u", button->gpio, button->level);
  65. }
  66. /****************************************************************************************
  67. * Buttons debounce/longpress timer
  68. */
  69. static void buttons_timer( TimerHandle_t xTimer ) {
  70. struct button_s *button = (struct button_s*) pvTimerGetTimerID (xTimer);
  71. button->level = gpio_get_level(button->gpio);
  72. if (button->shifter && button->shifter->type == button->shifter->level) button->shifter->shifting = true;
  73. if (button->long_press && !button->long_timer && button->level == button->type) {
  74. // detect a long press, so hold event generation
  75. ESP_LOGD(TAG, "setting long timer gpio:%u level:%u", button->gpio, button->level);
  76. xTimerChangePeriod(xTimer, button->long_press / portTICK_RATE_MS, 0);
  77. button->long_timer = true;
  78. } else {
  79. // send a button pressed/released event (content is copied in queue)
  80. ESP_LOGD(TAG, "sending event for gpio:%u level:%u", button->gpio, button->level);
  81. // queue will have a copy of button's context
  82. xQueueSend(button_evt_queue, button, 0);
  83. button->long_timer = false;
  84. }
  85. }
  86. /****************************************************************************************
  87. * Tasks that calls the appropriate functions when buttons are pressed
  88. */
  89. static void buttons_task(void* arg) {
  90. ESP_LOGI(TAG, "starting button tasks");
  91. while (1) {
  92. QueueSetMemberHandle_t xActivatedMember;
  93. // wait on button and rotary queues
  94. if ((xActivatedMember = xQueueSelectFromSet( button_queue_set, portMAX_DELAY )) == NULL) continue;
  95. if (xActivatedMember == button_evt_queue) {
  96. struct button_s button;
  97. button_event_e event;
  98. button_press_e press;
  99. // received a button event
  100. xQueueReceive(button_evt_queue, &button, 0);
  101. event = (button.level == button.type) ? BUTTON_PRESSED : BUTTON_RELEASED;
  102. 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);
  103. // find if shifting is activated
  104. if (button.shifter && button.shifter->type == button.shifter->level) press = BUTTON_SHIFTED;
  105. else press = BUTTON_NORMAL;
  106. /*
  107. long_timer will be set either because we truly have a long press
  108. or we have a release before the long press timer elapsed, so two
  109. events shall be sent
  110. */
  111. if (button.long_timer) {
  112. if (event == BUTTON_RELEASED) {
  113. // early release of a long-press button, send press/release
  114. if (!button.shifting) {
  115. (*button.handler)(button.client, BUTTON_PRESSED, press, false);
  116. (*button.handler)(button.client, BUTTON_RELEASED, press, false);
  117. }
  118. // button is a copy, so need to go to real context
  119. button.self->shifting = false;
  120. } else if (!button.shifting) {
  121. // normal long press and not shifting so don't discard
  122. (*button.handler)(button.client, BUTTON_PRESSED, press, true);
  123. }
  124. } else {
  125. // normal press/release of a button or release of a long-press button
  126. if (!button.shifting) (*button.handler)(button.client, event, press, button.long_press);
  127. // button is a copy, so need to go to real context
  128. button.self->shifting = false;
  129. }
  130. } else {
  131. rotary_encoder_event_t event = { 0 };
  132. // received a rotary event
  133. xQueueReceive(rotary.queue, &event, 0);
  134. ESP_LOGD(TAG, "Event: position %d, direction %s", event.state.position,
  135. event.state.direction ? (event.state.direction == ROTARY_ENCODER_DIRECTION_CLOCKWISE ? "CW" : "CCW") : "NOT_SET");
  136. rotary.handler(rotary.client, event.state.direction == ROTARY_ENCODER_DIRECTION_CLOCKWISE ?
  137. ROTARY_RIGHT : ROTARY_LEFT, false);
  138. }
  139. }
  140. }
  141. /****************************************************************************************
  142. * dummy button handler
  143. */
  144. void dummy_handler(void *id, button_event_e event, button_press_e press) {
  145. ESP_LOGW(TAG, "should not be here");
  146. }
  147. /****************************************************************************************
  148. * Create buttons
  149. */
  150. void button_create(void *client, int gpio, int type, bool pull, int debounce, button_handler handler, int long_press, int shifter_gpio) {
  151. static DRAM_ATTR StaticTask_t xTaskBuffer __attribute__ ((aligned (4)));
  152. static EXT_RAM_ATTR StackType_t xStack[BUTTON_STACK_SIZE] __attribute__ ((aligned (4)));
  153. if (n_buttons >= MAX_BUTTONS) return;
  154. 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);
  155. if (!n_buttons) {
  156. button_evt_queue = xQueueCreate(BUTTON_QUEUE_LEN, sizeof(struct button_s));
  157. if (!button_queue_set) button_queue_set = xQueueCreateSet(BUTTON_QUEUE_LEN + 1);
  158. xQueueAddToSet( button_evt_queue, button_queue_set );
  159. xTaskCreateStatic( (TaskFunction_t) buttons_task, "buttons_thread", BUTTON_STACK_SIZE, NULL, ESP_TASK_PRIO_MIN + 1, xStack, &xTaskBuffer);
  160. }
  161. // just in case this structure is allocated in a future release
  162. memset(buttons + n_buttons, 0, sizeof(struct button_s));
  163. // set mandatory parameters
  164. buttons[n_buttons].client = client;
  165. buttons[n_buttons].gpio = gpio;
  166. buttons[n_buttons].debounce = debounce ? debounce: DEBOUNCE;
  167. buttons[n_buttons].handler = handler;
  168. buttons[n_buttons].long_press = long_press;
  169. buttons[n_buttons].shifter_gpio = shifter_gpio;
  170. buttons[n_buttons].type = type;
  171. buttons[n_buttons].timer = xTimerCreate("buttonTimer", buttons[n_buttons].debounce / portTICK_RATE_MS, pdFALSE, (void *) &buttons[n_buttons], buttons_timer);
  172. buttons[n_buttons].self = buttons + n_buttons;
  173. for (int i = 0; i < n_buttons; i++) {
  174. // first try to find our shifter
  175. if (buttons[i].gpio == shifter_gpio) {
  176. buttons[n_buttons].shifter = buttons + i;
  177. // a shifter must have a long-press handler
  178. if (!buttons[i].long_press) buttons[i].long_press = -1;
  179. }
  180. // then try to see if we are a non-assigned shifter
  181. if (buttons[i].shifter_gpio == gpio) {
  182. buttons[i].shifter = buttons + n_buttons;
  183. ESP_LOGI(TAG, "post-assigned shifter gpio %u", buttons[i].gpio);
  184. }
  185. }
  186. gpio_pad_select_gpio(gpio);
  187. gpio_set_direction(gpio, GPIO_MODE_INPUT);
  188. // we need any edge detection
  189. gpio_set_intr_type(gpio, GPIO_INTR_ANYEDGE);
  190. // do we need pullup or pulldown
  191. if (pull) {
  192. if (GPIO_IS_VALID_OUTPUT_GPIO(gpio)) {
  193. if (type == BUTTON_LOW) gpio_set_pull_mode(gpio, GPIO_PULLUP_ONLY);
  194. else gpio_set_pull_mode(gpio, GPIO_PULLDOWN_ONLY);
  195. } else {
  196. ESP_LOGW(TAG, "cannot set pull up/down for gpio %u", gpio);
  197. }
  198. }
  199. // nasty ESP32 bug: fire-up constantly INT on GPIO 36/39 if ADC1, AMP, Hall used which WiFi does when PS is activated
  200. if (gpio == 36 || gpio == 39) gpio36_39_used = true;
  201. // and initialize level ...
  202. buttons[n_buttons].level = gpio_get_level(gpio);
  203. gpio_isr_handler_add(gpio, gpio_isr_handler, (void*) &buttons[n_buttons]);
  204. gpio_intr_enable(gpio);
  205. n_buttons++;
  206. }
  207. /****************************************************************************************
  208. * Get stored id
  209. */
  210. void *button_get_client(int gpio) {
  211. for (int i = 0; i < n_buttons; i++) {
  212. if (buttons[i].gpio == gpio) return buttons[i].client;
  213. }
  214. return NULL;
  215. }
  216. /****************************************************************************************
  217. * Get stored id
  218. */
  219. bool button_is_pressed(int gpio, void *client) {
  220. for (int i = 0; i < n_buttons; i++) {
  221. if (gpio != -1 && buttons[i].gpio == gpio) return buttons[i].level == buttons[i].type;
  222. else if (client && buttons[i].client == client) return buttons[i].level == buttons[i].type;
  223. }
  224. return false;
  225. }
  226. /****************************************************************************************
  227. * Update buttons
  228. */
  229. void *button_remap(void *client, int gpio, button_handler handler, int long_press, int shifter_gpio) {
  230. int i;
  231. struct button_s *button = NULL;
  232. void *prev_client;
  233. ESP_LOGI(TAG, "remapping GPIO %u, long press %u shifter %u", gpio, long_press, shifter_gpio);
  234. // find button
  235. for (i = 0; i < n_buttons; i++) {
  236. if (buttons[i].gpio == gpio) {
  237. button = buttons + i;
  238. break;
  239. }
  240. }
  241. // huh
  242. if (!button) return NULL;
  243. prev_client = button->client;
  244. button->client = client;
  245. button->handler = handler;
  246. button->long_press = long_press;
  247. button->shifter_gpio = shifter_gpio;
  248. // find our shifter (if any)
  249. for (i = 0; shifter_gpio != -1 && i < n_buttons; i++) {
  250. if (buttons[i].gpio == shifter_gpio) {
  251. button->shifter = buttons + i;
  252. // a shifter must have a long-press handler
  253. if (!buttons[i].long_press) buttons[i].long_press = -1;
  254. break;
  255. }
  256. }
  257. return prev_client;
  258. }
  259. /****************************************************************************************
  260. * Create rotary encoder
  261. */
  262. static void rotary_button_handler(void *id, button_event_e event, button_press_e mode, bool long_press) {
  263. ESP_LOGI(TAG, "Rotary push-button %d", event);
  264. rotary.handler(id, event == BUTTON_PRESSED ? ROTARY_PRESSED : ROTARY_RELEASED, long_press);
  265. }
  266. /****************************************************************************************
  267. * Create rotary encoder
  268. */
  269. bool create_rotary(void *id, int A, int B, int SW, int long_press, rotary_handler handler) {
  270. if (A == -1 || B == -1) {
  271. ESP_LOGI(TAG, "Cannot create rotary %d %d", A, B);
  272. return false;
  273. }
  274. rotary.A = A;
  275. rotary.B = B;
  276. rotary.SW = SW;
  277. rotary.client = id;
  278. rotary.handler = handler;
  279. // nasty ESP32 bug: fire-up constantly INT on GPIO 36/39 if ADC1, AMP, Hall used which WiFi does when PS is activated
  280. if (A == 36 || A == 39 || B == 36 || B == 39 || SW == 36 || SW == 39) gpio36_39_used = true;
  281. // Initialise the rotary encoder device with the GPIOs for A and B signals
  282. rotary_encoder_init(&rotary.info, A, B);
  283. // Create a queue for events from the rotary encoder driver.
  284. rotary.queue = rotary_encoder_create_queue();
  285. rotary_encoder_set_queue(&rotary.info, rotary.queue);
  286. if (!button_queue_set) button_queue_set = xQueueCreateSet(BUTTON_QUEUE_LEN + 1);
  287. xQueueAddToSet( rotary.queue, button_queue_set );
  288. // create companion button if rotary has a switch
  289. if (SW != -1) button_create(id, SW, BUTTON_LOW, true, 0, rotary_button_handler, long_press, -1);
  290. ESP_LOGI(TAG, "Creating rotary encoder A:%d B:%d, SW:%d", A, B, SW);
  291. return true;
  292. }