From c5dfb6e3b14f3b6818e6e54e226cfa68895a7836 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Thu, 6 Oct 2022 19:15:57 +0200 Subject: [PATCH] Moved flipdot code to its own file --- src/flipdot.rs | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 78 ++--------------------------------------------- 2 files changed, 84 insertions(+), 76 deletions(-) create mode 100644 src/flipdot.rs diff --git a/src/flipdot.rs b/src/flipdot.rs new file mode 100644 index 0000000..e63cd16 --- /dev/null +++ b/src/flipdot.rs @@ -0,0 +1,82 @@ +use core::fmt::Debug; +use embedded_hal::digital::v2::OutputPin; + +fn toggle_clock(clock_pin: &mut ClockPin, delay: &mut cortex_m::delay::Delay) +where + ClockPin: OutputPin, + Error: Debug, +{ + clock_pin.set_high().unwrap(); + delay.delay_us(1); + clock_pin.set_low().unwrap(); + delay.delay_us(1); +} + +fn shift_out_bits( + data: u32, + len: u8, + clock_pin: &mut ClockPin, + data_pin: &mut DataPin, + delay: &mut cortex_m::delay::Delay, +) where + ClockPin: OutputPin, + DataPin: OutputPin, + 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, + ColDataPin: OutputPin, + RowClockPin: OutputPin, + RowDataPin: OutputPin, + StrobePin: OutputPin, + WhiteOEPin: OutputPin, + BlackOEPin: OutputPin, + 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(); + } +} diff --git a/src/main.rs b/src/main.rs index 665c774..027ffb1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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(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( - 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() -> ! {