feat(neopixel): first commit
This commit is contained in:
96
src/lib.rs
Normal file
96
src/lib.rs
Normal file
@@ -0,0 +1,96 @@
|
||||
#![no_std]
|
||||
#![feature(asm_experimental_arch)]
|
||||
|
||||
extern crate core;
|
||||
|
||||
mod macros;
|
||||
mod types;
|
||||
|
||||
use types::*;
|
||||
|
||||
use core::arch::asm;
|
||||
|
||||
use arduino_hal::{
|
||||
delay_us,
|
||||
hal::port::{PD0, PD1, PD2, PD3, PD4, PD5, PD6, PD7},
|
||||
pac::PORTD,
|
||||
port::{mode::Output, Pin, PinOps},
|
||||
};
|
||||
use avr_device::interrupt::free;
|
||||
use neopixel_macros::impl_static_pin;
|
||||
|
||||
use smart_leds::{SmartLedsWrite, RGB8};
|
||||
|
||||
const LAST_BIT: u8 = 0x80;
|
||||
|
||||
#[repr(transparent)]
|
||||
pub struct Neopixel<P> {
|
||||
_pin: Pin<Output, P>,
|
||||
}
|
||||
|
||||
impl<P> Neopixel<P>
|
||||
where
|
||||
P: PinOps + StaticPin,
|
||||
{
|
||||
#[inline]
|
||||
pub const fn new(_pin: Pin<Output, P>) -> Self {
|
||||
Self { _pin }
|
||||
}
|
||||
}
|
||||
|
||||
impl<P> SmartLedsWrite for Neopixel<P>
|
||||
where
|
||||
P: PinOps + StaticPin,
|
||||
{
|
||||
type Error = ();
|
||||
type Color = smart_leds::RGB<u8>;
|
||||
fn write<T, I>(&mut self, iterator: T) -> Result<(), Self::Error>
|
||||
where
|
||||
T: IntoIterator<Item = I>,
|
||||
I: Into<Self::Color>,
|
||||
{
|
||||
free(|_cs| {
|
||||
for i in iterator {
|
||||
let RGB8 { r, g, b } = i.into();
|
||||
for value in [g, r, b] {
|
||||
self.priv_write(value);
|
||||
}
|
||||
}
|
||||
});
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl<P> Neopixel<P>
|
||||
where
|
||||
P: PinOps + StaticPin,
|
||||
{
|
||||
fn priv_write(&self, data: u8) {
|
||||
let (mut count, mut data, port_data, pin_data) = (8, data, P::read(), 1 << P::PIN_NUM);
|
||||
unsafe {
|
||||
while count > 0 {
|
||||
P::write(port_data | pin_data);
|
||||
asm!("rjmp +0");
|
||||
if data & LAST_BIT == 0 {
|
||||
P::write(port_data & !pin_data);
|
||||
}
|
||||
asm!(
|
||||
"
|
||||
rjmp +0
|
||||
rjmp +0
|
||||
"
|
||||
);
|
||||
P::write(port_data & !pin_data);
|
||||
asm!(
|
||||
"
|
||||
rjmp +0
|
||||
rjmp +0
|
||||
rjmp +0
|
||||
"
|
||||
);
|
||||
data <<= 1;
|
||||
count -= 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user