use num_enum::IntoPrimitive;
use num_enum::TryFromPrimitive;
use std::convert::TryFrom;
use byteorder::{ByteOrder, LittleEndian};
#[derive(Clone, Copy, Debug, Eq, PartialEq, IntoPrimitive, TryFromPrimitive)]
#[repr(u32)]
pub enum ModelNumber {
None = 0x00000000,
Drone3DroneP1 = 0x00031001,
Drone3DroneP2 = 0x00031002,
Drone3DroneP3 = 0x00031003,
Drone3DroneP4 = 0x00031004,
Drone3DroneP5 = 0x00031005,
Drone3DroneP6 = 0x00031006,
Drone3DroneP7 = 0x00031007,
Drone3DroneP8 = 0x00031008,
Drone3DroneP9 = 0x00031009,
Drone3DroneP10 = 0x0003100A,
Drone3ControllerP1 = 0x00032001,
Drone3ControllerP2 = 0x00032002,
Drone3ControllerP3 = 0x00032003,
Drone3ControllerP4 = 0x00032004,
Drone3ControllerP5 = 0x00032005,
Drone3LinkP0 = 0x00033000,
Drone3TesterP4 = 0x0003A004,
Drone3TesterP6 = 0x0003A006,
Drone3TesterP7 = 0x0003A007,
Drone4DroneP5 = 0x00041005,
Drone4DroneP6 = 0x00041006,
Drone4DroneP7 = 0x00041007,
Drone4ControllerP1 = 0x00042001,
Drone4ControllerP2 = 0x00042002,
Drone4ControllerP3 = 0x00042003,
Drone4ControllerP4 = 0x00042004,
Drone4LinkP0 = 0x00043000,
Drone4TesterP6 = 0x0004A006,
Drone4TesterP7 = 0x0004A007,
Drone7DroneP2 = 0x00071002,
Drone7BleClientP0 = 0x00073200,
Drone7BleClientP5 = 0x00073205,
Drone7BleServerP2 = 0x00073302,
Drone7TesterP5 = 0x0007A005,
Drone7TesterP6 = 0x0007A006,
Drone7MonitorP4 = 0x0007A104,
Drone7MonitorP5 = 0x0007A105,
Drone8DroneP0 = 0x00081000,
Drone8DroneP1 = 0x00081001,
Drone8TesterP6 = 0x0008A006,
Drone8MonitorP6 = 0x0008A106,
Drone9DroneP0 = 0x00091000,
Drone9DroneP1 = 0x00091001,
Drone9DroneP2 = 0x00091002,
Drone9TesterP6 = 0x0009A006,
}
impl ModelNumber {
pub fn from_u32(data_u32: u32) -> ModelNumber {
match ModelNumber::try_from( data_u32 ) {
Ok(data) => { data },
_ => { ModelNumber::None },
}
}
pub fn to_array(&self) -> [u8; 4] {
let mut buf = [0; 4];
LittleEndian::write_u32(&mut buf, self.clone().into());
buf
}
pub fn get_device_type(&self) -> DeviceType {
DeviceType::from_u8(self.to_array()[1])
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, IntoPrimitive, TryFromPrimitive)]
#[repr(u8)]
pub enum DeviceType {
None = 0x00,
Drone = 0x10,
Controller = 0x20,
LinkClient = 0x30,
LinkServer = 0x31,
BleClient = 0x32,
BleServer = 0x33,
Range = 0x40,
Base = 0x70,
ByScratch = 0x80,
Scratch = 0x81,
Entry = 0x82,
Tester = 0xA0,
Monitor = 0xA1,
Updater = 0xA2,
Encrypter = 0xA3,
Whispering = 0xFE,
Broadcasting = 0xFF,
}
impl DeviceType {
pub fn from_u8(data_u8: u8) -> DeviceType {
match DeviceType::try_from( data_u8 ) {
Ok(data) => { data },
_ => { DeviceType::None },
}
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, IntoPrimitive, TryFromPrimitive)]
#[repr(u8)]
pub enum ModeSystem {
None = 0x00,
Boot = 0x10,
Start = 0x11,
Running = 0x12,
ReadyToReset = 0x13,
Error = 0xA0,
}
impl ModeSystem {
pub fn from_u8(data_u8: u8) -> ModeSystem {
match ModeSystem::try_from( data_u8 ) {
Ok(data) => { data },
_ => { ModeSystem::None },
}
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, IntoPrimitive, TryFromPrimitive)]
#[repr(u8)]
pub enum ModeControlFlight {
None = 0x00,
Attitude = 0x10,
Position = 0x11,
Manual = 0x12,
Rate = 0x13,
Function = 0x14,
Error = 0xA0,
}
impl ModeControlFlight {
pub fn from_u8(data_u8: u8) -> ModeControlFlight {
match ModeControlFlight::try_from( data_u8 ) {
Ok(data) => { data },
_ => { ModeControlFlight::None },
}
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, IntoPrimitive, TryFromPrimitive)]
#[repr(u8)]
pub enum ModeFlight {
None = 0x00,
Ready = 0x10,
Start = 0x11,
TakeOff = 0x12,
Flight = 0x13,
Landing = 0x14,
Flip = 0x15,
Reverse = 0x16,
Stop = 0x20,
Accident = 0x30,
Error = 0x31,
Test = 0x40,
}
impl ModeFlight {
pub fn from_u8(data_u8: u8) -> ModeFlight {
match ModeFlight::try_from( data_u8 ) {
Ok(data) => { data },
_ => { ModeFlight::None },
}
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, IntoPrimitive, TryFromPrimitive)]
#[repr(u8)]
pub enum ModeMovement {
None = 0x00,
Ready = 0x01,
Hovering = 0x02,
Moving = 0x03,
ReturnHome = 0x04
}
impl ModeMovement {
pub fn from_u8(data_u8: u8) -> ModeMovement {
match ModeMovement::try_from( data_u8 ) {
Ok(data) => { data },
_ => { ModeMovement::None },
}
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, IntoPrimitive, TryFromPrimitive)]
#[repr(u8)]
pub enum ModeUpdate {
None = 0x00,
Ready = 0x01,
Update = 0x02,
Complete = 0x03,
Failed = 0x04,
NotAvailable = 0x05,
RunApplication = 0x06,
NotRegistered = 0x07,
EndOfType = 0x08,
}
impl ModeUpdate {
pub fn from_u8(data_u8: u8) -> ModeUpdate {
match ModeUpdate::try_from( data_u8 ) {
Ok(data) => { data },
_ => { ModeUpdate::None },
}
}
}
#[derive(Debug, Copy, Clone)]
pub struct Version {
pub build: u16,
pub minor: u8,
pub major: u8,
}
impl Version {
pub fn from_slice(data_array: &[u8]) -> Version {
if data_array.len() == 4 {
Version {
build: LittleEndian::read_u16(&data_array[0..2]),
minor: data_array[2],
major: data_array[3],
}
}
else {
Version {
build: 1,
minor: 1,
major: 21,
}
}
}
pub fn from_u32(version: u32) -> Version {
Version {
build: (version & 0xFFFF) as u16,
minor: ((version >> 16) & 0xFF) as u8,
major: ((version >> 24) & 0xFF) as u8,
}
}
pub fn to_array(&self) -> [u8; 4] {
let mut buf = [0; 4];
LittleEndian::write_u32(&mut buf, self.to_u32());
buf
}
pub fn to_u32(&self) -> u32 {
((self.major as u32) << 24) | ((self.minor as u32) << 16) | self.build as u32
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, IntoPrimitive, TryFromPrimitive)]
#[repr(u32)]
pub enum ErrorFlagsForSensor {
None = 0x00000000,
MotionNoAnswer = 0x00000001,
MotionWrongValue = 0x00000002,
MotionNotCalibrated = 0x00000004,
MotionCalibrating = 0x00000008,
PressureNoAnswer = 0x00000010,
PressureWrongValue = 0x00000020,
RangeGroundNoAnswer = 0x00000100,
RangeGroundWrongValue = 0x00000200,
FlowNoAnswer = 0x00001000,
FlowWrongValue = 0x00002000,
FlowCannotRecognizeGroundImage = 0x00004000,
}
impl ErrorFlagsForSensor {
pub fn from_u8(data_u32: u32) -> ErrorFlagsForSensor {
match ErrorFlagsForSensor::try_from( data_u32 ) {
Ok(data) => { data },
_ => { ErrorFlagsForSensor::None },
}
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, IntoPrimitive, TryFromPrimitive)]
#[repr(u32)]
pub enum ErrorFlagsForState {
None = 0x00000000,
NotRegistered = 0x00000001,
FlashReadLockUnLocked = 0x00000002,
BootloaderWriteLockUnLocked = 0x00000004,
LowBattery = 0x00000008,
TakeoffFailureCheckPropellerAndMotor = 0x00000010,
CheckPropellerVibration = 0x00000020,
AttitudeNotStable = 0x00000040,
CanNotFlipLowBattery = 0x00000100,
CanNotFlipTooHeavy = 0x00000200,
}
impl ErrorFlagsForState {
pub fn from_u8(data_u32: u32) -> ErrorFlagsForState {
match ErrorFlagsForState::try_from( data_u32 ) {
Ok(data) => { data },
_ => { ErrorFlagsForState::None },
}
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, IntoPrimitive, TryFromPrimitive)]
#[repr(u8)]
pub enum Direction {
None = 0,
Left,
Front,
Right,
Rear,
Top,
Bottom,
Center,
}
impl Direction {
pub fn from_u8(data_u8: u8) -> Direction {
match Direction::try_from( data_u8 ) {
Ok(data) => { data },
_ => { Direction::None },
}
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, IntoPrimitive, TryFromPrimitive)]
#[repr(u8)]
pub enum Rotation {
None = 0x00,
Clockwise = 0x01,
Counterclockwise = 0x02,
}
impl Rotation {
pub fn from_u8(data_u8: u8) -> Rotation {
match Rotation::try_from( data_u8 ) {
Ok(data) => { data },
_ => { Rotation::None },
}
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, IntoPrimitive, TryFromPrimitive)]
#[repr(u8)]
pub enum FlightEvent {
None = 0x00,
Stop = 0x10,
Takeoff = 0x11,
Landing = 0x12,
Reverse = 0x14,
FlipFront = 0x15,
FlipRear = 0x16,
FlipLeft = 0x17,
FlipRight = 0x18,
Return = 0x19,
Shot = 0x90,
UnderAttack = 0x91,
ResetHeading = 0xA0,
}
impl FlightEvent {
pub fn from_u8(data_u8: u8) -> FlightEvent {
match FlightEvent::try_from( data_u8 ) {
Ok(data) => { data },
_ => { FlightEvent::None },
}
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, IntoPrimitive, TryFromPrimitive)]
#[repr(u8)]
pub enum SensorOrientation {
None = 0,
Normal,
ReverseStart,
Reversed,
}
impl SensorOrientation {
pub fn from_u8(data_u8: u8) -> SensorOrientation {
match SensorOrientation::try_from( data_u8 ) {
Ok(data) => { data },
_ => { SensorOrientation::None },
}
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, IntoPrimitive, TryFromPrimitive)]
#[repr(u8)]
pub enum Headless {
None = 0,
Headless,
Normal,
}
impl Headless {
pub fn from_u8(data_u8: u8) -> Headless {
match Headless::try_from( data_u8 ) {
Ok(data) => { data },
_ => { Headless::None },
}
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, IntoPrimitive, TryFromPrimitive)]
#[repr(u8)]
pub enum TrimDirection {
None = 0,
RollIncrease,
RollDecrease,
PitchIncrease,
PitchDecrease,
YawIncrease,
YawDecrease,
ThrottleIncrease,
ThrottleDecrease,
Reset,
}
impl TrimDirection {
pub fn from_u8(data_u8: u8) -> TrimDirection {
match TrimDirection::try_from( data_u8 ) {
Ok(data) => { data },
_ => { TrimDirection::None },
}
}
}