Skip to content

Commit

Permalink
Redefine midi::Error as an enum
Browse files Browse the repository at this point in the history
  • Loading branch information
antoinevg committed May 17, 2021
1 parent c12a67f commit b4d30f3
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions src/midi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,15 @@ mod log {
}


// - Interface ----------------------------------------------------------------
// - Error --------------------------------------------------------------------

#[derive(Debug)]
pub enum Error {
Usart
}

type Error = u32;

// - Interface ----------------------------------------------------------------

#[repr(C)]
pub struct Interface<'a> {
Expand All @@ -37,7 +43,8 @@ impl<'a> Interface<'a> {
let serial = usart1.serial(pins, 31_250.bps(), usart1_rec, clocks);
match serial {
Ok(mut serial) => {
serial.listen(hal::serial::Event::Rxne); // TODO Tx & errors too
serial.listen(hal::serial::Event::Rxne);
serial.listen(hal::serial::Event::Txne);
let (tx, rx) = serial.split();
Ok(Self {
rx,
Expand All @@ -49,7 +56,7 @@ impl<'a> Interface<'a> {
_marker: core::marker::PhantomData
})
},
Err(_e) => Err(0), // TODO pass actual error up
Err(_e) => Err(Error::Usart),
}
}

Expand All @@ -58,15 +65,15 @@ impl<'a> Interface<'a> {
pub fn start(mut self, function_ptr:fn (u8)) -> Result<Self, Error> {
self.function_ptr = Some(function_ptr);
unsafe { pac::NVIC::unmask(pac::Interrupt::USART1); }
Ok(self) // TODO type state for started interface
Ok(self)
}

/// assign closure for interrupt callback and start interface
#[cfg(any(feature = "alloc"))]
pub fn start<F: FnMut(u8) + Send + 'a>(mut self, closure: F) -> Result<Self, Error> {
self.closure = Some(Box::new(closure));
unsafe { pac::NVIC::unmask(pac::Interrupt::USART1); }
Ok(self) // TODO type state for started interface
Ok(self)
}

pub fn handle_interrupt_usart1(&mut self) -> Result<(), Error> {
Expand Down Expand Up @@ -98,7 +105,7 @@ impl<'a> Interface<'a> {
fn invoke_callback(&mut self, byte: u8) {
#[cfg(not(feature = "alloc"))]
if let Some(function_ptr) = self.function_ptr.as_mut() {
function_ptr(byte); // TODO should we pass self?
function_ptr(byte);
}

#[cfg(any(feature = "alloc"))]
Expand Down

0 comments on commit b4d30f3

Please sign in to comment.