Skip to content

Commit 9a86d6f

Browse files
committed
fix(example): Use proper naming of func and variables
1 parent 2fdeef7 commit 9a86d6f

File tree

1 file changed

+37
-37
lines changed

1 file changed

+37
-37
lines changed

libraries/Zigbee/examples/Zigbee_On_Off_Switch/Zigbee_On_Off_Switch.ino

+37-37
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
#define SWITCH_ENDPOINT_NUMBER 5
3838

3939
/* Switch configuration */
40-
#define GPIO_INPUT_IO_TOGGLE_SWITCH GPIO_NUM_9
40+
#define GPIO_INPUT_IO_TOGGLE_SWITCH 9
4141
#define PAIR_SIZE(TYPE_STR_PAIR) (sizeof(TYPE_STR_PAIR) / sizeof(TYPE_STR_PAIR[0]))
4242

4343
typedef enum {
@@ -48,22 +48,22 @@ typedef enum {
4848
SWITCH_LEVEL_DOWN_CONTROL,
4949
SWITCH_LEVEL_CYCLE_CONTROL,
5050
SWITCH_COLOR_CONTROL,
51-
} switch_func_t;
51+
} SwitchFunction;
5252

5353
typedef struct {
5454
uint8_t pin;
55-
switch_func_t func;
56-
} switch_func_pair_t;
55+
SwitchFunction func;
56+
} SwitchData;
5757

5858
typedef enum {
5959
SWITCH_IDLE,
6060
SWITCH_PRESS_ARMED,
6161
SWITCH_PRESS_DETECTED,
6262
SWITCH_PRESSED,
6363
SWITCH_RELEASE_DETECTED,
64-
} switch_state_t;
64+
} SwitchState;
6565

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}};
6767

6868
/* Zigbee switch */
6969
class MyZigbeeSwitch : public ZigbeeSwitch {
@@ -85,7 +85,7 @@ public:
8585
MyZigbeeSwitch zbSwitch = MyZigbeeSwitch(SWITCH_ENDPOINT_NUMBER);
8686

8787
/********************* Zigbee functions **************************/
88-
static void esp_zb_buttons_handler(switch_func_pair_t *button_func_pair) {
88+
static void onZbButton(SwitchData *button_func_pair) {
8989
if (button_func_pair->func == SWITCH_ONOFF_TOGGLE_CONTROL) {
9090
// Send toggle command to the light
9191
zbSwitch.lightToggle();
@@ -95,16 +95,16 @@ static void esp_zb_buttons_handler(switch_func_pair_t *button_func_pair) {
9595
/********************* GPIO functions **************************/
9696
static QueueHandle_t gpio_evt_queue = NULL;
9797

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);
100100
}
101101

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) {
104104
if (enabled) {
105-
enableInterrupt((button_func_pair[i]).pin);
105+
enableInterrupt((buttonFunctionPair[i]).pin);
106106
} else {
107-
disableInterrupt((button_func_pair[i]).pin);
107+
disableInterrupt((buttonFunctionPair[i]).pin);
108108
}
109109
}
110110
}
@@ -129,15 +129,15 @@ void setup() {
129129

130130

131131
// 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);
134134
/* 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));
136136
if (gpio_evt_queue == 0) {
137137
log_e("Queue was not created and must not be used");
138138
while (1);
139139
}
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);
141141
}
142142

143143
// When all EPs are registered, start Zigbee with ZIGBEE_COORDINATOR mode
@@ -157,41 +157,41 @@ void setup() {
157157
void loop() {
158158
// Handle button switch in loop()
159159
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;
163163

164164

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;
170170
}
171-
while (evt_flag) {
171+
while (eventFlag) {
172172
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;
176176
case SWITCH_RELEASE_DETECTED:
177-
switch_state = SWITCH_IDLE;
177+
buttonState = SWITCH_IDLE;
178178
/* callback to button_handler */
179-
(*esp_zb_buttons_handler)(&button_func_pair);
179+
(*onZbButton)(&buttonSwitch);
180180
break;
181181
default: break;
182182
}
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;
186186
break;
187187
}
188188
vTaskDelay(10 / portTICK_PERIOD_MS);
189189
}
190190

191191
// 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();
195195
zbSwitch.printBoundDevices();
196196
}
197197
}

0 commit comments

Comments
 (0)