First tests

This commit is contained in:
hodasemi 2019-11-24 12:41:15 +01:00
parent 7671b4a90c
commit 5ec6ace462
3 changed files with 101 additions and 33 deletions

View file

@ -7,16 +7,51 @@ enum State {
WaitingForAck, WaitingForAck,
}; };
struct Button {
const byte pin_id;
uint8_t last_state;
};
/// ---------------------------------------------------------------- /// ----------------------------------------------------------------
/// ----------------------- Board Specifics ------------------------ /// ----------------------- Board Specifics ------------------------
/// ---------------------------------------------------------------- /// ----------------------------------------------------------------
const int pin_count = 8; const int button_count = 8;
const byte pins[pin_count] = { const Button buttons[button_count] = {
2, 3, 4, 5, 6, 7, 8, 9 {
.pin_id = 2,
.last_state = LOW
},
{
.pin_id = 3,
.last_state = LOW
},
{
.pin_id = 4,
.last_state = LOW
},
{
.pin_id = 5,
.last_state = LOW
},
{
.pin_id = 6,
.last_state = LOW
},
{
.pin_id = 7,
.last_state = LOW
},
{
.pin_id = 8,
.last_state = LOW
},
{
.pin_id = 9,
.last_state = LOW
}
}; };
const char message_start = '<'; const char message_start = '<';
@ -44,38 +79,62 @@ void setup() {
current_state = WaitingForInput; current_state = WaitingForInput;
// setup pins // setup pins
for (byte i = 0; i < pin_count; i++) { for (byte i = 0; i < button_count; i++) {
pinMode(pins[i], INPUT); pinMode(&buttons[i], INPUT);
} }
} }
void loop() { void loop() {
// check for possible input // check for possible input
/*
if (current_state == WaitingForInput) { if (current_state == WaitingForInput) {
for (byte i = 0; i < pin_count; i++) { for (byte i = 0; i < button_count; i++) {
check_pin(pins[i], i); check_pin(&buttons[i], i);
} }
} }
*/
// debug
Serial.println("test");
delay(2000);
} }
void check_pin(byte pin, byte index) { void check_pin(Button* button, byte index) {
if (digitalRead(pin) == HIGH) { if (digitalRead(button->pin_id) == HIGH) {
if (button->last_state == LOW) {
button->last_state = HIGH;
send_message("HIGH");
}
} else {
if (button->last_state == HIGH) {
button->last_state = LOW;
send_message("LOW");
}
}
}
void send_message(char c) {
// assemble message // assemble message
Serial.print(message_start); Serial.print(message_start);
Serial.print(index + 1); Serial.print(c);
Serial.print(message_end);
// force new line
Serial.print('\n');
}
void send_message(char c[]) {
// assemble message
Serial.print(message_start);
Serial.print(c);
Serial.print(message_end);
// force new line
Serial.print('\n');
}
void send_message(byte b) {
// assemble message
Serial.print(message_start);
Serial.print(b);
Serial.print(message_end); Serial.print(message_end);
// force new line // force new line
Serial.print('\n'); Serial.print('\n');
}
} }

View file

@ -9,6 +9,11 @@ use std::io;
pub use serialport::{DataBits, FlowControl, Parity, SerialPortSettings, StopBits}; pub use serialport::{DataBits, FlowControl, Parity, SerialPortSettings, StopBits};
pub use std::time::Duration; pub use std::time::Duration;
pub enum SerialReadResult {
Message(String),
Timeout,
}
pub struct Port { pub struct Port {
serial_port: RefCell<Box<dyn SerialPort>>, serial_port: RefCell<Box<dyn SerialPort>>,
} }
@ -31,17 +36,21 @@ impl Port {
}) })
} }
pub fn read(&self) -> VerboseResult<String> { #[allow(unused)]
pub fn read(&self) -> VerboseResult<SerialReadResult> {
let mut buf: Vec<u8> = (0..255).collect(); let mut buf: Vec<u8> = (0..255).collect();
match self.serial_port.try_borrow_mut()?.read(&mut buf[..]) { match self.serial_port.try_borrow_mut()?.read(&mut buf[..]) {
Ok(t) => Ok(String::from_utf8(buf[0..t].to_vec()) Ok(t) => Ok(SerialReadResult::Message(
.map_err(|err| format!("failed converting utf8 buffer ({})", err))?), String::from_utf8(buf[0..t].to_vec())
Err(ref e) if e.kind() == io::ErrorKind::TimedOut => Ok(String::new()), .map_err(|err| format!("failed converting utf8 buffer ({})", err))?,
)),
Err(ref e) if e.kind() == io::ErrorKind::TimedOut => Ok(SerialReadResult::Timeout),
Err(err) => create_error!(format!("failed reading serial port ({})", err)), Err(err) => create_error!(format!("failed reading serial port ({})", err)),
} }
} }
#[allow(unused)]
pub fn write(&self, msg: &str) -> VerboseResult<()> { pub fn write(&self, msg: &str) -> VerboseResult<()> {
self.serial_port self.serial_port
.try_borrow_mut()? .try_borrow_mut()?

View file

@ -17,9 +17,9 @@ fn main() -> VerboseResult<()> {
let port = Port::open(settings)?; let port = Port::open(settings)?;
loop { loop {
match port.read() { match port.read()? {
Ok(msg) => println!("{}", msg), SerialReadResult::Message(msg) => println!("{}", msg),
Err(err) => println!("{}", err), SerialReadResult::Timeout => (),
} }
} }
} }