From f422888d37469f5a5adfa23a8db14d259363bc80 Mon Sep 17 00:00:00 2001 From: Jacob Date: Mon, 21 Apr 2025 16:42:54 -0400 Subject: [PATCH] feat: early stage of defining protocols that can be implemented by drivers --- protocol/Cargo.toml | 8 ++++++++ protocol/src/lib.rs | 1 + protocol/src/light.rs | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+) create mode 100644 protocol/Cargo.toml create mode 100644 protocol/src/lib.rs create mode 100644 protocol/src/light.rs diff --git a/protocol/Cargo.toml b/protocol/Cargo.toml new file mode 100644 index 0000000..1304277 --- /dev/null +++ b/protocol/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "protocol" +version = "0.1.0" +edition = "2021" + +[dependencies] +deranged = { workspace = true } +derive_more = { workspace = true } diff --git a/protocol/src/lib.rs b/protocol/src/lib.rs new file mode 100644 index 0000000..ad8f30e --- /dev/null +++ b/protocol/src/lib.rs @@ -0,0 +1 @@ +pub mod light; diff --git a/protocol/src/light.rs b/protocol/src/light.rs new file mode 100644 index 0000000..2825800 --- /dev/null +++ b/protocol/src/light.rs @@ -0,0 +1,37 @@ +use std::error::Error; + +use deranged::RangedU16; + +pub trait Light { + type IsOnError: Error; + async fn is_on(&self) -> Result; + + type IsOffError: Error; + async fn is_off(&self) -> Result; + + type TurnOnError: Error; + async fn turn_on(&mut self) -> Result<(), Self::TurnOnError>; + + type TurnOffError: Error; + async fn turn_off(&mut self) -> Result<(), Self::TurnOffError>; + + type ToggleError: Error; + async fn toggle(&mut self) -> Result<(), Self::ToggleError>; +} + +#[derive(Debug, Clone, Copy, derive_more::From, derive_more::Into)] +pub struct Kelvin(pub RangedU16<2000, 10000>); + +pub trait KelvinLight: Light { + type TurnToKelvinError: Error; + async fn turn_to_kelvin(&mut self, temperature: Kelvin) -> Result<(), Self::TurnToKelvinError>; +} + +// TODO: replace with a type from a respected and useful library +#[derive(Debug, Clone, Copy, derive_more::From, derive_more::Into)] +pub struct Rgb(pub u8, pub u8, pub u8); + +pub trait RgbLight: Light { + type TurnToRgbError: Error; + async fn turn_to_rgb(&mut self, color: Rgb) -> Result<(), Self::TurnToRgbError>; +}