|
1 | 1 | /*
|
2 | 2 | *
|
3 |
| - * Copyright (c) 2022 Project CHIP Authors |
| 3 | + * Copyright (c) 2022-2023 Project CHIP Authors |
| 4 | + * All rights reserved. |
4 | 5 | *
|
5 | 6 | * Licensed under the Apache License, Version 2.0 (the "License");
|
6 | 7 | * you may not use this file except in compliance with the License.
|
|
14 | 15 | * See the License for the specific language governing permissions and
|
15 | 16 | * limitations under the License.
|
16 | 17 | */
|
| 18 | +#include "driver/gpio.h" |
| 19 | +#include "esp_check.h" |
| 20 | +#include "esp_log.h" |
| 21 | +#include "esp_system.h" |
17 | 22 |
|
| 23 | +#include "AppTask.h" |
18 | 24 | #include "Button.h"
|
19 |
| -#include "esp_attr.h" |
| 25 | +#include "Globals.h" |
| 26 | +#include "ScreenManager.h" |
| 27 | +#include <lib/support/CodeUtils.h> |
| 28 | +#include <platform/CHIPDeviceLayer.h> |
| 29 | +#include <vector> |
20 | 30 |
|
21 |
| -#define GPIO_INPUT_IO_0 9 |
22 |
| -#define GPIO_INPUT_PIN_SEL (1ULL << GPIO_INPUT_IO_0) |
23 |
| -#define ESP_INTR_FLAG_DEFAULT 0 |
| 31 | +static const char * TAG = "Button.cpp"; |
24 | 32 |
|
25 |
| -static const char * TAG = "Button"; |
| 33 | +extern Button gButtons[BUTTON_NUMBER]; |
26 | 34 |
|
27 |
| -static Button::ButtonPressCallback button_press_handler = nullptr; |
| 35 | +Button::Button() {} |
28 | 36 |
|
29 |
| -static void IRAM_ATTR gpio_isr_handler(void * arg) |
| 37 | +Button::Button(gpio_num_t gpioNum) |
30 | 38 | {
|
31 |
| - if (button_press_handler != nullptr) |
| 39 | + mGPIONum = gpioNum; |
| 40 | +} |
| 41 | + |
| 42 | +int32_t Find_Button_Via_Pin(gpio_num_t gpioNum) |
| 43 | +{ |
| 44 | + for (int i = 0; i < BUTTON_NUMBER; i++) |
32 | 45 | {
|
33 |
| - button_press_handler(); |
| 46 | + if (gButtons[i].GetGPIONum() == gpioNum) |
| 47 | + { |
| 48 | + return i; |
| 49 | + } |
34 | 50 | }
|
| 51 | + return -1; |
35 | 52 | }
|
36 | 53 |
|
37 |
| -void Button::Init() |
| 54 | +void IRAM_ATTR button_isr_handler(void * arg) |
38 | 55 | {
|
39 |
| - /* Initialize button interrupt*/ |
40 |
| - // zero-initialize the config structure. |
| 56 | + uint32_t gpio_num = (uint32_t) arg; |
| 57 | + int32_t idx = Find_Button_Via_Pin((gpio_num_t) gpio_num); |
| 58 | + if (idx == -1) |
| 59 | + { |
| 60 | + return; |
| 61 | + } |
| 62 | + BaseType_t taskWoken = pdFALSE; |
| 63 | + xTimerStartFromISR(gButtons[idx].mbuttonTimer, |
| 64 | + &taskWoken); // If the timer had already been started ,restart it will reset its expiry time |
| 65 | +} |
| 66 | + |
| 67 | +esp_err_t Button::Init() |
| 68 | +{ |
| 69 | + return Init(mGPIONum); |
| 70 | +} |
| 71 | + |
| 72 | +esp_err_t Button::Init(gpio_num_t gpioNum) |
| 73 | +{ |
| 74 | + esp_err_t ret = ESP_OK; |
| 75 | + |
| 76 | + mGPIONum = gpioNum; |
| 77 | + // zero-initialize the config structure. |
41 | 78 | gpio_config_t io_conf = {};
|
42 |
| - // interrupt of rising edge |
| 79 | + // interrupt of falling edge |
43 | 80 | io_conf.intr_type = GPIO_INTR_NEGEDGE;
|
44 | 81 | // bit mask of the pins, use GPIO4/5 here
|
45 |
| - io_conf.pin_bit_mask = GPIO_INPUT_PIN_SEL; |
| 82 | + io_conf.pin_bit_mask = 1ULL << gpioNum; |
46 | 83 | // set as input mode
|
47 | 84 | io_conf.mode = GPIO_MODE_INPUT;
|
48 | 85 | // enable pull-up mode
|
49 | 86 | io_conf.pull_up_en = GPIO_PULLUP_ENABLE;
|
| 87 | + |
50 | 88 | gpio_config(&io_conf);
|
51 | 89 |
|
52 |
| - // install gpio isr service |
53 |
| - gpio_install_isr_service(ESP_INTR_FLAG_DEFAULT); |
54 | 90 | // hook isr handler for specific gpio pin
|
55 |
| - gpio_isr_handler_add(static_cast<gpio_num_t>(GPIO_INPUT_IO_0), gpio_isr_handler, (void *) GPIO_INPUT_IO_0); |
| 91 | + ret = gpio_isr_handler_add(gpioNum, button_isr_handler, (void *) gpioNum); |
| 92 | + ESP_RETURN_ON_ERROR(ret, TAG, "gpio_isr_handler_add failed: %s", esp_err_to_name(ret)); |
56 | 93 |
|
57 |
| - ESP_LOGI(TAG, "Button initialized.."); |
58 |
| -} |
| 94 | + mbuttonTimer = xTimerCreate("BtnTmr", // Just a text name, not used by the RTOS kernel |
| 95 | + pdMS_TO_TICKS(50), // timer period |
| 96 | + false, // no timer reload (==one-shot) |
| 97 | + (void *) (int) gpioNum, // init timer id = gpioNum index |
| 98 | + TimerCallback // timer callback handler (all buttons use |
| 99 | + // the same timer cn function) |
| 100 | + ); |
59 | 101 |
|
60 |
| -void Button::SetButtonPressCallback(ButtonPressCallback button_callback) |
| 102 | + return ESP_OK; |
| 103 | +} |
| 104 | +void Button::TimerCallback(TimerHandle_t xTimer) |
61 | 105 | {
|
62 |
| - if (button_callback != nullptr) |
63 |
| - { |
64 |
| - button_press_handler = button_callback; |
65 |
| - } |
| 106 | + // Get the button index of the expired timer and call button event Handler. |
| 107 | + uint32_t gpio_num = (uint32_t) pvTimerGetTimerID(xTimer); |
| 108 | + GetAppTask().ButtonEventHandler(gpio_num, APP_BUTTON_PRESSED); |
66 | 109 | }
|
0 commit comments