Skip to content

Commit

Permalink
Simple ds in examples that doesn't work.
Browse files Browse the repository at this point in the history
  • Loading branch information
grebneerg committed Nov 22, 2018
1 parent 41399d7 commit 86592d6
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 26 deletions.
15 changes: 14 additions & 1 deletion examples/simple.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
extern crate libds;

use std::thread;
use std::time;

use libds::DriverStation;

fn main() {
// let mut ds = DriverStation::new();
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");
}
37 changes: 18 additions & 19 deletions src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,23 @@ pub struct DSConnection {
}

impl DSConnection {
pub fn release(self) {
drop(self);
}

pub fn new(addr: IpAddr, state: Arc<Mutex<DriverStationState>>) -> io::Result<Self> {
let (sender_signal, receiver_signal) = mpsc::channel::<Signal>();

let (sender_res, receiver_res) = mpsc::channel::<io::Result<()>>();

let mut tcp = TcpStream::connect(SocketAddr::new(addr, 1740))?;
// 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, 1150))?;
let udp_recv = UdpSocket::bind(SocketAddr::new(addr.clone(), 1150))?;
udp_recv.set_nonblocking(true)?;

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

let mut last = Instant::now();
let mut last = Instant::now();

let t = thread::spawn(move || loop {
match receiver_signal.try_recv() {
Expand Down Expand Up @@ -77,17 +76,17 @@ impl DSConnection {
}
}

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();
}
}
}
}
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();
}
}
}
}
});

Ok(DSConnection {
Expand Down
2 changes: 1 addition & 1 deletion src/ds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const TIMEZONE: &'static str = "UTC";
pub struct DriverStationState {
joysticks: Vec<Option<Joystick>>,
estop: bool,
enabled: bool,
pub enabled: bool,
mode: RobotMode,
alliance: Alliance,
game_data: String,
Expand Down
15 changes: 11 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,27 @@ use packet::PacketWriter;
use states::{Alliance, RobotMode};

pub struct DriverStation {
state: Arc<Mutex<DriverStationState>>,
pub state: Arc<Mutex<DriverStationState>>,
connection: Option<DSConnection>,
}

impl DriverStation {
fn connect(&mut self, addr: IpAddr) -> io::Result<()> {
pub fn new() -> Self {
DriverStation {
state: Arc::new(Mutex::new(Default::default())),
connection: None,
}
}

pub fn connect(&mut self, addr: IpAddr) -> io::Result<()> {
if let Some(conn) = self.connection.take() {
conn.release();
drop(conn);
}
self.connection = Some(DSConnection::new(addr, self.state.clone())?);
Ok(())
}

fn is_connected(&self) -> bool {
pub fn is_connected(&self) -> bool {
match self.connection {
None => false,
Some(ref conn) => match conn.status() {
Expand Down
3 changes: 2 additions & 1 deletion testrobots/v2018/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
id "jaci.openrio.gradle.GradleRIO" version "2018.02.17"
}

def TEAM = 639
def TEAM = 9639
def ROBOT_CLASS = "org.team639.robot.Robot"

sourceCompatibility = 1.8
Expand All @@ -17,6 +17,7 @@ deploy {
targets {
target("roborio", jaci.openrio.gradle.frc.RoboRIO) {
team = TEAM
addresses << "169.254.204.207"
}
}
artifacts {
Expand Down

0 comments on commit 86592d6

Please sign in to comment.