use esp_hal::gpio::Output;
pub struct Hub75Pins<'d> {
r1: Output<'d>,
g1: Output<'d>,
b1: Output<'d>,
r2: Output<'d>,
g2: Output<'d>,
b2: Output<'d>,
a: Output<'d>,
b: Output<'d>,
c: Output<'d>,
d: Output<'d>,
e: Output<'d>,
clk: Output<'d>,
lat: Output<'d>,
oe: Output<'d>,
}
fn delay_spin(n: u32) {
for _ in 0..n {
core::hint::spin_loop();
}
}
impl<'d> Hub75Pins<'d> {
pub fn new(
r1: Output<'d>,
g1: Output<'d>,
b1: Output<'d>,
r2: Output<'d>,
g2: Output<'d>,
b2: Output<'d>,
a: Output<'d>,
b: Output<'d>,
c: Output<'d>,
d: Output<'d>,
e: Output<'d>,
clk: Output<'d>,
lat: Output<'d>,
oe: Output<'d>,
) -> 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();
} else {
self.a.set_low();
}
if row & 0x02 != 0 {
self.b.set_high();
} else {
self.b.set_low();
}
if row & 0x04 != 0 {
self.c.set_high();
} else {
self.c.set_low();
}
if row & 0x08 != 0 {
self.d.set_high();
} else {
self.d.set_low();
}
if row & 0x10 != 0 {
self.e.set_high();
} else {
self.e.set_low();
}
}
pub fn set_data(&mut self, r1: bool, g1: bool, b1: bool, r2: bool, g2: bool, b2: bool) {
if r1 {
self.r1.set_high();
} else {
self.r1.set_low();
}
if g1 {
self.g1.set_high();
} else {
self.g1.set_low();
}
if b1 {
self.b1.set_high();
} else {
self.b1.set_low();
}
if r2 {
self.r2.set_high();
} else {
self.r2.set_low();
}
if g2 {
self.g2.set_high();
} else {
self.g2.set_low();
}
if b2 {
self.b2.set_high();
} else {
self.b2.set_low();
}
}
pub fn pulse_clk(&mut self) {
self.clk.set_high();
delay_spin(30);
self.clk.set_low();
delay_spin(30);
}
pub fn latch(&mut self) {
self.lat.set_high();
delay_spin(100);
self.lat.set_low();
}
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();
}
pub fn oe_on(&mut self) {
self.oe.set_low();
}
}