37
37
#define SWITCH_ENDPOINT_NUMBER 5
38
38
39
39
/* Switch configuration */
40
- #define GPIO_INPUT_IO_TOGGLE_SWITCH GPIO_NUM_9
40
+ #define GPIO_INPUT_IO_TOGGLE_SWITCH 9
41
41
#define PAIR_SIZE (TYPE_STR_PAIR ) (sizeof (TYPE_STR_PAIR) / sizeof (TYPE_STR_PAIR[0 ]))
42
42
43
43
typedef enum {
@@ -48,22 +48,22 @@ typedef enum {
48
48
SWITCH_LEVEL_DOWN_CONTROL,
49
49
SWITCH_LEVEL_CYCLE_CONTROL,
50
50
SWITCH_COLOR_CONTROL,
51
- } switch_func_t ;
51
+ } SwitchFunction ;
52
52
53
53
typedef struct {
54
54
uint8_t pin;
55
- switch_func_t func;
56
- } switch_func_pair_t ;
55
+ SwitchFunction func;
56
+ } SwitchData ;
57
57
58
58
typedef enum {
59
59
SWITCH_IDLE,
60
60
SWITCH_PRESS_ARMED,
61
61
SWITCH_PRESS_DETECTED,
62
62
SWITCH_PRESSED,
63
63
SWITCH_RELEASE_DETECTED,
64
- } switch_state_t ;
64
+ } SwitchState ;
65
65
66
- static switch_func_pair_t button_func_pair [] = {{GPIO_INPUT_IO_TOGGLE_SWITCH, SWITCH_ONOFF_TOGGLE_CONTROL}};
66
+ static SwitchData buttonFunctionPair [] = {{GPIO_INPUT_IO_TOGGLE_SWITCH, SWITCH_ONOFF_TOGGLE_CONTROL}};
67
67
68
68
/* Zigbee switch */
69
69
class MyZigbeeSwitch : public ZigbeeSwitch {
@@ -85,7 +85,7 @@ public:
85
85
MyZigbeeSwitch zbSwitch = MyZigbeeSwitch(SWITCH_ENDPOINT_NUMBER);
86
86
87
87
/* ******************** Zigbee functions **************************/
88
- static void esp_zb_buttons_handler ( switch_func_pair_t *button_func_pair) {
88
+ static void onZbButton (SwitchData *button_func_pair) {
89
89
if (button_func_pair->func == SWITCH_ONOFF_TOGGLE_CONTROL) {
90
90
// Send toggle command to the light
91
91
zbSwitch.lightToggle ();
@@ -95,16 +95,16 @@ static void esp_zb_buttons_handler(switch_func_pair_t *button_func_pair) {
95
95
/* ******************** GPIO functions **************************/
96
96
static QueueHandle_t gpio_evt_queue = NULL ;
97
97
98
- static void IRAM_ATTR gpio_isr_handler (void *arg) {
99
- xQueueSendFromISR (gpio_evt_queue, (switch_func_pair_t *)arg, NULL );
98
+ static void IRAM_ATTR onGpioInterrupt (void *arg) {
99
+ xQueueSendFromISR (gpio_evt_queue, (SwitchData *)arg, NULL );
100
100
}
101
101
102
- static void switch_gpios_intr_enabled (bool enabled) {
103
- for (int i = 0 ; i < PAIR_SIZE (button_func_pair ); ++i) {
102
+ static void enableGpioInterrupt (bool enabled) {
103
+ for (int i = 0 ; i < PAIR_SIZE (buttonFunctionPair ); ++i) {
104
104
if (enabled) {
105
- enableInterrupt ((button_func_pair [i]).pin );
105
+ enableInterrupt ((buttonFunctionPair [i]).pin );
106
106
} else {
107
- disableInterrupt ((button_func_pair [i]).pin );
107
+ disableInterrupt ((buttonFunctionPair [i]).pin );
108
108
}
109
109
}
110
110
}
@@ -129,15 +129,15 @@ void setup() {
129
129
130
130
131
131
// Init button switch
132
- for (int i = 0 ; i < PAIR_SIZE (button_func_pair ); i++) {
133
- pinMode (button_func_pair [i].pin , INPUT_PULLUP);
132
+ for (int i = 0 ; i < PAIR_SIZE (buttonFunctionPair ); i++) {
133
+ pinMode (buttonFunctionPair [i].pin , INPUT_PULLUP);
134
134
/* create a queue to handle gpio event from isr */
135
- gpio_evt_queue = xQueueCreate (10 , sizeof (switch_func_pair_t ));
135
+ gpio_evt_queue = xQueueCreate (10 , sizeof (SwitchData ));
136
136
if (gpio_evt_queue == 0 ) {
137
137
log_e (" Queue was not created and must not be used" );
138
138
while (1 );
139
139
}
140
- attachInterruptArg (button_func_pair [i].pin , gpio_isr_handler , (void *)(button_func_pair + i), FALLING);
140
+ attachInterruptArg (buttonFunctionPair [i].pin , onGpioInterrupt , (void *)(buttonFunctionPair + i), FALLING);
141
141
}
142
142
143
143
// When all EPs are registered, start Zigbee with ZIGBEE_COORDINATOR mode
@@ -157,41 +157,41 @@ void setup() {
157
157
void loop () {
158
158
// Handle button switch in loop()
159
159
uint8_t pin = 0 ;
160
- switch_func_pair_t button_func_pair ;
161
- static switch_state_t switch_state = SWITCH_IDLE;
162
- bool evt_flag = false ;
160
+ SwitchData buttonSwitch ;
161
+ static SwitchState buttonState = SWITCH_IDLE;
162
+ bool eventFlag = false ;
163
163
164
164
165
- /* check if there is any queue received, if yes read out the button_func_pair */
166
- if (xQueueReceive (gpio_evt_queue, &button_func_pair , portMAX_DELAY)) {
167
- pin = button_func_pair .pin ;
168
- switch_gpios_intr_enabled (false );
169
- evt_flag = true ;
165
+ /* check if there is any queue received, if yes read out the buttonSwitch */
166
+ if (xQueueReceive (gpio_evt_queue, &buttonSwitch , portMAX_DELAY)) {
167
+ pin = buttonSwitch .pin ;
168
+ enableGpioInterrupt (false );
169
+ eventFlag = true ;
170
170
}
171
- while (evt_flag ) {
171
+ while (eventFlag ) {
172
172
bool value = digitalRead (pin);
173
- switch (switch_state ) {
174
- case SWITCH_IDLE: switch_state = (value == LOW) ? SWITCH_PRESS_DETECTED : SWITCH_IDLE; break ;
175
- case SWITCH_PRESS_DETECTED: switch_state = (value == LOW) ? SWITCH_PRESS_DETECTED : SWITCH_RELEASE_DETECTED; break ;
173
+ switch (buttonState ) {
174
+ case SWITCH_IDLE: buttonState = (value == LOW) ? SWITCH_PRESS_DETECTED : SWITCH_IDLE; break ;
175
+ case SWITCH_PRESS_DETECTED: buttonState = (value == LOW) ? SWITCH_PRESS_DETECTED : SWITCH_RELEASE_DETECTED; break ;
176
176
case SWITCH_RELEASE_DETECTED:
177
- switch_state = SWITCH_IDLE;
177
+ buttonState = SWITCH_IDLE;
178
178
/* callback to button_handler */
179
- (*esp_zb_buttons_handler )(&button_func_pair );
179
+ (*onZbButton )(&buttonSwitch );
180
180
break ;
181
181
default : break ;
182
182
}
183
- if (switch_state == SWITCH_IDLE) {
184
- switch_gpios_intr_enabled (true );
185
- evt_flag = false ;
183
+ if (buttonState == SWITCH_IDLE) {
184
+ enableGpioInterrupt (true );
185
+ eventFlag = false ;
186
186
break ;
187
187
}
188
188
vTaskDelay (10 / portTICK_PERIOD_MS);
189
189
}
190
190
191
191
// print the bound lights every 10 seconds
192
- static uint32_t last_print = 0 ;
193
- if (millis () - last_print > 10000 ) {
194
- last_print = millis ();
192
+ static uint32_t lastPrint = 0 ;
193
+ if (millis () - lastPrint > 10000 ) {
194
+ lastPrint = millis ();
195
195
zbSwitch.printBoundDevices ();
196
196
}
197
197
}
0 commit comments