Moved flipdot code to its own file

This commit is contained in:
Sebastian 2022-10-06 19:15:57 +02:00
parent e648234467
commit c5dfb6e3b1
2 changed files with 84 additions and 76 deletions

82
src/flipdot.rs Normal file
View File

@ -0,0 +1,82 @@
use core::fmt::Debug;
use embedded_hal::digital::v2::OutputPin;
fn toggle_clock<ClockPin, Error>(clock_pin: &mut ClockPin, delay: &mut cortex_m::delay::Delay)
where
ClockPin: OutputPin<Error = Error>,
Error: Debug,
{
clock_pin.set_high().unwrap();
delay.delay_us(1);
clock_pin.set_low().unwrap();
delay.delay_us(1);
}
fn shift_out_bits<ClockPin, DataPin, Error>(
data: u32,
len: u8,
clock_pin: &mut ClockPin,
data_pin: &mut DataPin,
delay: &mut cortex_m::delay::Delay,
) where
ClockPin: OutputPin<Error = Error>,
DataPin: OutputPin<Error = Error>,
Error: Debug,
{
for i in 0..len {
if data & (1 << (len - i - 1) as u32) != 0 {
data_pin.set_high().unwrap();
} else {
data_pin.set_low().unwrap();
}
toggle_clock(clock_pin, delay);
}
}
pub fn write_display<
ColClockPin,
ColDataPin,
RowClockPin,
RowDataPin,
StrobePin,
WhiteOEPin,
BlackOEPin,
Error,
>(
pixels: &[u32; 16],
col_clock_pin: &mut ColClockPin,
col_data_pin: &mut ColDataPin,
row_clock_pin: &mut RowClockPin,
row_data_pin: &mut RowDataPin,
strobe_pin: &mut StrobePin,
white_oe_pin: &mut WhiteOEPin,
black_oe_pin: &mut BlackOEPin,
delay: &mut cortex_m::delay::Delay,
) where
ColClockPin: OutputPin<Error = Error>,
ColDataPin: OutputPin<Error = Error>,
RowClockPin: OutputPin<Error = Error>,
RowDataPin: OutputPin<Error = Error>,
StrobePin: OutputPin<Error = Error>,
WhiteOEPin: OutputPin<Error = Error>,
BlackOEPin: OutputPin<Error = Error>,
Error: Debug,
{
for y in 0..16 {
shift_out_bits(pixels[y], 24, col_clock_pin, col_data_pin, delay);
shift_out_bits(1 << y, 16, row_clock_pin, row_data_pin, delay);
strobe_pin.set_high().unwrap();
delay.delay_us(1);
strobe_pin.set_low().unwrap();
white_oe_pin.set_high().unwrap();
delay.delay_ms(10);
white_oe_pin.set_low().unwrap();
black_oe_pin.set_high().unwrap();
delay.delay_ms(10);
black_oe_pin.set_low().unwrap();
}
}

View File

@ -8,7 +8,6 @@ use bsp::entry;
use defmt::*;
use defmt_rtt as _;
use embedded_hal::digital::v2::OutputPin;
use num_traits::{Float, FloatConst};
use panic_probe as _;
// Provide an alias for our BSP so we can switch targets quickly.
@ -23,81 +22,8 @@ use bsp::hal::{
watchdog::Watchdog,
};
fn toggle_clock<ClockPin>(clock_pin: &mut ClockPin, delay: &mut cortex_m::delay::Delay)
where
ClockPin: OutputPin,
{
clock_pin.set_high();
delay.delay_us(1);
clock_pin.set_low();
delay.delay_us(1);
}
fn shift_out_bits<ClockPin, DataPin>(
data: u32,
len: u8,
clock_pin: &mut ClockPin,
data_pin: &mut DataPin,
delay: &mut cortex_m::delay::Delay,
) where
ClockPin: OutputPin,
DataPin: OutputPin,
{
for i in 0..len {
if data & (1 << (len - i - 1) as u32) != 0 {
data_pin.set_high();
} else {
data_pin.set_low();
}
toggle_clock(clock_pin, delay);
}
}
fn write_display<
ColClockPin,
ColDataPin,
RowClockPin,
RowDataPin,
StrobePin,
WhiteOEPin,
BlackOEPin,
>(
pixels: &[u32; 16],
col_clock_pin: &mut ColClockPin,
col_data_pin: &mut ColDataPin,
row_clock_pin: &mut RowClockPin,
row_data_pin: &mut RowDataPin,
strobe_pin: &mut StrobePin,
white_oe_pin: &mut WhiteOEPin,
black_oe_pin: &mut BlackOEPin,
delay: &mut cortex_m::delay::Delay,
) where
ColClockPin: OutputPin,
ColDataPin: OutputPin,
RowClockPin: OutputPin,
RowDataPin: OutputPin,
StrobePin: OutputPin,
WhiteOEPin: OutputPin,
BlackOEPin: OutputPin,
{
for y in 0..16 {
shift_out_bits(pixels[y], 24, col_clock_pin, col_data_pin, delay);
shift_out_bits(1 << y, 16, row_clock_pin, row_data_pin, delay);
strobe_pin.set_high();
delay.delay_us(1);
strobe_pin.set_low();
white_oe_pin.set_high();
delay.delay_ms(10);
white_oe_pin.set_low();
black_oe_pin.set_high();
delay.delay_ms(10);
black_oe_pin.set_low();
}
}
mod flipdot;
use crate::flipdot::write_display;
#[entry]
fn main() -> ! {