Skip to content

Commit

Permalink
[WIP] Scroll impl and it Compiles
Browse files Browse the repository at this point in the history
  • Loading branch information
elpiel committed May 1, 2020
1 parent 1c121ce commit 211af57
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 4 deletions.
19 changes: 19 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 @@ -14,3 +14,5 @@ serde = {version = "1.0", features = ["derive"]}
serde_json = "1.0"
dashmap = "3.11"
chrono = "0.4"
scroll = "0.10"
scroll_derive = "0.10"
42 changes: 38 additions & 4 deletions arsdk-rs/src/ardrone3.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
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 +28,52 @@ 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

let take_off: u16 = 1;

take_off.to_le_bytes().to_vec()
}
}

pub mod scroll_impl {
use super::*;
use scroll::{ctx, Endian, Pread, BE};

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

// 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,
_ => return Err(scroll::Error::Custom("Out of range".into()))
};

Ok((ardrone3, *offset))
}

}
}
57 changes: 57 additions & 0 deletions arsdk-rs/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,63 @@ impl Data for Feature {
}
}

pub mod scroll_impl {
use super::*;
use scroll::{ctx, Endian, Pread, BE};

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

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

let feature: u8 = src.gread_with(offset, endian)?;

let feature = match feature {
0 => {
todo!()
// Self::Common()
},
1 => {
let ardrone3 = src.gread_with(offset, endian)?;

Self::ArDrone3(ardrone3)
},
2 => Self::Minidrone,
3 => {
todo!()
// Self::JumpingSumo(_)
},
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,
_ => return Err(scroll::Error::Custom("Out of range".into()))
};

Ok((feature, *offset))


// let buffer_id = src.gread_with(offset, endian)?;
// let sequence_id = src.gread_with(offset, endian)?;
// let feature_len: u32 = src.gread_with(offset, endian)?;
// let feature = src.gread_with(offset, endian)?;
// Ok((Feature { frame_type, buffer_id, sequence_id, feature }, *offset))
}
}
}

// --------------------- Tests --------------------- //

#[cfg(test)]
Expand Down
52 changes: 52 additions & 0 deletions arsdk-rs/src/frame.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{command, Drone};
use anyhow::{anyhow, Error as AnyError, Result as AnyResult};
use std::convert::TryFrom;
use scroll_derive::{Pread, Pwrite};

pub trait IntoRawFrame {
fn into_raw(self) -> RawFrame;
Expand Down Expand Up @@ -191,6 +192,57 @@ impl Into<u8> for BufferID {
}
}

pub mod impl_scroll {
use super::*;

use scroll::{ctx, Endian, Pread, LE};

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

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

let frame_type = src.gread_with(offset, endian)?;
let buffer_id = src.gread_with(offset, endian)?;
let sequence_id = src.gread_with(offset, endian)?;
let feature_len: u32 = src.gread_with(offset, endian)?;
let feature = src.gread_with(offset, endian)?;

Ok((Frame { frame_type, buffer_id, sequence_id, feature }, *offset))
}
}

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

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

Type::try_from(frame_value)
.map(|frame_type| (frame_type, *offset))
.map_err(|err| scroll::Error::Custom(err.to_string()))
}
}

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

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

BufferID::try_from(id_value)
.map(|buffer_id| (buffer_id, *offset))
.map_err(|err| scroll::Error::Custom(err.to_string()))
}
}
}

// --------------------- Tests --------------------- //

#[cfg(test)]
Expand Down
3 changes: 3 additions & 0 deletions bebop2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ impl Bebop2 {
Ok(Self { drone })
}

// ARCOMMANDS_ID_ARDRONE3_PILOTING_CMD_NAVIGATEHOME
// ARCOMMANDS_ID_ARDRONE3_PILOTING_CMD_AUTOTAKEOFFMODE

pub fn take_off(&self) -> AnyResult<()> {
// Ardrone3
// ARCOMMANDS_ID_ARDRONE3_CLASS_PILOTING
Expand Down

0 comments on commit 211af57

Please sign in to comment.