e_drone/protocol/
motor.rs

1use crate::protocol::{*};
2use crate::communication::extractor::Extractor;
3
4
5// -- MotorV -----------------------------------------------------------------------------------------------
6#[derive(Debug, Copy, Clone)]
7pub struct MotorV {
8    pub value: i16,
9}
10
11
12impl MotorV {
13    pub fn new() -> MotorV{
14        MotorV {
15            value: 0,
16        }
17    }
18
19
20    pub const fn size() -> usize { 2 }
21
22
23    pub fn parse(slice_data: &[u8]) -> Result<MotorV, &'static str> {
24        if slice_data.len() == MotorV::size() {
25            let mut ext: Extractor = Extractor::from_slice(slice_data);
26            Ok(MotorV{
27                value: ext.get_i16(),
28            })
29        }
30        else { Err("Wrong length") }
31    }
32}
33
34
35impl Serializable for MotorV {
36    fn to_vec(&self) -> Vec<u8> {
37        let mut vec_data : Vec<u8> = Vec::new();
38
39        vec_data.extend_from_slice(&self.value.to_le_bytes());
40
41        vec_data
42    }
43}
44
45
46// -- MotorRV -----------------------------------------------------------------------------------------------
47#[derive(Debug, Copy, Clone)]
48pub struct MotorRV {
49    pub rotation: Rotation,
50    pub value: i16,
51}
52
53
54impl MotorRV {
55    pub fn new() -> MotorRV{
56        MotorRV {
57            rotation: Rotation::Clockwise,
58            value: 0,
59        }
60    }
61
62
63    pub const fn size() -> usize { 3 }
64
65
66    pub fn parse(slice_data: &[u8]) -> Result<MotorRV, &'static str> {
67        if slice_data.len() == MotorRV::size() {
68            let mut ext: Extractor = Extractor::from_slice(slice_data);
69            Ok(MotorRV{
70                rotation: Rotation::from_u8(ext.get_u8()),
71                value: ext.get_i16(),
72            })
73        }
74        else { Err("Wrong length") }
75    }
76}
77
78
79impl Serializable for MotorRV {
80    fn to_vec(&self) -> Vec<u8> {
81        let mut vec_data : Vec<u8> = Vec::new();
82
83        vec_data.push(self.rotation.into());
84        vec_data.extend_from_slice(&self.value.to_le_bytes());
85
86        vec_data
87    }
88}
89
90
91// -- MotorVA -----------------------------------------------------------------------------------------------
92#[derive(Debug, Copy, Clone)]
93pub struct MotorVA {
94    pub value: i16,
95    pub adc: i16,
96}
97
98
99impl MotorVA {
100    pub fn new() -> MotorVA{
101        MotorVA {
102            value: 0,
103            adc: 0,
104        }
105    }
106
107
108    pub const fn size() -> usize { 4 }
109
110
111    pub fn parse(slice_data: &[u8]) -> Result<MotorVA, &'static str> {
112        if slice_data.len() == MotorVA::size() {
113            let mut ext: Extractor = Extractor::from_slice(slice_data);
114            Ok(MotorVA{
115                value: ext.get_i16(),
116                adc: ext.get_i16(),
117            })
118        }
119        else { Err("Wrong length") }
120    }
121}
122
123
124impl Serializable for MotorVA {
125    fn to_vec(&self) -> Vec<u8> {
126        let mut vec_data : Vec<u8> = Vec::new();
127
128        vec_data.extend_from_slice(&self.value.to_le_bytes());
129        vec_data.extend_from_slice(&self.adc.to_le_bytes());
130
131        vec_data
132    }
133}
134
135
136// -- MotorRVA -----------------------------------------------------------------------------------------------
137#[derive(Debug, Copy, Clone)]
138pub struct MotorRVA {
139    pub rotation: Rotation,
140    pub value: i16,
141    pub adc: i16,
142}
143
144
145impl MotorRVA {
146    pub fn new() -> MotorRVA{
147        MotorRVA {
148            rotation: Rotation::Clockwise,
149            value: 0,
150            adc: 0,
151        }
152    }
153
154
155    pub const fn size() -> usize { 5 }
156
157
158    pub fn parse(slice_data: &[u8]) -> Result<MotorRVA, &'static str> {
159        if slice_data.len() == MotorRVA::size() {
160            let mut ext: Extractor = Extractor::from_slice(slice_data);
161            Ok(MotorRVA{
162                rotation: Rotation::from_u8(ext.get_u8()),
163                value: ext.get_i16(),
164                adc: ext.get_i16(),
165            })
166        }
167        else { Err("Wrong length") }
168    }
169}
170
171
172impl Serializable for MotorRVA {
173    fn to_vec(&self) -> Vec<u8> {
174        let mut vec_data : Vec<u8> = Vec::new();
175
176        vec_data.push(self.rotation.into());
177        vec_data.extend_from_slice(&self.value.to_le_bytes());
178        vec_data.extend_from_slice(&self.adc.to_le_bytes());
179
180        vec_data
181    }
182}
183
184
185
186// -- MotorSingleV -----------------------------------------------------------------------------------------------
187#[derive(Debug, Copy, Clone)]
188pub struct MotorSingleV {
189    pub target: u8,
190    pub value: i16,
191}
192
193
194impl MotorSingleV {
195    pub fn new() -> MotorSingleV{
196        MotorSingleV {
197            target: 0,
198            value: 0,
199        }
200    }
201
202
203    pub const fn size() -> usize { 3 }
204
205
206    pub fn parse(slice_data: &[u8]) -> Result<MotorSingleV, &'static str> {
207        if slice_data.len() == MotorSingleV::size() {
208            let mut ext: Extractor = Extractor::from_slice(slice_data);
209            Ok(MotorSingleV{
210                target: ext.get_u8(),
211                value: ext.get_i16(),
212            })
213        }
214        else { Err("Wrong length") }
215    }
216}
217
218
219impl Serializable for MotorSingleV {
220    fn to_vec(&self) -> Vec<u8> {
221        let mut vec_data : Vec<u8> = Vec::new();
222
223        vec_data.extend_from_slice(&self.target.to_le_bytes());
224        vec_data.extend_from_slice(&self.value.to_le_bytes());
225
226        vec_data
227    }
228}
229
230
231// -- MotorSingleRV -----------------------------------------------------------------------------------------------
232#[derive(Debug, Copy, Clone)]
233pub struct MotorSingleRV {
234    pub target: u8,
235    pub rotation: Rotation,
236    pub value: i16,
237}
238
239
240impl MotorSingleRV {
241    pub fn new() -> MotorSingleRV{
242        MotorSingleRV {
243            target: 0,
244            rotation: Rotation::Clockwise,
245            value: 0,
246        }
247    }
248
249
250    pub const fn size() -> usize { 4 }
251
252
253    pub fn parse(slice_data: &[u8]) -> Result<MotorSingleRV, &'static str> {
254        if slice_data.len() == MotorSingleRV::size() {
255            let mut ext: Extractor = Extractor::from_slice(slice_data);
256            Ok(MotorSingleRV{
257                target: ext.get_u8(),
258                rotation: Rotation::from_u8(ext.get_u8()),
259                value: ext.get_i16(),
260            })
261        }
262        else { Err("Wrong length") }
263    }
264}
265
266
267impl Serializable for MotorSingleRV {
268    fn to_vec(&self) -> Vec<u8> {
269        let mut vec_data : Vec<u8> = Vec::new();
270
271        vec_data.extend_from_slice(&self.target.to_le_bytes());
272        vec_data.push(self.rotation.into());
273        vec_data.extend_from_slice(&self.value.to_le_bytes());
274
275        vec_data
276    }
277}