ble_ledly/communication_protocol/
generic_rgb.rs

1use crate::{
2    capability::{
3        brightness::BrightnessOption,
4        color::ColorOption,
5        hw_animate::{
6            HWAnimateOption, HWAnimationSpeedSetting, HWStaticColorOption,
7        },
8        light::LightOption,
9    },
10    communication_protocol::Protocol,
11};
12
13pub struct GenericRGB {}
14
15impl Default for GenericRGB {
16    fn default() -> Self {
17        Self {}
18    }
19}
20
21
22impl Protocol for GenericRGB {
23    // Light //
24    fn light(&self, option: &LightOption) -> Vec<u8> {
25        // this doesn't work when
26        // HWspecific effects are turn_on
27        // use legacy mode instead
28        match option {
29            LightOption::On => vec![0xcc, 0x23, 0x33],
30            LightOption::Off => vec![0xcc, 0x24, 0x33],
31        }
32    }
33
34    fn color(&self, option: &ColorOption) -> Vec<u8> {
35        match option {
36            ColorOption::RGB(r, g, b) => vec![0x56, *r, *g, *b, 0x00, 0xF0, 0xAA],
37        }
38    }
39
40    fn brightness(&self, option: &BrightnessOption) -> Vec<u8> {
41        match option {
42            BrightnessOption::Level(_level) => {
43                unimplemented!("Brightness without ColorOption, not supported yet")
44            }
45            BrightnessOption::LevelWithColor(level, color) => match color {
46                ColorOption::RGB(r, g, b) => {
47                    self.color(&ColorOption::RGB((*r as f32 * level) as u8, (*g as f32 * level) as u8, (*b as f32 * level) as u8))
48                }
49            },
50        }
51    }
52    //-----------//
53    // HWAnimate //
54    //-----------//
55    fn hw_animate(&self, option: &HWAnimateOption) -> Vec<u8> {
56        match option {
57            HWAnimateOption::Pulsating(color, speed) => {
58                vec![
59                    0xBB,
60                    GenericRGB::_static_color(&color),
61                    GenericRGB::_animation_speed(&speed),
62                    0x44,
63                ]
64            }
65        }
66    }
67
68    fn _animation_speed(setting: &HWAnimationSpeedSetting) -> u8 {
69        match setting {
70            HWAnimationSpeedSetting::Speed1 => 0x1F,
71            HWAnimationSpeedSetting::Speed2 => 0x1B,
72            HWAnimationSpeedSetting::Speed3 => 0x1A,
73            HWAnimationSpeedSetting::Speed4 => 0x17,
74            HWAnimationSpeedSetting::Speed5 => 0x13,
75            HWAnimationSpeedSetting::Speed6 => 0x10,
76            HWAnimationSpeedSetting::Speed7 => 0x0C,
77            HWAnimationSpeedSetting::Speed8 => 0x05,
78            HWAnimationSpeedSetting::Speed9 => 0x01,
79        }
80    }
81
82    fn _static_color(color: &HWStaticColorOption) -> u8 {
83        match color {
84            HWStaticColorOption::Red => 0x26,
85            HWStaticColorOption::Green => 0x27,
86            HWStaticColorOption::Blue => 0x28,
87        }
88    }
89}