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

View file

@ -9,6 +9,11 @@ use std::io;
pub use serialport::{DataBits, FlowControl, Parity, SerialPortSettings, StopBits};
pub use std::time::Duration;
pub enum SerialReadResult {
Message(String),
Timeout,
}
pub struct Port {
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();
match self.serial_port.try_borrow_mut()?.read(&mut buf[..]) {
Ok(t) => Ok(String::from_utf8(buf[0..t].to_vec())
.map_err(|err| format!("failed converting utf8 buffer ({})", err))?),
Err(ref e) if e.kind() == io::ErrorKind::TimedOut => Ok(String::new()),
Ok(t) => Ok(SerialReadResult::Message(
String::from_utf8(buf[0..t].to_vec())
.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)),
}
}
#[allow(unused)]
pub fn write(&self, msg: &str) -> VerboseResult<()> {
self.serial_port
.try_borrow_mut()?

View file

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