Skip to content

Commit 916e949

Browse files
committed
feat(matter): global color space functions
1 parent 52a5e89 commit 916e949

File tree

13 files changed

+195
-129
lines changed

13 files changed

+195
-129
lines changed

CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ endif()
2525
set(CORE_SRCS
2626
cores/esp32/base64.cpp
2727
cores/esp32/cbuf.cpp
28+
cores/esp32/ColorFormat.c
2829
cores/esp32/chip-debug-report.cpp
2930
cores/esp32/esp32-hal-adc.c
3031
cores/esp32/esp32-hal-bt.c
@@ -171,7 +172,6 @@ set(ARDUINO_LIBRARY_Matter_SRCS
171172
libraries/Matter/src/MatterEndpoints/MatterDimmableLight.cpp
172173
libraries/Matter/src/MatterEndpoints/MatterColorTemperatureLight.cpp
173174
libraries/Matter/src/MatterEndpoints/MatterColorLight.cpp
174-
libraries/Matter/src/MatterUtil/ColorFormat.cpp
175175
libraries/Matter/src/Matter.cpp)
176176

177177
set(ARDUINO_LIBRARY_PPP_SRCS

libraries/Matter/src/MatterUtil/ColorFormat.cpp renamed to cores/esp32/ColorFormat.c

+58-10
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,38 @@
2323
// define a clamp macro to substitute the std::clamp macro which is available from C++17 onwards
2424
#define clamp(a, min, max) ((a) < (min) ? (min) : ((a) > (max) ? (max) : (a)))
2525

26-
RgbColor_t HsvToRgb(HsvColor_t hsv) {
27-
RgbColor_t rgb;
26+
const espHsvColor_t HSV_BLACK = {0, 0, 0};
27+
const espHsvColor_t HSV_WHITE = {0, 0, 254};
28+
const espHsvColor_t HSV_RED = {0, 254, 254};
29+
const espHsvColor_t HSV_YELLOW = {42, 254, 254};
30+
const espHsvColor_t HSV_GREEN = {84, 254, 254};
31+
const espHsvColor_t HSV_CYAN = {127, 254, 254};
32+
const espHsvColor_t HSV_BLUE = {169, 254, 254};
33+
const espHsvColor_t HSV_MAGENTA = {211, 254, 254};
34+
35+
const espRgbColor_t RGB_BLACK = {0, 0, 0};
36+
const espRgbColor_t RGB_WHITE = {255, 255, 255};
37+
const espRgbColor_t RGB_RED = {255, 0, 0};
38+
const espRgbColor_t RGB_YELLOW = {255, 255, 0};
39+
const espRgbColor_t RGB_GREEN = {0, 255, 0};
40+
const espRgbColor_t RGB_CYAN = {0, 255, 255};
41+
const espRgbColor_t RGB_BLUE = {0, 0, 255};
42+
const espRgbColor_t RGB_MAGENTA = {255, 0, 255};
43+
44+
// main color temperature values
45+
const espCtColor_t COOL_WHITE_COLOR_TEMPERATURE = { 142 };
46+
const espCtColor_t DAYLIGHT_WHITE_COLOR_TEMPERATURE = { 181 };
47+
const espCtColor_t WHITE_COLOR_TEMPERATURE = { 250 };
48+
const espCtColor_t SOFT_WHITE_COLOR_TEMPERATURE = { 370 };
49+
const espCtColor_t WARM_WHITE_COLOR_TEMPERATURE = { 454 };
50+
51+
espRgbColor_t espHsvToRgbColor(uint16_t h, uint8_t s, uint8_t v) {
52+
espHsvColor_t hsv = {h, s, v};
53+
return espHsvColorToRgbColor(hsv);
54+
}
55+
56+
espRgbColor_t espHsvColorToRgbColor(espHsvColor_t hsv) {
57+
espRgbColor_t rgb;
2858

2959
uint8_t region, p, q, t;
3060
uint32_t h, s, v, remainder;
@@ -66,8 +96,13 @@ RgbColor_t HsvToRgb(HsvColor_t hsv) {
6696
return rgb;
6797
}
6898

69-
HsvColor_t RgbToHsv(RgbColor_t rgb) {
70-
HsvColor_t hsv;
99+
espHsvColor_t espRgbToHsvColor(uint8_t r, uint8_t g, uint8_t b) {
100+
espRgbColor_t rgb = {r, g, b};
101+
return espRgbColorToHsvColor(rgb);
102+
}
103+
104+
espHsvColor_t espRgbColorToHsvColor(espRgbColor_t rgb) {
105+
espHsvColor_t hsv;
71106
uint8_t rgbMin, rgbMax;
72107

73108
rgbMin = rgb.r < rgb.g ? (rgb.r < rgb.b ? rgb.r : rgb.b) : (rgb.g < rgb.b ? rgb.g : rgb.b);
@@ -95,7 +130,11 @@ HsvColor_t RgbToHsv(RgbColor_t rgb) {
95130
return hsv;
96131
}
97132

98-
RgbColor_t XYToRgb(uint8_t Level, uint16_t current_X, uint16_t current_Y) {
133+
espRgbColor_t espXYColorToRgbColor(uint8_t Level, espXyColor_t xy) {
134+
return espXYToRgbColor(Level, xy.x, xy.y);
135+
}
136+
137+
espRgbColor_t espXYToRgbColor(uint8_t Level, uint16_t current_X, uint16_t current_Y) {
99138
// convert xyY color space to RGB
100139

101140
// https://www.easyrgb.com/en/math.php
@@ -108,7 +147,7 @@ RgbColor_t XYToRgb(uint8_t Level, uint16_t current_X, uint16_t current_Y) {
108147
// y = current_Y/65536
109148
// z = 1-x-y
110149

111-
RgbColor_t rgb;
150+
espRgbColor_t rgb;
112151

113152
float x, y, z;
114153
float X, Y, Z;
@@ -155,14 +194,19 @@ RgbColor_t XYToRgb(uint8_t Level, uint16_t current_X, uint16_t current_Y) {
155194
return rgb;
156195
}
157196

158-
XyColor_t RgbToXY(RgbColor_t rgb) {
197+
espXyColor_t espRgbToXYColor(uint8_t r, uint8_t g, uint8_t b){
198+
espRgbColor_t rgb = {r, g, b};
199+
return espRgbColorToXYColor(rgb);
200+
}
201+
202+
espXyColor_t espRgbColorToXYColor(espRgbColor_t rgb) {
159203
// convert RGB to xy color space
160204

161205
// https://www.easyrgb.com/en/math.php
162206
// https://en.wikipedia.org/wiki/SRGB
163207
// refer https://en.wikipedia.org/wiki/CIE_1931_color_space#CIE_xy_chromaticity_diagram_and_the_CIE_xyY_color_space
164208

165-
XyColor_t xy;
209+
espXyColor_t xy;
166210

167211
float r, g, b;
168212
float X, Y, Z;
@@ -198,9 +242,13 @@ XyColor_t RgbToXY(RgbColor_t rgb) {
198242
return xy;
199243
}
200244

245+
espRgbColor_t espCTToRgbColor(uint16_t ct){
246+
espCtColor_t ctColor = {ct};
247+
return espCTColorToRgbColor(ctColor);
248+
}
201249

202-
RgbColor_t CTToRgb(CtColor_t ct) {
203-
RgbColor_t rgb = {0, 0, 0};
250+
espRgbColor_t espCTColorToRgbColor(espCtColor_t ct) {
251+
espRgbColor_t rgb = {0, 0, 0};
204252
float r, g, b;
205253

206254
if (ct.ctMireds == 0) {

cores/esp32/ColorFormat.h

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
*
3+
* Copyright (c) 2021 Project CHIP Authors
4+
* All rights reserved.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
#pragma once
20+
21+
#include <stdint.h>
22+
#ifdef __cplusplus
23+
extern "C" {
24+
#endif
25+
26+
struct RgbColor_t {
27+
uint8_t r;
28+
uint8_t g;
29+
uint8_t b;
30+
};
31+
32+
struct HsvColor_t {
33+
uint16_t h;
34+
uint8_t s;
35+
uint8_t v;
36+
};
37+
38+
struct XyColor_t {
39+
uint16_t x;
40+
uint16_t y;
41+
};
42+
43+
struct CtColor_t {
44+
uint16_t ctMireds;
45+
};
46+
47+
typedef struct RgbColor_t espRgbColor_t;
48+
typedef struct HsvColor_t espHsvColor_t;
49+
typedef struct XyColor_t espXyColor_t;
50+
typedef struct CtColor_t espCtColor_t;
51+
52+
espRgbColor_t espXYToRgbColor(uint8_t Level, uint16_t current_X, uint16_t current_Y);
53+
espRgbColor_t espXYColorToRgb(uint8_t Level, espXyColor_t xy);
54+
espXyColor_t espRgbColorToXYColor(espRgbColor_t rgb);
55+
espXyColor_t espRgbToXYColor(uint8_t r, uint8_t g, uint8_t b);
56+
espRgbColor_t espHsvColorToRgbColor(espHsvColor_t hsv);
57+
espRgbColor_t espHsvToRgbColor(uint16_t h, uint8_t s, uint8_t v);
58+
espRgbColor_t espCTColorToRgbColor(espCtColor_t ct);
59+
espRgbColor_t espCTToRgbColor(uint16_t ct);
60+
espHsvColor_t espRgbColorToHsvColor(espRgbColor_t rgb);
61+
espHsvColor_t espRgbToHsvColor(uint8_t r, uint8_t g, uint8_t b);
62+
63+
extern const espHsvColor_t HSV_BLACK, HSV_WHITE, HSV_RED, HSV_YELLOW, HSV_GREEN, HSV_CYAN, HSV_BLUE, HSV_MAGENTA;
64+
extern const espCtColor_t COOL_WHITE_COLOR_TEMPERATURE, DAYLIGHT_WHITE_COLOR_TEMPERATURE, WHITE_COLOR_TEMPERATURE, SOFT_WHITE_COLOR_TEMPERATURE, WARM_WHITE_COLOR_TEMPERATURE;
65+
extern const espRgbColor_t RGB_BLACK, RGB_WHITE, RGB_RED, RGB_YELLOW, RGB_GREEN, RGB_CYAN, RGB_BLUE, RGB_MAGENTA;
66+
67+
#ifdef __cplusplus
68+
}
69+
#endif

libraries/ESP32/keywords.txt

+38-1
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,51 @@
66
# Datatypes (KEYWORD1)
77
#######################################
88

9-
Serial4 KEYWORD1
9+
Serial4 KEYWORD1
10+
espCtColor_t KEYWORD1
11+
espXyColor_t KEYWORD1
12+
espHsvColor_t KEYWORD1
13+
espRgbColor_t KEYWORD1
1014

1115
#######################################
1216
# Methods and Functions (KEYWORD2)
1317
#######################################
1418

19+
espXYToRgbColor KEYWORD2
20+
espXYColorToRgb KEYWORD2
21+
espRgbColorToXYColor KEYWORD2
22+
espRgbToXYColor KEYWORD2
23+
espHsvColorToRgbColor KEYWORD2
24+
espHsvToRgbColor KEYWORD2
25+
espCTColorToRgbColor KEYWORD2
26+
espCTToRgbColor KEYWORD2
27+
espRgbColorToHsvColor KEYWORD2
28+
espRgbToHsvColor KEYWORD2
29+
1530
#######################################
1631
# Constants (LITERAL1)
1732
#######################################
1833

1934
RGB_BUILTIN LITERAL1
35+
HSV_BLACK LITERAL1
36+
HSV_WHITE LITERAL1
37+
HSV_RED LITERAL1
38+
HSV_YELLOW LITERAL1
39+
HSV_GREEN LITERAL1
40+
HSV_CYAN LITERAL1
41+
HSV_BLUE LITERAL1
42+
HSV_MAGENTA LITERAL1
43+
COOL_WHITE_COLOR_TEMPERATURE LITERAL1
44+
DAYLIGHT_WHITE_COLOR_TEMPERATURE LITERAL1
45+
WHITE_COLOR_TEMPERATURE LITERAL1
46+
SOFT_WHITE_COLOR_TEMPERATURE LITERAL1
47+
WARM_WHITE_COLOR_TEMPERATURE LITERAL1
48+
RGB_BLACK LITERAL1
49+
RGB_WHITE LITERAL1
50+
RGB_RED LITERAL1
51+
RGB_YELLOW LITERAL1
52+
RGB_GREEN LITERAL1
53+
RGB_CYAN LITERAL1
54+
RGB_BLUE LITERAL1
55+
RGB_MAGENTA LITERAL1
56+

libraries/Matter/examples/Matter_CW_WW_Light/Matter_CW_WW_Light.ino

+2-3
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ bool setLightState(bool state, uint8_t brightness, uint16_t temperature_Mireds)
4747

4848
if (state) {
4949
#ifdef RGB_BUILTIN
50-
CtColor_t ct = {temperature_Mireds};
51-
RgbColor_t rgb_ct = CTToRgb(ct);
50+
espRgbColor_t rgb_ct = espCTToRgbColor(temperature_Mireds);
5251
// simple intensity correction
5352
float brightnessPercent = (float)brightness / MatterColorTemperatureLight::MAX_BRIGHTNESS;
5453
rgb_ct.r = brightnessPercent * rgb_ct.r;
@@ -106,7 +105,7 @@ void setup() {
106105
// default brightness ~= 6% (15/255)
107106
uint8_t lastBrightness = matterPref.getUChar(brightnessPrefKey, 15);
108107
// default temperature ~= 454 Mireds (Warm White)
109-
uint16_t lastTemperature = matterPref.getUShort(temperaturePrefKey, MatterColorTemperatureLight::WARM_WHITE_COLOR_TEMPERATURE);
108+
uint16_t lastTemperature = matterPref.getUShort(temperaturePrefKey, WARM_WHITE_COLOR_TEMPERATURE.ctMireds);
110109
CW_WW_Light.begin(lastOnOffState, lastBrightness, lastTemperature);
111110
// set the callback function to handle the Light state change
112111
CW_WW_Light.onChange(setLightState);

libraries/Matter/examples/Matter_ColorLight/Matter_ColorLight.ino

+3-3
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ const char *ssid = "your-ssid"; // Change this to your WiFi SSID
4242
const char *password = "your-password"; // Change this to your WiFi password
4343

4444
// Set the RGB LED Light based on the current state of the Color Light
45-
bool setLightState(bool state, HsvColor_t colorHSV) {
45+
bool setLightState(bool state, espHsvColor_t colorHSV) {
4646

4747
if (state) {
4848
#ifdef RGB_BUILTIN
49-
RgbColor_t rgbColor = HsvToRgb(colorHSV);
49+
espRgbColor_t rgbColor = espHsvColorToRgbColor(colorHSV);
5050
// set the RGB LED
5151
rgbLedWrite(ledPin, rgbColor.r, rgbColor.g, rgbColor.b);
5252
#else
@@ -97,7 +97,7 @@ void setup() {
9797
bool lastOnOffState = matterPref.getBool(onOffPrefKey, true);
9898
// default HSV color is blue HSV(169, 254, 254)
9999
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)};
100+
espHsvColor_t lastHsvColor = {uint8_t(prefHsvColor >> 16), uint8_t(prefHsvColor >> 8), uint8_t(prefHsvColor)};
101101
ColorLight.begin(lastOnOffState, lastHsvColor);
102102
// set the callback function to handle the Light state change
103103
ColorLight.onChange(setLightState);

libraries/Matter/keywords.txt

+7-28
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#######################################
2-
# Syntax Coloring Map For OpenThread
2+
# Syntax Coloring Map For Matter
33
#######################################
44

55
#######################################
@@ -10,13 +10,9 @@ Matter KEYWORD1
1010
ArduinoMatter KEYWORD1
1111
MatterOnOffLight KEYWORD1
1212
MatterDimmableLight KEYWORD1
13-
MatterColorTemperatureLight KEYWORD1
13+
MatterColorTemperatureLight KEYWORD1
1414
MatterColorLight KEYWORD1
1515
MatterEndPoint KEYWORD1
16-
CtColor_t KEYWORD1
17-
XyColor_t KEYWORD1
18-
HsvColor_t KEYWORD1
19-
RgbColor_t KEYWORD1
2016

2117
#######################################
2218
# Methods and Functions (KEYWORD2)
@@ -47,31 +43,14 @@ updateAccessory KEYWORD2
4743
onChange KEYWORD2
4844
onChangeOnOff KEYWORD2
4945
onChangeBrightness KEYWORD2
50-
onChangeColorTemperature KEYWORD2
46+
onChangeColorTemperature KEYWORD2
5147
onChangeColorHSV KEYWORD2
52-
XYToRgb KEYWORD2
53-
HsvToRgb KEYWORD2
54-
CTToRgb KEYWORD2
55-
RgbToHsv KEYWORD2
48+
5649

5750
#######################################
5851
# Constants (LITERAL1)
5952
#######################################
6053

61-
MAX_BRIGHTNESS LITERAL1
62-
MAX_COLOR_TEMPERATURE LITERAL1
63-
MIN_COLOR_TEMPERATURE LITERAL1
64-
COOL_WHITE_COLOR_TEMPERATURE LITERAL1
65-
DAYLIGHT_WHITE_COLOR_TEMPERATURE LITERAL1
66-
WHITE_COLOR_TEMPERATURE LITERAL1
67-
SOFT_WHITE_COLOR_TEMPERATURE LITERAL1
68-
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-
54+
MAX_BRIGHTNESS LITERAL1
55+
MAX_COLOR_TEMPERATURE LITERAL1
56+
MIN_COLOR_TEMPERATURE LITERAL1

libraries/Matter/src/Matter.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
#include <Matter.h>
1919
#include <app/server/Server.h>
20-
#include "MatterEndPoint.h"
2120

2221
using namespace esp_matter;
2322
using namespace esp_matter::attribute;

libraries/Matter/src/Matter.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
#include <Arduino.h>
2020
#include <esp_matter.h>
21-
#include <MatterUtil/ColorFormat.h>
21+
#include <ColorFormat.h>
2222
#include <MatterEndpoints/MatterOnOffLight.h>
2323
#include <MatterEndpoints/MatterDimmableLight.h>
2424
#include <MatterEndpoints/MatterColorTemperatureLight.h>

0 commit comments

Comments
 (0)