1use crate::{ encode, Command, DataType, Encoding::*, ValError };
7use crate::utils::ChainByte;
8
9pub enum LedColor {
11 Red,
12 Orange,
13 Green,
14 Off
15}
16
17pub enum LedEffect {
19 Static,
20 Blink,
21 Pulse
22}
23
24pub fn motor_speed(port: u8, speed: i8, layer: u8) -> Result<Vec<u8>, ValError> {
26 if port > 15 { return Err(ValError::InvalidRange(port as i32, 0, 15)); }
27 if !(-100..=100).contains(&speed) { return Err(ValError::InvalidRange(speed as i32, -100, 100)) }
28 if layer > 3 { return Err(ValError::InvalidRange(layer as i32, 0, 3)) }
29 let mut byte = ChainByte::new();
30 byte.push(0xA5)
31 .add(encode(LC0(layer as i8))?)
32 .add(encode(LC0(port as i8))?)
33 .add(encode(LC1(speed))?)
34 .push(0xA6)
35 .add(encode(LC0(layer as i8))?)
36 .add(encode(LC0(port as i8))?);
37 Ok(byte.bytes)
38}
39
40pub fn stop_motor(port: u16, layer: u8, hard: bool) -> Result<Vec<u8>, ValError> {
42 if port > 15 { return Err(ValError::InvalidRange(port as i32, 0, 15)); }
43 if layer > 3 { return Err(ValError::InvalidRange(layer as i32, 0, 3)); }
44 let mut byte = ChainByte::new();
45 byte.push(0xA3)
46 .add(encode(LC0(layer as i8))?)
47 .add(encode(LC0(port as i8))?)
48 .add(encode(LC0(match hard {
49 true => 1, false => 0
50 }))?);
51 Ok(byte.bytes)
52}
53
54pub fn battery_percentage(cmd: &mut Command) -> Result<(Vec<u8>, Vec<DataType>), ValError> {
57 let mut byte = ChainByte::new();
58 byte.add(vec![0x81, 0x12])
59 .add(cmd.allocate(DataType::DATA8, true)?);
60 Ok((byte.bytes, vec![DataType::DATA8]))
61}
62
63pub fn show_led(color: LedColor, effect: LedEffect) -> Vec<u8> {
65 let mut byte = ChainByte::new();
66 byte.add(vec![0x82, 0x1B]);
67 if let LedColor::Off = color {
68 byte.add(encode(LC0(0)).unwrap());
69 }
70 let mut code: i8 = 0;
71 code += match color {
72 LedColor::Green => 1,
73 LedColor::Red => 2,
74 LedColor::Orange => 3,
75 _ => 0
76 };
77 code += match effect {
78 LedEffect::Static => 0,
79 LedEffect::Blink => 3,
80 LedEffect::Pulse => 6
81 };
82 byte.add(encode(LC0(code)).unwrap());
83 byte.bytes
84}
85
86