phoenix_rec/
data_types.rs

1use serde::{Deserialize, Serialize};
2use strum_macros::FromRepr;
3
4#[derive(Debug, Serialize, Deserialize, PartialEq, FromRepr, Clone, Copy)]
5// todo: make macros for the write and from_string functions
6#[repr(u8)]
7pub enum DataType {
8    // length of data that should be there
9    // todo: fix this, its a giant mess
10    None(u8),
11    /// right, left
12    Color(i16, i16),
13    // todo: track the whole distance in the same way as DrivenDistance does, or scrap this data type
14    Distance(i16),
15    /// right, left
16    CalcSpeed(i16, i16),
17    /// right, left
18    SyncSpeed(i16, i16),
19    /// right, left
20    RealSpeeds(i16, i16),
21    /// right, left
22    DrivenDistance(f32, f32),
23    SyncError(f32),
24    /// right, left
25    Correction(f32, f32),
26    /// right, left
27    AverageSpeed(f32, f32),
28    /// right, left
29    RGB((i16, i16, i16), (i16, i16, i16)),
30    /// cur_speed, target_speed
31    CurTarSpeeds(i16, i16),
32    InstantDerivative(f32)
33    // todo: add custom data type for the user to use and define
34}
35
36impl DataType {
37    pub fn write(&self) -> String {
38        match self {
39            // maybe we should use something like "n" instead of "null" to save space
40            DataType::None(u8) => vec!["null".to_string(); Self::get_none(*u8) as usize].join(", "),
41            DataType::Color(r, l) => format!("{}, {}", r, l),
42            DataType::Distance(d) => format!("{}", d),
43            DataType::CalcSpeed(r, l) => format!("{}, {}", r, l),
44            DataType::SyncSpeed(r, l) => format!("{}, {}", r, l),
45            DataType::RealSpeeds(r, l) => format!("{}, {}", r, l),
46            DataType::DrivenDistance(r, l) => format!("{}, {}", r, l),
47            DataType::SyncError(e) => format!("{}", e),
48            DataType::Correction(r, l) => format!("{}, {}", r, l),
49            DataType::AverageSpeed(r, l) => format!("{}, {}", r, l),
50            DataType::RGB((r, g, b), (r1, g1, b1)) => {
51                format!("{}, {}, {}, {}, {}, {}", r, g, b, r1, g1, b1)
52            }
53            DataType::CurTarSpeeds(c, t) => format!("{}, {}", c, t),
54            DataType::InstantDerivative(d) => format!("{}", d)
55        }
56    }
57
58    pub fn get_none(i: u8) -> u8 {
59        match i {
60            0 => 0,
61            1 => 2,
62            2 => 1,
63            3 => 2,
64            4 => 2,
65            5 => 2,
66            6 => 2,
67            7 => 1,
68            8 => 2,
69            9 => 2,
70            10 => 6,
71            11 => 2,
72            12 => 1,
73            _ => panic!("Unknown data type: {}", i),
74        }
75    }
76
77    pub fn none(&self) -> u8 {
78        match self {
79            DataType::None(u8) => *u8,
80            DataType::Color(_, _) => 2,
81            DataType::Distance(_) => 1,
82            DataType::CalcSpeed(_, _) => 2,
83            DataType::SyncSpeed(_, _) => 2,
84            DataType::RealSpeeds(_, _) => 2,
85            DataType::DrivenDistance(_, _) => 2,
86            DataType::SyncError(_) => 1,
87            DataType::Correction(_, _) => 2,
88            DataType::AverageSpeed(_, _) => 2,
89            DataType::RGB(_, _) => 6,
90            DataType::CurTarSpeeds(_, _) => 2,
91            DataType::InstantDerivative(_) => 1
92        }
93    }
94
95    pub fn to_u8(&self) -> u8 {
96        unsafe { (self as *const DataType as *const u8).read() }
97    }
98
99    /// This converts a string in this format "1, 30, 30" to a DataType::Color(30, 30)
100    pub fn from_string(s: String) -> DataType {
101        let mut parts = s.split(", ");
102        let ty = parts.next().unwrap();
103        let data = match DataType::from_repr(ty.parse::<u8>().unwrap()) {
104            Some(d) => d,
105            None => panic!("Unknown data type: {}", ty),
106        };
107        macro_rules! ty1 {
108            ($name:ident, $ty:ty) => {{
109                let r = parts.next().unwrap().parse::<$ty>().unwrap();
110                DataType::$name(r)
111            }};
112        }
113        macro_rules! ty2 {
114            ($name:ident, $ty:ty) => {{
115                let r = parts.next().unwrap().parse::<$ty>().unwrap();
116                let l = parts.next().unwrap().parse::<$ty>().unwrap();
117                DataType::$name(r, l)
118            }};
119        }
120        match data {
121            DataType::None(_) => DataType::None(0),
122            DataType::Color(_, _) => ty2!(Color, i16),
123            DataType::Distance(_) => ty1!(Distance, i16),
124            DataType::CalcSpeed(_, _) => ty2!(CalcSpeed, i16),
125            DataType::SyncSpeed(_, _) => ty2!(SyncSpeed, i16),
126            DataType::RealSpeeds(_, _) => ty2!(RealSpeeds, i16),
127            DataType::DrivenDistance(_, _) => ty2!(DrivenDistance, f32),
128            DataType::SyncError(_) => ty1!(SyncError, f32),
129            DataType::Correction(_, _) => ty2!(Correction, f32),
130            DataType::AverageSpeed(_, _) => ty2!(AverageSpeed, f32),
131            DataType::RGB(_, _) => {
132                let r = parts.next().unwrap().parse::<i16>().unwrap();
133                let g = parts.next().unwrap().parse::<i16>().unwrap();
134                let b = parts.next().unwrap().parse::<i16>().unwrap();
135                let r1 = parts.next().unwrap().parse::<i16>().unwrap();
136                let g1 = parts.next().unwrap().parse::<i16>().unwrap();
137                let b1 = parts.next().unwrap().parse::<i16>().unwrap();
138                DataType::RGB((r, g, b), (r1, g1, b1))
139            }
140            DataType::CurTarSpeeds(_, _) => ty2!(CurTarSpeeds, i16),
141            DataType::InstantDerivative(_) => ty1!(InstantDerivative, f32)
142        }
143    }
144
145    /// Writes the description of the data type like this: for Color: "right color, left color"
146    pub fn write_description(&self) -> String {
147        match self {
148            DataType::None(_) => String::new(),
149            DataType::Color(_, _) => "right color, left color".to_string(),
150            DataType::Distance(_) => "dist".to_string(),
151            DataType::CalcSpeed(_, _) => "right calculated v, left calculated v".to_string(),
152            DataType::SyncSpeed(_, _) => "right synced v, left synced v".to_string(),
153            DataType::RealSpeeds(_, _) => "right real v, left real v".to_string(),
154            DataType::DrivenDistance(_, _) => "right distance, left distance".to_string(),
155            DataType::SyncError(_) => "sync error".to_string(),
156            DataType::Correction(_, _) => "right correction, left correction".to_string(),
157            DataType::AverageSpeed(_, _) => "right average speed, left average speed".to_string(),
158            DataType::RGB(_, _) => "right r, right g, right b, left r, left g, left b".to_string(),
159            DataType::CurTarSpeeds(_, _) => "current speed, target speed".to_string(),
160            DataType::InstantDerivative(_) => "instant derivative".to_string()
161        }
162    }
163}