Starting service

This commit is contained in:
hodasemi 2019-11-23 23:31:21 +01:00
parent ed24166595
commit a22670ad08
4 changed files with 87 additions and 47 deletions

1
09-macroboard.rules Normal file
View file

@ -0,0 +1 @@
SUBSYSTEMS=="usb", KERNEL=="ttyUSB[0-9]*", ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6001", MODE="0660", GROUP="plugdev", SYMLINK+="macroboard/tty$attr{serial}"

View file

@ -51,11 +51,21 @@ void setup() {
void loop() {
// check for possible input
/*
if (current_state == WaitingForInput) {
for (byte i = 0; i < pin_count; i++) {
check_pin(pins[i], i);
}
}
*/
// debug
Serial.println("test");
delay(1000);
}
void check_pin(byte pin, byte index) {

60
src/port.rs Normal file
View file

@ -0,0 +1,60 @@
use serial;
use serial::prelude::*;
pub use serial::PortSettings;
use utilities::prelude::*;
use std::cell::RefCell;
use std::ffi::OsStr;
use std::time::Duration;
pub struct Port {
serial_port: RefCell<Box<dyn SerialPort>>,
}
impl Port {
pub fn open<T: AsRef<OsStr> + ?Sized + std::fmt::Display>(
port: &T,
settings: PortSettings,
) -> VerboseResult<Self> {
let mut port = serial::open(port).map_err(|err| {
format!(
"could not open serial port ({:?}, {}, {})",
err.kind(),
err,
port
)
})?;
port.configure(&settings)
.map_err(|err| format!("failed configuring serial port ({})", err))?;
port.set_timeout(Duration::from_millis(2500))
.map_err(|err| format!("failed setting time out for serial port ({})", err))?;
Ok(Port {
serial_port: RefCell::new(Box::new(port)),
})
}
pub fn read(&self) -> VerboseResult<String> {
let mut buf: Vec<u8> = (0..255).collect();
self.serial_port
.try_borrow_mut()?
.read(&mut buf[..])
.map_err(|err| format!("failed reading serial port ({})", err))?;
Ok(String::from_utf8(buf)
.map_err(|err| format!("failed converting utf8 buffer ({})", err))?)
}
pub fn write(&self, msg: &str) -> VerboseResult<()> {
self.serial_port
.try_borrow_mut()?
.write(msg.as_bytes())
.map_err(|err| format!("failed writing to serial port ({})", err))?;
Ok(())
}
}

View file

@ -1,55 +1,24 @@
use serial;
use serial::prelude::*;
mod port;
use serial;
use utilities::prelude::*;
use std::ffi::OsStr;
use std::time::Duration;
use port::{Port, PortSettings};
fn main() -> VerboseResult<()> {
let mut port = open("/dev/ttyACM0")?;
let settings = PortSettings {
baud_rate: serial::Baud9600,
char_size: serial::Bits8,
parity: serial::ParityNone,
stop_bits: serial::Stop1,
flow_control: serial::FlowNone,
};
write(&mut port, "hello world")?;
let msg = read(&mut port)?;
let port = Port::open("/dev/macroboard/tty00000000", settings)?;
loop {
if let Ok(msg) = port.read() {
println!("{}", msg);
Ok(())
}
fn open<T: AsRef<OsStr> + ?Sized>(port: &T) -> VerboseResult<impl SerialPort> {
let mut port = serial::open(port).map_err(|_| "could not open serial port")?;
port.reconfigure(&|port_settings| {
port_settings.set_baud_rate(serial::Baud9600)?;
port_settings.set_char_size(serial::Bits8);
port_settings.set_parity(serial::ParityNone);
port_settings.set_stop_bits(serial::Stop1);
port_settings.set_flow_control(serial::FlowNone);
Ok(())
})
.map_err(|_| "failed configuring serial port")?;
port.set_timeout(Duration::from_millis(1000))
.map_err(|_| "failed setting time out for serial port")?;
Ok(port)
}
fn read<T: SerialPort>(port: &mut T) -> VerboseResult<String> {
let mut buf: Vec<u8> = (0..255).collect();
port.read(&mut buf[..])
.map_err(|_| "failed reading serial port")?;
Ok(String::from_utf8(buf).map_err(|_| "failed converting utf8 buffer")?)
}
fn write<T: SerialPort>(port: &mut T, msg: &str) -> VerboseResult<()> {
port.write(msg.as_bytes())
.map_err(|_| "failed writing to serial port")?;
Ok(())
}