A personal rust firmware for the Badger 2040 W

wip it up

Changed files
+46 -38
src
badge_display
-2
src/badge_display/display_image.rs
··· 1 - use core::sync::atomic::{AtomicBool, AtomicU8}; 2 - 3 use embedded_graphics::prelude::Point; 4 5 use super::CURRENT_IMAGE;
··· 1 use embedded_graphics::prelude::Point; 2 3 use super::CURRENT_IMAGE;
+46 -36
src/badge_display/mod.rs
··· 1 pub mod display_image; 2 3 use core::sync::atomic::{AtomicBool, AtomicU32, AtomicU8}; 4 use display_image::get_current_image; 5 use embassy_embedded_hal::shared_bus::asynch::spi::SpiDevice; 6 use embassy_rp::gpio; ··· 9 use embedded_graphics::{ 10 image::Image, 11 mono_font::{ascii::*, MonoTextStyle}, 12 - pixelcolor::BinaryColor, 13 prelude::*, 14 - primitives::{PrimitiveStyle, Rectangle}, 15 }; 16 use embedded_text::{ 17 alignment::HorizontalAlignment, ··· 28 29 use crate::Spi0Bus; 30 31 pub static CURRENT_IMAGE: AtomicU8 = AtomicU8::new(0); 32 pub static CHANGE_IMAGE: AtomicBool = AtomicBool::new(true); 33 - 34 - static WIFI_COUNT: AtomicU32 = AtomicU32::new(0); 35 36 #[embassy_executor::task] 37 pub async fn run_the_display( ··· 77 78 let _ = display.update().await; 79 80 - let delay: Duration = Duration::from_secs(30); 81 let mut text: String<16> = String::<16>::new(); 82 83 loop { 84 - let count = WIFI_COUNT.load(core::sync::atomic::Ordering::Relaxed); 85 - // let _ = core::fmt::write(&mut text, format_args!("Count: {}", count)); 86 - // let count_bounds = Rectangle::new(Point::new(0, 0), Size::new(WIDTH, 24)); 87 - // count_bounds 88 - // .into_styled( 89 - // PrimitiveStyleBuilder::default() 90 - // .stroke_color(BinaryColor::Off) 91 - // .fill_color(BinaryColor::On) 92 - // .stroke_width(1) 93 - // .build(), 94 - // ) 95 - // .draw(&mut display) 96 - // .unwrap(); 97 98 - // Text::new(text.as_str(), Point::new(8, 16), character_style) 99 - // .draw(&mut display) 100 - // .unwrap(); 101 102 - // // // Draw the text box. 103 - // let result = display 104 - // .partial_update(count_bounds.try_into().unwrap()) 105 - // .await; 106 - // match result { 107 - // Ok(_) => {} 108 - // Err(_) => { 109 - // info!("Error updating display"); 110 - // } 111 - // } 112 - // text.clear(); 113 - // let _ = display.clear(Rgb565::WHITE.into()); 114 - // let _ = display.update().await; 115 - WIFI_COUNT.store(count + 1, core::sync::atomic::Ordering::Relaxed); 116 117 if CHANGE_IMAGE.load(core::sync::atomic::Ordering::Relaxed) { 118 let current_image = get_current_image(); ··· 133 CHANGE_IMAGE.store(false, core::sync::atomic::Ordering::Relaxed); 134 } 135 136 - Timer::after(Duration::from_millis(500)).await; 137 } 138 }
··· 1 pub mod display_image; 2 3 use core::sync::atomic::{AtomicBool, AtomicU32, AtomicU8}; 4 + use defmt::*; 5 use display_image::get_current_image; 6 use embassy_embedded_hal::shared_bus::asynch::spi::SpiDevice; 7 use embassy_rp::gpio; ··· 10 use embedded_graphics::{ 11 image::Image, 12 mono_font::{ascii::*, MonoTextStyle}, 13 + pixelcolor::{BinaryColor, Rgb565}, 14 prelude::*, 15 + primitives::{PrimitiveStyle, PrimitiveStyleBuilder, Rectangle}, 16 + text::Text, 17 }; 18 use embedded_text::{ 19 alignment::HorizontalAlignment, ··· 30 31 use crate::Spi0Bus; 32 33 + //Display state 34 pub static CURRENT_IMAGE: AtomicU8 = AtomicU8::new(0); 35 pub static CHANGE_IMAGE: AtomicBool = AtomicBool::new(true); 36 + pub static WIFI_COUNT: AtomicU32 = AtomicU32::new(0); 37 38 #[embassy_executor::task] 39 pub async fn run_the_display( ··· 79 80 let _ = display.update().await; 81 82 + let cycle: Duration = Duration::from_millis(500); 83 + let mut first_run = true; 84 let mut text: String<16> = String::<16>::new(); 85 + let cycles_to_skip = 30; 86 + let mut cycles_since_last_clear = 0; 87 88 loop { 89 + if cycles_since_last_clear >= cycles_to_skip || first_run { 90 + let count = WIFI_COUNT.load(core::sync::atomic::Ordering::Relaxed); 91 + let _ = core::fmt::write(&mut text, format_args!("Count: {}", count)); 92 + let count_bounds = Rectangle::new(Point::new(0, 0), Size::new(WIDTH, 24)); 93 + count_bounds 94 + .into_styled( 95 + PrimitiveStyleBuilder::default() 96 + .stroke_color(BinaryColor::Off) 97 + .fill_color(BinaryColor::On) 98 + .stroke_width(1) 99 + .build(), 100 + ) 101 + .draw(&mut display) 102 + .unwrap(); 103 104 + Text::new(text.as_str(), Point::new(8, 16), character_style) 105 + .draw(&mut display) 106 + .unwrap(); 107 108 + // // Draw the text box. 109 + let result = display 110 + .partial_update(count_bounds.try_into().unwrap()) 111 + .await; 112 + match result { 113 + Ok(_) => {} 114 + Err(_) => { 115 + info!("Error updating display"); 116 + } 117 + } 118 + text.clear(); 119 + // let _ = display.clear(Rgb565::WHITE.into()); 120 + let _ = display.update().await; 121 + WIFI_COUNT.store(count + 1, core::sync::atomic::Ordering::Relaxed); 122 + cycles_since_last_clear = 0; 123 + } 124 125 if CHANGE_IMAGE.load(core::sync::atomic::Ordering::Relaxed) { 126 let current_image = get_current_image(); ··· 141 CHANGE_IMAGE.store(false, core::sync::atomic::Ordering::Relaxed); 142 } 143 144 + cycles_since_last_clear += 1; 145 + first_run = false; 146 + Timer::after(cycle).await; 147 } 148 }