A personal rust firmware for the Badger 2040 W

Update for makers day

+141 -130
+65 -109
Cargo.lock
··· 1 # This file is automatically @generated by Cargo. 2 # It is not intended for manual editing. 3 - version = 3 4 5 [[package]] 6 name = "aead" ··· 182 183 [[package]] 184 name = "bt-hci" 185 - version = "0.1.0" 186 source = "registry+https://github.com/rust-lang/crates.io-index" 187 - checksum = "499d74e90e6b1e61660adc8fb5f17aeac9487bced16f57c1f91a8783736ada53" 188 dependencies = [ 189 "defmt", 190 "embassy-sync 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", 191 - "embassy-time", 192 "embedded-io", 193 "embedded-io-async", 194 "futures-intrusive", ··· 319 320 [[package]] 321 name = "critical-section" 322 - version = "1.1.2" 323 source = "registry+https://github.com/rust-lang/crates.io-index" 324 - checksum = "7059fff8937831a9ae6f0fe4d658ffabf58f2ca96aa9dec1c889f936f705f216" 325 326 [[package]] 327 name = "crunchy" ··· 620 [[package]] 621 name = "embassy-futures" 622 version = "0.1.1" 623 - source = "registry+https://github.com/rust-lang/crates.io-index" 624 - checksum = "1f878075b9794c1e4ac788c95b728f26aa6366d32eeb10c7051389f898f7d067" 625 - 626 - [[package]] 627 - name = "embassy-futures" 628 - version = "0.1.1" 629 source = "git+https://github.com/embassy-rs/embassy.git?rev=f0a86070512ad739641cee7d9fa39d63f5c8a9f6#f0a86070512ad739641cee7d9fa39d63f5c8a9f6" 630 631 [[package]] ··· 867 ] 868 869 [[package]] 870 - name = "embassy_rp_w_template" 871 - version = "0.1.0" 872 - dependencies = [ 873 - "assign-resources", 874 - "bt-hci", 875 - "byte-slice-cast 1.2.2", 876 - "cortex-m", 877 - "cortex-m-rt", 878 - "critical-section", 879 - "cyw43", 880 - "cyw43-pio", 881 - "defmt", 882 - "defmt-rtt", 883 - "display-interface", 884 - "display-interface-spi", 885 - "embassy-embedded-hal", 886 - "embassy-executor", 887 - "embassy-futures 0.1.1 (git+https://github.com/embassy-rs/embassy.git)", 888 - "embassy-net", 889 - "embassy-net-wiznet", 890 - "embassy-rp", 891 - "embassy-sync 0.6.0 (git+https://github.com/embassy-rs/embassy.git?rev=f0a86070512ad739641cee7d9fa39d63f5c8a9f6)", 892 - "embassy-time", 893 - "embassy-usb 0.3.0", 894 - "embassy-usb-logger", 895 - "embedded-graphics", 896 - "embedded-hal 1.0.0", 897 - "embedded-hal-async", 898 - "embedded-hal-bus", 899 - "embedded-io-async", 900 - "embedded-sdmmc", 901 - "embedded-storage", 902 - "embedded-text", 903 - "fixed", 904 - "fixed-macro", 905 - "heapless 0.8.0", 906 - "log", 907 - "panic-probe", 908 - "pio", 909 - "pio-proc", 910 - "portable-atomic", 911 - "postcard", 912 - "rand", 913 - "reqwless", 914 - "serde", 915 - "serde-json-core", 916 - "shtcx", 917 - "smart-leds", 918 - "st7789", 919 - "static_cell", 920 - "tinybmp", 921 - "trouble-host", 922 - "uc8151", 923 - "usbd-hid", 924 - ] 925 - 926 - [[package]] 927 name = "embedded-graphics" 928 version = "0.8.1" 929 source = "registry+https://github.com/rust-lang/crates.io-index" ··· 931 dependencies = [ 932 "az", 933 "byteorder", 934 - "embedded-graphics-core 0.4.0", 935 "float-cmp", 936 "micromath", 937 - ] 938 - 939 - [[package]] 940 - name = "embedded-graphics-core" 941 - version = "0.3.3" 942 - source = "registry+https://github.com/rust-lang/crates.io-index" 943 - checksum = "b8b1239db5f3eeb7e33e35bd10bd014e7b2537b17e071f726a09351431337cfa" 944 - dependencies = [ 945 - "az", 946 - "byteorder", 947 ] 948 949 [[package]] ··· 2058 checksum = "955d28af4278de8121b7ebeb796b6a45735dc01436d898801014aced2773a3d6" 2059 2060 [[package]] 2061 name = "ryu" 2062 version = "1.0.18" 2063 source = "registry+https://github.com/rust-lang/crates.io-index" ··· 2224 ] 2225 2226 [[package]] 2227 - name = "st7789" 2228 - version = "0.6.1" 2229 - source = "registry+https://github.com/rust-lang/crates.io-index" 2230 - checksum = "cba7306a9ca07ffcafd13ac1eaac2d5cedc8e9fc0d342ef674e0f0d059c8f8a9" 2231 - dependencies = [ 2232 - "display-interface", 2233 - "embedded-graphics-core 0.3.3", 2234 - "embedded-hal 0.2.7", 2235 - "heapless 0.7.17", 2236 - "nb 1.1.0", 2237 - ] 2238 - 2239 - [[package]] 2240 name = "stable_deref_trait" 2241 version = "1.2.0" 2242 source = "registry+https://github.com/rust-lang/crates.io-index" ··· 2357 ] 2358 2359 [[package]] 2360 - name = "trouble-host" 2361 - version = "0.1.0" 2362 - source = "git+https://github.com/embassy-rs/trouble.git?rev=4b8c0f499b34e46ca23a56e2d1640ede371722cf#4b8c0f499b34e46ca23a56e2d1640ede371722cf" 2363 - dependencies = [ 2364 - "bt-hci", 2365 - "defmt", 2366 - "embassy-futures 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", 2367 - "embassy-sync 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", 2368 - "embassy-time", 2369 - "embedded-io-async", 2370 - "futures", 2371 - "heapless 0.8.0", 2372 - ] 2373 - 2374 - [[package]] 2375 name = "typenum" 2376 version = "1.17.0" 2377 source = "registry+https://github.com/rust-lang/crates.io-index" ··· 2382 version = "0.3.0" 2383 source = "git+https://github.com/9names/uc8151-rs.git#37a7feee23716506b777bd4032327038e7e0786a" 2384 dependencies = [ 2385 - "embedded-graphics-core 0.4.0", 2386 "embedded-hal 1.0.0", 2387 "embedded-hal-async", 2388 ] ··· 2645 version = "1.8.1" 2646 source = "registry+https://github.com/rust-lang/crates.io-index" 2647 checksum = "ced3678a2879b30306d323f4542626697a464a97c0a07c9aebf7ebca65cd4dde"
··· 1 # This file is automatically @generated by Cargo. 2 # It is not intended for manual editing. 3 + version = 4 4 5 [[package]] 6 name = "aead" ··· 182 183 [[package]] 184 name = "bt-hci" 185 + version = "0.1.2" 186 source = "registry+https://github.com/rust-lang/crates.io-index" 187 + checksum = "d69e2db053409c36c45a403da58072b8bf75bdc962746c0a12df9dec0fce92d2" 188 dependencies = [ 189 "defmt", 190 "embassy-sync 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", 191 "embedded-io", 192 "embedded-io-async", 193 "futures-intrusive", ··· 318 319 [[package]] 320 name = "critical-section" 321 + version = "1.2.0" 322 source = "registry+https://github.com/rust-lang/crates.io-index" 323 + checksum = "790eea4361631c5e7d22598ecd5723ff611904e3344ce8720784c93e3d83d40b" 324 325 [[package]] 326 name = "crunchy" ··· 619 [[package]] 620 name = "embassy-futures" 621 version = "0.1.1" 622 source = "git+https://github.com/embassy-rs/embassy.git?rev=f0a86070512ad739641cee7d9fa39d63f5c8a9f6#f0a86070512ad739641cee7d9fa39d63f5c8a9f6" 623 624 [[package]] ··· 860 ] 861 862 [[package]] 863 name = "embedded-graphics" 864 version = "0.8.1" 865 source = "registry+https://github.com/rust-lang/crates.io-index" ··· 867 dependencies = [ 868 "az", 869 "byteorder", 870 + "embedded-graphics-core", 871 "float-cmp", 872 "micromath", 873 ] 874 875 [[package]] ··· 1984 checksum = "955d28af4278de8121b7ebeb796b6a45735dc01436d898801014aced2773a3d6" 1985 1986 [[package]] 1987 + name = "rusty_badger" 1988 + version = "0.1.0" 1989 + dependencies = [ 1990 + "assign-resources", 1991 + "byte-slice-cast 1.2.2", 1992 + "cortex-m", 1993 + "cortex-m-rt", 1994 + "critical-section", 1995 + "cyw43", 1996 + "cyw43-pio", 1997 + "defmt", 1998 + "defmt-rtt", 1999 + "display-interface-spi", 2000 + "embassy-embedded-hal", 2001 + "embassy-executor", 2002 + "embassy-futures 0.1.1 (git+https://github.com/embassy-rs/embassy.git)", 2003 + "embassy-net", 2004 + "embassy-net-wiznet", 2005 + "embassy-rp", 2006 + "embassy-sync 0.6.0 (git+https://github.com/embassy-rs/embassy.git?rev=f0a86070512ad739641cee7d9fa39d63f5c8a9f6)", 2007 + "embassy-time", 2008 + "embassy-usb 0.3.0", 2009 + "embassy-usb-logger", 2010 + "embedded-graphics", 2011 + "embedded-hal 1.0.0", 2012 + "embedded-hal-async", 2013 + "embedded-hal-bus", 2014 + "embedded-io-async", 2015 + "embedded-sdmmc", 2016 + "embedded-storage", 2017 + "embedded-text", 2018 + "fixed", 2019 + "fixed-macro", 2020 + "heapless 0.8.0", 2021 + "log", 2022 + "panic-probe", 2023 + "pio", 2024 + "pio-proc", 2025 + "portable-atomic", 2026 + "postcard", 2027 + "rand", 2028 + "reqwless", 2029 + "serde", 2030 + "serde-json-core", 2031 + "shtcx", 2032 + "smart-leds", 2033 + "static_cell", 2034 + "tinybmp", 2035 + "uc8151", 2036 + "usbd-hid", 2037 + ] 2038 + 2039 + [[package]] 2040 name = "ryu" 2041 version = "1.0.18" 2042 source = "registry+https://github.com/rust-lang/crates.io-index" ··· 2203 ] 2204 2205 [[package]] 2206 name = "stable_deref_trait" 2207 version = "1.2.0" 2208 source = "registry+https://github.com/rust-lang/crates.io-index" ··· 2323 ] 2324 2325 [[package]] 2326 name = "typenum" 2327 version = "1.17.0" 2328 source = "registry+https://github.com/rust-lang/crates.io-index" ··· 2333 version = "0.3.0" 2334 source = "git+https://github.com/9names/uc8151-rs.git#37a7feee23716506b777bd4032327038e7e0786a" 2335 dependencies = [ 2336 + "embedded-graphics-core", 2337 "embedded-hal 1.0.0", 2338 "embedded-hal-async", 2339 ] ··· 2596 version = "1.8.1" 2597 source = "registry+https://github.com/rust-lang/crates.io-index" 2598 checksum = "ced3678a2879b30306d323f4542626697a464a97c0a07c9aebf7ebca65cd4dde" 2599 + 2600 + [[patch.unused]] 2601 + name = "trouble-host" 2602 + version = "0.1.0" 2603 + source = "git+https://github.com/embassy-rs/trouble.git?rev=4b8c0f499b34e46ca23a56e2d1640ede371722cf#4b8c0f499b34e46ca23a56e2d1640ede371722cf"
+1 -6
Cargo.toml
··· 1 [package] 2 - name = "embassy_rp_w_template" 3 version = "0.1.0" 4 edition = "2021" 5 ··· 55 ] } 56 cyw43-pio = { version = "0.2.0", git = "https://github.com/embassy-rs/embassy.git", rev = "f0a86070512ad739641cee7d9fa39d63f5c8a9f6", features = [ 57 "defmt", 58 - "overclock", 59 ] } 60 61 defmt = "0.3" ··· 78 panic-probe = { version = "0.3", features = ["print-defmt"] } 79 display-interface-spi = "0.4.1" 80 embedded-graphics = "0.8.0" 81 - st7789 = "0.6.1" 82 - display-interface = "0.4.1" 83 byte-slice-cast = { version = "1.2.0", default-features = false } 84 smart-leds = "0.3.0" 85 heapless = { version = "0.8", features = ["serde"] } ··· 97 pio = "0.2.1" 98 rand = { version = "0.8.5", default-features = false } 99 embedded-sdmmc = "0.7.0" 100 - bt-hci = { version = "0.1.0", default-features = false, features = ["defmt"] } 101 - trouble-host = { version = "0.1.0", features = ["defmt", "gatt"] } 102 uc8151 = { git = "https://github.com/9names/uc8151-rs.git", features = [ 103 "graphics", 104 ] }
··· 1 [package] 2 + name = "rusty_badger" 3 version = "0.1.0" 4 edition = "2021" 5 ··· 55 ] } 56 cyw43-pio = { version = "0.2.0", git = "https://github.com/embassy-rs/embassy.git", rev = "f0a86070512ad739641cee7d9fa39d63f5c8a9f6", features = [ 57 "defmt", 58 ] } 59 60 defmt = "0.3" ··· 77 panic-probe = { version = "0.3", features = ["print-defmt"] } 78 display-interface-spi = "0.4.1" 79 embedded-graphics = "0.8.0" 80 byte-slice-cast = { version = "1.2.0", default-features = false } 81 smart-leds = "0.3.0" 82 heapless = { version = "0.8", features = ["serde"] } ··· 94 pio = "0.2.1" 95 rand = { version = "0.8.5", default-features = false } 96 embedded-sdmmc = "0.7.0" 97 uc8151 = { git = "https://github.com/9names/uc8151-rs.git", features = [ 98 "graphics", 99 ] }
images/mtras_logo.bmp

