feat: add RGB color picker support
This commit is contained in:
1 parent
f7f97e8685
commit
3e5ef7570f
5 files changed
+249
-10
No files matched your search
+162
-8
@@ -1,7 +1,7 @@
|
||||
//! Zigbee end device: commissioning, the stack task, and the clusters served.
|
||||
//!
|
||||
//! The device is a single Color Temperature Light: all strip pixels share the
|
||||
//! same on/off state, level, and color-temperature preset.
|
||||
//! The device is a single Extended Color Light: all strip pixels share the
|
||||
//! same on/off state, level, and color or color-temperature setting.
|
||||
|
||||
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::OnOffServer;
|
||||
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::level_control;
|
||||
use zigbee_cluster_library::clusters::lighting::level_control::LevelControlServer;
|
||||
@@ -89,8 +90,8 @@ static OUTPUT_CLUSTERS: [u16; 0] = [];
|
||||
static ENDPOINTS: [EndpointDescriptor; 1] = [EndpointDescriptor {
|
||||
endpoint: LIGHT_ENDPOINT,
|
||||
profile_id: profile::HOME_AUTOMATION,
|
||||
// HA device id 0x010c: Color Temperature Light.
|
||||
device_id: 0x010c,
|
||||
// HA device id 0x010d: Extended Color Light.
|
||||
device_id: 0x010d,
|
||||
device_version: 1,
|
||||
input_clusters: &INPUT_CLUSTERS,
|
||||
output_clusters: &OUTPUT_CLUSTERS,
|
||||
@@ -109,7 +110,7 @@ static BASIC: BasicServer = BasicServer {
|
||||
|
||||
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 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]
|
||||
pub async fn lighting_task(stack: &'static Stack) {
|
||||
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_level = LEVEL_CONTROL.current_level();
|
||||
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_level = 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 started_at = Instant::now();
|
||||
|
||||
@@ -303,8 +310,72 @@ pub async fn lighting_task(stack: &'static Stack) {
|
||||
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();
|
||||
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);
|
||||
last_mireds = actual_mireds;
|
||||
if actual_mireds != remote_mireds {
|
||||
@@ -315,10 +386,17 @@ pub async fn lighting_task(stack: &'static Stack) {
|
||||
}
|
||||
}
|
||||
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;
|
||||
COLOR_CONTROL.set_color_temperature_mireds(local_mireds);
|
||||
last_color = COLOR_CONTROL.color();
|
||||
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;
|
||||
@@ -374,6 +452,82 @@ pub async fn lighting_task(stack: &'static Stack) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user