Skip to content

Commit d791998

Browse files
committed
feat(matter): adds new endpoint color light
1 parent 6bc3ce6 commit d791998

File tree

9 files changed

+708
-67
lines changed

9 files changed

+708
-67
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ set(ARDUINO_LIBRARY_Matter_SRCS
170170
libraries/Matter/src/MatterEndpoints/MatterOnOffLight.cpp
171171
libraries/Matter/src/MatterEndpoints/MatterDimmableLight.cpp
172172
libraries/Matter/src/MatterEndpoints/MatterColorTemperatureLight.cpp
173+
libraries/Matter/src/MatterEndpoints/MatterColorLight.cpp
173174
libraries/Matter/src/MatterUtil/ColorFormat.cpp
174175
libraries/Matter/src/Matter.cpp)
175176

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
// Copyright 2024 Espressif Systems (Shanghai) PTE LTD
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// Matter Manager
16+
#include <Matter.h>
17+
#include <WiFi.h>
18+
#include <Preferences.h>
19+
20+
// List of Matter Endpoints for this Node
21+
// Color Light Endpoint
22+
MatterColorLight ColorLight;
23+
24+
// it will keep last OnOff & HSV Color state stored, using Preferences
25+
Preferences matterPref;
26+
const char *onOffPrefKey = "OnOff";
27+
const char *hsvColorPrefKey = "HSV";
28+
29+
// set your board RGB LED pin here
30+
#ifdef RGB_BUILTIN
31+
const uint8_t ledPin = RGB_BUILTIN;
32+
#else
33+
const uint8_t ledPin = 2; // Set your pin here if your board has not defined LED_BUILTIN
34+
#warning "Do not forget to set the RGB LED pin"
35+
#endif
36+
37+
// set your board USER BUTTON pin here
38+
const uint8_t buttonPin = 0; // Set your pin here. Using BOOT Button. C6/C3 use GPIO9.
39+
40+
// WiFi is manually set and started
41+
const char *ssid = "your-ssid"; // Change this to your WiFi SSID
42+
const char *password = "your-password"; // Change this to your WiFi password
43+
44+
// Set the RGB LED Light based on the current state of the Color Light
45+
bool setLightState(bool state, HsvColor_t colorHSV) {
46+
47+
if (state) {
48+
#ifdef RGB_BUILTIN
49+
RgbColor_t rgbColor = HsvToRgb(colorHSV);
50+
// set the RGB LED
51+
rgbLedWrite(ledPin, rgbColor.r, rgbColor.g, rgbColor.b);
52+
#else
53+
// No Color RGB LED, just use the HSV value (brightness) to control the LED
54+
analogWrite(ledPin, colorHSV.v);
55+
#endif
56+
} else {
57+
digitalWrite(ledPin, LOW);
58+
}
59+
// store last HSV Color and OnOff state for when the Light is restarted / power goes off
60+
matterPref.putBool(onOffPrefKey, state);
61+
matterPref.putUInt(hsvColorPrefKey, colorHSV.h << 16 | colorHSV.s << 8 | colorHSV.v);
62+
// This callback must return the success state to Matter core
63+
return true;
64+
}
65+
66+
void setup() {
67+
// Initialize the USER BUTTON (Boot button) GPIO that will act as a toggle switch
68+
pinMode(buttonPin, INPUT_PULLUP);
69+
// Initialize the LED (light) GPIO and Matter End Point
70+
pinMode(ledPin, OUTPUT);
71+
72+
Serial.begin(115200);
73+
while (!Serial) {
74+
delay(100);
75+
}
76+
77+
// We start by connecting to a WiFi network
78+
Serial.print("Connecting to ");
79+
Serial.println(ssid);
80+
// enable IPv6
81+
WiFi.enableIPv6(true);
82+
// Manually connect to WiFi
83+
WiFi.begin(ssid, password);
84+
// Wait for connection
85+
while (WiFi.status() != WL_CONNECTED) {
86+
delay(500);
87+
Serial.print(".");
88+
}
89+
Serial.println("\r\nWiFi connected");
90+
Serial.println("IP address: ");
91+
Serial.println(WiFi.localIP());
92+
delay(500);
93+
94+
// Initialize Matter EndPoint
95+
matterPref.begin("MatterPrefs", false);
96+
// default OnOff state is ON if not stored before
97+
bool lastOnOffState = matterPref.getBool(onOffPrefKey, true);
98+
// default HSV color is blue HSV(169, 254, 254)
99+
uint32_t prefHsvColor = matterPref.getUInt(hsvColorPrefKey, 169 << 16 | 254 << 8 | 254);
100+
HsvColor_t lastHsvColor = {uint8_t(prefHsvColor >> 16), uint8_t(prefHsvColor >> 8), uint8_t(prefHsvColor)};
101+
ColorLight.begin(lastOnOffState, lastHsvColor);
102+
// set the callback function to handle the Light state change
103+
ColorLight.onChange(setLightState);
104+
105+
// lambda functions are used to set the attribute change callbacks
106+
ColorLight.onChangeOnOff([](bool state) {
107+
Serial.printf("Light OnOff changed to %s\r\n", state ? "ON" : "OFF");
108+
return true;
109+
});
110+
ColorLight.onChangeColorHSV([](HsvColor_t hsvColor) {
111+
Serial.printf("Light HSV Color changed to (%d,%d,%d)\r\n", hsvColor.h, hsvColor.s, hsvColor.v);
112+
return true;
113+
});
114+
115+
// Matter beginning - Last step, after all EndPoints are initialized
116+
Matter.begin();
117+
// This may be a restart of a already commissioned Matter accessory
118+
if (Matter.isDeviceCommissioned()) {
119+
Serial.println("Matter Node is commissioned and connected to Wi-Fi. Ready for use.");
120+
Serial.printf(
121+
"Initial state: %s | RGB Color: (%d,%d,%d) \r\n", ColorLight ? "ON" : "OFF",
122+
ColorLight.getColorRGB().r, ColorLight.getColorRGB().g, ColorLight.getColorRGB().b
123+
);
124+
// configure the Light based on initial on-off state and its color
125+
ColorLight.updateAccessory();
126+
}
127+
}
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
133+
134+
void loop() {
135+
// Check Matter Light Commissioning state, which may change during execution of loop()
136+
if (!Matter.isDeviceCommissioned()) {
137+
Serial.println("");
138+
Serial.println("Matter Node is not commissioned yet.");
139+
Serial.println("Initiate the device discovery in your Matter environment.");
140+
Serial.println("Commission it to your Matter hub with the manual pairing code or QR code");
141+
Serial.printf("Manual pairing code: %s\r\n", Matter.getManualPairingCode().c_str());
142+
Serial.printf("QR code URL: %s\r\n", Matter.getOnboardingQRCodeUrl().c_str());
143+
// waits for Matter Light Commissioning.
144+
uint32_t timeCount = 0;
145+
while (!Matter.isDeviceCommissioned()) {
146+
delay(100);
147+
if ((timeCount++ % 50) == 0) { // 50*100ms = 5 sec
148+
Serial.println("Matter Node not commissioned yet. Waiting for commissioning.");
149+
}
150+
}
151+
Serial.printf(
152+
"Initial state: %s | RGB Color: (%d,%d,%d) \r\n", ColorLight ? "ON" : "OFF",
153+
ColorLight.getColorRGB().r, ColorLight.getColorRGB().g, ColorLight.getColorRGB().b
154+
);
155+
// configure the Light based on initial on-off state and its color
156+
ColorLight.updateAccessory();
157+
Serial.println("Matter Node is commissioned and connected to Wi-Fi. Ready for use.");
158+
}
159+
160+
// A button is also used to control the light
161+
// Check if the button has been pressed
162+
if (digitalRead(buttonPin) == LOW && !button_state) {
163+
// deals with button debouncing
164+
button_time_stamp = millis(); // record the time while the button is pressed.
165+
button_state = true; // pressed.
166+
}
167+
168+
// Onboard User Button is used as a Light toggle switch or to decommission it
169+
uint32_t time_diff = millis() - button_time_stamp;
170+
if (button_state && time_diff > debouceTime && digitalRead(buttonPin) == HIGH) {
171+
button_state = false; // released
172+
// Toggle button is released - toggle the light
173+
Serial.println("User button released. Toggling Light!");
174+
ColorLight.toggle(); // Matter Controller also can see the change
175+
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+
}
182+
}
183+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"fqbn_append": "PartitionScheme=huge_app",
3+
"requires": [
4+
"CONFIG_SOC_WIFI_SUPPORTED=y",
5+
"CONFIG_ESP_MATTER_ENABLE_DATA_MODEL=y"
6+
]
7+
}

