@@ -42,7 +42,6 @@ const char *password = "your-password"; // Change this to your WiFi password
42
42
43
43
// Set the RGB LED Light based on the current state of the Dimmable Light
44
44
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);
46
45
if (state) {
47
46
#ifdef RGB_BUILTIN
48
47
rgbLedWrite (ledPin, brightness, brightness, brightness);
@@ -92,13 +91,17 @@ void setup() {
92
91
bool lastOnOffState = lastStatePref.getBool (" lastOnOffState" , true );
93
92
uint8_t lastBrightness = lastStatePref.getUChar (" lastBrightness" , 15 ); // default brightness = 12%
94
93
DimmableLight.begin (lastOnOffState, lastBrightness);
94
+ // set the callback function to handle the Light state change
95
+ DimmableLight.onChange (setLightState);
95
96
96
97
// lambda functions are used to set the attribute change callbacks
97
98
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 ;
99
101
});
100
102
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 ;
102
105
});
103
106
104
107
// Matter beginning - Last step, after all EndPoints are initialized
@@ -108,7 +111,7 @@ void setup() {
108
111
Serial.println (" Matter Node is commissioned and connected to Wi-Fi. Ready for use." );
109
112
Serial.printf (" Initial state: %s | brightness: %d\r\n " , DimmableLight ? " ON" : " OFF" , DimmableLight.getBrightness ());
110
113
// configure the Light based on initial on-off state and brightness
111
- setLightState ( DimmableLight.getOnOff (), DimmableLight. getBrightness () );
114
+ DimmableLight.updateAccessory ( );
112
115
}
113
116
}
114
117
// Button control
@@ -136,7 +139,7 @@ void loop() {
136
139
}
137
140
Serial.printf (" Initial state: %s | brightness: %d\r\n " , DimmableLight ? " ON" : " OFF" , DimmableLight.getBrightness ());
138
141
// configure the Light based on initial on-off state and brightness
139
- setLightState ( DimmableLight.getOnOff (), DimmableLight. getBrightness () );
142
+ DimmableLight.updateAccessory ( );
140
143
Serial.println (" Matter Node is commissioned and connected to Wi-Fi. Ready for use." );
141
144
}
142
145
0 commit comments