Skip to content

Commit

Permalink
feat(matter): global color space functions
Browse files Browse the repository at this point in the history
  • Loading branch information
SuGlider committed Nov 25, 2024
1 parent 52a5e89 commit 916e949
Show file tree
Hide file tree
Showing 13 changed files with 195 additions and 129 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ endif()
set(CORE_SRCS
cores/esp32/base64.cpp
cores/esp32/cbuf.cpp
cores/esp32/ColorFormat.c
cores/esp32/chip-debug-report.cpp
cores/esp32/esp32-hal-adc.c
cores/esp32/esp32-hal-bt.c
Expand Down Expand Up @@ -171,7 +172,6 @@ set(ARDUINO_LIBRARY_Matter_SRCS
libraries/Matter/src/MatterEndpoints/MatterDimmableLight.cpp
libraries/Matter/src/MatterEndpoints/MatterColorTemperatureLight.cpp
libraries/Matter/src/MatterEndpoints/MatterColorLight.cpp
libraries/Matter/src/MatterUtil/ColorFormat.cpp
libraries/Matter/src/Matter.cpp)

set(ARDUINO_LIBRARY_PPP_SRCS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,38 @@
// define a clamp macro to substitute the std::clamp macro which is available from C++17 onwards
#define clamp(a, min, max) ((a) < (min) ? (min) : ((a) > (max) ? (max) : (a)))

RgbColor_t HsvToRgb(HsvColor_t hsv) {
RgbColor_t rgb;
const espHsvColor_t HSV_BLACK = {0, 0, 0};
const espHsvColor_t HSV_WHITE = {0, 0, 254};
const espHsvColor_t HSV_RED = {0, 254, 254};
const espHsvColor_t HSV_YELLOW = {42, 254, 254};
const espHsvColor_t HSV_GREEN = {84, 254, 254};
const espHsvColor_t HSV_CYAN = {127, 254, 254};
const espHsvColor_t HSV_BLUE = {169, 254, 254};
const espHsvColor_t HSV_MAGENTA = {211, 254, 254};

const espRgbColor_t RGB_BLACK = {0, 0, 0};
const espRgbColor_t RGB_WHITE = {255, 255, 255};
const espRgbColor_t RGB_RED = {255, 0, 0};
const espRgbColor_t RGB_YELLOW = {255, 255, 0};
const espRgbColor_t RGB_GREEN = {0, 255, 0};
const espRgbColor_t RGB_CYAN = {0, 255, 255};
const espRgbColor_t RGB_BLUE = {0, 0, 255};
const espRgbColor_t RGB_MAGENTA = {255, 0, 255};

// main color temperature values
const espCtColor_t COOL_WHITE_COLOR_TEMPERATURE = { 142 };
const espCtColor_t DAYLIGHT_WHITE_COLOR_TEMPERATURE = { 181 };
const espCtColor_t WHITE_COLOR_TEMPERATURE = { 250 };
const espCtColor_t SOFT_WHITE_COLOR_TEMPERATURE = { 370 };
const espCtColor_t WARM_WHITE_COLOR_TEMPERATURE = { 454 };

espRgbColor_t espHsvToRgbColor(uint16_t h, uint8_t s, uint8_t v) {
espHsvColor_t hsv = {h, s, v};
return espHsvColorToRgbColor(hsv);
}

espRgbColor_t espHsvColorToRgbColor(espHsvColor_t hsv) {
espRgbColor_t rgb;

uint8_t region, p, q, t;
uint32_t h, s, v, remainder;
Expand Down Expand Up @@ -66,8 +96,13 @@ RgbColor_t HsvToRgb(HsvColor_t hsv) {
return rgb;
}

HsvColor_t RgbToHsv(RgbColor_t rgb) {
HsvColor_t hsv;
espHsvColor_t espRgbToHsvColor(uint8_t r, uint8_t g, uint8_t b) {
espRgbColor_t rgb = {r, g, b};
return espRgbColorToHsvColor(rgb);
}

espHsvColor_t espRgbColorToHsvColor(espRgbColor_t rgb) {
espHsvColor_t hsv;
uint8_t rgbMin, rgbMax;

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

RgbColor_t XYToRgb(uint8_t Level, uint16_t current_X, uint16_t current_Y) {
espRgbColor_t espXYColorToRgbColor(uint8_t Level, espXyColor_t xy) {
return espXYToRgbColor(Level, xy.x, xy.y);
}

espRgbColor_t espXYToRgbColor(uint8_t Level, uint16_t current_X, uint16_t current_Y) {
// convert xyY color space to RGB

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

RgbColor_t rgb;
espRgbColor_t rgb;

float x, y, z;
float X, Y, Z;
Expand Down Expand Up @@ -155,14 +194,19 @@ RgbColor_t XYToRgb(uint8_t Level, uint16_t current_X, uint16_t current_Y) {
return rgb;
}

XyColor_t RgbToXY(RgbColor_t rgb) {
espXyColor_t espRgbToXYColor(uint8_t r, uint8_t g, uint8_t b){
espRgbColor_t rgb = {r, g, b};
return espRgbColorToXYColor(rgb);
}

espXyColor_t espRgbColorToXYColor(espRgbColor_t rgb) {
// convert RGB to xy color space

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

XyColor_t xy;
espXyColor_t xy;

float r, g, b;
float X, Y, Z;
Expand Down Expand Up @@ -198,9 +242,13 @@ XyColor_t RgbToXY(RgbColor_t rgb) {
return xy;
}

espRgbColor_t espCTToRgbColor(uint16_t ct){
espCtColor_t ctColor = {ct};
return espCTColorToRgbColor(ctColor);
}

RgbColor_t CTToRgb(CtColor_t ct) {
RgbColor_t rgb = {0, 0, 0};
espRgbColor_t espCTColorToRgbColor(espCtColor_t ct) {
espRgbColor_t rgb = {0, 0, 0};
float r, g, b;

if (ct.ctMireds == 0) {
Expand Down
69 changes: 69 additions & 0 deletions cores/esp32/ColorFormat.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
*
* Copyright (c) 2021 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once

#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif

struct RgbColor_t {
uint8_t r;
uint8_t g;
uint8_t b;
};

struct HsvColor_t {
uint16_t h;
uint8_t s;
uint8_t v;
};

struct XyColor_t {
uint16_t x;
uint16_t y;
};

struct CtColor_t {
uint16_t ctMireds;
};

typedef struct RgbColor_t espRgbColor_t;
typedef struct HsvColor_t espHsvColor_t;
typedef struct XyColor_t espXyColor_t;
typedef struct CtColor_t espCtColor_t;

espRgbColor_t espXYToRgbColor(uint8_t Level, uint16_t current_X, uint16_t current_Y);
espRgbColor_t espXYColorToRgb(uint8_t Level, espXyColor_t xy);
espXyColor_t espRgbColorToXYColor(espRgbColor_t rgb);
espXyColor_t espRgbToXYColor(uint8_t r, uint8_t g, uint8_t b);
espRgbColor_t espHsvColorToRgbColor(espHsvColor_t hsv);
espRgbColor_t espHsvToRgbColor(uint16_t h, uint8_t s, uint8_t v);
espRgbColor_t espCTColorToRgbColor(espCtColor_t ct);
espRgbColor_t espCTToRgbColor(uint16_t ct);
espHsvColor_t espRgbColorToHsvColor(espRgbColor_t rgb);
espHsvColor_t espRgbToHsvColor(uint8_t r, uint8_t g, uint8_t b);

extern const espHsvColor_t HSV_BLACK, HSV_WHITE, HSV_RED, HSV_YELLOW, HSV_GREEN, HSV_CYAN, HSV_BLUE, HSV_MAGENTA;
extern const espCtColor_t COOL_WHITE_COLOR_TEMPERATURE, DAYLIGHT_WHITE_COLOR_TEMPERATURE, WHITE_COLOR_TEMPERATURE, SOFT_WHITE_COLOR_TEMPERATURE, WARM_WHITE_COLOR_TEMPERATURE;
extern const espRgbColor_t RGB_BLACK, RGB_WHITE, RGB_RED, RGB_YELLOW, RGB_GREEN, RGB_CYAN, RGB_BLUE, RGB_MAGENTA;

#ifdef __cplusplus
}
#endif
39 changes: 38 additions & 1 deletion libraries/ESP32/keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,51 @@
# Datatypes (KEYWORD1)
#######################################

Serial4 KEYWORD1
Serial4 KEYWORD1
espCtColor_t KEYWORD1
espXyColor_t KEYWORD1
espHsvColor_t KEYWORD1
espRgbColor_t KEYWORD1

#######################################
# Methods and Functions (KEYWORD2)
#######################################

espXYToRgbColor KEYWORD2
espXYColorToRgb KEYWORD2
espRgbColorToXYColor KEYWORD2
espRgbToXYColor KEYWORD2
espHsvColorToRgbColor KEYWORD2
espHsvToRgbColor KEYWORD2
espCTColorToRgbColor KEYWORD2
espCTToRgbColor KEYWORD2
espRgbColorToHsvColor KEYWORD2
espRgbToHsvColor KEYWORD2

#######################################
# Constants (LITERAL1)
#######################################

RGB_BUILTIN LITERAL1
HSV_BLACK LITERAL1
HSV_WHITE LITERAL1
HSV_RED LITERAL1
HSV_YELLOW LITERAL1
HSV_GREEN LITERAL1
HSV_CYAN LITERAL1
HSV_BLUE LITERAL1
HSV_MAGENTA LITERAL1
COOL_WHITE_COLOR_TEMPERATURE LITERAL1
DAYLIGHT_WHITE_COLOR_TEMPERATURE LITERAL1
WHITE_COLOR_TEMPERATURE LITERAL1
SOFT_WHITE_COLOR_TEMPERATURE LITERAL1
WARM_WHITE_COLOR_TEMPERATURE LITERAL1
RGB_BLACK LITERAL1
RGB_WHITE LITERAL1
RGB_RED LITERAL1
RGB_YELLOW LITERAL1
RGB_GREEN LITERAL1
RGB_CYAN LITERAL1
RGB_BLUE LITERAL1
RGB_MAGENTA LITERAL1

Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ bool setLightState(bool state, uint8_t brightness, uint16_t temperature_Mireds)

if (state) {
#ifdef RGB_BUILTIN
CtColor_t ct = {temperature_Mireds};
RgbColor_t rgb_ct = CTToRgb(ct);
espRgbColor_t rgb_ct = espCTToRgbColor(temperature_Mireds);
// simple intensity correction
float brightnessPercent = (float)brightness / MatterColorTemperatureLight::MAX_BRIGHTNESS;
rgb_ct.r = brightnessPercent * rgb_ct.r;
Expand Down Expand Up @@ -106,7 +105,7 @@ void setup() {
// default brightness ~= 6% (15/255)
uint8_t lastBrightness = matterPref.getUChar(brightnessPrefKey, 15);
// default temperature ~= 454 Mireds (Warm White)
uint16_t lastTemperature = matterPref.getUShort(temperaturePrefKey, MatterColorTemperatureLight::WARM_WHITE_COLOR_TEMPERATURE);
uint16_t lastTemperature = matterPref.getUShort(temperaturePrefKey, WARM_WHITE_COLOR_TEMPERATURE.ctMireds);
CW_WW_Light.begin(lastOnOffState, lastBrightness, lastTemperature);
// set the callback function to handle the Light state change
CW_WW_Light.onChange(setLightState);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ const char *ssid = "your-ssid"; // Change this to your WiFi SSID
const char *password = "your-password"; // Change this to your WiFi password

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

if (state) {
#ifdef RGB_BUILTIN
RgbColor_t rgbColor = HsvToRgb(colorHSV);
espRgbColor_t rgbColor = espHsvColorToRgbColor(colorHSV);
// set the RGB LED
rgbLedWrite(ledPin, rgbColor.r, rgbColor.g, rgbColor.b);
#else
Expand Down Expand Up @@ -97,7 +97,7 @@ void setup() {
bool lastOnOffState = matterPref.getBool(onOffPrefKey, true);
// default HSV color is blue HSV(169, 254, 254)
uint32_t prefHsvColor = matterPref.getUInt(hsvColorPrefKey, 169 << 16 | 254 << 8 | 254);
HsvColor_t lastHsvColor = {uint8_t(prefHsvColor >> 16), uint8_t(prefHsvColor >> 8), uint8_t(prefHsvColor)};
espHsvColor_t lastHsvColor = {uint8_t(prefHsvColor >> 16), uint8_t(prefHsvColor >> 8), uint8_t(prefHsvColor)};
ColorLight.begin(lastOnOffState, lastHsvColor);
// set the callback function to handle the Light state change
ColorLight.onChange(setLightState);
Expand Down
35 changes: 7 additions & 28 deletions libraries/Matter/keywords.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#######################################
# Syntax Coloring Map For OpenThread
# Syntax Coloring Map For Matter
#######################################

#######################################
Expand All @@ -10,13 +10,9 @@ Matter KEYWORD1
ArduinoMatter KEYWORD1
MatterOnOffLight KEYWORD1
MatterDimmableLight KEYWORD1
MatterColorTemperatureLight KEYWORD1
MatterColorTemperatureLight KEYWORD1
MatterColorLight KEYWORD1
MatterEndPoint KEYWORD1
CtColor_t KEYWORD1
XyColor_t KEYWORD1
HsvColor_t KEYWORD1
RgbColor_t KEYWORD1

#######################################
# Methods and Functions (KEYWORD2)
Expand Down Expand Up @@ -47,31 +43,14 @@ updateAccessory KEYWORD2
onChange KEYWORD2
onChangeOnOff KEYWORD2
onChangeBrightness KEYWORD2
onChangeColorTemperature KEYWORD2
onChangeColorTemperature KEYWORD2
onChangeColorHSV KEYWORD2
XYToRgb KEYWORD2
HsvToRgb KEYWORD2
CTToRgb KEYWORD2
RgbToHsv KEYWORD2


#######################################
# Constants (LITERAL1)
#######################################

MAX_BRIGHTNESS LITERAL1
MAX_COLOR_TEMPERATURE LITERAL1
MIN_COLOR_TEMPERATURE LITERAL1
COOL_WHITE_COLOR_TEMPERATURE LITERAL1
DAYLIGHT_WHITE_COLOR_TEMPERATURE LITERAL1
WHITE_COLOR_TEMPERATURE LITERAL1
SOFT_WHITE_COLOR_TEMPERATURE LITERAL1
WARM_WHITE_COLOR_TEMPERATURE LITERAL1
HSV_BLACK LITERAL1
HSV_WHITE LITERAL1
HSV_RED LITERAL1
HSV_YELLOW LITERAL1
HSV_GREEN LITERAL1
HSV_CIAN LITERAL1
HSV_BLUE LITERAL1
HSV_MAGENTA LITERAL1

MAX_BRIGHTNESS LITERAL1
MAX_COLOR_TEMPERATURE LITERAL1
MIN_COLOR_TEMPERATURE LITERAL1
1 change: 0 additions & 1 deletion libraries/Matter/src/Matter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

#include <Matter.h>
#include <app/server/Server.h>
#include "MatterEndPoint.h"

using namespace esp_matter;
using namespace esp_matter::attribute;
Expand Down
2 changes: 1 addition & 1 deletion libraries/Matter/src/Matter.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

#include <Arduino.h>
#include <esp_matter.h>
#include <MatterUtil/ColorFormat.h>
#include <ColorFormat.h>
#include <MatterEndpoints/MatterOnOffLight.h>
#include <MatterEndpoints/MatterDimmableLight.h>
#include <MatterEndpoints/MatterColorTemperatureLight.h>
Expand Down
Loading

0 comments on commit 916e949

Please sign in to comment.