Skip to content

Commit 8b1f0b7

Browse files
committed
style(components): improve general devices, update licenses for examples
1. update licenses for example 2. modify button api in test code 3. fix some warnings in oled test code 4. add config for button 5. improve button driver and test code. 6. modify relay component 7. update power meter component 8. fix unit-test compile issue 9. update status led component
1 parent 0facc40 commit 8b1f0b7

File tree

69 files changed

+806
-742
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+806
-742
lines changed

components/__config/Kconfig.projbuild

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ menu "IoT Solution settings"
2121
default y
2222
help
2323
"Select this one to enable button device"
24+
25+
menu "Button"
26+
depends on IOT_BUTTON_ENABLE
27+
config IO_GLITCH_FILTER_TIME_MS
28+
int "IO glitch filter timer ms (10~100)"
29+
range 10 100
30+
default 50
31+
endmenu
2432
config IOT_DEBUG_ENABLE
2533
bool "DEBUG_COMPONENT_ENABLE"
2634
default n
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
menu "Button"
2+
config IO_GLITCH_FILTER_TIME_MS
3+
int "IO glitch filter timer ms (10~100)"
4+
range 10 100
5+
default 50
6+
endmenu

components/general/button/button.c renamed to components/general/button/button/button.c

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ struct btn_cb{
5959
struct button_dev{
6060
uint8_t io_num;
6161
uint8_t active_level;
62-
button_trigger_t trig_mode;
6362
uint32_t serial_thres_sec;
6463
button_status_t state;
6564
button_cb_t tap_short_cb;
@@ -69,6 +68,7 @@ struct button_dev{
6968
button_cb_t* cb_head;
7069
};
7170

71+
#define BUTTON_GLITCH_FILTER_TIME_MS CONFIG_IO_GLITCH_FILTER_TIME_MS
7272
static const char* TAG = "button";
7373

7474
static void button_press_cb(xTimerHandle tmr)
@@ -94,7 +94,7 @@ static void button_tap_psh_cb(xTimerHandle tmr)
9494
if (btn->active_level == lv) {
9595
// high, then key is up
9696
btn->state = BUTTON_STATE_PUSH;
97-
if (btn->trig_mode == BUTTON_SERIAL_TRIGGER) {
97+
if (btn->press_serial_cb.tmr) {
9898
xTimerChangePeriod(btn->press_serial_cb.tmr, btn->serial_thres_sec*1000 / portTICK_PERIOD_MS, portMAX_DELAY);
9999
xTimerReset(btn->press_serial_cb.tmr, portMAX_DELAY);
100100
}
@@ -126,7 +126,7 @@ static void button_tap_rls_cb(xTimerHandle tmr)
126126
}
127127
pcb = pcb->next_cb;
128128
}
129-
if (btn->trig_mode == BUTTON_SERIAL_TRIGGER && btn->press_serial_cb.tmr != NULL) {
129+
if (btn->press_serial_cb.tmr && btn->press_serial_cb.tmr != NULL) {
130130
xTimerStop(btn->press_serial_cb.tmr, portMAX_DELAY);
131131
}
132132
if (btn->tap_short_cb.cb && btn->state == BUTTON_STATE_PUSH) {
@@ -212,37 +212,26 @@ esp_err_t iot_button_delete(button_handle_t btn_handle)
212212
return ESP_OK;
213213
}
214214

215-
button_handle_t iot_button_create(gpio_num_t gpio_num, button_active_t active_level, button_trigger_t trigger, uint32_t serial_thres_sec)
215+
button_handle_t iot_button_create(gpio_num_t gpio_num, button_active_t active_level)
216216
{
217217
IOT_CHECK(TAG, gpio_num < GPIO_NUM_MAX, NULL);
218-
IOT_CHECK(TAG, serial_thres_sec != 0, NULL);
219218
button_dev_t* btn = (button_dev_t*) calloc(1, sizeof(button_dev_t));
220219
POINT_ASSERT(TAG, btn, NULL);
221220
btn->active_level = active_level;
222221
btn->io_num = gpio_num;
223222
btn->state = BUTTON_STATE_IDLE;
224-
btn->trig_mode = trigger;
225-
btn->serial_thres_sec = serial_thres_sec;
226223
btn->tap_rls_cb.arg = NULL;
227224
btn->tap_rls_cb.cb = NULL;
228-
btn->tap_rls_cb.interval = 50 / portTICK_PERIOD_MS;
225+
btn->tap_rls_cb.interval = BUTTON_GLITCH_FILTER_TIME_MS / portTICK_PERIOD_MS;
229226
btn->tap_rls_cb.pbtn = btn;
230227
btn->tap_rls_cb.tmr = xTimerCreate("btn_rls_tmr", btn->tap_rls_cb.interval, pdFALSE,
231228
&btn->tap_rls_cb, button_tap_rls_cb);
232229
btn->tap_psh_cb.arg = NULL;
233230
btn->tap_psh_cb.cb = NULL;
234-
btn->tap_psh_cb.interval = 50 / portTICK_PERIOD_MS;
231+
btn->tap_psh_cb.interval = BUTTON_GLITCH_FILTER_TIME_MS / portTICK_PERIOD_MS;
235232
btn->tap_psh_cb.pbtn = btn;
236233
btn->tap_psh_cb.tmr = xTimerCreate("btn_psh_tmr", btn->tap_psh_cb.interval, pdFALSE,
237234
&btn->tap_psh_cb, button_tap_psh_cb);
238-
if (trigger == BUTTON_SERIAL_TRIGGER) {
239-
btn->press_serial_cb.arg = NULL;
240-
btn->press_serial_cb.cb = NULL;
241-
btn->press_serial_cb.interval = 1000 / portTICK_PERIOD_MS;
242-
btn->press_serial_cb.pbtn = btn;
243-
btn->press_serial_cb.tmr = xTimerCreate("btn_serial_tmr", btn->serial_thres_sec*1000 / portTICK_PERIOD_MS,
244-
pdFALSE, btn, button_press_serial_cb);
245-
}
246235
gpio_install_isr_service(0);
247236
gpio_config_t gpio_conf;
248237
gpio_conf.intr_type = GPIO_INTR_ANYEDGE;
@@ -259,13 +248,13 @@ esp_err_t iot_button_rm_cb(button_handle_t btn_handle, button_cb_type_t type)
259248
{
260249
button_dev_t* btn = (button_dev_t*) btn_handle;
261250
button_cb_t* btn_cb = NULL;
262-
if (type == BUTTON_PUSH_CB) {
251+
if (type == BUTTON_CB_PUSH) {
263252
btn_cb = &btn->tap_psh_cb;
264-
} else if (type == BUTTON_RELEASE_CB) {
253+
} else if (type == BUTTON_CB_RELEASE) {
265254
btn_cb = &btn->tap_rls_cb;
266-
} else if (type == BUTTON_TAP_CB) {
255+
} else if (type == BUTTON_CB_TAP) {
267256
btn_cb = &btn->tap_short_cb;
268-
} else if (type == BUTTON_SERIAL_CB) {
257+
} else if (type == BUTTON_CB_SERIAL) {
269258
btn_cb = &btn->press_serial_cb;
270259
}
271260
btn_cb->cb = NULL;
@@ -275,33 +264,45 @@ esp_err_t iot_button_rm_cb(button_handle_t btn_handle, button_cb_type_t type)
275264
return ESP_OK;
276265
}
277266

278-
esp_err_t iot_button_add_cb(button_handle_t btn_handle, button_cb_type_t type, button_cb cb, void* arg, int interval_tick)
267+
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)
268+
{
269+
button_dev_t* btn = (button_dev_t*) btn_handle;
270+
btn->serial_thres_sec = start_after_sec;
271+
if (btn->press_serial_cb.tmr == NULL) {
272+
btn->press_serial_cb.tmr = xTimerCreate("btn_serial_tmr", btn->serial_thres_sec*1000 / portTICK_PERIOD_MS,
273+
pdFALSE, btn, button_press_serial_cb);
274+
}
275+
btn->press_serial_cb.arg = arg;
276+
btn->press_serial_cb.cb = cb;
277+
btn->press_serial_cb.interval = interval_tick;
278+
btn->press_serial_cb.pbtn = btn;
279+
xTimerChangePeriod(btn->press_serial_cb.tmr, btn->serial_thres_sec*1000 / portTICK_PERIOD_MS, portMAX_DELAY);
280+
return ESP_OK;
281+
}
282+
283+
esp_err_t iot_button_set_evt_cb(button_handle_t btn_handle, button_cb_type_t type, button_cb cb, void* arg)
279284
{
280285
POINT_ASSERT(TAG, btn_handle, ESP_ERR_INVALID_ARG);
281-
IOT_CHECK(TAG, interval_tick > 0, ESP_ERR_INVALID_ARG);
282286
button_dev_t* btn = (button_dev_t*) btn_handle;
283-
if (type == BUTTON_PUSH_CB) {
287+
if (type == BUTTON_CB_PUSH) {
284288
btn->tap_psh_cb.arg = arg;
285289
btn->tap_psh_cb.cb = cb;
286-
btn->tap_psh_cb.interval = interval_tick;
290+
btn->tap_psh_cb.interval = BUTTON_GLITCH_FILTER_TIME_MS / portTICK_RATE_MS;
287291
btn->tap_psh_cb.pbtn = btn;
288-
xTimerChangePeriod(btn->tap_psh_cb.tmr, interval_tick, portMAX_DELAY);
289-
} else if (type == BUTTON_RELEASE_CB) {
292+
xTimerChangePeriod(btn->tap_psh_cb.tmr, btn->tap_psh_cb.interval, portMAX_DELAY);
293+
} else if (type == BUTTON_CB_RELEASE) {
290294
btn->tap_rls_cb.arg = arg;
291295
btn->tap_rls_cb.cb = cb;
292-
btn->tap_rls_cb.interval = interval_tick;
296+
btn->tap_rls_cb.interval = BUTTON_GLITCH_FILTER_TIME_MS / portTICK_RATE_MS;
293297
btn->tap_rls_cb.pbtn = btn;
294-
xTimerChangePeriod(btn->tap_rls_cb.tmr, interval_tick, portMAX_DELAY);
295-
} else if (type == BUTTON_TAP_CB) {
298+
xTimerChangePeriod(btn->tap_rls_cb.tmr, btn->tap_psh_cb.interval, portMAX_DELAY);
299+
} else if (type == BUTTON_CB_TAP) {
296300
btn->tap_short_cb.arg = arg;
297301
btn->tap_short_cb.cb = cb;
298-
btn->tap_short_cb.interval = interval_tick;
302+
btn->tap_short_cb.interval = BUTTON_GLITCH_FILTER_TIME_MS / portTICK_RATE_MS;
299303
btn->tap_short_cb.pbtn = btn;
300-
} else if (type == BUTTON_SERIAL_CB) {
301-
btn->press_serial_cb.arg = arg;
302-
btn->press_serial_cb.cb = cb;
303-
btn->press_serial_cb.interval = interval_tick;
304-
btn->press_serial_cb.pbtn = btn;
304+
} else if (type == BUTTON_CB_SERIAL) {
305+
iot_button_set_serial_cb(btn_handle, 1, 1000 / portTICK_RATE_MS, cb, arg);
305306
}
306307
return ESP_OK;
307308
}

components/general/button/button_obj.cpp renamed to components/general/button/button/button_obj.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@
2727
#include "esp_system.h"
2828
#include "iot_button.h"
2929

30-
CButton::CButton(gpio_num_t gpio_num, button_active_t active_level, button_trigger_t trigger, uint32_t serial_thres_sec)
30+
CButton::CButton(gpio_num_t gpio_num, button_active_t active_level)
3131
{
32-
m_btn_handle = iot_button_create(gpio_num, active_level, trigger, serial_thres_sec);
32+
m_btn_handle = iot_button_create(gpio_num, active_level);
3333
}
3434

3535
CButton::~CButton()
@@ -38,9 +38,14 @@ CButton::~CButton()
3838
m_btn_handle = NULL;
3939
}
4040

41-
esp_err_t CButton::add_cb(button_cb_type_t type, button_cb cb, void* arg, int interval_tick)
41+
esp_err_t CButton::set_evt_cb(button_cb_type_t type, button_cb cb, void* arg)
4242
{
43-
return iot_button_add_cb(m_btn_handle, type, cb, arg, interval_tick);
43+
return iot_button_set_evt_cb(m_btn_handle, type, cb, arg);
44+
}
45+
46+
esp_err_t CButton::set_serial_cb(button_cb cb, void* arg, TickType_t interval_tick, uint32_t start_after_sec)
47+
{
48+
return iot_button_set_serial_cb(m_btn_handle, start_after_sec, interval_tick, cb, arg);
4449
}
4550

4651
esp_err_t CButton::add_custom_cb(uint32_t press_sec, button_cb cb, void* arg)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#
2+
# "main" pseudo-component makefile.
3+
#
4+
# (Uses default behaviour of compiling all source files in directory, adding 'include' to include path.)

components/general/button/include/iot_button.h renamed to components/general/button/button/include/iot_button.h

Lines changed: 47 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,24 +30,20 @@ extern "C" {
3030
#endif
3131

3232
#include "driver/gpio.h"
33+
#include "freertos/portmacro.h"
3334
typedef void (* button_cb)(void*);
3435
typedef void* button_handle_t;
3536

36-
typedef enum {
37-
BUTTON_SINGLE_TRIGGER, /**< button event will only trigger once no matter how long you press the touchpad */
38-
BUTTON_SERIAL_TRIGGER, /**< number of button event triggerred will be in direct proportion to the duration of press*/
39-
} button_trigger_t;
40-
4137
typedef enum {
4238
BUTTON_ACTIVE_HIGH = 1, /*!<button active level: high level*/
4339
BUTTON_ACTIVE_LOW = 0, /*!<button active level: low level*/
4440
} button_active_t;
4541

4642
typedef enum {
47-
BUTTON_PUSH_CB = 0, /*!<button push callback event */
48-
BUTTON_RELEASE_CB, /*!<button release callback event */
49-
BUTTON_TAP_CB, /*!<button quick tap callback event(will not trigger if there already is a "PRESS" event) */
50-
BUTTON_SERIAL_CB, /*!<button serial trigger callback event */
43+
BUTTON_CB_PUSH = 0, /*!<button push callback event */
44+
BUTTON_CB_RELEASE, /*!<button release callback event */
45+
BUTTON_CB_TAP, /*!<button quick tap callback event(will not trigger if there already is a "PRESS" event) */
46+
BUTTON_CB_SERIAL, /*!<button serial trigger callback event */
5147
} button_cb_type_t;
5248

5349
/**
@@ -56,12 +52,29 @@ typedef enum {
5652
* @param gpio_num GPIO index of the pin that the button uses
5753
* @param active_level button hardware active level.
5854
* For "BUTTON_ACTIVE_LOW" it means when the button pressed, the GPIO will read low level.
59-
* @param trigger button event trigger mode
60-
* @param serial_thres_sec the number of seconds more than whick a BUTTON_SERIAL_CB would accour
6155
*
6256
* @return A button_handle_t handle to the created button object, or NULL in case of error.
6357
*/
64-
button_handle_t iot_button_create(gpio_num_t gpio_num, button_active_t active_level, button_trigger_t trigger, uint32_t serial_thres_sec);
58+
button_handle_t iot_button_create(gpio_num_t gpio_num, button_active_t active_level);
59+
60+
/**
61+
* @brief Register a callback function for a serial trigger event.
62+
*
63+
* @param btn_handle handle of the button object
64+
* @start_after_sec define the time after which to start serial trigger action
65+
* @interval_tick serial trigger interval
66+
* @cb callback function for "TAP" action.
67+
* @arg Parameter for callback function
68+
* @note
69+
* Button callback functions execute in the context of the timer service task.
70+
* It is therefore essential that button callback functions never attempt to block.
71+
* For example, a button callback function must not call vTaskDelay(), vTaskDelayUntil(),
72+
* or specify a non zero block time when accessing a queue or a semaphore.
73+
* @return
74+
* - ESP_OK Success
75+
* - ESP_FAIL Parameter error
76+
*/
77+
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);
6578

6679
/**
6780
* @brief Register a callback function for a button_cb_type_t action.
@@ -70,7 +83,6 @@ button_handle_t iot_button_create(gpio_num_t gpio_num, button_active_t active_le
7083
* @param type callback function type
7184
* @param cb callback function for "TAP" action.
7285
* @param arg Parameter for callback function
73-
* @param interval_tick a filter to bypass glitch, unit: FreeRTOS ticks.
7486
* @note
7587
* Button callback functions execute in the context of the timer service task.
7688
* It is therefore essential that button callback functions never attempt to block.
@@ -80,7 +92,7 @@ button_handle_t iot_button_create(gpio_num_t gpio_num, button_active_t active_le
8092
* - ESP_OK Success
8193
* - ESP_FAIL Parameter error
8294
*/
83-
esp_err_t iot_button_add_cb(button_handle_t btn_handle, button_cb_type_t type, button_cb cb, void* arg, int interval_tick);
95+
esp_err_t iot_button_set_evt_cb(button_handle_t btn_handle, button_cb_type_t type, button_cb cb, void* arg);
8496

8597
/**
8698
* @brief
@@ -132,7 +144,7 @@ esp_err_t iot_button_rm_cb(button_handle_t btn_handle, button_cb_type_t type);
132144
* class of button
133145
* simple usage:
134146
* CButton* btn = new CButton(BUTTON_IO_NUM, BUTTON_ACTIVE_LEVEL, BUTTON_SERIAL_TRIGGER, 3);
135-
* btn->add_cb(BUTTON_PUSH_CB, button_tap_cb, (void*) push, 50 / portTICK_PERIOD_MS);
147+
* btn->add_cb(BUTTON_CB_PUSH, button_tap_cb, (void*) push, 50 / portTICK_PERIOD_MS);
136148
* btn->add_custom_cb(5, button_press_5s_cb, NULL);
137149
* ......
138150
* delete btn;
@@ -155,10 +167,8 @@ class CButton
155167
* @param gpio_num GPIO index of the pin that the button uses
156168
* @param active_level button hardware active level.
157169
* For "BUTTON_ACTIVE_LOW" it means when the button pressed, the GPIO will read low level.
158-
* @param trigger button event trigger mode
159-
* @param serial_thres_sec the number of seconds more than whick a BUTTON_SERIAL_CB would accour
160170
*/
161-
CButton(gpio_num_t gpio_num, button_active_t active_level = BUTTON_ACTIVE_LOW, button_trigger_t trigger = BUTTON_SINGLE_TRIGGER, uint32_t serial_thres_sec = 3);
171+
CButton(gpio_num_t gpio_num, button_active_t active_level = BUTTON_ACTIVE_LOW);
162172

163173
~CButton();
164174

@@ -168,7 +178,6 @@ class CButton
168178
* @param type callback function type
169179
* @param cb callback function for "TAP" action.
170180
* @param arg Parameter for callback function
171-
* @param interval_tick a filter to bypass glitch, unit: FreeRTOS ticks.
172181
* @note
173182
* Button callback functions execute in the context of the timer service task.
174183
* It is therefore essential that button callback functions never attempt to block.
@@ -178,8 +187,26 @@ class CButton
178187
* - ESP_OK Success
179188
* - ESP_FAIL Parameter error
180189
*/
181-
esp_err_t add_cb(button_cb_type_t type, button_cb cb, void* arg, int interval_tick);
190+
esp_err_t set_evt_cb(button_cb_type_t type, button_cb cb, void* arg);
182191

192+
/**
193+
* @brief Register a callback function for a serial trigger event.
194+
*
195+
* @param btn_handle handle of the button object
196+
* @start_after_sec define the time after which to start serial trigger action
197+
* @interval_tick serial trigger interval
198+
* @cb callback function for "TAP" action.
199+
* @arg Parameter for callback function
200+
* @note
201+
* Button callback functions execute in the context of the timer service task.
202+
* It is therefore essential that button callback functions never attempt to block.
203+
* For example, a button callback function must not call vTaskDelay(), vTaskDelayUntil(),
204+
* or specify a non zero block time when accessing a queue or a semaphore.
205+
* @return
206+
* - ESP_OK Success
207+
* - ESP_FAIL Parameter error
208+
*/
209+
esp_err_t set_serial_cb(button_cb cb, void* arg, TickType_t interval_tick, uint32_t start_after_sec);
183210

184211
/**
185212
* @brief

components/general/button/component.mk

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
#
44
# (Uses default behaviour of compiling all source files in directory, adding 'include' to include path.)
55
ifdef CONFIG_IOT_BUTTON_ENABLE
6-
COMPONENT_ADD_INCLUDEDIRS := include
7-
COMPONENT_SRCDIRS := .
6+
COMPONENT_ADD_INCLUDEDIRS := ./button/include
7+
COMPONENT_SRCDIRS := ./button
88
else
99
# Disable component
1010
COMPONENT_ADD_INCLUDEDIRS :=

components/general/button/test/button_obj_test.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,16 @@ extern "C" void button_obj_test()
5555
const char *release = "RELEASE";
5656
const char *tap = "TAP";
5757
const char *serial = "SERIAL";
58-
CButton* btn = new CButton(BUTTON_IO_NUM, BUTTON_ACTIVE_LEVEL, BUTTON_SERIAL_TRIGGER, 3);
59-
btn->add_cb(BUTTON_PUSH_CB, button_tap_cb, (void*) push, 50 / portTICK_PERIOD_MS);
60-
btn->add_cb(BUTTON_RELEASE_CB, button_tap_cb, (void*) release, 50 / portTICK_PERIOD_MS);
61-
btn->add_cb(BUTTON_TAP_CB, button_tap_cb, (void*) tap, 50 / portTICK_PERIOD_MS);
62-
btn->add_cb(BUTTON_SERIAL_CB, button_tap_cb, (void*) serial, 1000 / portTICK_PERIOD_MS);
58+
CButton* btn = new CButton(BUTTON_IO_NUM, BUTTON_ACTIVE_LEVEL);
59+
btn->set_evt_cb(BUTTON_CB_PUSH, button_tap_cb, (void*) push);
60+
btn->set_evt_cb(BUTTON_CB_RELEASE, button_tap_cb, (void*) release);
61+
btn->set_evt_cb(BUTTON_CB_TAP, button_tap_cb, (void*) tap);
62+
btn->set_evt_cb(BUTTON_CB_SERIAL, button_tap_cb, (void*) serial);
6363

6464
btn->add_custom_cb(2, button_press_2s_cb, NULL);
6565
btn->add_custom_cb(5, button_press_5s_cb, NULL);
6666

67-
vTaskDelay(40000 / portTICK_PERIOD_MS);
67+
vTaskDelay(10000 / portTICK_PERIOD_MS);
6868
delete btn;
6969
ESP_LOGI(TAG_BTN, "button is deleted");
7070
}

0 commit comments

Comments
 (0)