1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
extern crate embedded_hal as hal;
extern crate nb;
extern crate serial;

use serial::prelude::*;
use std::ffi::OsStr;
use std::io::prelude::*;

pub use serial::BaudRate;
pub use serial::CharSize;
pub use serial::FlowControl;
pub use serial::Parity;
pub use serial::PortSettings;
pub use serial::StopBits;

use std::cell::RefCell;
use std::rc::Rc;

/// Newtype over [`serial-rs`](https://crates.io/crates/serial)'s serial port abstraction.
pub struct Serial {
    inner: Rc<RefCell<serial::SystemPort>>,
}

pub struct Tx {
    inner: Rc<RefCell<serial::SystemPort>>,
}

pub struct Rx {
    inner: Rc<RefCell<serial::SystemPort>>,
}

impl Serial {
    pub fn new<T: AsRef<OsStr> + ?Sized>(
        port: &T,
        settings: &serial::PortSettings,
    ) -> serial::Result<Self> {
        let mut port = serial::open(&port)?;
        port.configure(settings)?;
        Ok(Serial {
            inner: Rc::new(RefCell::new(port)),
        })
    }

    pub fn split(self) -> (Tx, Rx) {
        (
            Tx {
                inner: Rc::clone(&self.inner),
            },
            Rx {
                inner: Rc::clone(&self.inner),
            },
        )
    }
}

impl hal::serial::Read<u8> for Rx {
    type Error = serial::Error;

    fn read(&mut self) -> nb::Result<u8, Self::Error> {
        let mut buf: [u8; 1] = [0];
        let mut inner = (*self.inner).borrow_mut();
        match inner.read(&mut buf) {
            Ok(_) => Ok(buf[0]),
            Err(e) => match e.kind() {
                std::io::ErrorKind::WouldBlock => Err(nb::Error::WouldBlock),
                std::io::ErrorKind::TimedOut => Err(nb::Error::WouldBlock),
                _ => Err(nb::Error::Other(serial::Error::new(
                    serial::ErrorKind::Io(e.kind()),
                    "bad read",
                ))),
            },
        }
    }
}

impl hal::serial::Write<u8> for Tx {
    type Error = serial::Error;

    fn write(&mut self, byte: u8) -> nb::Result<(), Self::Error> {
        let mut inner = (*self.inner).borrow_mut();
        match inner.write(&[byte]) {
            Ok(_) => Ok(()),
            Err(e) => Err(nb::Error::Other(serial::Error::new(
                serial::ErrorKind::Io(e.kind()),
                "bad write",
            ))),
        }
    }

    fn flush(&mut self) -> nb::Result<(), Self::Error> {
        let mut inner = (*self.inner).borrow_mut();
        match inner.flush() {
            Ok(_) => Ok(()),
            Err(e) => Err(nb::Error::Other(serial::Error::new(
                serial::ErrorKind::Io(e.kind()),
                "bad flush",
            ))),
        }
    }
}