Skip to content

Commit de4824f

Browse files
authored
feat(matter_examples): apply boot button change to all examples (#10702)
* feat(matter_examples): apply boot button change to all examples
1 parent 5a8cba8 commit de4824f

File tree

13 files changed

+260
-115
lines changed

13 files changed

+260
-115
lines changed

libraries/Matter/examples/Matter_ColorLight/Matter_ColorLight.ino libraries/Matter/examples/MatterColorLight/MatterColorLight.ino

+17-15
Original file line numberDiff line numberDiff line change
@@ -35,7 +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-
const uint8_t buttonPin = 0; // Set your pin here. Using BOOT Button. C6/C3 use GPIO9.
38+
const uint8_t buttonPin = BOOT_PIN; // Set your pin here. Using BOOT Button.
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
3945

4046
// WiFi is manually set and started
4147
const char *ssid = "your-ssid"; // Change this to your WiFi SSID
@@ -125,11 +131,6 @@ void setup() {
125131
ColorLight.updateAccessory();
126132
}
127133
}
128-
// Button control
129-
uint32_t button_time_stamp = 0; // debouncing control
130-
bool button_state = false; // false = released | true = pressed
131-
const uint32_t debouceTime = 250; // button debouncing time (ms)
132-
const uint32_t decommissioningTimeout = 10000; // keep the button pressed for 10s to decommission the light
133134

134135
void loop() {
135136
// Check Matter Light Commissioning state, which may change during execution of loop()
@@ -167,17 +168,18 @@ void loop() {
167168

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

176-
// Factory reset is triggered if the button is pressed longer than 10 seconds
177-
if (time_diff > decommissioningTimeout) {
178-
Serial.println("Decommissioning the Light Matter Accessory. It shall be commissioned again.");
179-
ColorLight = false; // turn the light off
180-
Matter.decommission();
181-
}
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
182184
}
183185
}

libraries/Matter/examples/MatterComposedLights/MatterComposedLights.ino

+55-19
Original file line numberDiff line numberDiff line change
@@ -18,31 +18,42 @@
1818

1919
// List of Matter Endpoints for this Node
2020
// There will be 3 On/Off Light Endpoints in the same Node
21-
MatterOnOffLight OnOffLight1;
22-
MatterOnOffLight OnOffLight2;
23-
MatterOnOffLight OnOffLight3;
21+
MatterOnOffLight Light1;
22+
MatterDimmableLight Light2;
23+
MatterColorLight Light3;
2424

2525
// WiFi is manually set and started
2626
const char *ssid = "your-ssid"; // Change this to your WiFi SSID
2727
const char *password = "your-password"; // Change this to your WiFi password
2828

29+
// set your board USER BUTTON pin here - USED to decommission the Matter Node
30+
const uint8_t buttonPin = BOOT_PIN; // Set your pin here. Using BOOT Button.
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
36+
2937
// Matter Protocol Endpoint Callback for each Light Accessory
3038
bool setLightOnOff1(bool state) {
31-
Serial.printf("CB-Light1 changed state to: %s\r\n", state ? "ON" : "OFF");
39+
Serial.printf("Light1 changed state to: %s\r\n", state ? "ON" : "OFF");
3240
return true;
3341
}
3442

3543
bool setLightOnOff2(bool state) {
36-
Serial.printf("CB-Light2 changed state to: %s\r\n", state ? "ON" : "OFF");
44+
Serial.printf("Light2 changed state to: %s\r\n", state ? "ON" : "OFF");
3745
return true;
3846
}
3947

4048
bool setLightOnOff3(bool state) {
41-
Serial.printf("CB-Light3 changed state to: %s\r\n", state ? "ON" : "OFF");
49+
Serial.printf("Light3 changed state to: %s\r\n", state ? "ON" : "OFF");
4250
return true;
4351
}
4452

