Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More BufferIDs & scroll (de)serializatoin #7

Merged
merged 16 commits into from
May 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions arsdk-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ keywords = ["drone", "parrot"]

[dependencies]
anyhow = "1.0"
thiserror = "1.0"
pnet = "0.25"
serde = {version = "1.0", features = ["derive"]}
serde_json = "1.0"
dashmap = "3.11"
chrono = "0.4"
scroll = "0.10"
56 changes: 47 additions & 9 deletions arsdk-rs/src/ardrone3.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use crate::frame::Data;

/// eARCOMMANDS_ID_ARDRONE3_PILOTING_CMD
#[repr(u8)]
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum ArDrone3 {
/// ARCOMMANDS_ID_ARDRONE3_PILOTING_CMD_FLATTRIM = 0
Expand Down Expand Up @@ -29,17 +26,58 @@ pub enum ArDrone3 {
/// ARCOMMANDS_ID_ARDRONE3_PILOTING_CMD_CANCELMOVETO = 11
CancelMoveTo = 11,
/// ARCOMMANDS_ID_ARDRONE3_PILOTING_CMD_STARTPILOTEDPOI = 12
StartPilotdPOI = 12,
StartPilotedPOI = 12,
/// ARCOMMANDS_ID_ARDRONE3_PILOTING_CMD_STOPPILOTEDPOI = 13
StopPilotedPOI = 13,
}

impl Data for ArDrone3 {
fn serialize(&self) -> Vec<u8> {
// todo: Fix this hardcoded value
pub mod scroll_impl {
use super::*;
use crate::MessageError;
use scroll::{ctx, Endian, Pread, Pwrite};

impl<'a> ctx::TryFromCtx<'a, Endian> for ArDrone3 {
type Error = MessageError;

// and the lifetime annotation on `&'a [u8]` here
fn try_from_ctx(src: &'a [u8], endian: Endian) -> Result<(Self, usize), Self::Error> {
use ArDrone3::*;

let offset = &mut 0;

let ardrone3 = match src.gread_with::<u16>(offset, endian)? {
0 => FlatTrim,
1 => TakeOff,
2 => PCMD,
3 => Landing,
4 => Emergency,
5 => NavigateHome,
6 => AutoTakeOffMode,
7 => MoveBy,
8 => UserTakeOff,
9 => Circle,
10 => MoveTo,
11 => CancelMoveTo,
12 => StartPilotedPOI,
13 => StopPilotedPOI,
value => {
return Err(MessageError::OutOfBound {
value: value.into(),
param: "ArDrone3".to_string(),
})
}
};

Ok((ardrone3, *offset))
}
}

let take_off: u16 = 1;
impl<'a> ctx::TryIntoCtx<Endian> for ArDrone3 {
type Error = MessageError;

take_off.to_le_bytes().to_vec()
fn try_into_ctx(self, this: &mut [u8], ctx: Endian) -> Result<usize, Self::Error> {
// TODO: Fix when we have more options
Ok(this.pwrite_with::<u16>(self as u16, 0, ctx)?)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
}
}
93 changes: 76 additions & 17 deletions arsdk-rs/src/command.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use crate::ardrone3::ArDrone3;
use crate::common;
use crate::frame::Data;
use crate::jumping_sumo;
#[derive(Debug, PartialEq, Clone, Copy)]

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum Feature {
Common(common::Class), // ARCOMMANDS_ID_FEATURE_COMMON = 0,
ArDrone3(ArDrone3), // ARCOMMANDS_ID_FEATURE_ARDRONE3 = 1,
Expand Down Expand Up @@ -52,23 +51,83 @@ impl Into<u8> for Feature {
}
}

impl Data for Feature {
fn serialize(&self) -> Vec<u8> {
let mut buf = Vec::new();
buf.push(self.clone().into());
match &self {
Feature::JumpingSumo(js) => {
buf.extend(js.serialize());
}
Feature::Common(common) => {
buf.extend(common.serialize());
}
Feature::ArDrone3(drone) => {
buf.extend(drone.serialize());
pub mod scroll_impl {
use super::*;
use crate::MessageError;
use scroll::{ctx, Endian, Pread, Pwrite};

impl<'a> ctx::TryFromCtx<'a, Endian> for Feature {
type Error = MessageError;

// and the lifetime annotation on `&'a [u8]` here
fn try_from_ctx(src: &'a [u8], endian: Endian) -> Result<(Self, usize), Self::Error> {
let mut offset = 0;
let feature = match src.gread_with::<u8>(&mut offset, endian)? {
0 => {
let common = src.gread_with(&mut offset, endian)?;

Self::Common(common)
}
1 => {
let ardrone3 = src.gread_with(&mut offset, endian)?;

Self::ArDrone3(ardrone3)
}
2 => Self::Minidrone,
3 => {
let js_class = src.gread_with(&mut offset, endian)?;

Feature::JumpingSumo(js_class)
}
4 => Self::SkyController,
8 => Self::PowerUp,
133 => Self::Generic,
134 => Self::FollowMe,
135 => Self::Wifi,
136 => Self::RC,
137 => Self::DroneManager,
138 => Self::Mapper,
139 => Self::Debug,
140 => Self::ControllerInfo,
141 => Self::MapperMini,
142 => Self::ThermalCam,
144 => Self::Animation,
147 => Self::SequoiaCam,
value => {
return Err(Self::Error::OutOfBound {
value: value.into(),
param: "Feature".to_string(),
})
}
};

Ok((feature, offset))
}
}

impl<'a> ctx::TryIntoCtx<Endian> for Feature {
type Error = MessageError;

fn try_into_ctx(self, this: &mut [u8], ctx: Endian) -> Result<usize, Self::Error> {
let mut offset = 0;

this.gwrite_with::<u8>(self.into(), &mut offset, ctx)?;

match self {
Self::Common(common) => {
this.gwrite_with(common, &mut offset, ctx)?;
}
Self::ArDrone3(ardrone3) => {
this.gwrite_with(ardrone3, &mut offset, ctx)?;
}
Self::JumpingSumo(js) => {
this.gwrite_with(js, &mut offset, ctx)?;
}
_ => unimplemented!("Not all Features are impled"),
}
_ => {}

Ok(offset)
}
buf
}
}

Expand Down
Loading