2020-05-01 16:06:22 -04:00
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/task.h"
|
|
|
|
#include "freertos/queue.h"
|
|
|
|
#include "driver/gpio.h"
|
|
|
|
|
2020-10-25 22:49:14 -04:00
|
|
|
#include "main.h"
|
|
|
|
#include "pumps.h"
|
2020-10-26 22:12:25 -04:00
|
|
|
#include "ble.h"
|
2020-05-01 16:06:22 -04:00
|
|
|
|
2020-10-25 22:49:14 -04:00
|
|
|
#define GPIO_USER_BUTTON (19)
|
|
|
|
|
|
|
|
static void IRAM_ATTR gpio_isr_handler(void* arg) {
|
|
|
|
uint32_t gpio_num = (uint32_t) arg;
|
|
|
|
|
|
|
|
xQueueSendFromISR(gpio_evt_queue, &gpio_num, NULL);
|
2020-05-01 16:06:22 -04:00
|
|
|
}
|
|
|
|
|
2020-10-26 22:12:25 -04:00
|
|
|
static void gpio_task(void* arg) {
|
2020-10-25 22:49:14 -04:00
|
|
|
uint32_t io_num;
|
2021-09-22 23:14:19 -04:00
|
|
|
uint8_t state = 0;
|
|
|
|
int8_t last_level = -1;
|
2020-10-25 22:49:14 -04:00
|
|
|
|
|
|
|
for(;;) {
|
|
|
|
if(xQueueReceive(gpio_evt_queue, &io_num, portMAX_DELAY)) {
|
2020-10-26 22:12:25 -04:00
|
|
|
uint8_t level = gpio_get_level(io_num);
|
2020-10-25 22:49:14 -04:00
|
|
|
|
2021-09-22 23:14:19 -04:00
|
|
|
if(level == last_level) continue;
|
|
|
|
last_level = level;
|
2020-10-26 22:12:25 -04:00
|
|
|
|
2021-09-22 23:14:19 -04:00
|
|
|
printf("GPIO[%d] intr, val: %d\n", io_num, level);
|
2020-10-26 22:12:25 -04:00
|
|
|
|
2021-09-22 23:14:19 -04:00
|
|
|
if(state == 0)
|
|
|
|
vTaskDelay(10 / portTICK_RATE_MS);
|
|
|
|
|
|
|
|
pumps_run();
|
2020-10-26 22:12:25 -04:00
|
|
|
|
2021-09-22 23:14:19 -04:00
|
|
|
state = !state;
|
2020-10-29 14:17:43 -04:00
|
|
|
|
2021-09-22 23:14:19 -04:00
|
|
|
ble_send_notification((void*)&state, 1);
|
2020-05-01 16:06:22 -04:00
|
|
|
}
|
2020-10-25 22:49:14 -04:00
|
|
|
}
|
2020-05-01 16:06:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void user_button_init() {
|
2020-10-25 22:49:14 -04:00
|
|
|
gpio_config_t io_conf;
|
2020-05-01 16:06:22 -04:00
|
|
|
|
2021-09-22 23:14:19 -04:00
|
|
|
io_conf.intr_type = GPIO_PIN_INTR_ANYEDGE;
|
2020-10-25 22:49:14 -04:00
|
|
|
io_conf.mode = GPIO_MODE_INPUT;
|
|
|
|
io_conf.pin_bit_mask = (1ULL << GPIO_USER_BUTTON);
|
|
|
|
io_conf.pull_down_en = 0;
|
|
|
|
io_conf.pull_up_en = 1;
|
2020-05-01 16:06:22 -04:00
|
|
|
|
2020-10-25 22:49:14 -04:00
|
|
|
gpio_config(&io_conf);
|
2020-05-01 16:06:22 -04:00
|
|
|
|
2020-10-29 14:17:43 -04:00
|
|
|
gpio_evt_queue = xQueueCreate(1, sizeof(uint32_t));
|
2020-10-26 22:12:25 -04:00
|
|
|
xTaskCreate(gpio_task, "gpio_task", 4096, NULL, 10, NULL);
|
2020-05-01 16:06:22 -04:00
|
|
|
|
2020-10-25 22:49:14 -04:00
|
|
|
gpio_install_isr_service(0);
|
2021-09-22 23:14:19 -04:00
|
|
|
gpio_isr_handler_add(GPIO_USER_BUTTON, gpio_isr_handler, (void*)GPIO_USER_BUTTON);
|
2020-05-01 16:06:22 -04:00
|
|
|
}
|