use core::ops::{Deref, DerefMut};
use avr_oxide::alloc::boxed::Box;
use avr_oxide::devices::button::Button;
use avr_oxide::devices::led::Led;
use avr_oxide::devices::masterclock::MasterClock;
use avr_oxide::devices::serialport::SerialPort;
use avr_oxide::devices::wallclock::WallClock;
use avr_oxide::hal::generic::port::Pin;
use avr_oxide::oxide::OxideSupervisor;
use avr_oxide::util::OwnOrBorrow;
pub mod masterclock;
pub mod wallclock;
pub mod led;
pub mod button;
pub mod serialport;
pub mod debouncer;
pub mod inverter;
pub type OxideButton<'a,'b> = Button<'a,OxideSupervisor<'b>>;
pub type OxideLed = Led;
pub type OxideMasterClock<'a,'b,T> = MasterClock<'a,T,OxideSupervisor<'b>>;
pub type OxideSerialPort<'a,'s> = SerialPort<'a,OxideSupervisor<'s>>;
pub type OxideWallClock<'a,'b,T> = WallClock<'a,T,OxideSupervisor<'b>>;
pub trait UsesPin {
fn using<OP: Into<OwnOrBorrow<'static, dyn Pin>>>(pin: OP) -> Self;
fn with_pin(pin: &'static dyn Pin) -> Self
where
Self: Sized
{
Self::using(pin)
}
fn static_using<OP: Into<OwnOrBorrow<'static, dyn Pin>>>(pin: OP) -> &'static mut Self where Self:Sized {
Box::leak(Box::new(Self::using(pin)))
}
fn static_with_pin(pin: &'static dyn Pin) -> &'static mut Self where Self:Sized {
Box::leak(Box::new(Self::with_pin(pin)))
}
}
pub struct Handle<T>
where
T: avr_oxide::devices::internal::StaticShareable
{
device: *mut T
}
pub(crate) mod internal {
pub trait StaticShareable {}
}
impl<T> Handle<T>
where
T: avr_oxide::devices::internal::StaticShareable
{
pub fn new(wrapping: T) -> Self {
let boxed = Box::new(wrapping);
Handle {
device: Box::leak(boxed) as *mut T
}
}
pub(crate) fn static_deref(&self) -> &'static T {
unsafe {
&*self.device
}
}
pub(crate) fn static_deref_mut(&self) -> &'static mut T {
unsafe {
&mut *self.device
}
}
}
impl<T> Deref for Handle<T>
where
T: avr_oxide::devices::internal::StaticShareable
{
type Target = T;
fn deref(&self) -> &Self::Target {
unsafe {
&*self.device
}
}
}
impl<T> DerefMut for Handle<T>
where
T: avr_oxide::devices::internal::StaticShareable
{
fn deref_mut(&mut self) -> &mut Self::Target {
unsafe {
&mut *self.device
}
}
}
impl<T> Clone for Handle<T>
where
T: avr_oxide::devices::internal::StaticShareable
{
fn clone(&self) -> Self {
Self {
device: self.device
}
}
}
impl<T> Copy for Handle<T>
where
T: avr_oxide::devices::internal::StaticShareable
{
}
unsafe impl<T> Send for Handle<T>
where
T: avr_oxide::devices::internal::StaticShareable + Send + 'static,
{ }
unsafe impl<T> Sync for Handle<T>
where
T: avr_oxide::devices::internal::StaticShareable + Sync + 'static,
{ }