Skip to content

Commit fcc554f

Browse files
committed
add Arduino/IDF Matter example
1 parent 9edf55d commit fcc554f

File tree

15 files changed

+1060
-0
lines changed

15 files changed

+1060
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
cmake_minimum_required(VERSION 3.16.0)
2+
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
3+
set(PROJECT_VER "1.0")
4+
set(PROJECT_VER_NUMBER 1)
5+
6+
project(matter-light)
7+
8+
9+
# WARNING: This is just an example for using key for decrypting the encrypted OTA image
10+
# Please do not use it as is.
11+
if(CONFIG_ENABLE_ENCRYPTED_OTA)
12+
target_add_binary_data(light.elf "esp_image_encryption_key.pem" TEXT)
13+
endif()
14+
15+
if(CONFIG_IDF_TARGET_ESP32C2)
16+
include(relinker)
17+
endif()
18+
19+
idf_build_set_property(CXX_COMPILE_OPTIONS "-std=gnu++17;-Os;-DCHIP_HAVE_CONFIG_H" APPEND)
20+
idf_build_set_property(C_COMPILE_OPTIONS "-Os" APPEND)
21+
# For RISCV chips, project_include.cmake sets -Wno-format, but does not clear various
22+
# flags that depend on -Wformat
23+
idf_build_set_property(COMPILE_OPTIONS "-Wno-format-nonliteral;-Wno-format-security" APPEND)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## Arduino/IDF Matter Light example
2+
3+
The needed Matter component is added via the IDF component manager -> `idf_component.yml`.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
This directory is intended for project header files.
3+
4+
A header file is a file containing C declarations and macro definitions
5+
to be shared between several project source files. You request the use of a
6+
header file in your project source file (C, C++, etc) located in `src` folder
7+
by including it, with the C preprocessing directive `#include'.
8+
9+
```src/main.c
10+
11+
#include "header.h"
12+
13+
int main (void)
14+
{
15+
...
16+
}
17+
```
18+
19+
Including a header file produces the same results as copying the header file
20+
into each source file that needs it. Such copying would be time-consuming
21+
and error-prone. With a header file, the related declarations appear
22+
in only one place. If they need to be changed, they can be changed in one
23+
place, and programs that include the header file will automatically use the
24+
new version when next recompiled. The header file eliminates the labor of
25+
finding and changing all the copies as well as the risk that a failure to
26+
find one copy will result in inconsistencies within a program.
27+
28+
In C, the usual convention is to give header files names that end with `.h'.
29+
It is most portable to use only letters, digits, dashes, and underscores in
30+
header file names, and at most one dot.
31+
32+
Read more about using header files in official GCC documentation:
33+
34+
* Include Syntax
35+
* Include Operation
36+
* Once-Only Headers
37+
* Computed Includes
38+
39+
https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
This directory is intended for project specific (private) libraries.
3+
PlatformIO will compile them to static libraries and link into executable file.
4+
5+
The source code of each library should be placed in an own separate directory
6+
("lib/your_library_name/[here are source files]").
7+
8+
For example, see a structure of the following two libraries `Foo` and `Bar`:
9+
10+
|--lib
11+
| |
12+
| |--Bar
13+
| | |--docs
14+
| | |--examples
15+
| | |--src
16+
| | |- Bar.c
17+
| | |- Bar.h
18+
| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html
19+
| |
20+
| |--Foo
21+
| | |- Foo.c
22+
| | |- Foo.h
23+
| |
24+
| |- README --> THIS FILE
25+
|
26+
|- platformio.ini
27+
|--src
28+
|- main.c
29+
30+
and a contents of `src/main.c`:
31+
```
32+
#include <Foo.h>
33+
#include <Bar.h>
34+
35+
int main (void)
36+
{
37+
...
38+
}
39+
40+
```
41+
42+
PlatformIO Library Dependency Finder will find automatically dependent
43+
libraries scanning project source files.
44+
45+
More information about PlatformIO Library Dependency Finder
46+
- https://docs.platformio.org/page/librarymanager/ldf.html
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# This file was automatically generated for projects
2+
# without default 'CMakeLists.txt' file.
3+
4+
FILE(GLOB_RECURSE app_sources ${CMAKE_SOURCE_DIR}/main/*.*)
5+
6+
idf_component_register(SRCS ${app_sources})
7+
8+
#set_property(TARGET ${COMPONENT_LIB} PROPERTY CXX_STANDARD 17)
9+
#target_compile_options(${COMPONENT_LIB} PRIVATE "-DCHIP_HAVE_CONFIG_H")
Lines changed: 251 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,251 @@
1+
/*
2+
This example code is in the Public Domain (or CC0 licensed, at your option.)
3+
4+
Unless required by applicable law or agreed to in writing, this
5+
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
6+
CONDITIONS OF ANY KIND, either express or implied.
7+
8+
This will implement the onboard WS2812b LED as a LED indicator
9+
It can be used to indicate some state or status of the device
10+
The LED can be controlled using RGB, HSV or color temperature, brightness
11+
12+
In this example, the LED Indicator class is used as the Matter light accessory
13+
*/
14+
15+
#include <Arduino.h>
16+
#include "builtinLED.h"
17+
18+
typedef struct {
19+
uint16_t hue;
20+
uint8_t saturation;
21+
} HS_color_t;
22+
23+
static const HS_color_t temperatureTable[] = {
24+
{4, 100}, {8, 100}, {11, 100}, {14, 100}, {16, 100}, {18, 100}, {20, 100}, {22, 100}, {24, 100}, {25, 100},
25+
{27, 100}, {28, 100}, {30, 100}, {31, 100}, {31, 95}, {30, 89}, {30, 85}, {29, 80}, {29, 76}, {29, 73},
26+
{29, 69}, {28, 66}, {28, 63}, {28, 60}, {28, 57}, {28, 54}, {28, 52}, {27, 49}, {27, 47}, {27, 45},
27+
{27, 43}, {27, 41}, {27, 39}, {27, 37}, {27, 35}, {27, 33}, {27, 31}, {27, 30}, {27, 28}, {27, 26},
28+
{27, 25}, {27, 23}, {27, 22}, {27, 21}, {27, 19}, {27, 18}, {27, 17}, {27, 15}, {28, 14}, {28, 13},
29+
{28, 12}, {29, 10}, {29, 9}, {30, 8}, {31, 7}, {32, 6}, {34, 5}, {36, 4}, {41, 3}, {49, 2},
30+
{0, 0}, {294, 2}, {265, 3}, {251, 4}, {242, 5}, {237, 6}, {233, 7}, {231, 8}, {229, 9}, {228, 10},
31+
{227, 11}, {226, 11}, {226, 12}, {225, 13}, {225, 13}, {224, 14}, {224, 14}, {224, 15}, {224, 15}, {223, 16},
32+
{223, 16}, {223, 17}, {223, 17}, {223, 17}, {222, 18}, {222, 18}, {222, 19}, {222, 19}, {222, 19}, {222, 19},
33+
{222, 20}, {222, 20}, {222, 20}, {222, 21}, {222, 21}
34+
};
35+
36+
/* step brightness table: gamma = 2.3 */
37+
static const uint8_t gamma_table[MAX_PROGRESS] = {
38+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
39+
0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2,
40+
2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5,
41+
5, 5, 6, 6, 6, 6, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10,
42+
10, 10, 11, 11, 12, 12, 13, 13, 13, 14, 14, 15, 15, 16, 16, 17,
43+
17, 18, 18, 19, 19, 20, 20, 21, 22, 22, 23, 23, 24, 25, 25, 26,
44+
26, 27, 28, 28, 29, 30, 30, 31, 32, 33, 33, 34, 35, 36, 36, 37,
45+
38, 39, 40, 40, 41, 42, 43, 44, 45, 45, 46, 47, 48, 49, 50, 51,
46+
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67,
47+
68, 69, 70, 71, 72, 74, 75, 76, 77, 78, 79, 81, 82, 83, 84, 86,
48+
87, 88, 89, 91, 92, 93, 95, 96, 97, 99, 100, 101, 103, 104, 105, 107,
49+
108, 110, 111, 112, 114, 115, 117, 118, 120, 121, 123, 124, 126, 128, 129, 131,
50+
132, 134, 135, 137, 139, 140, 142, 144, 145, 147, 149, 150, 152, 154, 156, 157,
51+
159, 161, 163, 164, 166, 168, 170, 172, 174, 175, 177, 179, 181, 183, 185, 187,
52+
189, 191, 193, 195, 197, 199, 201, 203, 205, 207, 209, 211, 213, 215, 217, 219,
53+
221, 223, 226, 228, 230, 232, 234, 236, 239, 241, 243, 245, 248, 250, 252, 255,
54+
};
55+
56+
BuiltInLED::BuiltInLED() {
57+
pin_number = (uint8_t) -1; // no pin number
58+
state = false; // LED is off
59+
hsv_color.value = 0; // black color
60+
}
61+
62+
BuiltInLED::~BuiltInLED(){
63+
end();
64+
}
65+
66+
led_indicator_color_hsv_t BuiltInLED::rgb2hsv(led_indicator_color_rgb_t rgb) {
67+
led_indicator_color_hsv_t hsv;
68+
uint8_t minRGB, maxRGB;
69+
uint8_t delta;
70+
71+
minRGB = rgb.r < rgb.g ? (rgb.r < rgb.b ? rgb.r : rgb.b) : (rgb.g < rgb.b ? rgb.g : rgb.b);
72+
maxRGB = rgb.r > rgb.g ? (rgb.r > rgb.b ? rgb.r : rgb.b) : (rgb.g > rgb.b ? rgb.g : rgb.b);
73+
hsv.value = 0;
74+
hsv.v = maxRGB;
75+
delta = maxRGB - minRGB;
76+
77+
if (delta == 0) {
78+
hsv.h = 0;
79+
hsv.s = 0;
80+
} else {
81+
hsv.s = delta * 255 / maxRGB;
82+
83+
if (rgb.r == maxRGB) {
84+
hsv.h = (60 * (rgb.g - rgb.b) / delta + 360) % 360;
85+
} else if (rgb.g == maxRGB) {
86+
hsv.h = (60 * (rgb.b - rgb.r) / delta + 120);
87+
} else {
88+
hsv.h = (60 * (rgb.r - rgb.g) / delta + 240);
89+
}
90+
}
91+
return hsv;
92+
}
93+
94+
led_indicator_color_rgb_t BuiltInLED::hsv2rgb(led_indicator_color_hsv_t hsv) {
95+
led_indicator_color_rgb_t rgb;
96+
uint8_t rgb_max = hsv.v;
97+
uint8_t rgb_min = rgb_max * (255 - hsv.s) / 255.0f;
98+
99+
uint8_t i = hsv.h / 60;
100+
uint8_t diff = hsv.h % 60;
101+
102+
// RGB adjustment amount by hue
103+
uint8_t rgb_adj = (rgb_max - rgb_min) * diff / 60;
104+
rgb.value = 0;
105+
switch (i) {
106+
case 0:
107+
rgb.r = rgb_max;
108+
rgb.g = rgb_min + rgb_adj;
109+
rgb.b = rgb_min;
110+
break;
111+
case 1:
112+
rgb.r = rgb_max - rgb_adj;
113+
rgb.g = rgb_max;
114+
rgb.b = rgb_min;
115+
break;
116+
case 2:
117+
rgb.r = rgb_min;
118+
rgb.g = rgb_max;
119+
rgb.b = rgb_min + rgb_adj;
120+
break;
121+
case 3:
122+
rgb.r = rgb_min;
123+
rgb.g = rgb_max - rgb_adj;
124+
rgb.b = rgb_max;
125+
break;
126+
case 4:
127+
rgb.r = rgb_min + rgb_adj;
128+
rgb.g = rgb_min;
129+
rgb.b = rgb_max;
130+
break;
131+
default:
132+
rgb.r = rgb_max;
133+
rgb.g = rgb_min;
134+
rgb.b = rgb_max - rgb_adj;
135+
break;
136+
}
137+
138+
// gamma correction
139+
rgb.r = gamma_table[rgb.r];
140+
rgb.g = gamma_table[rgb.g];
141+
rgb.b = gamma_table[rgb.b];
142+
return rgb;
143+
}
144+
145+
void BuiltInLED::begin(uint8_t pin){
146+
if (pin < NUM_DIGITAL_PINS) {
147+
pin_number = pin;
148+
write();
149+
} else {
150+
log_e("Invalid pin (%d) number", pin);
151+
}
152+
}
153+
void BuiltInLED::end(){
154+
state = false;
155+
write();
156+
if (pin_number < NUM_DIGITAL_PINS) {
157+
if (!rmtDeinit(pin_number)) {
158+
log_e("Failed to deinitialize RMT");
159+
}
160+
}
161+
}
162+
163+
void BuiltInLED::on(){
164+
state = true;
165+
}
166+
167+
void BuiltInLED::off(){
168+
state = false;
169+
}
170+
171+
void BuiltInLED::toggle(){
172+
state = !state;
173+
}
174+
175+
bool BuiltInLED::getState(){
176+
return state;
177+
}
178+
179+
bool BuiltInLED::write(){
180+
led_indicator_color_rgb_t rgb_color = getRGB();
181+
log_d("Writing to pin %d with state = %s", pin_number, state ? "ON" : "OFF");
182+
log_d("HSV: %d, %d, %d", hsv_color.h, hsv_color.s, hsv_color.v);
183+
log_d("RGB: %d, %d, %d", rgb_color.r, rgb_color.g, rgb_color.b);
184+
if(pin_number < NUM_DIGITAL_PINS){
185+
if (state) {
186+
rgbLedWrite(pin_number, rgb_color.r, rgb_color.g, rgb_color.b);
187+
} else {
188+
rgbLedWrite(pin_number, 0, 0, 0);
189+
}
190+
return true;
191+
} else {
192+
log_e("Invalid pin (%d) number", pin_number);
193+
return false;
194+
}
195+
}
196+
197+
void BuiltInLED::setBrightness(uint8_t brightness){
198+
hsv_color.v = brightness;
199+
}
200+
201+
uint8_t BuiltInLED::getBrightness(){
202+
return hsv_color.v;
203+
}
204+
205+
void BuiltInLED::setHSV(led_indicator_color_hsv_t hsv){
206+
if (hsv.h > MAX_HUE) {
207+
hsv.h = MAX_HUE;
208+
}
209+
hsv_color.value = hsv.value;
210+
}
211+
212+
led_indicator_color_hsv_t BuiltInLED::getHSV(){
213+
return hsv_color;
214+
}
215+
216+
void BuiltInLED::setRGB(led_indicator_color_rgb_t rgb_color){
217+
hsv_color = rgb2hsv(rgb_color);
218+
}
219+
220+
led_indicator_color_rgb_t BuiltInLED::getRGB(){
221+
return hsv2rgb(hsv_color);
222+
}
223+
224+
void BuiltInLED::setTemperature(uint32_t temperature){
225+
uint16_t hue;
226+
uint8_t saturation;
227+
228+
log_d("Requested Temperature: %ld", temperature);
229+
//hsv_color.v = gamma_table[((temperature >> 25) & 0x7F)];
230+
temperature &= 0xFFFFFF;
231+
if (temperature < 600) {
232+
hue = 0;
233+
saturation = 100;
234+
} else {
235+
if (temperature > 10000) {
236+
hue = 222;
237+
saturation = 21 + (temperature - 10000) * 41 / 990000;
238+
} else {
239+
temperature -= 600;
240+
temperature /= 100;
241+
hue = temperatureTable[temperature].hue;
242+
saturation = temperatureTable[temperature].saturation;
243+
}
244+
}
245+
saturation = (saturation * 255) / 100;
246+
// brightness is not changed
247+
hsv_color.h = hue;
248+
hsv_color.s = saturation;
249+
log_d("Calculated Temperature: %ld, Hue: %d, Saturation: %d, Brightness: %d", temperature, hue, saturation, hsv_color.v);
250+
}
251+

0 commit comments

Comments
 (0)