diff options
| Commit | 7a249f6152b4493cf0c51785313d0f1d4755839f (patch) | |
|---|---|---|
| Author | 2024-05-05 22:46:11 +0300 | |
| Committer | 2024-05-05 22:46:11 +0300 | |
| Files | e0038b801c8355673b280696678ceab1be3f68c2 | |
| uni-projects-7a249f6152b4493cf0c51785313d0f1d4755839f.tar uni-projects-7a249f6152b4493cf0c51785313d0f1d4755839f.tar.gz uni-projects-7a249f6152b4493cf0c51785313d0f1d4755839f.zip | ||
| Parent | 63fbf3df12dce61caf168637841f7ff938da7a94 (diff) | |
[iot/goam] Implemented button functionality
4 files changed, 97 insertions, 34 deletions
| -rw-r--r-- | Year 3/Internet of things/goam/main/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | Year 3/Internet of things/goam/main/buttons.c | 78 | ||||
| -rw-r--r-- | Year 3/Internet of things/goam/main/buttons.h | 4 | ||||
| -rw-r--r-- | Year 3/Internet of things/goam/main/main.c | 48 |
diff --git a/Year 3/Internet of things/goam/main/CMakeLists.txt b/Year 3/Internet of things/goam/main/CMakeLists.txt index 6819229..c4fd248 100644 --- a/Year 3/Internet of things/goam/main/CMakeLists.txt +++ b/Year 3/Internet of things/goam/main/CMakeLists.txt @@ -1,3 +1,4 @@ idf_component_register(SRCS "main.c" "ptc06.c" + "buttons.c" INCLUDE_DIRS "") diff --git a/Year 3/Internet of things/goam/main/buttons.c b/Year 3/Internet of things/goam/main/buttons.c new file mode 100644 index 0000000..18b9df5 --- /dev/null +++ b/Year 3/Internet of things/goam/main/buttons.c @@ -0,0 +1,78 @@ +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "freertos/queue.h" + +#include "driver/gpio.h" +#include "buttons.h" + +#define ESP_INTR_FLAG_DEFAULT 0 + +#define gpio_to_bitmask(pin) (1ULL << pin) + +static QueueHandle_t gpio_button_queue = NULL; + +/* + * 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 }; +static void (*button_off[40])() = { NULL }; + +static void on_button_event(void* arg) +{ + uint32_t io_num; + while (true) { + if (!xQueueReceive(gpio_button_queue, &io_num, portMAX_DELAY)) continue; + + printf("GPIO[%ld] intr, val: %d\n", io_num, gpio_get_level(io_num)); + + if (gpio_get_level(io_num)) { + if (button_on[io_num]) button_on[io_num](); + } + else { + if (button_off[io_num]) button_off[io_num](); + } + + vTaskDelay(100 / portTICK_PERIOD_MS); + xQueueReset(gpio_button_queue); + } +} + +/* + * Public interface + */ + +void buttons_init() { + //create a queue to handle gpio event from isr + gpio_button_queue = xQueueCreate(10, sizeof(uint32_t)); + //start gpio task + xTaskCreate(on_button_event, "on_button_event", 2048, NULL, 10, NULL); + + //install gpio isr service + gpio_install_isr_service(ESP_INTR_FLAG_DEFAULT); +} + +void add_push_button(unsigned pin, void (*on_press)(), void (*off_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); + //hook isr handler for specific gpio pin + gpio_isr_handler_add(pin, gpio_button_interrupt_handler, (void*)pin); + button_on[pin] = on_press; + button_off[pin] = off_press; +} diff --git a/Year 3/Internet of things/goam/main/buttons.h b/Year 3/Internet of things/goam/main/buttons.h new file mode 100644 index 0000000..7586973 --- /dev/null +++ b/Year 3/Internet of things/goam/main/buttons.h @@ -0,0 +1,4 @@ +#pragma once + +void buttons_init(); +void add_push_button(unsigned pin, void (*on_press)(), void (*off_press)()); diff --git a/Year 3/Internet of things/goam/main/main.c b/Year 3/Internet of things/goam/main/main.c index b1e9b99..ce87b7b 100644 --- a/Year 3/Internet of things/goam/main/main.c +++ b/Year 3/Internet of things/goam/main/main.c @@ -15,6 +15,7 @@ #include "driver/gpio.h" #include "driver/uart.h" #include "ptc06.h" +#include "buttons.h" void print_chip_data() { /* Print chip information */ @@ -43,38 +44,9 @@ void print_chip_data() { printf("Minimum free heap size: %" PRIu32 " bytes\n", esp_get_minimum_free_heap_size()); } -#define GPIO_INPUT_CAMERA 13 -#define GPIO_OUTPUT_CAMERA 14 +#define BTN_SHOOT_PIN 13 -#define gpio_to_bitmask(pin) (1ULL << pin) - -#define ESP_INTR_FLAG_DEFAULT 0 - -static QueueHandle_t gpio_evt_queue = NULL; - -/* static void IRAM_ATTR gpio_isr_handler(void* arg) */ -/* { */ -/* uint32_t gpio_num = (uint32_t) arg; */ -/* xQueueSendFromISR(gpio_evt_queue, &gpio_num, NULL); */ -/* } */ - -void app_main(void) -{ - gpio_config_t o_conf = { - .intr_type = GPIO_INTR_DISABLE, - //set as output mode - .mode = GPIO_MODE_OUTPUT, - //bit mask of the pins that you want to set,e.g.GPIO18/19 - .pin_bit_mask = gpio_to_bitmask(GPIO_OUTPUT_CAMERA), - //disable pull-down mode - .pull_down_en = 0, - //disable pull-up mode - .pull_up_en = 0 - }; - gpio_config(&o_conf); - - - ptc06_init(); +void take_photo() { ptc06_set_image_resolution(RES_160X120); char* image; size_t length = ptc06_get_image(&image); @@ -85,11 +57,19 @@ void app_main(void) } printf("\n"); free(image); +} + +void app_main(void) +{ + ptc06_init(); + + buttons_init(); + add_push_button(BTN_SHOOT_PIN, take_photo, NULL); for (int i = 0; true; i++) { - ptc06_reset(); - printf("%d seconds, %d\n", i, gpio_get_level(GPIO_INPUT_CAMERA)); - /* vTaskDelay(1000 / portTICK_PERIOD_MS); */ + /* ptc06_reset(); */ + /* printf("%d seconds, %d\n", i, gpio_get_level(GPIO_INPUT_CAMERA)); */ + vTaskDelay(1000 / portTICK_PERIOD_MS); } printf("Restarting now.\n"); |
