#![no_std]
#![forbid(unsafe_code)]
#![cfg_attr(docsrs, feature(doc_cfg))]
#[cfg(all(feature = "hal-0_2", feature = "hal-1_0"))]
compile_error!(
"HAL feature \"hal-0_2\" and feature \"hal-1_0\" cannot be enabled at the same time"
);
#[cfg(not(any(feature = "hal-0_2", feature = "hal-1_0")))]
compile_error!("A HAL feature (\"hal-0_2\" or \"hal-1_0\") must be enabled");
#[cfg(feature = "hal-0_2")]
#[cfg_attr(docsrs, doc(cfg(feature = "hal-0_2")))]
use hal_0_2::digital::v2::OutputPin;
#[cfg(feature = "hal-1_0")]
#[cfg_attr(docsrs, doc(cfg(feature = "hal-1_0")))]
use hal_1_0::digital::OutputPin;
pub trait ChipSelect {
#[must_use]
fn is_auto_select(&self) -> bool;
fn auto_select(&mut self) {
if self.is_auto_select() {
self.select()
}
}
fn select(&mut self);
fn deselect(&mut self);
}
pub trait ActiveLow {}
pub trait ActiveHigh {}
pub struct ChipSelectActiveLow<Pin>(bool, Pin);
pub struct ChipSelectActiveHigh<Pin>(bool, Pin);
impl<Pin> ChipSelectActiveLow<Pin>
where
Pin: OutputPin,
{
pub const fn new(pin: Pin) -> Self {
Self(false, pin)
}
pub fn with_auto_select(mut self, enabled: bool) -> Self {
self.0 = enabled;
self
}
pub fn is_auto_select(&self) -> bool {
self.0
}
pub fn auto_select(&mut self) {
if self.0 {
self.select()
}
}
pub fn select(&mut self) {
<Pin as OutputPin>::set_low(&mut self.1).ok();
}
pub fn deselect(&mut self) {
<Pin as OutputPin>::set_high(&mut self.1).ok();
}
#[must_use]
pub fn into_inner(self) -> Pin {
self.1
}
}
impl<Pin> ChipSelectActiveHigh<Pin>
where
Pin: OutputPin,
{
pub const fn new(pin: Pin) -> Self {
Self(false, pin)
}
pub fn with_auto_select(mut self, enabled: bool) -> Self {
self.0 = enabled;
self
}
pub fn is_auto_select(&self) -> bool {
self.0
}
pub fn auto_select(&mut self) {
if self.0 {
self.select()
}
}
pub fn select(&mut self) {
<Pin as OutputPin>::set_high(&mut self.1).ok();
}
pub fn deselect(&mut self) {
<Pin as OutputPin>::set_low(&mut self.1).ok();
}
#[must_use]
pub fn into_inner(self) -> Pin {
self.1
}
}
impl<Pin> From<Pin> for ChipSelectActiveLow<Pin>
where
Pin: OutputPin,
{
fn from(value: Pin) -> Self {
Self::new(value)
}
}
impl<Pin> From<Pin> for ChipSelectActiveHigh<Pin>
where
Pin: OutputPin,
{
fn from(value: Pin) -> Self {
Self::new(value)
}
}
impl<Pin> ActiveLow for ChipSelectActiveLow<Pin> where Pin: OutputPin {}
impl<Pin> ActiveHigh for ChipSelectActiveHigh<Pin> where Pin: OutputPin {}
impl<Pin> ChipSelect for ChipSelectActiveLow<Pin>
where
Pin: OutputPin,
{
fn is_auto_select(&self) -> bool {
self.is_auto_select()
}
fn select(&mut self) {
self.select()
}
fn deselect(&mut self) {
self.deselect()
}
}
impl<Pin> ChipSelect for ChipSelectActiveHigh<Pin>
where
Pin: OutputPin,
{
fn is_auto_select(&self) -> bool {
self.is_auto_select()
}
fn select(&mut self) {
self.select()
}
fn deselect(&mut self) {
self.deselect()
}
}