feat: add RGB color picker support

This commit is contained in:
Matt Spencer committed 2026-11-17 23:26:50 +00:00
1 parent f7f97e8685
commit 3e5ef7570f
5 files changed
+249 -10

No files matched your search

Generated
+7
View File
@@ -1419,6 +1419,12 @@ version = "0.2.191"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "65cd87c9a6d60536580b316ad985726104730a5151c99845f2490428ef91240a" checksum = "65cd87c9a6d60536580b316ad985726104730a5151c99845f2490428ef91240a"
[[package]]
name = "libm"
version = "0.2.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b6d2cec3eae94f9f509c767b45932f1ada8350c4bdb85af2fcab4a3c14807981"
[[package]] [[package]]
name = "linked_list_allocator" name = "linked_list_allocator"
version = "0.10.6" version = "0.10.6"
@@ -1849,6 +1855,7 @@ dependencies = [
"esp-radio", "esp-radio",
"esp-rtos", "esp-rtos",
"esp-storage", "esp-storage",
"libm",
"log", "log",
"smart-leds", "smart-leds",
"smart-leds-trait", "smart-leds-trait",
+1
View File
@@ -42,6 +42,7 @@ log = "0.4"
smart-leds = "0.4" smart-leds = "0.4"
smart-leds-trait = "0.3" smart-leds-trait = "0.3"
esp-hal-smartled = "0.18" esp-hal-smartled = "0.18"
libm = "0.2"
# vendored upstream: the lighting cluster is not implemented there yet, so the # vendored upstream: the lighting cluster is not implemented there yet, so the
# path dependency lets us add it in-tree and upstream it later # path dependency lets us add it in-tree and upstream it later
+78 -1
View File
@@ -14,6 +14,8 @@ use smart_leds::RGB8;
use smart_leds::SmartLedsWriteAsync; use smart_leds::SmartLedsWriteAsync;
use smart_leds::brightness as scale_brightness; use smart_leds::brightness as scale_brightness;
use smart_leds::gamma; use smart_leds::gamma;
use smart_leds::hsv::Hsv;
use smart_leds::hsv::hsv2rgb;
use crate::config::LED_COUNT; use crate::config::LED_COUNT;
use crate::config::MAX_DUTY; use crate::config::MAX_DUTY;
@@ -38,6 +40,12 @@ static ON: AtomicBool = AtomicBool::new(true);
static BRIGHTNESS: AtomicU8 = AtomicU8::new(128); static BRIGHTNESS: AtomicU8 = AtomicU8::new(128);
/// Index into [`TEMPERATURES`]; 2700K is the warm white a lamp usually sits at. /// Index into [`TEMPERATURES`]; 2700K is the warm white a lamp usually sits at.
static TEMPERATURE: AtomicU8 = AtomicU8::new(1); static TEMPERATURE: AtomicU8 = AtomicU8::new(1);
static COLOR_MODE: AtomicBool = AtomicBool::new(false);
static XY_MODE: AtomicBool = AtomicBool::new(false);
static HUE: AtomicU8 = AtomicU8::new(0);
static SATURATION: AtomicU8 = AtomicU8::new(0);
static X: AtomicU8 = AtomicU8::new(122);
static Y: AtomicU8 = AtomicU8::new(128);
static DIRTY: Signal<CriticalSectionRawMutex, ()> = Signal::new(); static DIRTY: Signal<CriticalSectionRawMutex, ()> = Signal::new();
pub fn is_on() -> bool { pub fn is_on() -> bool {
@@ -81,6 +89,26 @@ pub fn temperature_mireds() -> u16 {
(1_000_000u32 / u32::from(temperature().0)) as u16 (1_000_000u32 / u32::from(temperature().0)) as u16
} }
pub fn is_color_mode() -> bool {
COLOR_MODE.load(Ordering::Relaxed)
}
pub fn set_hue_saturation(hue: u8, saturation: u8) {
HUE.store(hue.min(254), Ordering::Relaxed);
SATURATION.store(saturation.min(254), Ordering::Relaxed);
COLOR_MODE.store(true, Ordering::Relaxed);
XY_MODE.store(false, Ordering::Relaxed);
DIRTY.signal(());
}
pub fn set_xy(x: u16, y: u16) {
X.store((u32::from(x) * 255 / 65_535) as u8, Ordering::Relaxed);
Y.store((u32::from(y) * 255 / 65_535) as u8, Ordering::Relaxed);
COLOR_MODE.store(true, Ordering::Relaxed);
XY_MODE.store(true, Ordering::Relaxed);
DIRTY.signal(());
}
/// Select the nearest available RGB preset for a Zigbee mired request. /// Select the nearest available RGB preset for a Zigbee mired request.
pub fn set_temperature_mireds(mireds: u16) -> u16 { pub fn set_temperature_mireds(mireds: u16) -> u16 {
let target_kelvin = 1_000_000 / u32::from(mireds.max(1)); let target_kelvin = 1_000_000 / u32::from(mireds.max(1));
@@ -96,6 +124,8 @@ pub fn set_temperature_mireds(mireds: u16) -> u16 {
} }
TEMPERATURE.store(closest_index as u8, Ordering::Relaxed); TEMPERATURE.store(closest_index as u8, Ordering::Relaxed);
COLOR_MODE.store(false, Ordering::Relaxed);
XY_MODE.store(false, Ordering::Relaxed);
DIRTY.signal(()); DIRTY.signal(());
temperature_mireds() temperature_mireds()
} }
@@ -104,6 +134,8 @@ pub fn set_temperature_mireds(mireds: u16) -> u16 {
pub fn next_temperature() -> (u16, RGB8) { pub fn next_temperature() -> (u16, RGB8) {
let next = (TEMPERATURE.load(Ordering::Relaxed) + 1) % TEMPERATURES.len() as u8; let next = (TEMPERATURE.load(Ordering::Relaxed) + 1) % TEMPERATURES.len() as u8;
TEMPERATURE.store(next, Ordering::Relaxed); TEMPERATURE.store(next, Ordering::Relaxed);
COLOR_MODE.store(false, Ordering::Relaxed);
XY_MODE.store(false, Ordering::Relaxed);
DIRTY.signal(()); DIRTY.signal(());
temperature() temperature()
} }
@@ -113,7 +145,19 @@ pub async fn render_task(mut strip: Strip<'static>) {
let mut frame = [RGB8::default(); LED_COUNT]; let mut frame = [RGB8::default(); LED_COUNT];
loop { loop {
let (_, colour) = temperature(); let colour = if is_color_mode() {
if XY_MODE.load(Ordering::Relaxed) {
xy_to_rgb(X.load(Ordering::Relaxed), Y.load(Ordering::Relaxed))
} else {
hsv2rgb(Hsv {
hue: (u16::from(HUE.load(Ordering::Relaxed)) * 255 / 254) as u8,
sat: (u16::from(SATURATION.load(Ordering::Relaxed)) * 255 / 254) as u8,
val: 255,
})
}
} else {
temperature().1
};
frame.fill(colour); frame.fill(colour);
// off is just zero brightness, so the colour survives a power cycle of // off is just zero brightness, so the colour survives a power cycle of
@@ -142,3 +186,36 @@ pub async fn render_task(mut strip: Strip<'static>) {
DIRTY.wait().await; DIRTY.wait().await;
} }
} }
fn xy_to_rgb(x: u8, y: u8) -> RGB8 {
let x = f32::from(x) / 255.0;
let y = f32::from(y) / 255.0;
if y == 0.0 {
return RGB8::default();
}
let z = (1.0 - x - y) / y;
let x = x / y;
let mut red = 3.2406 * x - 1.5372 - 0.4986 * z;
let mut green = -0.9689 * x + 1.8758 + 0.0415 * z;
let mut blue = 0.0557 * x - 0.2040 + 1.0570 * z;
let max = red.max(green).max(blue).max(1.0);
red = (red / max).max(0.0);
green = (green / max).max(0.0);
blue = (blue / max).max(0.0);
fn srgb(value: f32) -> u8 {
let linear = if value <= 0.003_130_8 {
12.92 * value
} else {
1.055 * libm::powf(value, 1.0 / 2.4) - 0.055
};
(linear * 255.0 + 0.5) as u8
}
RGB8 {
r: srgb(red),
g: srgb(green),
b: srgb(blue),
}
}
+162 -8
View File
@@ -1,7 +1,7 @@
//! Zigbee end device: commissioning, the stack task, and the clusters served. //! Zigbee end device: commissioning, the stack task, and the clusters served.
//! //!
//! The device is a single Color Temperature Light: all strip pixels share the //! The device is a single Extended Color Light: all strip pixels share the
//! same on/off state, level, and color-temperature preset. //! same on/off state, level, and color or color-temperature setting.
use core::ops::Range; use core::ops::Range;
@@ -32,6 +32,7 @@ use zigbee_cluster_library::clusters::general::identify::IdentifyServer;
use zigbee_cluster_library::clusters::general::on_off; use zigbee_cluster_library::clusters::general::on_off;
use zigbee_cluster_library::clusters::general::on_off::OnOffServer; use zigbee_cluster_library::clusters::general::on_off::OnOffServer;
use zigbee_cluster_library::clusters::lighting::color_control; use zigbee_cluster_library::clusters::lighting::color_control;
use zigbee_cluster_library::clusters::lighting::color_control::ColorMode;
use zigbee_cluster_library::clusters::lighting::color_control::ColorControlServer; use zigbee_cluster_library::clusters::lighting::color_control::ColorControlServer;
use zigbee_cluster_library::clusters::lighting::level_control; use zigbee_cluster_library::clusters::lighting::level_control;
use zigbee_cluster_library::clusters::lighting::level_control::LevelControlServer; use zigbee_cluster_library::clusters::lighting::level_control::LevelControlServer;
@@ -89,8 +90,8 @@ static OUTPUT_CLUSTERS: [u16; 0] = [];
static ENDPOINTS: [EndpointDescriptor; 1] = [EndpointDescriptor { static ENDPOINTS: [EndpointDescriptor; 1] = [EndpointDescriptor {
endpoint: LIGHT_ENDPOINT, endpoint: LIGHT_ENDPOINT,
profile_id: profile::HOME_AUTOMATION, profile_id: profile::HOME_AUTOMATION,
// HA device id 0x010c: Color Temperature Light. // HA device id 0x010d: Extended Color Light.
device_id: 0x010c, device_id: 0x010d,
device_version: 1, device_version: 1,
input_clusters: &INPUT_CLUSTERS, input_clusters: &INPUT_CLUSTERS,
output_clusters: &OUTPUT_CLUSTERS, output_clusters: &OUTPUT_CLUSTERS,
@@ -109,7 +110,7 @@ static BASIC: BasicServer = BasicServer {
pub static IDENTIFY: IdentifyServer = IdentifyServer::new(); pub static IDENTIFY: IdentifyServer = IdentifyServer::new();
static REPORTING: ReportingTable<4> = ReportingTable::new(); static REPORTING: ReportingTable<7> = ReportingTable::new();
static ON_OFF: OnOffServer = OnOffServer::new().with_reporting(&REPORTING); static ON_OFF: OnOffServer = OnOffServer::new().with_reporting(&REPORTING);
static LEVEL_CONTROL: LevelControlServer<'static> = static LEVEL_CONTROL: LevelControlServer<'static> =
@@ -251,7 +252,7 @@ where
} }
} }
/// Keeps the single strip synchronized with Zigbee On/Off, level and color temperature. /// Keeps the single strip synchronized with Zigbee On/Off, level and color.
#[embassy_executor::task] #[embassy_executor::task]
pub async fn lighting_task(stack: &'static Stack) { pub async fn lighting_task(stack: &'static Stack) {
stack.wait_until_joined().await; stack.wait_until_joined().await;
@@ -259,9 +260,15 @@ pub async fn lighting_task(stack: &'static Stack) {
let mut last_on = ON_OFF.on_off(); let mut last_on = ON_OFF.on_off();
let mut last_level = LEVEL_CONTROL.current_level(); let mut last_level = LEVEL_CONTROL.current_level();
let mut last_mireds = COLOR_CONTROL.color_temperature_mireds(); let mut last_mireds = COLOR_CONTROL.color_temperature_mireds();
let mut last_color = COLOR_CONTROL.color();
let mut last_xy = COLOR_CONTROL.xy();
let mut pending_on = None; let mut pending_on = None;
let mut pending_level = None; let mut pending_level = None;
let mut pending_mireds = None; let mut pending_mireds = None;
let mut pending_hue = None;
let mut pending_saturation = None;
let mut pending_x = None;
let mut pending_y = None;
let mut sequence_number = 0u8; let mut sequence_number = 0u8;
let started_at = Instant::now(); let started_at = Instant::now();
@@ -303,8 +310,72 @@ pub async fn lighting_task(stack: &'static Stack) {
pending_level = Some(local_level); pending_level = Some(local_level);
} }
let remote_color = COLOR_CONTROL.color();
let color_changed = remote_color != last_color;
let remote_xy = COLOR_CONTROL.xy();
let xy_changed = remote_xy != last_xy;
if color_changed {
last_color = remote_color;
if remote_color.0 == ColorMode::HueSaturation {
light::set_hue_saturation(remote_color.1, remote_color.2);
pending_mireds = None;
pending_hue = REPORTING
.configuration(
color_control::CLUSTER.id(),
color_control::CURRENT_HUE.id(),
)
.map(|_| remote_color.1);
pending_saturation = REPORTING
.configuration(
color_control::CLUSTER.id(),
color_control::CURRENT_SATURATION.id(),
)
.map(|_| remote_color.2);
} else if remote_color.0 == ColorMode::Xy {
light::set_xy(remote_xy.0, remote_xy.1);
pending_mireds = None;
pending_hue = None;
pending_saturation = None;
pending_x = REPORTING
.configuration(
color_control::CLUSTER.id(),
color_control::CURRENT_X.id(),
)
.map(|_| remote_xy.0);
pending_y = REPORTING
.configuration(
color_control::CLUSTER.id(),
color_control::CURRENT_Y.id(),
)
.map(|_| remote_xy.1);
} else {
pending_hue = None;
pending_saturation = None;
}
}
if remote_color.0 == ColorMode::Xy && xy_changed {
last_xy = remote_xy;
pending_x = REPORTING
.configuration(
color_control::CLUSTER.id(),
color_control::CURRENT_X.id(),
)
.map(|_| remote_xy.0);
pending_y = REPORTING
.configuration(
color_control::CLUSTER.id(),
color_control::CURRENT_Y.id(),
)
.map(|_| remote_xy.1);
if !color_changed {
light::set_xy(remote_xy.0, remote_xy.1);
}
}
let remote_mireds = COLOR_CONTROL.color_temperature_mireds(); let remote_mireds = COLOR_CONTROL.color_temperature_mireds();
if remote_mireds != last_mireds { if remote_color.0 == ColorMode::ColorTemperature
&& (remote_mireds != last_mireds || color_changed)
{
let actual_mireds = light::set_temperature_mireds(remote_mireds); let actual_mireds = light::set_temperature_mireds(remote_mireds);
last_mireds = actual_mireds; last_mireds = actual_mireds;
if actual_mireds != remote_mireds { if actual_mireds != remote_mireds {
@@ -315,10 +386,17 @@ pub async fn lighting_task(stack: &'static Stack) {
} }
} }
let local_mireds = light::temperature_mireds(); let local_mireds = light::temperature_mireds();
if local_mireds != last_mireds { if !light::is_color_mode()
&& (local_mireds != last_mireds || last_color.0 != ColorMode::ColorTemperature)
{
last_mireds = local_mireds; last_mireds = local_mireds;
COLOR_CONTROL.set_color_temperature_mireds(local_mireds); COLOR_CONTROL.set_color_temperature_mireds(local_mireds);
last_color = COLOR_CONTROL.color();
pending_mireds = Some(local_mireds); pending_mireds = Some(local_mireds);
pending_hue = None;
pending_saturation = None;
pending_x = None;
pending_y = None;
} }
let now_secs = started_at.elapsed().as_secs().min(u64::from(u32::MAX)) as u32; let now_secs = started_at.elapsed().as_secs().min(u64::from(u32::MAX)) as u32;
@@ -374,6 +452,82 @@ pub async fn lighting_task(stack: &'static Stack) {
log::info!("reported local color temperature: {value} mireds"); log::info!("reported local color temperature: {value} mireds");
} }
} }
if let Some(value) = pending_hue {
let cluster = color_control::CLUSTER.id();
let attribute = color_control::CURRENT_HUE.id();
let due = REPORTING.configuration(cluster, attribute).is_none()
|| REPORTING.should_report(cluster, attribute, now_secs, i64::from(value));
if due
&& report_attribute(
stack,
&color_control::CURRENT_HUE,
Uint8(value),
&mut sequence_number,
)
.await
{
REPORTING.reported(cluster, attribute, now_secs, i64::from(value));
pending_hue = None;
}
}
if let Some(value) = pending_saturation {
let cluster = color_control::CLUSTER.id();
let attribute = color_control::CURRENT_SATURATION.id();
let due = REPORTING.configuration(cluster, attribute).is_none()
|| REPORTING.should_report(cluster, attribute, now_secs, i64::from(value));
if due
&& report_attribute(
stack,
&color_control::CURRENT_SATURATION,
Uint8(value),
&mut sequence_number,
)
.await
{
REPORTING.reported(cluster, attribute, now_secs, i64::from(value));
pending_saturation = None;
}
}
if let Some(value) = pending_x {
let cluster = color_control::CLUSTER.id();
let attribute = color_control::CURRENT_X.id();
let due = REPORTING.configuration(cluster, attribute).is_none()
|| REPORTING.should_report(cluster, attribute, now_secs, i64::from(value));
if due
&& report_attribute(
stack,
&color_control::CURRENT_X,
Uint16(value),
&mut sequence_number,
)
.await
{
REPORTING.reported(cluster, attribute, now_secs, i64::from(value));
pending_x = None;
}
}
if let Some(value) = pending_y {
let cluster = color_control::CLUSTER.id();
let attribute = color_control::CURRENT_Y.id();
let due = REPORTING.configuration(cluster, attribute).is_none()
|| REPORTING.should_report(cluster, attribute, now_secs, i64::from(value));
if due
&& report_attribute(
stack,
&color_control::CURRENT_Y,
Uint16(value),
&mut sequence_number,
)
.await
{
REPORTING.reported(cluster, attribute, now_secs, i64::from(value));
pending_y = None;
}
}
} }
} }