Skip to content

Commit

Permalink
It enables.
Browse files Browse the repository at this point in the history
  • Loading branch information
grebneerg committed Nov 26, 2018
1 parent 86592d6 commit 45a30df
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 64 deletions.
18 changes: 9 additions & 9 deletions examples/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ use libds::DriverStation;
fn main() {
let mut ds = DriverStation::new();
ds.connect([169, 254, 204, 207].into()).unwrap();
println!("we connected");
thread::sleep(time::Duration::from_millis(2000));
ds.state.lock().unwrap().enabled = true;
println!("we enabled");
thread::sleep(time::Duration::from_millis(2000));
ds.state.lock().unwrap().enabled = false;
println!("we disabled");
thread::sleep(time::Duration::from_millis(2000));
println!("we done");
println!("we connected");
thread::sleep(time::Duration::from_millis(2000));
ds.state.lock().unwrap().enabled = true;
println!("we enabled");
thread::sleep(time::Duration::from_millis(2000));
ds.state.lock().unwrap().enabled = false;
println!("we disabled");
thread::sleep(time::Duration::from_millis(2000));
println!("we done");
}
102 changes: 56 additions & 46 deletions src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,65 +27,75 @@ impl DSConnection {
// TODO: Create sockets within other thread and add mechanism to detect status of connections
// (connecting, failed, connected, error, etc)

let mut tcp = TcpStream::connect(SocketAddr::new(addr.clone(), 1740))?;
tcp.set_nonblocking(true)?;

let udp_recv = UdpSocket::bind(SocketAddr::new(addr.clone(), 1150))?;
udp_recv.set_nonblocking(true)?;

let udp_send = UdpSocket::bind(SocketAddr::new(addr.clone(), 1110))?;

let mut last = Instant::now();

let t = thread::spawn(move || loop {
match receiver_signal.try_recv() {
Ok(Signal::Disconnect) | Err(mpsc::TryRecvError::Disconnected) => break,
_ => {}
}
let t = thread::spawn(move || {
println!("udp start");
let udp = UdpSocket::bind(SocketAddr::new([169, 254, 65, 205].into(), 1150)).unwrap();
println!("udp 2");
udp.connect(SocketAddr::new(addr.clone(), 1110)).unwrap();
udp.set_nonblocking(true).unwrap();
println!("udp started");

udp
.send(state.lock().unwrap().udp_packet().as_ref())
.unwrap();

println!("tcp start");
// let mut tcp = TcpStream::connect(SocketAddr::new(addr.clone(), 1740)).unwrap();
// tcp.set_nonblocking(true).unwrap();

loop {
match receiver_signal.try_recv() {
Ok(Signal::Disconnect) | Err(mpsc::TryRecvError::Disconnected) => break,
_ => {}
}

let mut udp_buf = vec![0u8; 100];
match udp_recv.recv(&mut udp_buf) {
Ok(n) => unimplemented!(),
Err(e) => {
if e.kind() != io::ErrorKind::WouldBlock {
sender_res.send(Err(e)).unwrap();
let mut udp_buf = vec![0u8; 100];
match udp.recv_from(&mut udp_buf) {
Ok(n) => println!("{:?}", udp_buf),
Err(e) => {
if e.kind() != io::ErrorKind::WouldBlock {
sender_res.send(Err(e)).unwrap();
}
}
}
}

let mut size_buf = vec![0u8; 2];
match tcp.read_exact(&mut size_buf) {
Ok(_) => {
let size = NetworkEndian::read_u16(&size_buf);
let mut buf = vec![0u8; size as usize];
match tcp.read_exact(&mut buf) {
Ok(_) => if let Some(packet) = RioTcpPacket::from_bytes(buf) {
state.lock().unwrap().update_from_tcp(packet);
},
let mut size_buf = vec![0u8; 2];
// match tcp.read_exact(&mut size_buf) {
// Ok(_) => {
// let size = NetworkEndian::read_u16(&size_buf);
// let mut buf = vec![0u8; size as usize];
// match tcp.read_exact(&mut buf) {
// Ok(_) => if let Some(packet) = RioTcpPacket::from_bytes(buf) {
// state.lock().unwrap().update_from_tcp(packet);
// },
// Err(e) => {
// if e.kind() != io::ErrorKind::WouldBlock {
// sender_res.send(Err(e)).unwrap();
// }
// }
// }
// }
// Err(e) => {
// if e.kind() != io::ErrorKind::WouldBlock {
// sender_res.send(Err(e)).unwrap();
// }
// }
// }

if last.elapsed() >= Duration::from_millis(20) {
last = Instant::now();
println!("{:?}", state.lock().unwrap().udp_packet());
match udp.send(state.lock().unwrap().udp_packet().as_ref()) {
Ok(s) => println!("sent {}", s),
Err(e) => {
if e.kind() != io::ErrorKind::WouldBlock {
sender_res.send(Err(e)).unwrap();
}
}
}
}
Err(e) => {
if e.kind() != io::ErrorKind::WouldBlock {
sender_res.send(Err(e)).unwrap();
}
}
}

if last.elapsed() >= Duration::from_millis(20) {
last = Instant::now();
match udp_send.send(state.lock().unwrap().udp_packet().as_ref()) {
Ok(s) => {}
Err(e) => {
if e.kind() != io::ErrorKind::WouldBlock {
sender_res.send(Err(e)).unwrap();
}
}
}
}
});

Expand Down
5 changes: 5 additions & 0 deletions src/ds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ impl DriverStationState {
pub fn update_from_tcp(&mut self, packet: RioTcpPacket) {
unimplemented!();
}

pub fn update_from_udp(&mut self, packet: RioUdpPacket) {
self.request_time = packet.request_date;
// TODO: Finish implementing this
}
}

fn date_packet() -> Vec<u8> {
Expand Down
18 changes: 9 additions & 9 deletions src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::convert::From;
use packet::PacketReader;
use states::RobotMode;

pub(crate) struct Trace {
pub struct Trace {
robot_code: bool,
is_roborio: bool,
// these modes don't seem to quite line up with what we send
Expand All @@ -26,7 +26,7 @@ impl From<u8> for Trace {
}
}

pub(crate) struct Status {
pub struct Status {
e_stop: bool,
brownout: bool,
code_initializing: bool,
Expand All @@ -47,13 +47,13 @@ impl From<u8> for Status {
}

pub struct RioUdpPacket {
sequence_num: u16,
comm_version: u8,
status: Status,
trace: Trace,
battery_voltage: f32,
request_date: bool,
tags: PacketReader, // TODO: parse tags from this Packet
pub sequence_num: u16,
pub comm_version: u8,
pub status: Status,
pub trace: Trace,
pub battery_voltage: f32,
pub request_date: bool,
pub tags: PacketReader, // TODO: parse tags from this Packet
}

impl RioUdpPacket {
Expand Down

0 comments on commit 45a30df

Please sign in to comment.