-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Starting to rewrite darklight_driver to be more clean and hopefully f…
…aster.
- Loading branch information
1 parent
6418c16
commit b0f8fb4
Showing
22 changed files
with
517 additions
and
594 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
use crate::streams::StreamsHandler; | ||
use dlwp::{ | ||
config::DLConfig, distributor::READ_AVAILABLE, message::contents_to_string, | ||
serialport::posix::TTYPort, | ||
}; | ||
use std::{ | ||
io::{Read, Write}, | ||
net::TcpStream, | ||
}; | ||
|
||
pub trait Test: Copy + Read + Write { | ||
fn empty() {} | ||
} | ||
|
||
pub struct DarkLightDriver { | ||
pub streams_handler: StreamsHandler, | ||
pub config: DLConfig, | ||
pub tcp_stream: Option<TcpStream>, | ||
pub serial_port: Option<dlwp::serialport::posix::TTYPort>, | ||
} | ||
|
||
impl DarkLightDriver { | ||
pub fn empty() -> Self { | ||
return DarkLightDriver { | ||
streams_handler: StreamsHandler::new(), | ||
config: DLConfig::empty(), | ||
tcp_stream: None, | ||
serial_port: None, | ||
}; | ||
} | ||
|
||
pub fn new(streams_handler: StreamsHandler, config: DLConfig) -> Self { | ||
return DarkLightDriver { | ||
streams_handler, | ||
config, | ||
tcp_stream: None, | ||
serial_port: None, | ||
}; | ||
} | ||
} | ||
|
||
pub fn read<R: Read>(mut stream: &mut R) -> [u8; 4096] { | ||
let mut buf = [0; 4096]; | ||
stream.read(&mut buf).unwrap_or(0); | ||
|
||
buf | ||
} | ||
|
||
// Returns a message if one is received while waiting for send | ||
pub fn write<RW: Read + Write>(mut stream: &mut RW, write: String, wait: bool) -> Option<String> { | ||
if wait == true { | ||
let mut read_bytes = read(stream); | ||
|
||
while read_bytes == [0; 4096] { | ||
read_bytes = read(stream); | ||
} | ||
|
||
let read_str = contents_to_string(read_bytes); | ||
if read_str.contains(READ_AVAILABLE) { | ||
stream.write(write.as_bytes()).unwrap(); | ||
stream.flush().unwrap(); | ||
} else { | ||
return Some(read_str); | ||
} | ||
} | ||
|
||
stream.write(write.as_bytes()).unwrap(); | ||
stream.flush().unwrap(); | ||
|
||
None | ||
} |
Oops, something went wrong.