Skip to content

Commit 1203f2a

Browse files
committed
feat(matter): adjusts boot button functionality for all examples
1 parent e9f1b92 commit 1203f2a

File tree

11 files changed

+173
-202
lines changed

11 files changed

+173
-202
lines changed

Diff for: libraries/Matter/examples/MatterColorLight/MatterColorLight.ino

+15-18
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,13 @@ const uint8_t ledPin = 2; // Set your pin here if your board has not defined LE
3535
#endif
3636

3737
// set your board USER BUTTON pin here
38-
#ifdef BOOT_PIN
3938
const uint8_t buttonPin = BOOT_PIN; // Set your pin here. Using BOOT Button.
40-
#else
41-
const uint8_t buttonPin = 0; // Set your button pin here.
42-
#warning "Do not forget to set the USER BUTTON pin"
43-
#endif
39+
40+
// Button control
41+
uint32_t button_time_stamp = 0; // debouncing control
42+
bool button_state = false; // false = released | true = pressed
43+
const uint32_t debouceTime = 250; // button debouncing time (ms)
44+
const uint32_t decommissioningTimeout = 5000; // keep the button pressed for 5s, or longer, to decommission
4445

4546
// WiFi is manually set and started
4647
const char *ssid = "your-ssid"; // Change this to your WiFi SSID
@@ -130,11 +131,6 @@ void setup() {
130131
ColorLight.updateAccessory();
131132
}
132133
}
133-
// Button control
134-
uint32_t button_time_stamp = 0; // debouncing control
135-
bool button_state = false; // false = released | true = pressed
136-
const uint32_t debouceTime = 250; // button debouncing time (ms)
137-
const uint32_t decommissioningTimeout = 10000; // keep the button pressed for 10s to decommission the light
138134

