refactor!: use for loop instead while, use PhantomData<Pin<PINMODE, P>>, rename method priv_write -> write_color_channel and etc
This commit is contained in:
parent
0a668af6e4
commit
42d63292a8
70
src/lib.rs
70
src/lib.rs
|
@ -1,7 +1,9 @@
|
||||||
#![no_std]
|
#![no_std]
|
||||||
#![feature(asm_experimental_arch)]
|
#![feature(asm_experimental_arch)]
|
||||||
|
|
||||||
|
use core::{arch::asm, marker::PhantomData};
|
||||||
|
|
||||||
|
use arduino_hal::port::{mode::Output, Pin, PinOps};
|
||||||
use avr_device::interrupt::free;
|
use avr_device::interrupt::free;
|
||||||
|
|
||||||
use smart_leds::{SmartLedsWrite, RGB8};
|
use smart_leds::{SmartLedsWrite, RGB8};
|
||||||
|
@ -11,7 +13,7 @@ const MSB: u8 = 0x80;
|
||||||
|
|
||||||
#[repr(transparent)]
|
#[repr(transparent)]
|
||||||
pub struct Neopixel<P> {
|
pub struct Neopixel<P> {
|
||||||
_pin: Pin<Output, P>,
|
_pin: PhantomData<Pin<Output, P>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<P> Neopixel<P>
|
impl<P> Neopixel<P>
|
||||||
|
@ -19,9 +21,38 @@ where
|
||||||
P: PinOps + StaticPinOps,
|
P: PinOps + StaticPinOps,
|
||||||
{
|
{
|
||||||
#[inline]
|
#[inline]
|
||||||
pub const fn new(_pin: Pin<Output, P>) -> Self {
|
pub fn new(_pin: Pin<Output, P>) -> Self {
|
||||||
Self { _pin }
|
Self {
|
||||||
|
_pin: PhantomData {},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn write_color_channel(&self, data: u8) {
|
||||||
|
let (mut data, port_data) = (data, P::read());
|
||||||
|
unsafe {
|
||||||
|
for _ in 0..8 {
|
||||||
|
P::write(port_data | P::PIN_POS);
|
||||||
|
asm!("rjmp +0");
|
||||||
if data & MSB == 0 {
|
if data & MSB == 0 {
|
||||||
|
P::write(port_data & !P::PIN_POS);
|
||||||
|
}
|
||||||
|
asm!(
|
||||||
|
"
|
||||||
|
rjmp +0
|
||||||
|
rjmp +0
|
||||||
|
"
|
||||||
|
);
|
||||||
|
P::write(port_data & !P::PIN_POS);
|
||||||
|
asm!(
|
||||||
|
"
|
||||||
|
rjmp +0
|
||||||
|
rjmp +0
|
||||||
|
rjmp +0
|
||||||
|
"
|
||||||
|
);
|
||||||
|
data <<= 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -40,7 +71,7 @@ where
|
||||||
for i in iterator {
|
for i in iterator {
|
||||||
let RGB8 { r, g, b } = i.into();
|
let RGB8 { r, g, b } = i.into();
|
||||||
for value in [g, r, b] {
|
for value in [g, r, b] {
|
||||||
self.priv_write(value);
|
self.write_color_channel(value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -48,34 +79,3 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<P> Neopixel<P>
|
|
||||||
where
|
|
||||||
{
|
|
||||||
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");
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue