@@ -41,8 +41,8 @@ const char *ssid = "your-ssid"; // Change this to your WiFi SSID
41
41
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
- 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);
46
46
if (state) {
47
47
#ifdef RGB_BUILTIN
48
48
rgbLedWrite (ledPin, brightness, brightness, brightness);
@@ -59,16 +59,6 @@ bool setRGBLight(bool state, uint8_t brightness) {
59
59
return true ;
60
60
}
61
61
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
-
72
62
void setup () {
73
63
// Initialize the USER BUTTON (Boot button) GPIO that will act as a toggle switch
74
64
pinMode (buttonPin, INPUT_PULLUP);
@@ -102,17 +92,23 @@ void setup() {
102
92
bool lastOnOffState = lastStatePref.getBool (" lastOnOffState" , true );
103
93
uint8_t lastBrightness = lastStatePref.getUChar (" lastBrightness" , 15 ); // default brightness = 12%
104
94
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
+ });
107
103
108
104
// Matter beginning - Last step, after all EndPoints are initialized
109
105
Matter.begin ();
110
106
// This may be a restart of a already commissioned Matter accessory
111
107
if (Matter.isDeviceCommissioned ()) {
112
108
Serial.println (" Matter Node is commissioned and connected to Wi-Fi. Ready for use." );
113
109
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 ());
116
112
}
117
113
}
118
114
// Button control
@@ -139,8 +135,8 @@ void loop() {
139
135
}
140
136
}
141
137
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 ());
144
140
Serial.println (" Matter Node is commissioned and connected to Wi-Fi. Ready for use." );
145
141
}
146
142
0 commit comments