Kestrel Interface
Loading...
Searching...
No Matches
kest_footswitch.c
Go to the documentation of this file.
1#include <freertos/FreeRTOS.h>
2#include <driver/gpio.h>
3
4#include "kest_int.h"
5
6#define HW_SWITCH_GPIO_0 22 // n = 0
7#define HW_SWITCH_GPIO_1 23 // n = 1
8
9#define HW_SWITCH_DEBOUNCE_MS 100
10#define HW_SWITCH_TASK_STACK 2048
11#define HW_SWITCH_TASK_PRIO 6
12
15
16static QueueHandle_t hw_switch_queue = NULL;
17
18static void IRAM_ATTR hw_switch_isr(void *arg)
19{
20 int n = (int)arg;
21 BaseType_t xHigherPriorityTaskWoken = pdFALSE;
22
23 xQueueSendFromISR(hw_switch_queue, &n, &xHigherPriorityTaskWoken);
24
25 if (xHigherPriorityTaskWoken)
26 {
27 portYIELD_FROM_ISR();
28 }
29}
30
31void footswitch_task(void *arg)
32{
33 int n;
34 TickType_t last_event_tick[2] = {0, 0};
35 const TickType_t debounce_ticks = pdMS_TO_TICKS(HW_SWITCH_DEBOUNCE_MS);
36
37 while (true)
38 {
39 if (xQueueReceive(hw_switch_queue, &n, portMAX_DELAY) == pdTRUE)
40 {
41 TickType_t now = xTaskGetTickCount();
42
43 kest_printf("now = %d, last_event_tick[n] = %d; now - last_event_tick[n] = %d, debounce_ticks = %d\n", now, last_event_tick[n],
44 now - last_event_tick[n], debounce_ticks);
45 if ((now - last_event_tick[n]) >= debounce_ticks)
46 {
47 last_event_tick[n] = now;
48
50 }
51 }
52 }
53}
54
56{
57 hw_switch_queue = xQueueCreate(8, sizeof(int));
58
59 gpio_config_t cfg = {
60 .pin_bit_mask =
61 (1ULL << HW_SWITCH_GPIO_0) |
62 (1ULL << HW_SWITCH_GPIO_1),
63 .mode = GPIO_MODE_INPUT,
64 .pull_up_en = GPIO_PULLUP_ENABLE,
65 .pull_down_en = GPIO_PULLDOWN_DISABLE,
66 .intr_type = GPIO_INTR_ANYEDGE
67 };
68
69 gpio_config(&cfg);
70
71 gpio_install_isr_service(0);
72
73 gpio_isr_handler_add(HW_SWITCH_GPIO_0, hw_switch_isr, (void *)0);
74 gpio_isr_handler_add(HW_SWITCH_GPIO_1, hw_switch_isr, (void *)1);
75
76 xTaskCreate(
78 "hw_switch_task",
80 NULL,
82 NULL
83 );
84
85 return NO_ERROR;
86}
int cxt_handle_hw_switch(kest_context *cxt, int sw)
#define NO_ERROR
int init_footswitch_task()
#define HW_SWITCH_GPIO_0
uint8_t right_switch_state
#define HW_SWITCH_DEBOUNCE_MS
uint8_t left_switch_state
#define HW_SWITCH_GPIO_1
#define HW_SWITCH_TASK_STACK
#define HW_SWITCH_TASK_PRIO
void footswitch_task(void *arg)
kest_context global_cxt
Definition kest_int.c:12
void kest_printf(const char *fmt,...)
Definition kest_printf.c:21