summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorXavier Del Campo <xavi92psx@gmail.com>2018-12-31 00:28:46 +0100
committerXavier Del Campo <xavi92psx@gmail.com>2018-12-31 00:28:46 +0100
commit42f2476aeebc5eb5a9366a6a366c9678eba6eb1b (patch)
tree2098f4cdd976edae82d841f56298a6030512a792
parentb47361f283777f266970f9c082782ebac6c04aef (diff)
Some more work, but it still does not compile
-rw-r--r--src/app.rs22
-rw-r--r--src/cmdline.rs15
-rw-r--r--src/main.rs2
-rw-r--r--src/transfer.rs16
4 files changed, 50 insertions, 5 deletions
diff --git a/src/app.rs b/src/app.rs
index 360cbe4..c766576 100644
--- a/src/app.rs
+++ b/src/app.rs
@@ -53,7 +53,19 @@ fn setup_tcp(tcp_addr : &String) -> io::Result<()> {
}
fn serial_comm(addr : Option<&String>, port_name : &String, baud_rate : Option<&String>) -> io::Result<()> {
- let port = serial_init(addr, port_name, baud_rate).unwrap();
+ use transfer;
+ use transfer::TransferState;
+
+ let mut port = serial_init(addr, port_name, baud_rate).unwrap();
+
+ let mut state = TransferState::FirstContact;
+
+ loop {
+ state = match state {
+ TransferState::FirstContact => transfer::first_contact(&mut port),
+ _ => TransferState::Finished
+ }
+ }
Ok(())
}
@@ -67,13 +79,19 @@ fn serial_init(addr : Option<&String>, port_name : &String, baud_rate : Option<&
// a SystemPort instance will be returned.
let port = serial::open(port_name);
+ // This variable will be bound to a SystemPort
+ // instance if everything could be configured successfully.
let mut port_unwrapped;
let baud = match baud_rate {
- None => serial::Baud4800,
+ // Assign default baud rate if no
+ // option was specified.
+ None => serial::Baud115200,
Some(b) => {
match b.parse() {
+ // Parse user-specific baud rate.
Ok(s) => serial::BaudRate::from_speed(s),
+ // Could not parse input baud rate.
Err(_) => return Err(io::Error::new(io::ErrorKind::Other, "Invalid baudrate")),
}
}
diff --git a/src/cmdline.rs b/src/cmdline.rs
index 6405301..d4828b2 100644
--- a/src/cmdline.rs
+++ b/src/cmdline.rs
@@ -17,7 +17,12 @@ pub const PORT_NAME_ARG : &'static str = "--port-name";
/// This parameter disables sending any information
/// coming from the console to stdout.
pub const DISABLE_OUTPUT_ARG : &'static str = "--disable-output";
-pub const BAUDRATE_ARG : &'static str = "--baudrate";
+
+/// This parameter allows defining a specific baud rate,
+pub const BAUDRATE_ARG : &'static str = "--baud-rate";
+
+/// This parameter allows using a TCP connection
+/// against a GUI front-end.
pub const TCP_ARG : &'static str = "--tcp";
const CMD_LINE_ARGS : [CmdLineArg; 4] =
@@ -40,7 +45,7 @@ const CMD_LINE_ARGS : [CmdLineArg; 4] =
arg_str : BAUDRATE_ARG,
param_str : Some("[BAUDRATE]"),
is_required : false,
- explanation : "Sets serial port baudrate. Defaults to 4800bps"
+ explanation : "Sets serial port baudrate. Defaults to 115200 bps"
},
CmdLineArg {
@@ -68,6 +73,10 @@ fn show_help() {
}
}
+/// This function creates a Hashmap instance
+/// that relates all command line arguments
+/// against its parameters. For example:
+/// ["--baud-rate", "4800"], ["--disable-output", ""]
pub fn process_arguments() -> Option<HashMap<String, String>> {
if env::args_os().count() <= 1 {
@@ -79,7 +88,7 @@ pub fn process_arguments() -> Option<HashMap<String, String>> {
// This enum defines a finite-state machine that
// will be used to determine what type of data is
- // being retrieved
+ // being retrieved from command line arguments.
enum ExpectedParameter
{
ParameterOption,
diff --git a/src/main.rs b/src/main.rs
index c65130a..c532fde 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,6 +1,7 @@
extern crate serial;
mod cmdline;
mod app;
+mod transfer;
/// Main function.
fn main() {
@@ -8,6 +9,7 @@ fn main() {
match cmdline::process_arguments() {
None => return,
Some(hash) => {
+ // Execute application logic.
match app::app(hash) {
_ => return
}
diff --git a/src/transfer.rs b/src/transfer.rs
new file mode 100644
index 0000000..617eb62
--- /dev/null
+++ b/src/transfer.rs
@@ -0,0 +1,16 @@
+const initial_transmission : u8 = 'b' as u8;
+
+pub enum TransferState {
+ FirstContact,
+ WaitAck,
+ Finished
+}
+
+use serial;
+use serial::SystemPort;
+
+pub fn first_contact(port : &mut serial::SystemPort) -> TransferState {
+
+ *port.write(&initial_transmission);
+ TransferState::WaitAck
+}