Skip to content

Commit

Permalink
feat: Added new driving test for my railroad
Browse files Browse the repository at this point in the history
  • Loading branch information
juhu1705 committed Sep 4, 2022
1 parent 76449ed commit c10606e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 34 deletions.
25 changes: 2 additions & 23 deletions src/loco_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ use std::fmt::Debug;
use crate::error::{LocoDriveSendingError, MessageParseError};
use crate::protocol::Message;
use std::sync::{Arc, Mutex};
use std::thread::{spawn};
use tokio::time::{sleep, Duration};
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::runtime::Runtime;
use tokio::sync::broadcast::Sender;
use tokio::task::JoinHandle;
use tokio::sync::Notify;
Expand Down Expand Up @@ -239,27 +237,10 @@ impl LocoDriveController {
/// This function panics if the reading thread has panicked or the reading thread was killed,
/// by some external source.
fn stop_reader(&mut self) {
if let Some(reader) = self.reading_thread.take() {
if let Some(_reader) = self.reading_thread.take() {
// Note the thread to end reading
*self.stop.lock().unwrap() = true;
self.fire_stop.notify_waiters();
// Wait until the thread is stopped
match spawn(move || {
let runtime = match Runtime::new() {
Ok(runtime) => runtime,
Err(_) => { return; }
};
match runtime.block_on(reader) {
Ok(_) => "",
Err(_) => "",
};
}).join() {
Ok(_) => "",
Err(_) => "",
};

// We allow new threads to spawn and read from the port
*self.stop.lock().unwrap() = false;
(*self.fire_stop).notify_waiters();
}
}

Expand Down Expand Up @@ -384,8 +365,6 @@ impl LocoDriveController {
// We read the next message from the serial port
let parsed = LocoDriveController::read_next_message(port, send, stopping, ignore_send_messages).await;

println!("Read: {:?}", parsed);

// We check which type the message we received is
match parsed {
// We can at this level ignore update messages
Expand Down
34 changes: 23 additions & 11 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ mod tests {
let mut loco_controller = match LocoDriveController::new(
"/dev/ttyUSB0",
115_200,
5000,
FlowControl::Software,
50000,
FlowControl::None,
sender,
false,
).await {
Expand All @@ -180,6 +180,12 @@ mod tests {
}
};

let _m = Message::parse(
Message::SwReq(SwitchArg::new(15, SwitchDirection::Curved, true)).to_message().as_slice()
).unwrap();

println!("Message: {:?}", Message::SwReq(SwitchArg::new(15, SwitchDirection::Curved, true)).to_message());

loco_controller.send_message(GpOn).await.unwrap();

println!("Setup test train");
Expand Down Expand Up @@ -229,15 +235,15 @@ mod tests {

println!("Known Trains: {:?}", slot_adr_map);

for i in 1..11 {
for i in 1..3 {
println!("Drive round {}", i);

if i % 2 == 0 {
loco_controller.send_message(Message::SwReq(SwitchArg::new(8, SwitchDirection::Straight, true))).await.unwrap();
loco_controller.send_message(Message::SwReq(SwitchArg::new(9, SwitchDirection::Curved, true))).await.unwrap();
loco_controller.send_message(Message::SwReq(SwitchArg::new(15, SwitchDirection::Straight, true))).await.unwrap();
loco_controller.send_message(Message::SwReq(SwitchArg::new(18, SwitchDirection::Straight, true))).await.unwrap();
} else {
loco_controller.send_message(Message::SwReq(SwitchArg::new(8, SwitchDirection::Curved, true))).await.unwrap();
loco_controller.send_message(Message::SwReq(SwitchArg::new(9, SwitchDirection::Straight, true))).await.unwrap();
loco_controller.send_message(Message::SwReq(SwitchArg::new(15, SwitchDirection::Curved, true))).await.unwrap();
loco_controller.send_message(Message::SwReq(SwitchArg::new(18, SwitchDirection::Curved, true))).await.unwrap();
}

loco_controller.send_message(LocoSpd(*slot_adr_map.get(&adr).unwrap(), SpeedArg::Drive(100))).await.unwrap();
Expand All @@ -249,13 +255,15 @@ mod tests {
LocoDriveMessage::Message(message) => {
match message {
Message::InputRep(in_arg) => {
if i % 2 == 0 && in_arg.address() == 1 && in_arg.input_source() == SourceType::Switch && in_arg.sensor_level() == SensorLevel::High {
if i % 2 == 0 && in_arg.address() == 3 && in_arg.input_source() == SourceType::Switch && in_arg.sensor_level() == SensorLevel::High {
waiting = false;
loco_controller.send_message(LocoSpd(*slot_adr_map.get(&adr).unwrap(), SpeedArg::Drive(50))).await.unwrap();
} else if i % 2 == 1 && in_arg.address() == 2 && in_arg.input_source() == SourceType::Ds54Aux && in_arg.sensor_level() == SensorLevel::High {
} else if i % 2 == 1 && in_arg.address() == 8 && in_arg.input_source() == SourceType::Switch && in_arg.sensor_level() == SensorLevel::High {
waiting = false;
loco_controller.send_message(LocoSpd(*slot_adr_map.get(&adr).unwrap(), SpeedArg::Drive(50))).await.unwrap();
} else if !waiting && in_arg.address() == 2 && in_arg.input_source() == SourceType::Switch && in_arg.sensor_level() == SensorLevel::Low {
} else if !waiting && in_arg.address() == 8 && in_arg.input_source() == SourceType::Ds54Aux && in_arg.sensor_level() == SensorLevel::Low {
break;
} else if !waiting && in_arg.address() == 1 && in_arg.input_source() == SourceType::Ds54Aux && in_arg.sensor_level() == SensorLevel::Low {
break;
}
},
Expand All @@ -279,6 +287,10 @@ mod tests {
sleep(Duration::from_secs(6)).await;
}

println!("Drive 10 rounds!")
println!("Drive 10 rounds!");

drop(loco_controller);

println!("Closed loco net!");
}
}

0 comments on commit c10606e

Please sign in to comment.