From c10606ea9636c52a440302eb7ea3f512374b620e Mon Sep 17 00:00:00 2001 From: juhu1705 Date: Sun, 4 Sep 2022 12:23:06 +0200 Subject: [PATCH] feat: Added new driving test for my railroad --- src/loco_controller.rs | 25 ++----------------------- src/tests.rs | 34 +++++++++++++++++++++++----------- 2 files changed, 25 insertions(+), 34 deletions(-) diff --git a/src/loco_controller.rs b/src/loco_controller.rs index b1c8761..4c24d7f 100644 --- a/src/loco_controller.rs +++ b/src/loco_controller.rs @@ -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; @@ -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(); } } @@ -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 diff --git a/src/tests.rs b/src/tests.rs index 0e56781..e56cf16 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -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 { @@ -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"); @@ -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(); @@ -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; } }, @@ -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!"); } }