use crate::{
byte_converter::*, converting_callback_receiver::ConvertingCallbackReceiver, converting_receiver::ConvertingReceiver, device::*,
ip_connection::GetRequestSender,
};
pub enum ColorBrickletFunction {
GetColor,
SetColorCallbackPeriod,
GetColorCallbackPeriod,
SetColorCallbackThreshold,
GetColorCallbackThreshold,
SetDebouncePeriod,
GetDebouncePeriod,
LightOn,
LightOff,
IsLightOn,
SetConfig,
GetConfig,
GetIlluminance,
GetColorTemperature,
SetIlluminanceCallbackPeriod,
GetIlluminanceCallbackPeriod,
SetColorTemperatureCallbackPeriod,
GetColorTemperatureCallbackPeriod,
GetIdentity,
CallbackColor,
CallbackColorReached,
CallbackIlluminance,
CallbackColorTemperature,
}
impl From<ColorBrickletFunction> for u8 {
fn from(fun: ColorBrickletFunction) -> Self {
match fun {
ColorBrickletFunction::GetColor => 1,
ColorBrickletFunction::SetColorCallbackPeriod => 2,
ColorBrickletFunction::GetColorCallbackPeriod => 3,
ColorBrickletFunction::SetColorCallbackThreshold => 4,
ColorBrickletFunction::GetColorCallbackThreshold => 5,
ColorBrickletFunction::SetDebouncePeriod => 6,
ColorBrickletFunction::GetDebouncePeriod => 7,
ColorBrickletFunction::LightOn => 10,
ColorBrickletFunction::LightOff => 11,
ColorBrickletFunction::IsLightOn => 12,
ColorBrickletFunction::SetConfig => 13,
ColorBrickletFunction::GetConfig => 14,
ColorBrickletFunction::GetIlluminance => 15,
ColorBrickletFunction::GetColorTemperature => 16,
ColorBrickletFunction::SetIlluminanceCallbackPeriod => 17,
ColorBrickletFunction::GetIlluminanceCallbackPeriod => 18,
ColorBrickletFunction::SetColorTemperatureCallbackPeriod => 19,
ColorBrickletFunction::GetColorTemperatureCallbackPeriod => 20,
ColorBrickletFunction::GetIdentity => 255,
ColorBrickletFunction::CallbackColor => 8,
ColorBrickletFunction::CallbackColorReached => 9,
ColorBrickletFunction::CallbackIlluminance => 21,
ColorBrickletFunction::CallbackColorTemperature => 22,
}
}
}
pub const COLOR_BRICKLET_THRESHOLD_OPTION_OFF: char = 'x';
pub const COLOR_BRICKLET_THRESHOLD_OPTION_OUTSIDE: char = 'o';
pub const COLOR_BRICKLET_THRESHOLD_OPTION_INSIDE: char = 'i';
pub const COLOR_BRICKLET_THRESHOLD_OPTION_SMALLER: char = '<';
pub const COLOR_BRICKLET_THRESHOLD_OPTION_GREATER: char = '>';
pub const COLOR_BRICKLET_LIGHT_ON: u8 = 0;
pub const COLOR_BRICKLET_LIGHT_OFF: u8 = 1;
pub const COLOR_BRICKLET_GAIN_1X: u8 = 0;
pub const COLOR_BRICKLET_GAIN_4X: u8 = 1;
pub const COLOR_BRICKLET_GAIN_16X: u8 = 2;
pub const COLOR_BRICKLET_GAIN_60X: u8 = 3;
pub const COLOR_BRICKLET_INTEGRATION_TIME_2MS: u8 = 0;
pub const COLOR_BRICKLET_INTEGRATION_TIME_24MS: u8 = 1;
pub const COLOR_BRICKLET_INTEGRATION_TIME_101MS: u8 = 2;
pub const COLOR_BRICKLET_INTEGRATION_TIME_154MS: u8 = 3;
pub const COLOR_BRICKLET_INTEGRATION_TIME_700MS: u8 = 4;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct Color {
pub r: u16,
pub g: u16,
pub b: u16,
pub c: u16,
}
impl FromByteSlice for Color {
fn bytes_expected() -> usize { 8 }
fn from_le_byte_slice(bytes: &[u8]) -> Color {
Color {
r: <u16>::from_le_byte_slice(&bytes[0..2]),
g: <u16>::from_le_byte_slice(&bytes[2..4]),
b: <u16>::from_le_byte_slice(&bytes[4..6]),
c: <u16>::from_le_byte_slice(&bytes[6..8]),
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct ColorCallbackThreshold {
pub option: char,
pub min_r: u16,
pub max_r: u16,
pub min_g: u16,
pub max_g: u16,
pub min_b: u16,
pub max_b: u16,
pub min_c: u16,
pub max_c: u16,
}
impl FromByteSlice for ColorCallbackThreshold {
fn bytes_expected() -> usize { 17 }
fn from_le_byte_slice(bytes: &[u8]) -> ColorCallbackThreshold {
ColorCallbackThreshold {
option: <char>::from_le_byte_slice(&bytes[0..1]),
min_r: <u16>::from_le_byte_slice(&bytes[1..3]),
max_r: <u16>::from_le_byte_slice(&bytes[3..5]),
min_g: <u16>::from_le_byte_slice(&bytes[5..7]),
max_g: <u16>::from_le_byte_slice(&bytes[7..9]),
min_b: <u16>::from_le_byte_slice(&bytes[9..11]),
max_b: <u16>::from_le_byte_slice(&bytes[11..13]),
min_c: <u16>::from_le_byte_slice(&bytes[13..15]),
max_c: <u16>::from_le_byte_slice(&bytes[15..17]),
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct ColorEvent {
pub r: u16,
pub g: u16,
pub b: u16,
pub c: u16,
}
impl FromByteSlice for ColorEvent {
fn bytes_expected() -> usize { 8 }
fn from_le_byte_slice(bytes: &[u8]) -> ColorEvent {
ColorEvent {
r: <u16>::from_le_byte_slice(&bytes[0..2]),
g: <u16>::from_le_byte_slice(&bytes[2..4]),
b: <u16>::from_le_byte_slice(&bytes[4..6]),
c: <u16>::from_le_byte_slice(&bytes[6..8]),
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct ColorReachedEvent {
pub r: u16,
pub g: u16,
pub b: u16,
pub c: u16,
}
impl FromByteSlice for ColorReachedEvent {
fn bytes_expected() -> usize { 8 }
fn from_le_byte_slice(bytes: &[u8]) -> ColorReachedEvent {
ColorReachedEvent {
r: <u16>::from_le_byte_slice(&bytes[0..2]),
g: <u16>::from_le_byte_slice(&bytes[2..4]),
b: <u16>::from_le_byte_slice(&bytes[4..6]),
c: <u16>::from_le_byte_slice(&bytes[6..8]),
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct Config {
pub gain: u8,
pub integration_time: u8,
}
impl FromByteSlice for Config {
fn bytes_expected() -> usize { 2 }
fn from_le_byte_slice(bytes: &[u8]) -> Config {
Config { gain: <u8>::from_le_byte_slice(&bytes[0..1]), integration_time: <u8>::from_le_byte_slice(&bytes[1..2]) }
}
}
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
pub struct Identity {
pub uid: String,
pub connected_uid: String,
pub position: char,
pub hardware_version: [u8; 3],
pub firmware_version: [u8; 3],
pub device_identifier: u16,
}
impl FromByteSlice for Identity {
fn bytes_expected() -> usize { 25 }
fn from_le_byte_slice(bytes: &[u8]) -> Identity {
Identity {
uid: <String>::from_le_byte_slice(&bytes[0..8]),
connected_uid: <String>::from_le_byte_slice(&bytes[8..16]),
position: <char>::from_le_byte_slice(&bytes[16..17]),
hardware_version: <[u8; 3]>::from_le_byte_slice(&bytes[17..20]),
firmware_version: <[u8; 3]>::from_le_byte_slice(&bytes[20..23]),
device_identifier: <u16>::from_le_byte_slice(&bytes[23..25]),
}
}
}
#[derive(Clone)]
pub struct ColorBricklet {
device: Device,
}
impl ColorBricklet {
pub const DEVICE_IDENTIFIER: u16 = 243;
pub const DEVICE_DISPLAY_NAME: &'static str = "Color Bricklet";
pub fn new<T: GetRequestSender>(uid: &str, req_sender: T) -> ColorBricklet {
let mut result = ColorBricklet { device: Device::new([2, 0, 0], uid, req_sender, 0) };
result.device.response_expected[u8::from(ColorBrickletFunction::GetColor) as usize] = ResponseExpectedFlag::AlwaysTrue;
result.device.response_expected[u8::from(ColorBrickletFunction::SetColorCallbackPeriod) as usize] = ResponseExpectedFlag::True;
result.device.response_expected[u8::from(ColorBrickletFunction::GetColorCallbackPeriod) as usize] =
ResponseExpectedFlag::AlwaysTrue;
result.device.response_expected[u8::from(ColorBrickletFunction::SetColorCallbackThreshold) as usize] = ResponseExpectedFlag::True;
result.device.response_expected[u8::from(ColorBrickletFunction::GetColorCallbackThreshold) as usize] =
ResponseExpectedFlag::AlwaysTrue;
result.device.response_expected[u8::from(ColorBrickletFunction::SetDebouncePeriod) as usize] = ResponseExpectedFlag::True;
result.device.response_expected[u8::from(ColorBrickletFunction::GetDebouncePeriod) as usize] = ResponseExpectedFlag::AlwaysTrue;
result.device.response_expected[u8::from(ColorBrickletFunction::LightOn) as usize] = ResponseExpectedFlag::False;
result.device.response_expected[u8::from(ColorBrickletFunction::LightOff) as usize] = ResponseExpectedFlag::False;
result.device.response_expected[u8::from(ColorBrickletFunction::IsLightOn) as usize] = ResponseExpectedFlag::AlwaysTrue;
result.device.response_expected[u8::from(ColorBrickletFunction::SetConfig) as usize] = ResponseExpectedFlag::False;
result.device.response_expected[u8::from(ColorBrickletFunction::GetConfig) as usize] = ResponseExpectedFlag::AlwaysTrue;
result.device.response_expected[u8::from(ColorBrickletFunction::GetIlluminance) as usize] = ResponseExpectedFlag::AlwaysTrue;
result.device.response_expected[u8::from(ColorBrickletFunction::GetColorTemperature) as usize] = ResponseExpectedFlag::AlwaysTrue;
result.device.response_expected[u8::from(ColorBrickletFunction::SetIlluminanceCallbackPeriod) as usize] =
ResponseExpectedFlag::True;
result.device.response_expected[u8::from(ColorBrickletFunction::GetIlluminanceCallbackPeriod) as usize] =
ResponseExpectedFlag::AlwaysTrue;
result.device.response_expected[u8::from(ColorBrickletFunction::SetColorTemperatureCallbackPeriod) as usize] =
ResponseExpectedFlag::True;
result.device.response_expected[u8::from(ColorBrickletFunction::GetColorTemperatureCallbackPeriod) as usize] =
ResponseExpectedFlag::AlwaysTrue;
result.device.response_expected[u8::from(ColorBrickletFunction::GetIdentity) as usize] = ResponseExpectedFlag::AlwaysTrue;
result
}
pub fn get_response_expected(&mut self, fun: ColorBrickletFunction) -> Result<bool, GetResponseExpectedError> {
self.device.get_response_expected(u8::from(fun))
}
pub fn set_response_expected(&mut self, fun: ColorBrickletFunction, response_expected: bool) -> Result<(), SetResponseExpectedError> {
self.device.set_response_expected(u8::from(fun), response_expected)
}
pub fn set_response_expected_all(&mut self, response_expected: bool) { self.device.set_response_expected_all(response_expected) }
pub fn get_api_version(&self) -> [u8; 3] { self.device.api_version }
pub fn get_color_callback_receiver(&self) -> ConvertingCallbackReceiver<ColorEvent> {
self.device.get_callback_receiver(u8::from(ColorBrickletFunction::CallbackColor))
}
pub fn get_color_reached_callback_receiver(&self) -> ConvertingCallbackReceiver<ColorReachedEvent> {
self.device.get_callback_receiver(u8::from(ColorBrickletFunction::CallbackColorReached))
}
pub fn get_illuminance_callback_receiver(&self) -> ConvertingCallbackReceiver<u32> {
self.device.get_callback_receiver(u8::from(ColorBrickletFunction::CallbackIlluminance))
}
pub fn get_color_temperature_callback_receiver(&self) -> ConvertingCallbackReceiver<u16> {
self.device.get_callback_receiver(u8::from(ColorBrickletFunction::CallbackColorTemperature))
}
pub fn get_color(&self) -> ConvertingReceiver<Color> {
let payload = vec![0; 0];
self.device.get(u8::from(ColorBrickletFunction::GetColor), payload)
}
pub fn set_color_callback_period(&self, period: u32) -> ConvertingReceiver<()> {
let mut payload = vec![0; 4];
payload[0..4].copy_from_slice(&<u32>::to_le_byte_vec(period));
self.device.set(u8::from(ColorBrickletFunction::SetColorCallbackPeriod), payload)
}
pub fn get_color_callback_period(&self) -> ConvertingReceiver<u32> {
let payload = vec![0; 0];
self.device.get(u8::from(ColorBrickletFunction::GetColorCallbackPeriod), payload)
}
pub fn set_color_callback_threshold(
&self,
option: char,
min_r: u16,
max_r: u16,
min_g: u16,
max_g: u16,
min_b: u16,
max_b: u16,
min_c: u16,
max_c: u16,
) -> ConvertingReceiver<()> {
let mut payload = vec![0; 17];
payload[0..1].copy_from_slice(&<char>::to_le_byte_vec(option));
payload[1..3].copy_from_slice(&<u16>::to_le_byte_vec(min_r));
payload[3..5].copy_from_slice(&<u16>::to_le_byte_vec(max_r));
payload[5..7].copy_from_slice(&<u16>::to_le_byte_vec(min_g));
payload[7..9].copy_from_slice(&<u16>::to_le_byte_vec(max_g));
payload[9..11].copy_from_slice(&<u16>::to_le_byte_vec(min_b));
payload[11..13].copy_from_slice(&<u16>::to_le_byte_vec(max_b));
payload[13..15].copy_from_slice(&<u16>::to_le_byte_vec(min_c));
payload[15..17].copy_from_slice(&<u16>::to_le_byte_vec(max_c));
self.device.set(u8::from(ColorBrickletFunction::SetColorCallbackThreshold), payload)
}
pub fn get_color_callback_threshold(&self) -> ConvertingReceiver<ColorCallbackThreshold> {
let payload = vec![0; 0];
self.device.get(u8::from(ColorBrickletFunction::GetColorCallbackThreshold), payload)
}
pub fn set_debounce_period(&self, debounce: u32) -> ConvertingReceiver<()> {
let mut payload = vec![0; 4];
payload[0..4].copy_from_slice(&<u32>::to_le_byte_vec(debounce));
self.device.set(u8::from(ColorBrickletFunction::SetDebouncePeriod), payload)
}
pub fn get_debounce_period(&self) -> ConvertingReceiver<u32> {
let payload = vec![0; 0];
self.device.get(u8::from(ColorBrickletFunction::GetDebouncePeriod), payload)
}
pub fn light_on(&self) -> ConvertingReceiver<()> {
let payload = vec![0; 0];
self.device.set(u8::from(ColorBrickletFunction::LightOn), payload)
}
pub fn light_off(&self) -> ConvertingReceiver<()> {
let payload = vec![0; 0];
self.device.set(u8::from(ColorBrickletFunction::LightOff), payload)
}
pub fn is_light_on(&self) -> ConvertingReceiver<u8> {
let payload = vec![0; 0];
self.device.get(u8::from(ColorBrickletFunction::IsLightOn), payload)
}
pub fn set_config(&self, gain: u8, integration_time: u8) -> ConvertingReceiver<()> {
let mut payload = vec![0; 2];
payload[0..1].copy_from_slice(&<u8>::to_le_byte_vec(gain));
payload[1..2].copy_from_slice(&<u8>::to_le_byte_vec(integration_time));
self.device.set(u8::from(ColorBrickletFunction::SetConfig), payload)
}
pub fn get_config(&self) -> ConvertingReceiver<Config> {
let payload = vec![0; 0];
self.device.get(u8::from(ColorBrickletFunction::GetConfig), payload)
}
pub fn get_illuminance(&self) -> ConvertingReceiver<u32> {
let payload = vec![0; 0];
self.device.get(u8::from(ColorBrickletFunction::GetIlluminance), payload)
}
pub fn get_color_temperature(&self) -> ConvertingReceiver<u16> {
let payload = vec![0; 0];
self.device.get(u8::from(ColorBrickletFunction::GetColorTemperature), payload)
}
pub fn set_illuminance_callback_period(&self, period: u32) -> ConvertingReceiver<()> {
let mut payload = vec![0; 4];
payload[0..4].copy_from_slice(&<u32>::to_le_byte_vec(period));
self.device.set(u8::from(ColorBrickletFunction::SetIlluminanceCallbackPeriod), payload)
}
pub fn get_illuminance_callback_period(&self) -> ConvertingReceiver<u32> {
let payload = vec![0; 0];
self.device.get(u8::from(ColorBrickletFunction::GetIlluminanceCallbackPeriod), payload)
}
pub fn set_color_temperature_callback_period(&self, period: u32) -> ConvertingReceiver<()> {
let mut payload = vec![0; 4];
payload[0..4].copy_from_slice(&<u32>::to_le_byte_vec(period));
self.device.set(u8::from(ColorBrickletFunction::SetColorTemperatureCallbackPeriod), payload)
}
pub fn get_color_temperature_callback_period(&self) -> ConvertingReceiver<u32> {
let payload = vec![0; 0];
self.device.get(u8::from(ColorBrickletFunction::GetColorTemperatureCallbackPeriod), payload)
}
pub fn get_identity(&self) -> ConvertingReceiver<Identity> {
let payload = vec![0; 0];
self.device.get(u8::from(ColorBrickletFunction::GetIdentity), payload)
}
}