|
| 1 | +// Copyright 2020 Espressif Systems (Shanghai) PTE LTD |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | + |
| 16 | +#ifndef _IOT_BUTTON_H_ |
| 17 | +#define _IOT_BUTTON_H_ |
| 18 | + |
| 19 | +#ifdef __cplusplus |
| 20 | +extern "C" { |
| 21 | +#endif |
| 22 | + |
| 23 | +#include <driver/gpio.h> |
| 24 | +#include <freertos/FreeRTOS.h> |
| 25 | +#include <freertos/portmacro.h> |
| 26 | +typedef void (* button_cb)(void*); |
| 27 | +typedef void* button_handle_t; |
| 28 | + |
| 29 | +typedef enum { |
| 30 | + BUTTON_ACTIVE_HIGH = 1, /*!<button active level: high level*/ |
| 31 | + BUTTON_ACTIVE_LOW = 0, /*!<button active level: low level*/ |
| 32 | +} button_active_t; |
| 33 | + |
| 34 | +typedef enum { |
| 35 | + BUTTON_CB_PUSH = 0, /*!<button push callback event */ |
| 36 | + BUTTON_CB_RELEASE, /*!<button release callback event */ |
| 37 | + BUTTON_CB_TAP, /*!<button quick tap callback event(will not trigger if there already is a "PRESS" event) */ |
| 38 | + BUTTON_CB_SERIAL, /*!<button serial trigger callback event */ |
| 39 | +} button_cb_type_t; |
| 40 | + |
| 41 | +/** |
| 42 | + * @brief Init button functions |
| 43 | + * |
| 44 | + * @param gpio_num GPIO index of the pin that the button uses |
| 45 | + * @param active_level button hardware active level. |
| 46 | + * For "BUTTON_ACTIVE_LOW" it means when the button pressed, the GPIO will read low level. |
| 47 | + * |
| 48 | + * @return A button_handle_t handle to the created button object, or NULL in case of error. |
| 49 | + */ |
| 50 | +button_handle_t iot_button_create(gpio_num_t gpio_num, button_active_t active_level); |
| 51 | + |
| 52 | +/** |
| 53 | + * @brief Register a callback function for a serial trigger event. |
| 54 | + * |
| 55 | + * @param btn_handle handle of the button object |
| 56 | + * @param start_after_sec define the time after which to start serial trigger action |
| 57 | + * @param interval_tick serial trigger interval |
| 58 | + * @param cb callback function for "TAP" action. |
| 59 | + * @param arg Parameter for callback function |
| 60 | + * @note |
| 61 | + * Button callback functions execute in the context of the timer service task. |
| 62 | + * It is therefore essential that button callback functions never attempt to block. |
| 63 | + * For example, a button callback function must not call vTaskDelay(), vTaskDelayUntil(), |
| 64 | + * or specify a non zero block time when accessing a queue or a semaphore. |
| 65 | + * @return |
| 66 | + * - ESP_OK Success |
| 67 | + * - ESP_FAIL Parameter error |
| 68 | + */ |
| 69 | +esp_err_t iot_button_set_serial_cb(button_handle_t btn_handle, uint32_t start_after_sec, TickType_t interval_tick, button_cb cb, void* arg); |
| 70 | + |
| 71 | +/** |
| 72 | + * @brief Register a callback function for a button_cb_type_t action. |
| 73 | + * |
| 74 | + * @param btn_handle handle of the button object |
| 75 | + * @param type callback function type |
| 76 | + * @param cb callback function for "TAP" action. |
| 77 | + * @param arg Parameter for callback function |
| 78 | + * @note |
| 79 | + * Button callback functions execute in the context of the timer service task. |
| 80 | + * It is therefore essential that button callback functions never attempt to block. |
| 81 | + * For example, a button callback function must not call vTaskDelay(), vTaskDelayUntil(), |
| 82 | + * or specify a non zero block time when accessing a queue or a semaphore. |
| 83 | + * @return |
| 84 | + * - ESP_OK Success |
| 85 | + * - ESP_FAIL Parameter error |
| 86 | + */ |
| 87 | +esp_err_t iot_button_set_evt_cb(button_handle_t btn_handle, button_cb_type_t type, button_cb cb, void* arg); |
| 88 | + |
| 89 | +/** |
| 90 | + * @brief Callbacks invoked as timer events occur while button is pressed. |
| 91 | + * Example: If a button is configured for 2 sec, 5 sec and 7 sec callbacks and if the button is pressed for 6 sec then 2 sec callback would be invoked at 2 sec event and 5 sec callback would be invoked at 5 sec event |
| 92 | + * |
| 93 | + * @param btn_handle handle of the button object |
| 94 | + * @param press_sec the callback function would be called if you press the button for a specified period of time |
| 95 | + * @param cb callback function for "PRESS and HOLD" action. |
| 96 | + * @param arg Parameter for callback function |
| 97 | + * |
| 98 | + * @note |
| 99 | + * Button callback functions execute in the context of the timer service task. |
| 100 | + * It is therefore essential that button callback functions never attempt to block. |
| 101 | + * For example, a button callback function must not call vTaskDelay(), vTaskDelayUntil(), |
| 102 | + * or specify a non zero block time when accessing a queue or a semaphore. |
| 103 | + * @return |
| 104 | + * - ESP_OK Success |
| 105 | + * - ESP_FAIL Parameter error |
| 106 | + */ |
| 107 | +esp_err_t iot_button_add_on_press_cb(button_handle_t btn_handle, uint32_t press_sec, button_cb cb, void* arg); |
| 108 | + |
| 109 | +/** |
| 110 | + * @brief Single callback invoked according to the latest timer event on button release. |
| 111 | + * Example: If a button is configured for 2 sec, 5 sec and 7 sec callbacks and if the button is released at 6 sec then only 5 sec callback would be invoked |
| 112 | + * |
| 113 | + * @param btn_handle handle of the button object |
| 114 | + * @param press_sec the callback function would be called if you press the button for a specified period of time |
| 115 | + * @param cb callback function for "PRESS and RELEASE" action. |
| 116 | + * @param arg Parameter for callback function |
| 117 | + * |
| 118 | + * @note |
| 119 | + * Button callback functions execute in the context of the timer service task. |
| 120 | + * It is therefore essential that button callback functions never attempt to block. |
| 121 | + * For example, a button callback function must not call vTaskDelay(), vTaskDelayUntil(), |
| 122 | + * or specify a non zero block time when accessing a queue or a semaphore. |
| 123 | + * @return |
| 124 | + * - ESP_OK Success |
| 125 | + * - ESP_FAIL Parameter error |
| 126 | + */ |
| 127 | +esp_err_t iot_button_add_on_release_cb(button_handle_t btn_handle, uint32_t press_sec, button_cb cb, void* arg); |
| 128 | + |
| 129 | +/** |
| 130 | + * @brief Delete button object and free memory |
| 131 | + * @param btn_handle handle of the button object |
| 132 | + * |
| 133 | + * @return |
| 134 | + * - ESP_OK Success |
| 135 | + * - ESP_FAIL Parameter error |
| 136 | + */ |
| 137 | +esp_err_t iot_button_delete(button_handle_t btn_handle); |
| 138 | + |
| 139 | +/** |
| 140 | + * @brief Remove callback |
| 141 | + * |
| 142 | + * @param btn_handle The handle of the button object |
| 143 | + * @param type callback function event type |
| 144 | + * |
| 145 | + * @return |
| 146 | + * - ESP_OK Success |
| 147 | + */ |
| 148 | +esp_err_t iot_button_rm_cb(button_handle_t btn_handle, button_cb_type_t type); |
| 149 | + |
| 150 | +#ifdef __cplusplus |
| 151 | +} |
| 152 | +#endif |
| 153 | + |
| 154 | +#ifdef __cplusplus |
| 155 | + |
| 156 | +/** |
| 157 | + * class of button |
| 158 | + * simple usage: |
| 159 | + * CButton* btn = new CButton(BUTTON_IO_NUM, BUTTON_ACTIVE_LEVEL, BUTTON_SERIAL_TRIGGER, 3); |
| 160 | + * btn->add_cb(BUTTON_CB_PUSH, button_tap_cb, (void*) push, 50 / portTICK_PERIOD_MS); |
| 161 | + * btn->add_custom_cb(5, button_press_5s_cb, NULL); |
| 162 | + * ...... |
| 163 | + * delete btn; |
| 164 | + */ |
| 165 | +class CButton |
| 166 | +{ |
| 167 | +private: |
| 168 | + button_handle_t m_btn_handle; |
| 169 | + |
| 170 | + /** |
| 171 | + * prevent copy constructing |
| 172 | + */ |
| 173 | + CButton(const CButton&); |
| 174 | + CButton& operator = (const CButton&); |
| 175 | +public: |
| 176 | + |
| 177 | + /** |
| 178 | + * @brief constructor of CButton |
| 179 | + * |
| 180 | + * @param gpio_num GPIO index of the pin that the button uses |
| 181 | + * @param active_level button hardware active level. |
| 182 | + * For "BUTTON_ACTIVE_LOW" it means when the button pressed, the GPIO will read low level. |
| 183 | + */ |
| 184 | + CButton(gpio_num_t gpio_num, button_active_t active_level = BUTTON_ACTIVE_LOW); |
| 185 | + |
| 186 | + ~CButton(); |
| 187 | + |
| 188 | + /** |
| 189 | + * @brief Register a callback function for a button_cb_type_t action. |
| 190 | + * |
| 191 | + * @param type callback function type |
| 192 | + * @param cb callback function for "TAP" action. |
| 193 | + * @param arg Parameter for callback function |
| 194 | + * @note |
| 195 | + * Button callback functions execute in the context of the timer service task. |
| 196 | + * It is therefore essential that button callback functions never attempt to block. |
| 197 | + * For example, a button callback function must not call vTaskDelay(), vTaskDelayUntil(), |
| 198 | + * or specify a non zero block time when accessing a queue or a semaphore. |
| 199 | + * @return |
| 200 | + * - ESP_OK Success |
| 201 | + * - ESP_FAIL Parameter error |
| 202 | + */ |
| 203 | + esp_err_t set_evt_cb(button_cb_type_t type, button_cb cb, void* arg); |
| 204 | + |
| 205 | + /** |
| 206 | + * @brief Register a callback function for a serial trigger event. |
| 207 | + * |
| 208 | + * @param btn_handle handle of the button object |
| 209 | + * @param start_after_sec define the time after which to start serial trigger action |
| 210 | + * @param interval_tick serial trigger interval |
| 211 | + * @param cb callback function for "TAP" action. |
| 212 | + * @param arg Parameter for callback function |
| 213 | + * @note |
| 214 | + * Button callback functions execute in the context of the timer service task. |
| 215 | + * It is therefore essential that button callback functions never attempt to block. |
| 216 | + * For example, a button callback function must not call vTaskDelay(), vTaskDelayUntil(), |
| 217 | + * or specify a non zero block time when accessing a queue or a semaphore. |
| 218 | + * @return |
| 219 | + * - ESP_OK Success |
| 220 | + * - ESP_FAIL Parameter error |
| 221 | + */ |
| 222 | + esp_err_t set_serial_cb(button_cb cb, void* arg, TickType_t interval_tick, uint32_t start_after_sec); |
| 223 | + |
| 224 | + /** |
| 225 | + * @brief Callbacks invoked as timer events occur while button is pressed |
| 226 | + * |
| 227 | + * @param press_sec the callback function would be called if you press the button for a specified period of time |
| 228 | + * @param cb callback function for "PRESS and HOLD" action. |
| 229 | + * @param arg Parameter for callback function |
| 230 | + * |
| 231 | + * @note |
| 232 | + * Button callback functions execute in the context of the timer service task. |
| 233 | + * It is therefore essential that button callback functions never attempt to block. |
| 234 | + * For example, a button callback function must not call vTaskDelay(), vTaskDelayUntil(), |
| 235 | + * or specify a non zero block time when accessing a queue or a semaphore. |
| 236 | + * @return |
| 237 | + * - ESP_OK Success |
| 238 | + * - ESP_FAIL Parameter error |
| 239 | + */ |
| 240 | + esp_err_t add_on_press_cb(uint32_t press_sec, button_cb cb, void* arg); |
| 241 | + |
| 242 | + /** |
| 243 | + * @brief Single callback invoked according to the latest timer event on button release. |
| 244 | + * |
| 245 | + * @param press_sec the callback function would be called if you press the button for a specified period of time |
| 246 | + * @param cb callback function for "PRESS and RELEASE" action. |
| 247 | + * @param arg Parameter for callback function |
| 248 | + * |
| 249 | + * @note |
| 250 | + * Button callback functions execute in the context of the timer service task. |
| 251 | + * It is therefore essential that button callback functions never attempt to block. |
| 252 | + * For example, a button callback function must not call vTaskDelay(), vTaskDelayUntil(), |
| 253 | + * or specify a non zero block time when accessing a queue or a semaphore. |
| 254 | + * @return |
| 255 | + * - ESP_OK Success |
| 256 | + * - ESP_FAIL Parameter error |
| 257 | + */ |
| 258 | + esp_err_t add_on_release_cb(uint32_t press_sec, button_cb cb, void* arg); |
| 259 | + |
| 260 | + /** |
| 261 | + * @brief Remove callback |
| 262 | + * |
| 263 | + * @param type callback function event type |
| 264 | + * |
| 265 | + * @return |
| 266 | + * - ESP_OK Success |
| 267 | + */ |
| 268 | + esp_err_t rm_cb(button_cb_type_t type); |
| 269 | +}; |
| 270 | +#endif |
| 271 | + |
| 272 | +#endif |
0 commit comments