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
//! # Shared pin
//!
//! **NOTE: This module is for internal use only! It is exposed only, because it is used in
//! public interface definitions**
//!
//! This module implements a "shared pin". A pin, which is used by multiple components of the IC.
//! Because of this, a "shared pin" must be cloneable to allow controlling it from multiple
//! components of the IC.
//!
//! In this crate, the [SharedPin] is used by the [L293x](crate::L293x) struct to define the two
//! shared enable pins of the four [HalfH](crate::bridge::HalfH) bridges, which the L293x internally
//! consists of.
//!
//! # Note
//!
//! Please keep in mind, that because of its shared nature, changing the state of a [SharedPin],
//! always has side effects to multiple parts of the IC!
extern crate alloc;
use alloc::rc::Rc;
use core::cell::RefCell;
use embedded_hal::digital::{ErrorType, OutputPin, StatefulOutputPin};
/// A common pin used by multiple parts of the circuit.
///
/// This struct is used in the [L293x](crate::L293x) struct to share the common
/// enable pins with the [Half-H bridges](crate::bridge::HalfH).
///
/// A shared pin is a pin, which always affects all parts using it. Thus, setting the
/// state of this pin will always have side effects to multiple parts of the circuit.
#[derive(Debug)]
#[repr(transparent)]
pub struct SharedPin<P>
where
P: OutputPin,
{
pin: Rc<RefCell<P>>,
}
impl<P> SharedPin<P>
where
P: OutputPin,
{
pub(crate) fn new(pin: P) -> Self {
Self {
pin: Rc::new(RefCell::new(pin)),
}
}
pub(crate) fn clone(&self) -> Self {
Self {
pin: Rc::clone(&self.pin),
}
}
}
impl<P> ErrorType for SharedPin<P>
where
P: OutputPin,
{
type Error = P::Error;
}
impl<P> OutputPin for SharedPin<P>
where
P: OutputPin,
{
#[inline]
fn set_low(&mut self) -> Result<(), Self::Error> {
self.pin.borrow_mut().set_low()
}
#[inline]
fn set_high(&mut self) -> Result<(), Self::Error> {
self.pin.borrow_mut().set_high()
}
}
impl<P> StatefulOutputPin for SharedPin<P>
where
P: StatefulOutputPin,
{
#[inline]
fn is_set_high(&mut self) -> Result<bool, Self::Error> {
self.pin.borrow_mut().is_set_high()
}
#[inline]
fn is_set_low(&mut self) -> Result<bool, Self::Error> {
self.pin.borrow_mut().is_set_low()
}
}