Skip to content

Commit 0a99d69

Browse files
committed
feat(matter): example code adjustment to use improved api
1 parent b08e951 commit 0a99d69

File tree

3 files changed

+14
-11
lines changed

3 files changed

+14
-11
lines changed

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ void setup() {
7070
OnOffLight1.begin();
7171
OnOffLight2.begin();
7272
OnOffLight3.begin();
73-
OnOffLight1.onChangeOnOff(setLightOnOff1);
74-
OnOffLight2.onChangeOnOff(setLightOnOff2);
75-
OnOffLight3.onChangeOnOff(setLightOnOff3);
73+
OnOffLight1.onChange(setLightOnOff1);
74+
OnOffLight2.onChange(setLightOnOff2);
75+
OnOffLight3.onChange(setLightOnOff3);
7676

7777
// Matter beginning - Last step, after all EndPoints are initialized
7878
Matter.begin();

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

+8-5
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ const char *password = "your-password"; // Change this to your WiFi password
4242

4343
// Set the RGB LED Light based on the current state of the Dimmable Light
4444
bool setLightState(bool state, uint8_t brightness) {
45-
Serial.printf("Changing Light: old[%s,%d]->new[%s,%d]\r\n", DimmableLight ? "ON" : "OFF", DimmableLight.getBrightness(), state ? "ON" : "OFF", brightness);
4645
if (state) {
4746
#ifdef RGB_BUILTIN
4847
rgbLedWrite(ledPin, brightness, brightness, brightness);
@@ -92,13 +91,17 @@ void setup() {
9291
bool lastOnOffState = lastStatePref.getBool("lastOnOffState", true);
9392
uint8_t lastBrightness = lastStatePref.getUChar("lastBrightness", 15); // default brightness = 12%
9493
DimmableLight.begin(lastOnOffState, lastBrightness);
94+
// set the callback function to handle the Light state change
95+
DimmableLight.onChange(setLightState);
9596

9697
// lambda functions are used to set the attribute change callbacks
9798
DimmableLight.onChangeOnOff([](bool state) {
98-
return setLightState(state, DimmableLight.getBrightness());
99+
Serial.printf("Light OnOff changed to %s\r\n", state ? "ON" : "OFF");
100+
return true;
99101
});
100102
DimmableLight.onChangeBrightness([](uint8_t level) {
101-
return setLightState(DimmableLight.getOnOff(), level);
103+
Serial.printf("Light Brightness changed to %d\r\n", level);
104+
return true;
102105
});
103106

104107
// Matter beginning - Last step, after all EndPoints are initialized
@@ -108,7 +111,7 @@ void setup() {
108111
Serial.println("Matter Node is commissioned and connected to Wi-Fi. Ready for use.");
109112
Serial.printf("Initial state: %s | brightness: %d\r\n", DimmableLight ? "ON" : "OFF", DimmableLight.getBrightness());
110113
// configure the Light based on initial on-off state and brightness
111-
setLightState(DimmableLight.getOnOff(), DimmableLight.getBrightness());
114+
DimmableLight.updateAccessory();
112115
}
113116
}
114117
// Button control
@@ -136,7 +139,7 @@ void loop() {
136139
}
137140
Serial.printf("Initial state: %s | brightness: %d\r\n", DimmableLight ? "ON" : "OFF", DimmableLight.getBrightness());
138141
// configure the Light based on initial on-off state and brightness
139-
setLightState(DimmableLight.getOnOff(), DimmableLight.getBrightness());
142+
DimmableLight.updateAccessory();
140143
Serial.println("Matter Node is commissioned and connected to Wi-Fi. Ready for use.");
141144
}
142145

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,15 @@ void setup() {
8686
lastStatePref.begin("matterLight", false);
8787
bool lastOnOffState = lastStatePref.getBool("lastOnOffState", true);
8888
OnOffLight.begin(lastOnOffState);
89-
OnOffLight.onChangeOnOff(setLightOnOff);
89+
OnOffLight.onChange(setLightOnOff);
9090

9191
// Matter beginning - Last step, after all EndPoints are initialized
9292
Matter.begin();
9393
// This may be a restart of a already commissioned Matter accessory
9494
if (Matter.isDeviceCommissioned()) {
9595
Serial.println("Matter Node is commissioned and connected to Wi-Fi. Ready for use.");
9696
Serial.printf("Initial state: %s\r\n", OnOffLight.getOnOff() ? "ON" : "OFF");
97-
setLightOnOff(OnOffLight.getOnOff()); // configure the Light based on initial state
97+
OnOffLight.updateAccessory(); // configure the Light based on initial state
9898
}
9999
}
100100
// Button control
@@ -121,7 +121,7 @@ void loop() {
121121
}
122122
}
123123
Serial.printf("Initial state: %s\r\n", OnOffLight.getOnOff() ? "ON" : "OFF");
124-
setLightOnOff(OnOffLight.getOnOff()); // configure the Light based on initial state
124+
OnOffLight.updateAccessory(); // configure the Light based on initial state
125125
Serial.println("Matter Node is commissioned and connected to Wi-Fi. Ready for use.");
126126
}
127127

0 commit comments

Comments
 (0)