Skip to content

Commit

Permalink
Format code using 'cargo fmt'
Browse files Browse the repository at this point in the history
  • Loading branch information
Atul9 committed Apr 6, 2019
1 parent 72a6e77 commit f02171a
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 64 deletions.
6 changes: 2 additions & 4 deletions examples/h61-forward-march.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@

/// Import std stuff
use std::error::Error;
use std::time::Duration;
use std::thread;
use std::time::Duration;

// Import traits
use ufo_rs::traits::control::*;
use ufo_rs::traits::drone::*;


fn main() -> Result<(), Box<dyn Error>> {
// Import controller
use ufo_rs::drones::jjrc::h61;
Expand Down Expand Up @@ -49,7 +47,7 @@ fn main() -> Result<(), Box<dyn Error>> {
println!("Landing...");

driver.land()?;

println!("Sent!");

// Ta-dah!
Expand Down
9 changes: 2 additions & 7 deletions examples/h61-hover.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@

/// Import std stuff
use std::error::Error;
use std::time::Duration;
use std::thread;
use std::time::Duration;

// Import traits
use ufo_rs::traits::control::*;
use ufo_rs::traits::drone::*;


fn main() -> Result<(), Box<dyn Error>> {
// Import controller
use ufo_rs::drones::jjrc::h61;
Expand All @@ -35,19 +33,16 @@ fn main() -> Result<(), Box<dyn Error>> {

println!("Sent!");


driver.hover()?;

// Wait for a second
thread::sleep(delay);



// Land
println!("Landing...");

driver.land()?;

println!("Sent!");

// Ta-dah!
Expand Down
5 changes: 2 additions & 3 deletions examples/h61-rotate90.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
/// Import std stuff
use std::error::Error;
use std::time::Duration;
use std::thread;
use std::time::Duration;

// Import traits
use ufo_rs::traits::control::*;
use ufo_rs::traits::drone::*;


fn main() -> Result<(), Box<dyn Error>> {
// Import controller
use ufo_rs::drones::jjrc::h61;
Expand Down Expand Up @@ -46,7 +45,7 @@ fn main() -> Result<(), Box<dyn Error>> {
println!("Landing...");

driver.land()?;

println!("Sent!");

// Ta-dah!
Expand Down
5 changes: 2 additions & 3 deletions examples/h61-takeoff-landing.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
/// Import std stuff
use std::error::Error;
use std::time::Duration;
use std::thread;
use std::time::Duration;

// Import traits
use ufo_rs::traits::control::*;
use ufo_rs::traits::drone::*;


fn main() -> Result<(), Box<dyn Error>> {
// Import controller
use ufo_rs::drones::jjrc::h61;
Expand Down Expand Up @@ -41,7 +40,7 @@ fn main() -> Result<(), Box<dyn Error>> {
println!("Landing...");

driver.land()?;

println!("Sent!");

// Ta-dah!
Expand Down
34 changes: 14 additions & 20 deletions src/drones/jjrc/h61.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub const CONN_TCP_PORT: &str = "8888";

pub enum TcpHex {
Photo,
Video
Video,
}

impl TcpHex {
Expand All @@ -29,14 +29,11 @@ impl TcpHex {
// Define hex codes
match self {
TcpHex::Photo => "000102030405060708092525".to_string(),
TcpHex::Video => "000102030405060708092828".to_string()
TcpHex::Video => "000102030405060708092828".to_string(),
}

}
}



// Hex Codes for UDP socket
/// Hex codes for different commands
pub enum UdpHex {
Expand Down Expand Up @@ -66,9 +63,6 @@ pub enum UdpHex {
RotLeft,
/// Hex code for rotating right
RotRight,



}

impl UdpHex {
Expand All @@ -91,16 +85,13 @@ impl UdpHex {
UdpHex::RotLeft => "ff087e00403f9012120046".to_string(),
UdpHex::RotRight => "ff087e7e403f90121200c8".to_string(),
}

}
}


// Code for drone programmer-facing API

/// UFO controls of the JJRC H61 foldable drone
pub struct Driver {

/// UDP Drone controller, handles connections etc.
connection: crate::DroneUdpConnection,
camera: crate::DroneTcpConnection,
Expand All @@ -116,8 +107,18 @@ impl Driver {
pub fn new() -> Self {
// create new connection
Driver {
connection: crate::DroneUdpConnection::new(BIND_IP.to_string(), BIND_PORT.to_string(), CONN_IP.to_string(), CONN_UDP_PORT.to_string()),
camera: crate::DroneTcpConnection::new(BIND_IP.to_string(), BIND_PORT.to_string(), CONN_IP.to_string(), CONN_TCP_PORT.to_string()),
connection: crate::DroneUdpConnection::new(
BIND_IP.to_string(),
BIND_PORT.to_string(),
CONN_IP.to_string(),
CONN_UDP_PORT.to_string(),
),
camera: crate::DroneTcpConnection::new(
BIND_IP.to_string(),
BIND_PORT.to_string(),
CONN_IP.to_string(),
CONN_TCP_PORT.to_string(),
),
}
}

Expand All @@ -133,7 +134,6 @@ impl Driver {
self.camera.read()
}


/// Connect to drone
pub fn connect(&mut self) -> Result<(), Box<dyn Error>> {
self.connection.connect()
Expand All @@ -154,7 +154,6 @@ impl drone::Hover for Driver {
}
}


// Implement FlightControl for H61 Driver
impl control::FlightControl for Driver {
fn take_off(&mut self) -> Result<(), Box<dyn Error>> {
Expand All @@ -174,7 +173,6 @@ impl drone::Stop for Driver {
// Stop propellers
self.connection.send_command(UdpHex::Stop.value())
}

}

// Add movement controls
Expand All @@ -189,7 +187,6 @@ impl control::Movement for Driver {

fn up(&mut self, time: usize) -> Result<(), Box<dyn Error>> {
self.connection.send_command(UdpHex::Up.value())

}
fn down(&mut self, time: usize) -> Result<(), Box<dyn Error>> {
self.connection.send_command(UdpHex::Down.value())
Expand All @@ -199,18 +196,15 @@ impl control::Movement for Driver {

fn forwards(&mut self, time: usize) -> Result<(), Box<dyn Error>> {
self.connection.send_command(UdpHex::Forwards.value())

}
fn backwards(&mut self, time: usize) -> Result<(), Box<dyn Error>> {
self.connection.send_command(UdpHex::Backwards.value())

}

// TODO Determine length of time parameter (milliseconds or seconds)

fn rot_left(&mut self, time: usize) -> Result<(), Box<dyn Error>> {
self.connection.send_command(UdpHex::RotLeft.value())

}
fn rot_right(&mut self, time: usize) -> Result<(), Box<dyn Error>> {
self.connection.send_command(UdpHex::RotRight.value())
Expand Down
50 changes: 28 additions & 22 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::net::UdpSocket;
use std::net::TcpStream;
use hex;
use std::error::Error;
use std::io::prelude::*;
use hex;
use std::net::TcpStream;
use std::net::UdpSocket;

pub mod drones;
pub mod traits;
Expand All @@ -17,7 +17,7 @@ mod tests {

pub mod errors {
use std::fmt;

/// Error for issues with socket connection
pub struct ConnectionError;

Expand All @@ -35,14 +35,16 @@ pub mod errors {
}
}


/// Error for issues with drone executing commands
pub struct CommandError;

// Implement std::fmt::Display for CommandError
impl fmt::Display for CommandError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "An Command Error Occurred, Please confirm the hex values for your drone are valid") // user-facing output
write!(
f,
"An Command Error Occurred, Please confirm the hex values for your drone are valid"
) // user-facing output
}
}

Expand All @@ -67,13 +69,17 @@ pub struct DroneUdpConnection {
connect_port: String,

/// Socket of drone
sock: UdpSocket
sock: UdpSocket,
}

impl DroneUdpConnection {

/// Create a new drone connection takes in `bind_ip`, `bind_port`, `connect_ip`, and `connect_port` all as `Strings`
pub fn new(bind_ip: String, bind_port: String, connect_ip: String, connect_port: String) -> Self {
pub fn new(
bind_ip: String,
bind_port: String,
connect_ip: String,
connect_port: String,
) -> Self {
let mut bind_url = String::new();

bind_url.push_str(&bind_ip);
Expand All @@ -86,7 +92,7 @@ impl DroneUdpConnection {
connect_ip,
connect_port,
// Not yet connected
sock: UdpSocket::bind(bind_url).unwrap()
sock: UdpSocket::bind(bind_url).unwrap(),
}
}

Expand All @@ -99,7 +105,6 @@ impl DroneUdpConnection {
// bind_url.push_str(":");
// bind_url.push_str(&self.bind_port);


// // Bind to port
// self.sock = UdpSocket::bind(bind_url)?;

Expand All @@ -113,7 +118,6 @@ impl DroneUdpConnection {
self.sock.connect(conn_url)?;

Ok(())

}

/// Send a static command to the drone
Expand All @@ -130,15 +134,14 @@ impl DroneUdpConnection {
// Return we're okay
Ok(())
}

}

/// Status of a drone
pub struct DroneStatus{
/// Status of a drone
pub struct DroneStatus {
/// Battery charge of the drone
bat_charge: usize,
/// Capacity of drone battery
bat_cap: usize
bat_cap: usize,
}

pub struct DroneTcpConnection {
Expand All @@ -153,13 +156,17 @@ pub struct DroneTcpConnection {
connect_port: String,

/// Socket of drone
sock: TcpStream
sock: TcpStream,
}

impl DroneTcpConnection {

/// Create a new drone connection takes in `bind_ip`, `bind_port`, `connect_ip`, and `connect_port` all as `Strings`
pub fn new(bind_ip: String, bind_port: String, connect_ip: String, connect_port: String) -> Self {
pub fn new(
bind_ip: String,
bind_port: String,
connect_ip: String,
connect_port: String,
) -> Self {
let mut connect_url = String::new();

connect_url.push_str(&connect_ip);
Expand All @@ -172,7 +179,7 @@ impl DroneTcpConnection {
connect_ip,
connect_port,
// Not yet connected
sock: TcpStream::connect(connect_url).unwrap()
sock: TcpStream::connect(connect_url).unwrap(),
}
}

Expand Down Expand Up @@ -209,11 +216,10 @@ impl DroneTcpConnection {
}

pub fn read(&mut self) -> Result<Vec<u8>, Box<dyn Error>> {
let mut buffer: &mut[u8] = &mut [0; 128];
let mut buffer: &mut [u8] = &mut [0; 128];

self.sock.read(&mut buffer)?;

Ok(buffer.to_owned())
}

}
Loading

0 comments on commit f02171a

Please sign in to comment.