@@ -41,8 +41,8 @@ const char *ssid = "your-ssid"; // Change this to your WiFi SSID
4141const 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
44- bool setRGBLight (bool state, uint8_t brightness) {
45- Serial.printf (" Setting Light to State: %s and Brightness: %d \r\n " , DimmableLight ? " ON" : " OFF" , brightness);
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);
4646 if (state) {
4747#ifdef RGB_BUILTIN
4848 rgbLedWrite (ledPin, brightness, brightness, brightness);
@@ -59,16 +59,6 @@ bool setRGBLight(bool state, uint8_t brightness) {
5959 return true ;
6060}
6161
62- // Matter Protocol Endpoint On-Off Change Callback
63- bool setLightOnOff (bool state) {
64- return setRGBLight (state, DimmableLight.getBrightness ());
65- }
66-
67- // Matter Protocol Endpoint Brightness Change Callback
68- bool setLightBrightness (uint8_t brightness) {
69- return setRGBLight (DimmableLight.getOnOff (), brightness);
70- }
71-
7262void setup () {
7363 // Initialize the USER BUTTON (Boot button) GPIO that will act as a toggle switch
7464 pinMode (buttonPin, INPUT_PULLUP);
@@ -102,17 +92,23 @@ void setup() {
10292 bool lastOnOffState = lastStatePref.getBool (" lastOnOffState" , true );
10393 uint8_t lastBrightness = lastStatePref.getUChar (" lastBrightness" , 15 ); // default brightness = 12%
10494 DimmableLight.begin (lastOnOffState, lastBrightness);
105- DimmableLight.onChangeOnOff (setLightOnOff);
106- DimmableLight.onChangeBrightness (setLightBrightness);
95+
96+ // lambda functions are used to set the attribute change callbacks
97+ DimmableLight.onChangeOnOff ([](bool state) {
98+ return setLightState (state, DimmableLight.getBrightness ());
99+ });
100+ DimmableLight.onChangeBrightness ([](uint8_t level) {
101+ return setLightState (DimmableLight.getOnOff (), level);
102+ });
107103
108104 // Matter beginning - Last step, after all EndPoints are initialized
109105 Matter.begin ();
110106 // This may be a restart of a already commissioned Matter accessory
111107 if (Matter.isDeviceCommissioned ()) {
112108 Serial.println (" Matter Node is commissioned and connected to Wi-Fi. Ready for use." );
113109 Serial.printf (" Initial state: %s | brightness: %d\r\n " , DimmableLight ? " ON" : " OFF" , DimmableLight.getBrightness ());
114- setLightOnOff (DimmableLight. getOnOff ()); // configure the Light based on initial state
115- setLightBrightness (DimmableLight.getBrightness ()); // configure the Light based on initial brightness
110+ // configure the Light based on initial on-off state and brightness
111+ setLightState (DimmableLight.getOnOff (), DimmableLight. getBrightness ());
116112 }
117113}
118114// Button control
@@ -139,8 +135,8 @@ void loop() {
139135 }
140136 }
141137 Serial.printf (" Initial state: %s | brightness: %d\r\n " , DimmableLight ? " ON" : " OFF" , DimmableLight.getBrightness ());
142- setLightOnOff (DimmableLight. getOnOff ()); // configure the Light based on initial state
143- setLightBrightness (DimmableLight.getBrightness ()); // configure the Light based on initial brightness
138+ // configure the Light based on initial on-off state and brightness
139+ setLightState (DimmableLight.getOnOff (), DimmableLight. getBrightness ());
144140 Serial.println (" Matter Node is commissioned and connected to Wi-Fi. Ready for use." );
145141 }
146142
0 commit comments