This is a binary file and will not be displayed.

+7 -1
src/badge_display/display_image.rs
··· 2 3 use super::CURRENT_IMAGE; 4 5 - static NUMBER_OF_IMAGES: u8 = 2; 6 static FERRIS_IMG: &[u8; 15722] = include_bytes!("../../images/ferris_w_a_knife.bmp"); 7 static REPO_IMG: &[u8; 11262] = include_bytes!("../../images/repo.bmp"); 8 9 pub enum DisplayImage { 10 Ferris = 0, 11 Repo = 1, 12 } 13 14 pub fn get_current_image() -> DisplayImage { ··· 20 match value { 21 0 => Some(Self::Ferris), 22 1 => Some(Self::Repo), 23 _ => None, 24 } 25 } ··· 28 match self { 29 Self::Ferris => 0, 30 Self::Repo => 1, 31 } 32 } 33 ··· 35 match self { 36 Self::Ferris => FERRIS_IMG, 37 Self::Repo => REPO_IMG, 38 } 39 } 40 ··· 57 match self { 58 Self::Ferris => Point::new(150, 26), 59 Self::Repo => Point::new(190, 26), 60 } 61 } 62 }
··· 2 3 use super::CURRENT_IMAGE; 4 5 + static NUMBER_OF_IMAGES: u8 = 3; 6 static FERRIS_IMG: &[u8; 15722] = include_bytes!("../../images/ferris_w_a_knife.bmp"); 7 static REPO_IMG: &[u8; 11262] = include_bytes!("../../images/repo.bmp"); 8 + static MTRAS_LOGO: &[u8; 10378] = include_bytes!("../../images/mtras_logo.bmp"); 9 10 pub enum DisplayImage { 11 Ferris = 0, 12 Repo = 1, 13 + MtrasLogo = 2, 14 } 15 16 pub fn get_current_image() -> DisplayImage { ··· 22 match value { 23 0 => Some(Self::Ferris), 24 1 => Some(Self::Repo), 25 + 2 => Some(Self::MtrasLogo), 26 _ => None, 27 } 28 } ··· 31 match self { 32 Self::Ferris => 0, 33 Self::Repo => 1, 34 + Self::MtrasLogo => 2, 35 } 36 } 37 ··· 39 match self { 40 Self::Ferris => FERRIS_IMG, 41 Self::Repo => REPO_IMG, 42 + Self::MtrasLogo => MTRAS_LOGO, 43 } 44 } 45 ··· 62 match self { 63 Self::Ferris => Point::new(150, 26), 64 Self::Repo => Point::new(190, 26), 65 + Self::MtrasLogo => Point::new(190, 26), 66 } 67 } 68 }
+13 -9
src/badge_display/mod.rs
··· 32 use uc8151::{asynch::Uc8151, HEIGHT}; 33 use {defmt_rtt as _, panic_probe as _}; 34 35 - use crate::{env::env_value, helpers::easy_format, Spi0Bus}; 36 37 pub type RecentWifiNetworksVec = Vec<String<32>, 4>; 38 ··· 91 .into_styled(PrimitiveStyle::with_fill(BinaryColor::On)) 92 .draw(&mut display) 93 .unwrap(); 94 - 95 - // Create the text box and apply styling options. 96 - let display_text = easy_format::<29>(format_args!( 97 - "{}\n{}", 98 - env_value("NAME"), 99 - env_value("DETAILS") 100 - )); 101 102 let name_and_detail_box = TextBox::with_textbox_style( 103 - &display_text, 104 name_and_detail_bounds, 105 character_style, 106 textbox_style,
··· 32 use uc8151::{asynch::Uc8151, HEIGHT}; 33 use {defmt_rtt as _, panic_probe as _}; 34 35 + use crate::{ 36 + env::env_value, 37 + helpers::{easy_format, easy_format_str}, 38 + Spi0Bus, 39 + }; 40 41 pub type RecentWifiNetworksVec = Vec<String<32>, 4>; 42 ··· 95 .into_styled(PrimitiveStyle::with_fill(BinaryColor::On)) 96 .draw(&mut display) 97 .unwrap(); 98 + info!("Name: {}", env_value("NAME")); 99 + info!("Details: {}", env_value("DETAILS")); 100 + let mut name_and_details_buffer = [0; 128]; 101 + let name_and_details = easy_format_str( 102 + format_args!("{}\n{}", env_value("NAME"), env_value("DETAILS")), 103 + &mut name_and_details_buffer, 104 + ); 105 106 let name_and_detail_box = TextBox::with_textbox_style( 107 + &name_and_details.unwrap(), 108 name_and_detail_bounds, 109 character_style, 110 textbox_style,
+2 -3
src/cyw43_driver.rs
··· 32 ) -> (Device<'a>, Control<'a>) { 33 let fw = include_bytes!("../cyw43-firmware/43439A0.bin"); 34 let clm = include_bytes!("../cyw43-firmware/43439A0_clm.bin"); 35 - let btfw = include_bytes!("../cyw43-firmware/43439A0_btfw.bin"); 36 37 // To make flashing faster for development, you may want to flash the firmwares independently 38 // at hardcoded addresses, instead of baking them into the program with `include_bytes!`: ··· 51 52 static STATE: StaticCell<cyw43::State> = StaticCell::new(); 53 let state = STATE.init(cyw43::State::new()); 54 - let (net_device, _bt_device, mut control, runner) = 55 - cyw43::new_with_bluetooth(state, pwr, spi, fw, btfw).await; 56 unwrap!(spawner.spawn(cyw43_task(runner))); 57 58 control.init(clm).await;
··· 32 ) -> (Device<'a>, Control<'a>) { 33 let fw = include_bytes!("../cyw43-firmware/43439A0.bin"); 34 let clm = include_bytes!("../cyw43-firmware/43439A0_clm.bin"); 35 + // let btfw = include_bytes!("../cyw43-firmware/43439A0_btfw.bin"); 36 37 // To make flashing faster for development, you may want to flash the firmwares independently 38 // at hardcoded addresses, instead of baking them into the program with `include_bytes!`: ··· 51 52 static STATE: StaticCell<cyw43::State> = StaticCell::new(); 53 let state = STATE.init(cyw43::State::new()); 54 + let (net_device, mut control, runner) = cyw43::new(state, pwr, spi, fw).await; 55 unwrap!(spawner.spawn(cyw43_task(runner))); 56 57 control.init(clm).await;
+48
src/helpers.rs
··· 12 } 13 } 14 }
··· 12 } 13 } 14 } 15 + 16 + pub fn easy_format_str<'a>( 17 + args: Arguments<'_>, 18 + buffer: &'a mut [u8], 19 + ) -> Result<&'a str, core::fmt::Error> { 20 + let mut writer = BufWriter::new(buffer); 21 + let result = core::fmt::write(&mut writer, args); 22 + 23 + match result { 24 + Ok(_) => { 25 + let len = writer.len(); 26 + let response_str = core::str::from_utf8(&buffer[..len]).unwrap(); 27 + Ok(response_str) 28 + } 29 + Err(_) => { 30 + panic!("Error formatting the string") 31 + } 32 + } 33 + } 34 + 35 + // A simple wrapper struct to use core::fmt::Write on a [u8] buffer 36 + pub struct BufWriter<'a> { 37 + buf: &'a mut [u8], 38 + pos: usize, 39 + } 40 + 41 + impl<'a> BufWriter<'a> { 42 + pub fn new(buf: &'a mut [u8]) -> Self { 43 + BufWriter { buf, pos: 0 } 44 + } 45 + 46 + pub fn len(&self) -> usize { 47 + self.pos 48 + } 49 + } 50 + 51 + impl<'a> core::fmt::Write for BufWriter<'a> { 52 + fn write_str(&mut self, s: &str) -> core::fmt::Result { 53 + let bytes = s.as_bytes(); 54 + if self.pos + bytes.len() > self.buf.len() { 55 + return Err(core::fmt::Error); // Buffer overflow 56 + } 57 + 58 + self.buf[self.pos..self.pos + bytes.len()].copy_from_slice(bytes); 59 + self.pos += bytes.len(); 60 + Ok(()) 61 + } 62 + }
+5 -2
src/main.rs
··· 328 329 let mut recent_networks = RecentWifiNetworksVec::new(); 330 let mut scanner = control.scan(Default::default()).await; 331 while let Some(bss) = scanner.next().await { 332 process_bssid(bss.bssid, &mut save.wifi_counted, &mut save.bssid); 333 if recent_networks.len() < 8 { 334 let possible_ssid = core::str::from_utf8(&bss.ssid); 335 match possible_ssid { 336 Ok(ssid) => { 337 - info!("ssid: {}", ssid); 338 - let ssid_string = easy_format::<32>(format_args!("{}", ssid.trim())); 339 if recent_networks.contains(&ssid_string) { 340 continue; 341 }
··· 328 329 let mut recent_networks = RecentWifiNetworksVec::new(); 330 let mut scanner = control.scan(Default::default()).await; 331 + 332 while let Some(bss) = scanner.next().await { 333 process_bssid(bss.bssid, &mut save.wifi_counted, &mut save.bssid); 334 if recent_networks.len() < 8 { 335 let possible_ssid = core::str::from_utf8(&bss.ssid); 336 match possible_ssid { 337 Ok(ssid) => { 338 + let removed_zeros = ssid.trim_end_matches(char::from(0)); 339 + let ssid_string: String<32> = 340 + easy_format::<32>(format_args!("{}", removed_zeros)); 341 + 342 if recent_networks.contains(&ssid_string) { 343 continue; 344 }