1#![allow(dead_code)]
3
4
5pub mod communication;
6pub mod file;
7pub mod protocol;
8pub mod system;
9
10
11use std::{thread};
12use std::time::{Duration, Instant};
13
14use communication::{*};
15use communication::receiver::{*};
16use system::{*};
17use protocol::{*};
18use protocol::display::{*};
19use protocol::command::{*};
20
21
22pub struct Drone
23{
24 pub time_start: Instant, pub time_transfer: Instant, pub time_receive: Instant, pub receiver: Receiver, pub header: Header, pub vec_data: Vec<u8>, pub data: Data, pub flag_show_debug_message: bool, }
33
34
35impl Drone {
36 pub fn new() -> Drone{
37 Drone{
38 time_start: Instant::now(),
39 time_transfer: Instant::now(),
40 time_receive: Instant::now(),
41 receiver: Receiver::new(),
42 header: Header::new(),
43 vec_data: Vec::new(),
44 data: Data::None,
45 flag_show_debug_message: false,
46 }
47 }
48
49
50 pub fn set_show_debug_message(&mut self, flag_show_debug_message: bool)
51 {
52 self.flag_show_debug_message = flag_show_debug_message;
53
54 self.receiver.set_show_debug_message(flag_show_debug_message);
55 }
56
57
58 pub fn push(&mut self, b: u8)
59 {
60 if self.flag_show_debug_message
61 {
62 println!("RX: {:X?}", b);
63 }
64
65 self.receiver.push(b);
66 }
67
68
69 pub fn push_slice(&mut self, slice_data: &[u8])
70 {
71 if slice_data.len() > 0
72 {
73 if self.flag_show_debug_message
74 {
75 println!("RX: {:X?}", slice_data);
76 }
77
78 self.receiver.push_slice(slice_data);
79 }
80
81 }
82
83
84 pub fn check(&mut self) -> bool
85 {
86 if let messaging::State::Loaded = self.receiver.check()
87 {
88 self.receiver.clear();
89 self.time_receive = Instant::now();
90
91 self.header = self.receiver.get_header().clone();
92 self.vec_data = self.receiver.get_data().clone();
93 self.data = handler::check(&self.header, &self.vec_data);
94
95 return true;
96 }
97
98 return false;
99 }
100
101
102 pub fn get_time_passed_from_start(&self) -> u128
103 {
104 self.time_start.elapsed().as_millis()
105 }
106
107 pub fn get_time_passed_from_last_transfer(&self) -> u128
108 {
109 self.time_transfer.elapsed().as_millis()
110 }
111
112 pub fn get_time_passed_from_last_receive(&self) -> u128
113 {
114 self.time_receive.elapsed().as_millis()
115 }
116
117
118 pub fn sleep(&self, time_sleep_ms: u64)
119 {
120 let duration_time_sleep_ms = Duration::from_millis(time_sleep_ms);
121
122 thread::sleep(duration_time_sleep_ms);
123 }
124
125
126 pub fn request(&mut self, target: DeviceType, data_type: DataType) -> Vec<u8>
128 {
129 transfer::transfer(DataType::Request, DeviceType::Base, target, &Request{data_type}.to_vec())
130 }
131
132
133 pub fn command(&mut self, target: DeviceType, command_type: CommandType, option: u8) -> Vec<u8>
135 {
136 transfer::transfer(DataType::Command, DeviceType::Base, target, &Command{command_type, option}.to_vec())
137 }
138
139
140 pub fn flight_event(&mut self, event: FlightEvent) -> Vec<u8>
142 {
143 transfer::transfer(DataType::Command, DeviceType::Base, DeviceType::Drone, &Command{command_type: CommandType::FlightEvent, option: event.into()}.to_vec())
144 }
145
146 pub fn takeoff(&mut self) -> Vec<u8>
147 {
148 self.flight_event(FlightEvent::Takeoff)
149 }
150
151 pub fn landing(&mut self) -> Vec<u8>
152 {
153 self.flight_event(FlightEvent::Landing)
154 }
155
156 pub fn stop(&mut self) -> Vec<u8>
157 {
158 self.flight_event(FlightEvent::Stop)
159 }
160
161
162 pub fn set_default(&mut self) -> Vec<u8>
164 {
165 self.command(DeviceType::Drone, CommandType::SetDefault, 0)
166 }
167
168 pub fn set_mode_control_flight(&mut self) -> Vec<u8>
169 {
170 self.command(DeviceType::Drone, CommandType::SetDefault, 0)
171 }
172
173 pub fn headless(&mut self, headless: Headless) -> Vec<u8>
174 {
175 self.command(DeviceType::Drone, CommandType::Headless, headless.into())
176 }
177
178 pub fn clear_bias(&mut self) -> Vec<u8>
179 {
180 self.command(DeviceType::Drone, CommandType::ClearBias, 0)
181 }
182
183 pub fn clear_trim(&mut self) -> Vec<u8>
184 {
185 self.command(DeviceType::Drone, CommandType::ClearTrim, 0)
186 }
187
188 pub fn trim(&mut self, roll: i16, pitch: i16, yaw: i16, throttle: i16) -> Vec<u8>
189 {
190 transfer::transfer(DataType::Trim, DeviceType::Base, DeviceType::Drone, &sensor::Trim{roll, pitch, yaw, throttle}.to_vec())
191 }
192
193
194 pub fn control(&mut self, roll: i8, pitch: i8, yaw: i8, throttle: i8) -> Vec<u8>
196 {
197 transfer::transfer(DataType::Control, DeviceType::Base, DeviceType::Drone, &control::Quad8{roll, pitch, yaw, throttle}.to_vec())
198 }
199
200 pub fn control_request(&mut self, roll: i8, pitch: i8, yaw: i8, throttle: i8, data_type: DataType) -> Vec<u8>
201 {
202 transfer::transfer(DataType::Control, DeviceType::Base, DeviceType::Drone, &control::Quad8AndRequestData{roll, pitch, yaw, throttle, data_type}.to_vec())
203 }
204
205 pub fn control_position(&mut self, x: f32, y: f32, z: f32, velocity: f32, heading: i16, rotational_velocity: i16) -> Vec<u8>
206 {
207 transfer::transfer(DataType::Control, DeviceType::Base, DeviceType::Drone, &control::Position{x, y, z, velocity, heading, rotational_velocity}.to_vec())
208 }
209
210
211 pub fn battle_ir_message(&mut self, ir_message: u8) -> Vec<u8>
213 {
214 transfer::transfer(DataType::Battle, DeviceType::Base, DeviceType::Drone, &battle::IrMessage{ir_message}.to_vec())
215 }
216
217 pub fn battle_light_event_command(&mut self, target:DeviceType, event: u8, interval: u16, repeat: u8, r: u8, g: u8, b: u8, command_type: command::CommandType, option: u8) -> Vec<u8>
218 {
219 transfer::transfer(DataType::Battle, DeviceType::Base, target, &battle::LightEventCommand{event:light::Event{event, interval, repeat}, color: light::Color{r, g, b}, command: command::Command{command_type, option}}.to_vec())
220 }
221
222 pub fn battle_ir_message_light_event_command(&mut self, target:DeviceType, ir_message: u8, event: u8, interval: u16, repeat: u8, r: u8, g: u8, b: u8, command_type: command::CommandType, option: u8) -> Vec<u8>
223 {
224 transfer::transfer(DataType::Battle, DeviceType::Base, target, &battle::IrMessageLightEventCommand{ir_message, event:light::Event{event, interval, repeat}, color: light::Color{r, g, b}, command: command::Command{command_type, option}}.to_vec())
225 }
226
227
228 pub fn light_manual(&mut self, target:DeviceType, flags: u16, brightness: u8) -> Vec<u8>
230 {
231 transfer::transfer(DataType::LightManual, DeviceType::Base, target, &light::Manual{flags, brightness}.to_vec())
232 }
233
234 pub fn light_mode(&mut self, target:DeviceType, mode: u8, interval: u16) -> Vec<u8>
235 {
236 transfer::transfer(DataType::LightMode, DeviceType::Base, target, &light::Mode{mode, interval}.to_vec())
237 }
238
239 pub fn light_event(&mut self, target:DeviceType, event: u8, interval: u16, repeat: u8) -> Vec<u8>
240 {
241 transfer::transfer(DataType::LightEvent, DeviceType::Base, target, &light::Event{event, interval, repeat}.to_vec())
242 }
243
244 pub fn light_mode_color(&mut self, target:DeviceType, mode: u8, interval: u16, r: u8, g: u8, b: u8) -> Vec<u8>
245 {
246 transfer::transfer(DataType::LightMode, DeviceType::Base, target, &light::ModeColor{mode:light::Mode{mode, interval}, color: light::Color{r, g, b}}.to_vec())
247 }
248
249 pub fn light_event_color(&mut self, target:DeviceType, event: u8, interval: u16, repeat: u8, r: u8, g: u8, b: u8) -> Vec<u8>
250 {
251 transfer::transfer(DataType::LightEvent, DeviceType::Base, target, &light::EventColor{event:light::Event{event, interval, repeat}, color: light::Color{r, g, b}}.to_vec())
252 }
253
254 pub fn light_default(&mut self, target:DeviceType, mode: u8, interval: u16, r: u8, g: u8, b: u8) -> Vec<u8>
255 {
256 transfer::transfer(DataType::LightDefault, DeviceType::Base, target, &light::ModeColor{mode:light::Mode{mode, interval}, color: light::Color{r, g, b}}.to_vec())
257 }
258
259
260 pub fn buzzer_stop(&mut self, target: DeviceType) -> Vec<u8>
262 {
263 transfer::transfer(DataType::Buzzer, DeviceType::Base, target, &buzzer::BuzzerHz{mode: buzzer::Mode::Stop, hz:0, time:0}.to_vec())
264 }
265
266 pub fn buzzer_scale(&mut self, target: DeviceType, scale: buzzer::Scale, time: u16) -> Vec<u8>
267 {
268 transfer::transfer(DataType::Buzzer, DeviceType::Base, target, &buzzer::BuzzerScale{mode: buzzer::Mode::ScaleInstantly, scale, time}.to_vec())
269 }
270
271 pub fn buzzer_scale_reserve(&mut self, target: DeviceType, scale: buzzer::Scale, time: u16) -> Vec<u8>
272 {
273 transfer::transfer(DataType::Buzzer, DeviceType::Base, target, &buzzer::BuzzerScale{mode: buzzer::Mode::ScaleContinually, scale, time}.to_vec())
274 }
275
276 pub fn buzzer_hz(&mut self, target: DeviceType, hz: u16, time: u16) -> Vec<u8>
277 {
278 transfer::transfer(DataType::Buzzer, DeviceType::Base, target, &buzzer::BuzzerHz{mode: buzzer::Mode::HzInstantly, hz, time}.to_vec())
279 }
280
281 pub fn buzzer_hz_reserve(&mut self, target: DeviceType, hz: u16, time: u16) -> Vec<u8>
282 {
283 transfer::transfer(DataType::Buzzer, DeviceType::Base, target, &buzzer::BuzzerHz{mode: buzzer::Mode::HzContinually, hz, time}.to_vec())
284 }
285
286 pub fn buzzer_mute(&mut self, target: DeviceType, time: u16) -> Vec<u8>
287 {
288 transfer::transfer(DataType::Buzzer, DeviceType::Base, target, &buzzer::BuzzerHz{mode: buzzer::Mode::MuteInstantly, hz: 0, time}.to_vec())
289 }
290
291 pub fn buzzer_mute_reserve(&mut self, target: DeviceType, time: u16) -> Vec<u8>
292 {
293 transfer::transfer(DataType::Buzzer, DeviceType::Base, target, &buzzer::BuzzerHz{mode: buzzer::Mode::MuteContinually, hz: 0, time}.to_vec())
294 }
295
296
297 pub fn vibrator(&mut self, on: u16, off: u16, time: u16) -> Vec<u8>
299 {
300 transfer::transfer(DataType::Vibrator, DeviceType::Base, DeviceType::Controller, &vibrator::Vibrator{mode: vibrator::Mode::Instantly, on, off, time}.to_vec())
301 }
302
303 pub fn vibrator_reserve(&mut self, on: u16, off: u16, time: u16) -> Vec<u8>
304 {
305 transfer::transfer(DataType::Vibrator, DeviceType::Base, DeviceType::Controller, &vibrator::Vibrator{mode: vibrator::Mode::Continually, on, off, time}.to_vec())
306 }
307
308
309 pub fn draw_clear_all(&mut self, pixel: Pixel) -> Vec<u8>
311 {
312 transfer::transfer(DataType::DisplayClear, DeviceType::Base, DeviceType::Controller, &ClearAll{pixel}.to_vec())
313 }
314
315 pub fn draw_clear(&mut self, x: i16, y: i16, width: i16, height: i16, pixel: Pixel) -> Vec<u8>
316 {
317 transfer::transfer(DataType::DisplayClear, DeviceType::Base, DeviceType::Controller, &Clear{x, y, width, height, pixel}.to_vec())
318 }
319
320 pub fn draw_invert(&mut self, x: i16, y: i16, width: i16, height: i16) -> Vec<u8>
321 {
322 transfer::transfer(DataType::DisplayInvert, DeviceType::Base, DeviceType::Controller, &Invert{x, y, width, height}.to_vec())
323 }
324
325 pub fn draw_point(&mut self, x: i16, y: i16, pixel: Pixel) -> Vec<u8>
326 {
327 transfer::transfer(DataType::DisplayDrawPoint, DeviceType::Base, DeviceType::Controller, &DrawPoint{x, y, pixel}.to_vec())
328 }
329
330 pub fn draw_line(&mut self, x1: i16, y1: i16, x2: i16, y2: i16, pixel: Pixel, line: Line) -> Vec<u8>
331 {
332 transfer::transfer(DataType::DisplayDrawLine, DeviceType::Base, DeviceType::Controller, &DrawLine{x1, y1, x2, y2, pixel, line}.to_vec())
333 }
334
335 pub fn draw_rect(&mut self, x: i16, y: i16, width: i16, height: i16, pixel: Pixel, fill: bool, line: Line) -> Vec<u8>
336 {
337 transfer::transfer(DataType::DisplayDrawRect, DeviceType::Base, DeviceType::Controller, &DrawRect{x, y, width, height, pixel, fill, line}.to_vec())
338 }
339
340 pub fn draw_circle(&mut self, x: i16, y: i16, radius: i16, pixel: Pixel, fill: bool) -> Vec<u8>
341 {
342 transfer::transfer(DataType::DisplayDrawCircle, DeviceType::Base, DeviceType::Controller, &DrawCircle{x, y, radius, pixel, fill}.to_vec())
343 }
344
345 pub fn draw_string(&mut self, x: i16, y: i16, font: Font, pixel: Pixel, string: String) -> Vec<u8>
346 {
347 transfer::transfer(DataType::DisplayDrawString, DeviceType::Base, DeviceType::Controller, &DrawString{x, y, font, pixel, string}.to_vec())
348 }
349
350 pub fn draw_string_align(&mut self, x_start: i16, x_end: i16, y: i16, align: Align, font: Font, pixel: Pixel, string: String) -> Vec<u8>
351 {
352 transfer::transfer(DataType::DisplayDrawStringAlign, DeviceType::Base, DeviceType::Controller, &DrawStringAlign{x_start, x_end, y, align, font, pixel, string}.to_vec())
353 }
354
355 pub fn draw_image(&mut self, x: i16, y: i16, width: i16, height: i16, vec_image: Vec<u8>) -> Vec<u8>
356 {
357 transfer::transfer(DataType::DisplayDrawImage, DeviceType::Base, DeviceType::Controller, &DrawImage{x, y, width, height, vec_image}.to_vec())
358 }
359}
360
361
362
363#[cfg(test)]
364mod tests {
365 #[test]
366 fn it_works() {
367 assert_eq!(2 + 2, 4);
368 }
369}