libraries/Matter/keywords.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ ArduinoMatter KEYWORD1
1111
MatterOnOffLight KEYWORD1
1212
MatterDimmableLight KEYWORD1
1313
MatterColorTemperatureLight KEYWORD1
14+
MatterColorLight KEYWORD1
1415
MatterEndPoint KEYWORD1
1516
CtColor_t KEYWORD1
1617
XyColor_t KEYWORD1
@@ -37,12 +38,17 @@ setBrightness KEYWORD2
3738
getBrightness KEYWORD2
3839
setColorTemperature KEYWORD2
3940
getColorTemperature KEYWORD2
41+
setColorRGB KEYWORD2
42+
getColorRGB KEYWORD2
43+
setColorHSV KEYWORD2
44+
getColorHSV KEYWORD2
4045
toggle KEYWORD2
4146
updateAccessory KEYWORD2
4247
onChange KEYWORD2
4348
onChangeOnOff KEYWORD2
4449
onChangeBrightness KEYWORD2
4550
onChangeColorTemperature KEYWORD2
51+
onChangeColorHSV KEYWORD2
4652
XYToRgb KEYWORD2
4753
HsvToRgb KEYWORD2
4854
CTToRgb KEYWORD2
@@ -60,3 +66,12 @@ DAYLIGHT_WHITE_COLOR_TEMPERATURE LITERAL1
6066
WHITE_COLOR_TEMPERATURE LITERAL1
6167
SOFT_WHITE_COLOR_TEMPERATURE LITERAL1
6268
WARM_WHITE_COLOR_TEMPERATURE LITERAL1
69+
HSV_BLACK LITERAL1
70+
HSV_WHITE LITERAL1
71+
HSV_RED LITERAL1
72+
HSV_YELLOW LITERAL1
73+
HSV_GREEN LITERAL1
74+
HSV_CIAN LITERAL1
75+
HSV_BLUE LITERAL1
76+
HSV_MAGENTA LITERAL1
77+

libraries/Matter/src/Matter.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <MatterEndpoints/MatterOnOffLight.h>
2323
#include <MatterEndpoints/MatterDimmableLight.h>
2424
#include <MatterEndpoints/MatterColorTemperatureLight.h>
25+
#include <MatterEndpoints/MatterColorLight.h>
2526

2627
using namespace esp_matter;
2728

@@ -50,6 +51,7 @@ class ArduinoMatter {
5051
friend class MatterOnOffLight;
5152
friend class MatterDimmableLight;
5253
friend class MatterColorTemperatureLight;
54+
friend class MatterColorLight;
5355

5456
protected:
5557
static void _init();

0 commit comments

Comments
 (0)