139135
void loop() {
140136
// Check Matter Light Commissioning state, which may change during execution of loop()
@@ -172,17 +168,18 @@ void loop() {
172168

173169
// Onboard User Button is used as a Light toggle switch or to decommission it
174170
uint32_t time_diff = millis() - button_time_stamp;
175-
if (button_state && time_diff > debouceTime && digitalRead(buttonPin) == HIGH) {
176-
button_state = false; // released
171+
if (digitalRead(buttonPin) == HIGH && button_state && time_diff > debouceTime) {
177172
// Toggle button is released - toggle the light
178173
Serial.println("User button released. Toggling Light!");
179174
ColorLight.toggle(); // Matter Controller also can see the change
175+
button_state = false; // released
176+
}
180177

181-
// Factory reset is triggered if the button is pressed longer than 10 seconds
182-
if (time_diff > decommissioningTimeout) {
183-
Serial.println("Decommissioning the Light Matter Accessory. It shall be commissioned again.");
184-
ColorLight = false; // turn the light off
185-
Matter.decommission();
186-
}
178+
// Onboard User Button is kept pressed for longer than 5 seconds in order to decommission matter node
179+
if (button_state && time_diff > decommissioningTimeout) {
180+
Serial.println("Decommissioning the Light Matter Accessory. It shall be commissioned again.");
181+
ColorLight = false; // turn the light off
182+
Matter.decommission();
183+
button_time_stamp = millis(); // avoid running decommissining again, reboot takes a second or so
187184
}
188185
}

Diff for: libraries/Matter/examples/MatterComposedLights/MatterComposedLights.ino

+23-23
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ const char *ssid = "your-ssid"; // Change this to your WiFi SSID
2727
const char *password = "your-password"; // Change this to your WiFi password
2828

2929
// set your board USER BUTTON pin here - USED to decommission the Matter Node
30-
#ifdef BOOT_PIN
3130
const uint8_t buttonPin = BOOT_PIN; // Set your pin here. Using BOOT Button.
32-
#else
33-
const uint8_t buttonPin = 0; // Set your button pin here.
34-
#warning "Do not forget to set the USER BUTTON pin"
35-
#endif
31+
32+
// Button control
33+
uint32_t button_time_stamp = 0; // debouncing control
34+
bool button_state = false; // false = released | true = pressed
35+
const uint32_t decommissioningTimeout = 5000; // keep the button pressed for 5s, or longer, to decommission
3636

3737
// Matter Protocol Endpoint Callback for each Light Accessory
3838
bool setLightOnOff1(bool state) {
@@ -89,12 +89,9 @@ void setup() {
8989
Matter.begin();
9090
}
9191

92-
// Button control
93-
uint32_t button_time_stamp = 0; // debouncing control
94-
bool button_state = false; // false = released | true = pressed
95-
const uint32_t decommissioningTimeout = 10000; // keep the button pressed for 10s to decommission the light
96-
9792
void loop() {
93+
static uint32_t timeCounter = 0;
94+
9895
// Check Matter Light Commissioning state
9996
if (!Matter.isDeviceCommissioned()) {
10097
Serial.println("");
@@ -115,10 +112,12 @@ void loop() {
115112
}
116113

117114
//displays the Light state every 5 seconds
118-
Serial.println("======================");
119-
Serial.printf("Matter Light #1 is %s\r\n", Light1.getOnOff() ? "ON" : "OFF");
120-
Serial.printf("Matter Light #2 is %s\r\n", Light2.getOnOff() ? "ON" : "OFF");
121-
Serial.printf("Matter Light #3 is %s\r\n", Light3.getOnOff() ? "ON" : "OFF");
115+
if (!(timeCounter++ % 10)) { // delaying for 500ms x 10 = 5s
116+
Serial.println("======================");
117+
Serial.printf("Matter Light #1 is %s\r\n", Light1.getOnOff() ? "ON" : "OFF");
118+
Serial.printf("Matter Light #2 is %s\r\n", Light2.getOnOff() ? "ON" : "OFF");
119+
Serial.printf("Matter Light #3 is %s\r\n", Light3.getOnOff() ? "ON" : "OFF");
120+
}
122121

123122
// Check if the button has been pressed
124123
if (digitalRead(buttonPin) == LOW && !button_state) {
@@ -127,16 +126,17 @@ void loop() {
127126
button_state = true; // pressed.
128127
}
129128

130-
// Onboard User Button is used to decommission matter fabric
131-
uint32_t time_diff = millis() - button_time_stamp;
132-
if (button_state && time_diff > decommissioningTimeout && digitalRead(buttonPin) == HIGH) {
129+
if (digitalRead(buttonPin) == HIGH && button_state) {
133130
button_state = false; // released
134-
// Factory reset is triggered if the button is pressed longer than 10 seconds
135-
if (time_diff > decommissioningTimeout) {
136-
Serial.println("Decommissioning the Light Matter Accessory. It shall be commissioned again.");
137-
Matter.decommission();
138-
}
139131
}
140132

141-
delay(5000);
133+
// Onboard User Button is kept pressed for longer than 5 seconds in order to decommission matter node
134+
uint32_t time_diff = millis() - button_time_stamp;
135+
if (button_state && time_diff > decommissioningTimeout) {
136+
Serial.println("Decommissioning the Light Matter Accessory. It shall be commissioned again.");
137+
Matter.decommission();
138+
button_time_stamp = millis(); // avoid running decommissining again, reboot takes a second or so
139+
}
140+
141+
delay(500);
142142
}

Diff for: libraries/Matter/examples/MatterDimmableLight/MatterDimmableLight.ino

+15-18
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,13 @@ const uint8_t ledPin = 2; // Set your pin here if your board has not defined LE
3535
#endif
3636

3737
// set your board USER BUTTON pin here
38-
#ifdef BOOT_PIN
3938
const uint8_t buttonPin = BOOT_PIN; // Set your pin here. Using BOOT Button.
40-
#else
41-
const uint8_t buttonPin = 0; // Set your button pin here.
42-
#warning "Do not forget to set the USER BUTTON pin"
43-
#endif
39+
40+
// Button control
41+
uint32_t button_time_stamp = 0; // debouncing control
42+
bool button_state = false; // false = released | true = pressed
43+
const uint32_t debouceTime = 250; // button debouncing time (ms)
44+
const uint32_t decommissioningTimeout = 5000; // keep the button pressed for 5s, or longer, to decommission
4445

4546
// WiFi is manually set and started
4647
const char *ssid = "your-ssid"; // Change this to your WiFi SSID
@@ -122,11 +123,6 @@ void setup() {
122123
DimmableLight.updateAccessory();
123124
}
124125
}
125-
// Button control
126-
uint32_t button_time_stamp = 0; // debouncing control
127-
bool button_state = false; // false = released | true = pressed
128-
const uint32_t debouceTime = 250; // button debouncing time (ms)
129-
const uint32_t decommissioningTimeout = 10000; // keep the button pressed for 10s to decommission the light
130126

131127
void loop() {
132128
// Check Matter Light Commissioning state, which may change during execution of loop()
@@ -161,17 +157,18 @@ void loop() {
161157

162158
// Onboard User Button is used as a Light toggle switch or to decommission it
163159
uint32_t time_diff = millis() - button_time_stamp;
164-
if (button_state && time_diff > debouceTime && digitalRead(buttonPin) == HIGH) {
165-
button_state = false; // released
160+
if (digitalRead(buttonPin) == HIGH && button_state && time_diff > debouceTime) {
166161
// Toggle button is released - toggle the light
167162
Serial.println("User button released. Toggling Light!");
168163
DimmableLight.toggle(); // Matter Controller also can see the change
164+
button_state = false; // released
165+
}
169166

170-
// Factory reset is triggered if the button is pressed longer than 10 seconds
171-
if (time_diff > decommissioningTimeout) {
172-
Serial.println("Decommissioning the Light Matter Accessory. It shall be commissioned again.");
173-
DimmableLight = false; // turn the light off
174-
Matter.decommission();
175-
}
167+
// Onboard User Button is kept pressed for longer than 5 seconds in order to decommission matter node
168+
if (button_state && time_diff > decommissioningTimeout) {
169+
Serial.println("Decommissioning the Light Matter Accessory. It shall be commissioned again.");
170+
DimmableLight = false; // turn the light off
171+
Matter.decommission();
172+
button_time_stamp = millis(); // avoid running decommissining again, reboot takes a second or so
176173
}
177174
}

Diff for: libraries/Matter/examples/MatterEnhancedColorLight/MatterEnhancedColorLight.ino

+13-16
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,13 @@ const uint8_t ledPin = 2; // Set your pin here if your board has not defined LE
3838
#endif
3939

4040
// set your board USER BUTTON pin here
41-
#ifdef BOOT_PIN
4241
const uint8_t buttonPin = BOOT_PIN; // Set your pin here. Using BOOT Button.
43-
#else
44-
const uint8_t buttonPin = 0; // Set your button pin here.
45-
#warning "Do not forget to set the USER BUTTON pin"
46-
#endif
42+
43+
// Button control
44+
uint32_t button_time_stamp = 0; // debouncing control
45+
bool button_state = false; // false = released | true = pressed
46+
const uint32_t debouceTime = 250; // button debouncing time (ms)
47+
const uint32_t decommissioningTimeout = 5000; // keep the button pressed for 5s, or longer, to decommission
4748

4849
// WiFi is manually set and started
4950
const char *ssid = "your-ssid"; // Change this to your WiFi SSID
@@ -152,11 +153,6 @@ void setup() {
152153
EnhancedColorLight.updateAccessory();
153154
}
154155
}
155-
// Button control
156-
uint32_t button_time_stamp = 0; // debouncing control
157-
bool button_state = false; // false = released | true = pressed
158-
const uint32_t debouceTime = 250; // button debouncing time (ms)
159-
const uint32_t decommissioningTimeout = 10000; // keep the button pressed for 10s to decommission the light
160156

161157
void loop() {
162158
// Check Matter Light Commissioning state, which may change during execution of loop()
@@ -199,12 +195,13 @@ void loop() {
199195
// Toggle button is released - toggle the light
200196
Serial.println("User button released. Toggling Light!");
201197
EnhancedColorLight.toggle(); // Matter Controller also can see the change
198+
}
202199

203-
// Factory reset is triggered if the button is pressed longer than 10 seconds
204-
if (time_diff > decommissioningTimeout) {
205-
Serial.println("Decommissioning the Light Matter Accessory. It shall be commissioned again.");
206-
EnhancedColorLight = false; // turn the light off
207-
Matter.decommission();
208-
}
200+
// Onboard User Button is kept pressed for longer than 5 seconds in order to decommission matter node
201+
if (button_state && time_diff > decommissioningTimeout) {
202+
Serial.println("Decommissioning the Light Matter Accessory. It shall be commissioned again.");
203+
EnhancedColorLight = false; // turn the light off
204+
Matter.decommission();
205+
button_time_stamp = millis(); // avoid running decommissining again, reboot takes a second or so
209206
}
210207
}

Diff for: libraries/Matter/examples/MatterFan/MatterFan.ino

+12-17
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@
2121
MatterFan Fan;
2222

2323
// set your board USER BUTTON pin here - used for toggling On/Off and decommission the Matter Node
24-
#ifdef BOOT_PIN
2524
const uint8_t buttonPin = BOOT_PIN; // Set your pin here. Using BOOT Button.
26-
#else
27-
const uint8_t buttonPin = 0; // Set your button pin here.
28-
#warning "Do not forget to set the USER BUTTON pin"
29-
#endif
3025

26+
// Button control
27+
uint32_t button_time_stamp = 0; // debouncing control
28+
bool button_state = false; // false = released | true = pressed
29+
const uint32_t debouceTime = 250; // button debouncing time (ms)
30+
const uint32_t decommissioningTimeout = 5000; // keep the button pressed for 5s, or longer, to decommission
3131

3232
// set your board Analog Pin here - used for changing the Fan speed
3333
const uint8_t analogPin = A0; // Analog Pin depends on each board
@@ -146,12 +146,6 @@ void setup() {
146146
}
147147
}
148148

149-
// Builtin Button control
150-
uint32_t button_time_stamp = 0; // debouncing control
151-
bool button_state = false; // false = released | true = pressed
152-
const uint32_t debouceTime = 250; // button debouncing time (ms)
153-
const uint32_t decommissioningTimeout = 10000; // keep the button pressed for 10s to decommission the Matter Fabric
154-
155149
void loop() {
156150
// Check Matter Accessory Commissioning state, which may change during execution of loop()
157151
if (!Matter.isDeviceCommissioned()) {
@@ -187,14 +181,15 @@ void loop() {
187181
// button is released - toggle Fan On/Off
188182
Fan.toggle();
189183
Serial.printf("User button released. Setting the Fan %s.\r\n", Fan > 0 ? "ON" : "OFF");
190-
191-
// Factory reset is triggered if the button is pressed longer than 10 seconds
192-
if (time_diff > decommissioningTimeout) {
193-
Serial.println("Decommissioning the Generic Switch Matter Accessory. It shall be commissioned again.");
194-
Matter.decommission();
195-
}
196184
}
197185

186+
// Onboard User Button is kept pressed for longer than 5 seconds in order to decommission matter node
187+
if (button_state && time_diff > decommissioningTimeout) {
188+
Serial.println("Decommissioning the Generic Switch Matter Accessory. It shall be commissioned again.");
189+
Matter.decommission();
190+
button_time_stamp = millis(); // avoid running decommissining again, reboot takes a second or so
191+
}
192+
198193
// checks Analog pin and adjust the speed only if it has changed
199194
static int lastRead = 0;
200195
// analog values (0..1023) / 103 => mapped into 10 steps (0..9)

Diff for: libraries/Matter/examples/MatterMinimum/MatterMinimum.ino

+16-20
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@ const uint8_t ledPin = LED_BUILTIN;
3535
const uint8_t ledPin = 2; // Set your pin here if your board has not defined LED_BUILTIN
3636
#endif
3737

38-
// set your board USER BUTTON pin here
39-
#ifdef BOOT_PIN
38+
// set your board USER BUTTON pin here - decommissioning button
4039
const uint8_t buttonPin = BOOT_PIN; // Set your pin here. Using BOOT Button.
41-
#else
42-
const uint8_t buttonPin = 0; // Set your button pin here.
43-
#warning "Do not forget to set the USER BUTTON pin"
44-
#endif
40+
41+
// Button control - decommision the Matter Node
42+
uint32_t button_time_stamp = 0; // debouncing control
43+
bool button_state = false; // false = released | true = pressed
44+
const uint32_t decommissioningTimeout = 5000; // keep the button pressed for 5s, or longer, to decommission
4545

4646
// Matter Protocol Endpoint (On/OFF Light) Callback
4747
bool matterCB(bool state) {
@@ -86,11 +86,6 @@ void setup() {
8686
}
8787
}
8888

89-
// Button control - decommision the Matter Node
90-
uint32_t button_time_stamp = 0; // debouncing control
91-
bool button_state = false; // false = released | true = pressed
92-
const uint32_t decommissioningTimeout = 10000; // keep the button pressed for 10s to decommission
93-
9489
void loop() {
9590
// Check if the button has been pressed
9691
if (digitalRead(buttonPin) == LOW && !button_state) {
@@ -99,16 +94,17 @@ void loop() {
9994
button_state = true; // pressed.
10095
}
10196

102-
// Onboard User Button is used to decommission matter node
103-
uint32_t time_diff = millis() - button_time_stamp;
104-
if (button_state && time_diff > decommissioningTimeout && digitalRead(buttonPin) == HIGH) {
97+
if (digitalRead(buttonPin) == HIGH && button_state) {
10598
button_state = false; // released
106-
// Factory reset is triggered if the button is pressed longer than 10 seconds
107-
if (time_diff > decommissioningTimeout) {
108-
Serial.println("Decommissioning the Light Matter Accessory. It shall be commissioned again.");
109-
Matter.decommission();
110-
}
11199
}
112100

113-
delay(5000);
101+
// Onboard User Button is kept pressed for longer than 5 seconds in order to decommission matter node
102+
uint32_t time_diff = millis() - button_time_stamp;
103+
if (button_state && time_diff > decommissioningTimeout) {
104+
Serial.println("Decommissioning the Light Matter Accessory. It shall be commissioned again.");
105+
Matter.decommission();
106+
button_time_stamp = millis(); // avoid running decommissining again, reboot takes a second or so
107+
}
108+
109+
delay(500);
114110
}

0 commit comments

Comments
 (0)