|
| 1 | +#![no_std] |
| 2 | +#![no_main] |
| 3 | + |
| 4 | +/// Makes the wio_terminal read the SD card and print the filenames |
| 5 | +/// of the first few entries. |
| 6 | +use embedded_graphics as eg; |
| 7 | +use panic_halt as _; |
| 8 | +use wio_terminal as wio; |
| 9 | + |
| 10 | +use eg::mono_font::{ascii::FONT_9X15, MonoTextStyle}; |
| 11 | +use eg::pixelcolor::Rgb565; |
| 12 | +use eg::prelude::*; |
| 13 | +use eg::primitives::{PrimitiveStyleBuilder, Rectangle}; |
| 14 | +use eg::text::{Baseline, Text}; |
| 15 | + |
| 16 | +use wio::entry; |
| 17 | +use wio::hal::clock::GenericClockController; |
| 18 | +use wio::hal::delay::Delay; |
| 19 | +use wio::pac::{CorePeripherals, Peripherals}; |
| 20 | +use wio::prelude::*; |
| 21 | + |
| 22 | +use core::fmt::Write; |
| 23 | +use heapless::String; |
| 24 | + |
| 25 | +use embedded_sdmmc::{TimeSource, Timestamp, VolumeIdx}; |
| 26 | +use wio::SDCardController; |
| 27 | + |
| 28 | +#[entry] |
| 29 | +fn main() -> ! { |
| 30 | + let mut peripherals = Peripherals::take().unwrap(); |
| 31 | + let core = CorePeripherals::take().unwrap(); |
| 32 | + |
| 33 | + let mut clocks = GenericClockController::with_external_32kosc( |
| 34 | + peripherals.gclk, |
| 35 | + &mut peripherals.mclk, |
| 36 | + &mut peripherals.osc32kctrl, |
| 37 | + &mut peripherals.oscctrl, |
| 38 | + &mut peripherals.nvmctrl, |
| 39 | + ); |
| 40 | + |
| 41 | + let mut delay = Delay::new(core.SYST, &mut clocks); |
| 42 | + let sets = wio::Pins::new(peripherals.port).split(); |
| 43 | + |
| 44 | + let (mut cont, _sd_present) = sets |
| 45 | + .sd_card |
| 46 | + .init( |
| 47 | + &mut clocks, |
| 48 | + peripherals.sercom6, |
| 49 | + &mut peripherals.mclk, |
| 50 | + Clock, |
| 51 | + ) |
| 52 | + .unwrap(); |
| 53 | + |
| 54 | + // Initialize the ILI9341-based LCD display. Create a black backdrop the size of |
| 55 | + // the screen. |
| 56 | + let (mut display, _backlight) = sets |
| 57 | + .display |
| 58 | + .init( |
| 59 | + &mut clocks, |
| 60 | + peripherals.sercom7, |
| 61 | + &mut peripherals.mclk, |
| 62 | + 58.MHz(), |
| 63 | + &mut delay, |
| 64 | + ) |
| 65 | + .unwrap(); |
| 66 | + |
| 67 | + let style = MonoTextStyle::new(&FONT_9X15, Rgb565::WHITE); |
| 68 | + |
| 69 | + loop { |
| 70 | + match cont.device().init() { |
| 71 | + Ok(_) => { |
| 72 | + // Now that we have initialized, we can run the SPI bus at |
| 73 | + // a reasonable speed. |
| 74 | + cont.set_baud(20.MHz()); |
| 75 | + |
| 76 | + let mut data = String::<128>::new(); |
| 77 | + write!(data, "OK! ").unwrap(); |
| 78 | + match cont.device().card_size_bytes() { |
| 79 | + Ok(size) => writeln!(data, "{}Mb", size / 1024 / 1024).unwrap(), |
| 80 | + Err(e) => writeln!(data, "Err: {:?}", e).unwrap(), |
| 81 | + } |
| 82 | + Text::with_baseline(data.as_str(), Point::new(4, 2), style, Baseline::Top) |
| 83 | + .draw(&mut display) |
| 84 | + .ok() |
| 85 | + .unwrap(); |
| 86 | + |
| 87 | + if let Err(e) = print_contents(&mut cont, &mut display) { |
| 88 | + let mut data = String::<128>::new(); |
| 89 | + writeln!(data, "Err: {:?}", e).unwrap(); |
| 90 | + Text::with_baseline(data.as_str(), Point::new(4, 20), style, Baseline::Top) |
| 91 | + .draw(&mut display) |
| 92 | + .ok() |
| 93 | + .unwrap(); |
| 94 | + } |
| 95 | + } |
| 96 | + Err(e) => { |
| 97 | + let mut data = String::<128>::new(); |
| 98 | + writeln!(data, "Error!: {:?}", e).unwrap(); |
| 99 | + Text::with_baseline(data.as_str(), Point::new(4, 2), style, Baseline::Top) |
| 100 | + .draw(&mut display) |
| 101 | + .ok() |
| 102 | + .unwrap(); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + delay.delay_ms(2500_u16); |
| 107 | + Rectangle::with_corners(Point::new(0, 0), Point::new(320, 240)) |
| 108 | + .into_styled( |
| 109 | + PrimitiveStyleBuilder::new() |
| 110 | + .fill_color(Rgb565::BLACK) |
| 111 | + .build(), |
| 112 | + ) |
| 113 | + .draw(&mut display) |
| 114 | + .ok() |
| 115 | + .unwrap(); |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +fn print_contents( |
| 120 | + cont: &mut SDCardController<Clock>, |
| 121 | + lcd: &mut wio::LCD, |
| 122 | +) -> Result<(), embedded_sdmmc::Error<embedded_sdmmc::SdMmcError>> { |
| 123 | + let style = MonoTextStyle::new(&FONT_9X15, Rgb565::WHITE); |
| 124 | + |
| 125 | + let volume = cont.get_volume(VolumeIdx(0))?; |
| 126 | + let dir = cont.open_root_dir(&volume)?; |
| 127 | + |
| 128 | + let mut count = 0; |
| 129 | + let out = cont.iterate_dir(&volume, &dir, |ent| { |
| 130 | + let mut data = String::<128>::new(); |
| 131 | + writeln!(data, "{} - {:?}", ent.name, ent.attributes).unwrap(); |
| 132 | + Text::with_baseline( |
| 133 | + data.as_str(), |
| 134 | + Point::new(4, 20 + count * 16), |
| 135 | + style, |
| 136 | + Baseline::Top, |
| 137 | + ) |
| 138 | + .draw(lcd) |
| 139 | + .ok() |
| 140 | + .unwrap(); |
| 141 | + count += 1; |
| 142 | + }); |
| 143 | + cont.close_dir(&volume, dir); |
| 144 | + out |
| 145 | +} |
| 146 | + |
| 147 | +struct Clock; |
| 148 | + |
| 149 | +impl TimeSource for Clock { |
| 150 | + fn get_timestamp(&self) -> Timestamp { |
| 151 | + Timestamp { |
| 152 | + year_since_1970: 0, |
| 153 | + zero_indexed_month: 0, |
| 154 | + zero_indexed_day: 0, |
| 155 | + hours: 0, |
| 156 | + minutes: 0, |
| 157 | + seconds: 0, |
| 158 | + } |
| 159 | + } |
| 160 | +} |
0 commit comments