use embedded_hal::digital::OutputPin;
pub struct Hub75Pins<P> {
r1: P,
g1: P,
b1: P,
r2: P,
g2: P,
b2: P,
a: P,
b: P,
c: P,
d: P,
e: P,
clk: P,
lat: P,
oe: P,
}
fn delay_spin(n: u32) {
for _ in 0..n {
core::hint::spin_loop();
}
}
impl<P: OutputPin> Hub75Pins<P> {
pub fn new(
r1: P,
g1: P,
b1: P,
r2: P,
g2: P,
b2: P,
a: P,
b: P,
c: P,
d: P,
e: P,
clk: P,
lat: P,
oe: P,
) -> Self {
Self {
r1,
g1,
b1,
r2,
g2,
b2,
a,
b,
c,
d,
e,
clk,
lat,
oe,
}
}
pub fn set_row(&mut self, row: u8) {
if row & 0x01 != 0 {
self.a.set_high().ok();
} else {
self.a.set_low().ok();
}
if row & 0x02 != 0 {
self.b.set_high().ok();
} else {
self.b.set_low().ok();
}
if row & 0x04 != 0 {
self.c.set_high().ok();
} else {
self.c.set_low().ok();
}
if row & 0x08 != 0 {
self.d.set_high().ok();
} else {
self.d.set_low().ok();
}
if row & 0x10 != 0 {
self.e.set_high().ok();
} else {
self.e.set_low().ok();
}
}
pub fn set_data(&mut self, r1: bool, g1: bool, b1: bool, r2: bool, g2: bool, b2: bool) {
if r1 {
self.r1.set_high().ok();
} else {
self.r1.set_low().ok();
}
if g1 {
self.g1.set_high().ok();
} else {
self.g1.set_low().ok();
}
if b1 {
self.b1.set_high().ok();
} else {
self.b1.set_low().ok();
}
if r2 {
self.r2.set_high().ok();
} else {
self.r2.set_low().ok();
}
if g2 {
self.g2.set_high().ok();
} else {
self.g2.set_low().ok();
}
if b2 {
self.b2.set_high().ok();
} else {
self.b2.set_low().ok();
}
}
pub fn pulse_clk(&mut self) {
self.clk.set_high().ok();
delay_spin(30);
self.clk.set_low().ok();
delay_spin(30);
}
pub fn latch(&mut self) {
self.lat.set_high().ok();
delay_spin(100);
self.lat.set_low().ok();
}
pub fn shift_pixel(&mut self, r1: bool, g1: bool, b1: bool, r2: bool, g2: bool, b2: bool) {
self.set_data(r1, g1, b1, r2, g2, b2);
self.pulse_clk();
}
pub fn oe_off(&mut self) {
self.oe.set_high().ok();
}
pub fn oe_on(&mut self) {
self.oe.set_low().ok();
}
}