4553
void setup() {
54+
// Initialize the USER BUTTON (Boot button) that will be used to decommission the Matter Node
55+
pinMode(buttonPin, INPUT_PULLUP);
56+
4657
Serial.begin(115200);
4758
while (!Serial) {
4859
delay(100);
@@ -60,24 +71,27 @@ void setup() {
6071
delay(500);
6172
Serial.print(".");
6273
}
63-
Serial.println("\r\nWiFi connected");
74+
Serial.println();
75+
Serial.println("WiFi connected");
6476
Serial.println("IP address: ");
6577
Serial.println(WiFi.localIP());
6678
delay(500);
6779

6880
// Initialize all 3 Matter EndPoints
69-
OnOffLight1.begin();
70-
OnOffLight2.begin();
71-
OnOffLight3.begin();
72-
OnOffLight1.onChange(setLightOnOff1);
73-
OnOffLight2.onChange(setLightOnOff2);
74-
OnOffLight3.onChange(setLightOnOff3);
81+
Light1.begin();
82+
Light2.begin();
83+
Light3.begin();
84+
Light1.onChangeOnOff(setLightOnOff1);
85+
Light2.onChangeOnOff(setLightOnOff2);
86+
Light3.onChangeOnOff(setLightOnOff3);
7587

7688
// Matter beginning - Last step, after all EndPoints are initialized
7789
Matter.begin();
7890
}
7991

8092
void loop() {
93+
static uint32_t timeCounter = 0;
94+
8195
// Check Matter Light Commissioning state
8296
if (!Matter.isDeviceCommissioned()) {
8397
Serial.println("");
@@ -97,10 +111,32 @@ void loop() {
97111
Serial.println("Matter Node is commissioned and connected to Wi-Fi. Ready for use.");
98112
}
99113

100-
//displays the Light state every 3 seconds
101-
Serial.println("======================");
102-
Serial.printf("Matter Light #1 is %s\r\n", OnOffLight1.getOnOff() ? "ON" : "OFF");
103-
Serial.printf("Matter Light #2 is %s\r\n", OnOffLight2.getOnOff() ? "ON" : "OFF");
104-
Serial.printf("Matter Light #3 is %s\r\n", OnOffLight3.getOnOff() ? "ON" : "OFF");
105-
delay(3000);
114+
//displays the Light state every 5 seconds
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+
}
121+
122+
// Check if the button has been pressed
123+
if (digitalRead(buttonPin) == LOW && !button_state) {
124+
// deals with button debouncing
125+
button_time_stamp = millis(); // record the time while the button is pressed.
126+
button_state = true; // pressed.
127+
}
128+
129+
if (digitalRead(buttonPin) == HIGH && button_state) {
130+
button_state = false; // released
131+
}
132+
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);
106142
}

libraries/Matter/examples/MatterDimmableLight/MatterDimmableLight.ino

+16-14
Original file line numberDiff line numberDiff line change
@@ -35,7 +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-
const uint8_t buttonPin = 0; // Set your pin here. Using BOOT Button. C6/C3 use GPIO9.
38+
const uint8_t buttonPin = BOOT_PIN; // Set your pin here. Using BOOT Button.
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
3945

4046
// WiFi is manually set and started
4147
const char *ssid = "your-ssid"; // Change this to your WiFi SSID
@@ -117,11 +123,6 @@ void setup() {
117123
DimmableLight.updateAccessory();
118124
}
119125
}
120-
// Button control
121-
uint32_t button_time_stamp = 0; // debouncing control
122-
bool button_state = false; // false = released | true = pressed
123-
const uint32_t debouceTime = 250; // button debouncing time (ms)
124-
const uint32_t decommissioningTimeout = 10000; // keep the button pressed for 10s to decommission the light
125126

126127
void loop() {
127128
// Check Matter Light Commissioning state, which may change during execution of loop()
@@ -156,17 +157,18 @@ void loop() {
156157

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

165-
// Factory reset is triggered if the button is pressed longer than 10 seconds
166-
if (time_diff > decommissioningTimeout) {
167-
Serial.println("Decommissioning the Light Matter Accessory. It shall be commissioned again.");
168-
DimmableLight = false; // turn the light off
169-
Matter.decommission();
170-
}
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
171173
}
172174
}

libraries/Matter/examples/MatterEnhancedColorLight/MatterEnhancedColorLight.ino

+14-12
Original file line numberDiff line numberDiff line change
@@ -38,7 +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-
const uint8_t buttonPin = 0; // Set your pin here. Using BOOT Button. C6/C3 use GPIO9.
41+
const uint8_t buttonPin = BOOT_PIN; // Set your pin here. Using BOOT Button.
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
4248

4349
// WiFi is manually set and started
4450
const char *ssid = "your-ssid"; // Change this to your WiFi SSID
@@ -147,11 +153,6 @@ void setup() {
147153
EnhancedColorLight.updateAccessory();
148154
}
149155
}
150-
// Button control
151-
uint32_t button_time_stamp = 0; // debouncing control
152-
bool button_state = false; // false = released | true = pressed
153-
const uint32_t debouceTime = 250; // button debouncing time (ms)
154-
const uint32_t decommissioningTimeout = 10000; // keep the button pressed for 10s to decommission the light
155156

156157
void loop() {
157158
// Check Matter Light Commissioning state, which may change during execution of loop()
@@ -194,12 +195,13 @@ void loop() {
194195
// Toggle button is released - toggle the light
195196
Serial.println("User button released. Toggling Light!");
196197
EnhancedColorLight.toggle(); // Matter Controller also can see the change
198+
}
197199

198-
// Factory reset is triggered if the button is pressed longer than 10 seconds
199-
if (time_diff > decommissioningTimeout) {
200-
Serial.println("Decommissioning the Light Matter Accessory. It shall be commissioned again.");
201-
EnhancedColorLight = false; // turn the light off
202-
Matter.decommission();
203-
}
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
204206
}
205207
}

libraries/Matter/examples/MatterFan/MatterFan.ino

+15-14
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,14 @@
2020
// Fan Endpoint - On/Off control + Speed Percent Control + Fan Modes
2121
MatterFan Fan;
2222

23-
// set your board USER BUTTON pin here - used for toggling On/Off
24-
const uint8_t buttonPin = 0; // Set your pin here. Using BOOT Button. C6/C3 use GPIO9.
23+
// set your board USER BUTTON pin here - used for toggling On/Off and decommission the Matter Node
24+
const uint8_t buttonPin = BOOT_PIN; // Set your pin here. Using BOOT Button.
25+
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
2531

2632
// set your board Analog Pin here - used for changing the Fan speed
2733
const uint8_t analogPin = A0; // Analog Pin depends on each board
@@ -56,7 +62,7 @@ void fanDCMotorDrive(bool fanState, uint8_t speedPercent) {
5662
}
5763

5864
void setup() {
59-
// Initialize the USER BUTTON (Boot button) GPIO that will toggle the Fan (On/Off)
65+
// Initialize the USER BUTTON (Boot button) GPIO that will toggle the Fan (On/Off) and decommission the Matter Node
6066
pinMode(buttonPin, INPUT_PULLUP);
6167
// Initialize the Analog Pin A0 used to read input voltage and to set the Fan speed accordingly
6268
pinMode(analogPin, INPUT);
@@ -140,12 +146,6 @@ void setup() {
140146
}
141147
}
142148

143-
// Builtin Button control
144-
uint32_t button_time_stamp = 0; // debouncing control
145-
bool button_state = false; // false = released | true = pressed
146-
const uint32_t debouceTime = 250; // button debouncing time (ms)
147-
const uint32_t decommissioningTimeout = 10000; // keep the button pressed for 10s to decommission the Matter Fabric
148-
149149
void loop() {
150150
// Check Matter Accessory Commissioning state, which may change during execution of loop()
151151
if (!Matter.isDeviceCommissioned()) {
@@ -181,12 +181,13 @@ void loop() {
181181
// button is released - toggle Fan On/Off
182182
Fan.toggle();
183183
Serial.printf("User button released. Setting the Fan %s.\r\n", Fan > 0 ? "ON" : "OFF");
184+
}
184185

185-
// Factory reset is triggered if the button is pressed longer than 10 seconds
186-
if (time_diff > decommissioningTimeout) {
187-
Serial.println("Decommissioning the Generic Switch Matter Accessory. It shall be commissioned again.");
188-
Matter.decommission();
189-
}
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
190191
}
191192

192193
// checks Analog pin and adjust the speed only if it has changed

libraries/Matter/examples/MatterMinimum/MatterMinimum.ino

+29
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ 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 - decommissioning button
39+
const uint8_t buttonPin = BOOT_PIN; // Set your pin here. Using BOOT Button.
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
45+
3846
// Matter Protocol Endpoint (On/OFF Light) Callback
3947
bool matterCB(bool state) {
4048
digitalWrite(ledPin, state ? HIGH : LOW);
@@ -47,6 +55,8 @@ const char *ssid = "your-ssid"; // Change this to your WiFi SSID
4755
const char *password = "your-password"; // Change this to your WiFi password
4856

4957
void setup() {
58+
// Initialize the USER BUTTON (Boot button) that will be used to decommission the Matter Node
59+
pinMode(buttonPin, INPUT_PULLUP);
5060
// Initialize the LED GPIO
5161
pinMode(ledPin, OUTPUT);
5262

@@ -77,5 +87,24 @@ void setup() {
7787
}
7888

7989
void loop() {
90+
// Check if the button has been pressed
91+
if (digitalRead(buttonPin) == LOW && !button_state) {
92+
// deals with button debouncing
93+
button_time_stamp = millis(); // record the time while the button is pressed.
94+
button_state = true; // pressed.
95+
}
96+
97+
if (digitalRead(buttonPin) == HIGH && button_state) {
98+
button_state = false; // released
99+
}
100+
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+
80109
delay(500);
81110
}

0 commit comments

Comments
 (0)