Summary Files Commits Branches @commit @diff @stats
path: root / Year 3 / Internet of things / goam / main / buttons.c
blob: dfda13c1d4b88e1bb2b2767adacdd00223d689eb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "esp_log.h"

#include "driver/gpio.h"
#include "buttons.h"

#define ESP_INTR_FLAG_DEFAULT 0

static QueueHandle_t gpio_button_queue = NULL;

static const char* TAG = "buttons";

/*
 * Handlers
 */

static void IRAM_ATTR gpio_button_interrupt_handler(void* arg)
{
	int gpio_num = (int)arg;
	xQueueSendFromISR(gpio_button_queue, &gpio_num, NULL);
}

static void (*button_on[40])()  = { NULL };

#define BUTTON_ACTION_COMPLETED_BIT 0b1

static TaskHandle_t on_button_event_handle = NULL;

static void button_action(void* arg) {
	uint32_t io_num = *(uint32_t*)arg;
	if (button_on[io_num]) button_on[io_num]();

	xTaskNotify(on_button_event_handle, BUTTON_ACTION_COMPLETED_BIT, eSetBits);
	vTaskDelete(NULL);
}

static void on_button_event_task(void* arg)
{
	uint32_t io_num;
	long unsigned int notification = 0;
	while (true) {
		if (!xQueueReceive(gpio_button_queue, &io_num, portMAX_DELAY)) continue;

		ESP_LOGD(TAG, "GPIO[%ld] intr, val: %d", io_num, gpio_get_level(io_num));

		// button_on/button_off functions require a lot of memory, but most of the time
		// we won't be running them, so its more memory efficient to split them off into
		// another task, which allocates a lot of memory, but then frees it
		xTaskCreate(button_action, "button_action", 2048, &io_num, 5, NULL);

		// We want button_action to be "blocking", in GoAm there is only one button, so
		// this logic is good enough. Under normal circumstances there should just be a
		// check to not create another task for the same button action
		do {
			xTaskNotifyWait(ULONG_MAX, 0, &notification, pdMS_TO_TICKS(100));
		} while (!(notification & BUTTON_ACTION_COMPLETED_BIT));

		// If any button was pressed, this effectively blocks all other button presses
		// that happened in the meantime.
		// Generally it's bad, but in my case the button would trigger multiple presses
		// when physically pressed once, and there is only one so its alright.
		// Better logic would be to remove all repeated occurences of the same current
		// event, but again, here we don't need to.
		vTaskDelay(pdMS_TO_TICKS(100));
		xQueueReset(gpio_button_queue);
	}
}

/*
 * Public interface
 */

void buttons_init() {
	//create a queue to handle gpio event from isr
	gpio_button_queue = xQueueCreate(5, sizeof(uint32_t));
	//start gpio task
	xTaskCreate(on_button_event_task, "on_button_event_task", 768, NULL, 15, &on_button_event_handle);

	//install gpio isr service
	gpio_install_isr_service(ESP_INTR_FLAG_DEFAULT);

	ESP_LOGI(TAG, "Initialized queue and ISR service");
}

#define gpio_to_bitmask(pin) (1ULL << pin)

void add_push_button(unsigned pin, void (*on_press)()) {
	gpio_config_t o_conf = {
		.intr_type = GPIO_INTR_POSEDGE,
		//set as output mode
		.mode = GPIO_MODE_INPUT,
		//bit mask of the pins that you want to set,e.g.GPIO18/19
		.pin_bit_mask = gpio_to_bitmask(pin),
		//disable pull-down mode
		.pull_down_en = 1,
		//disable pull-up mode
		.pull_up_en = 0
	};
	gpio_config(&o_conf);
	// Handler has to do as little work as humanly possible, and return execution
	// as soon as possible, while also being in RAM (IRAM_ATTR)
	// We get a crash if it takes too long, so we canot register on_press as the
	// handler
	gpio_isr_handler_add(pin, gpio_button_interrupt_handler, (void*)pin);
	button_on[pin]  = on_press;

	ESP_LOGD(TAG, "GPIO[%d] added intr handler, on_press: %d", pin, on_press != NULL);
}

void press_push_button(int gpio_num) {
	xQueueSend(gpio_button_queue, &gpio_num, pdMS_TO_TICKS(100));
}