4 // Panic provider crate
5 use panic_persist as _;
7 // Used to set the program entry point
8 use cortex_m_rt::entry;
10 extern crate feather_m4 as hal;
13 use hal::clock::GenericClockController;
14 use hal::pac::{CorePeripherals,Peripherals};
16 use hal::delay::Delay;
18 use hal::{i2c_master,spi_master};
19 use hal::time::U32Ext;
21 use rtt_target::{rprintln, rtt_init_print};
23 use embedded_graphics::{
24 fonts::{Font8x16, Text},
27 style::TextStyleBuilder,
30 use bbq10kbd::{Bbq10Kbd, KeyRaw};
34 use ili9341::{Ili9341, Orientation};
39 Ok(()) => cortex_m::peripheral::SCB::sys_reset(),
44 fn inner_main() -> Result<(), &'static str> {
45 let mut peripherals = Peripherals::take().ok_or("Error getting board!")?;
46 let mut _pins = Pins::new(peripherals.PORT);
47 let _core = CorePeripherals::take().unwrap();
49 let mut _rng: Trng = Trng::new(&mut peripherals.MCLK, peripherals.TRNG);
50 let mut _clocks = GenericClockController::with_external_32kosc(
52 &mut peripherals.MCLK,
53 &mut peripherals.OSC32KCTRL,
54 &mut peripherals.OSCCTRL,
55 &mut peripherals.NVMCTRL,
57 let mut delay = Delay::new(_core.SYST, &mut _clocks);
59 // set pins low to prevent floating values read as data by the pixels
60 let mut front_neopixel_pin = _pins.d11.into_push_pull_output(&mut _pins.port);
61 let mut rear_neopixel_pin = _pins.neopixel.into_push_pull_output(&mut _pins.port);
62 front_neopixel_pin.set_low().unwrap();
63 rear_neopixel_pin.set_high().unwrap();
65 // use ChannelMode::NoBlockS
66 rtt_init_print!(NoBlockSkip, 4096);
68 if let Some(msg) = panic_persist::get_panic_message_utf8() {
71 rprintln!("Clean boot!");
74 let kbd_lcd_reset = _pins.d5;
75 let _stm_cs = _pins.d6;
76 let lcd_cs = _pins.d9;
77 let lcd_dc = _pins.d10;
79 // i2c keyboard interface
83 let kbd_i2c = i2c_master(
87 &mut peripherals.MCLK,
93 let mut kbd = Bbq10Kbd::new(kbd_i2c);
95 let lcd_spi = spi_master(
99 &mut peripherals.MCLK,
106 let mut lcd = Ili9341::new_spi(
108 lcd_cs.into_push_pull_output(&mut _pins.port),
109 lcd_dc.into_push_pull_output(&mut _pins.port),
110 kbd_lcd_reset.into_push_pull_output(&mut _pins.port),
114 lcd.set_orientation(Orientation::Landscape).unwrap();
116 let mut buffy2 = [[0u16; 320]; 240];
118 let mut fbuffy = buffer::FrameBuffer::new(&mut buffy2);
120 let mut style = TextStyleBuilder::new(Font8x16)
121 .text_color(Rgb565::WHITE)
122 .background_color(Rgb565::BLACK)
125 kbd.set_backlight(255).unwrap();
127 let vers = kbd.get_version().unwrap();
129 rprintln!("Vers: {:?}", vers);
131 kbd.sw_reset().unwrap();
132 delay.delay_ms(10u8);
134 let vers = kbd.get_version().unwrap();
136 rprintln!("Vers: {:?}", vers);
138 let mut cursor = Cursor { x: 0, y: 0 };
140 lcd.clear(Rgb565::BLACK).map_err(|_| "Fade to error")?;
141 fbuffy.clear(Rgb565::BLACK).map_err(|_| "Fade to error")?;
144 let key = kbd.get_fifo_key_raw().map_err(|_| "bad fifo")?;
148 KeyRaw::Pressed(6) => {
149 style = TextStyleBuilder::new(Font8x16)
150 .text_color(Rgb565::WHITE)
151 .background_color(Rgb565::BLACK)
155 KeyRaw::Pressed(17) => {
156 style = TextStyleBuilder::new(Font8x16)
157 .text_color(Rgb565::RED)
158 .background_color(Rgb565::BLACK)
162 KeyRaw::Pressed(7) => {
163 style = TextStyleBuilder::new(Font8x16)
164 .text_color(Rgb565::GREEN)
165 .background_color(Rgb565::BLACK)
169 KeyRaw::Pressed(18) => {
170 style = TextStyleBuilder::new(Font8x16)
171 .text_color(Rgb565::BLUE)
172 .background_color(Rgb565::BLACK)
176 KeyRaw::Pressed(1) => {
180 KeyRaw::Pressed(2) => {
184 KeyRaw::Pressed(3) => {
188 KeyRaw::Pressed(4) => {
192 KeyRaw::Pressed(5) => {
193 kbd.sw_reset().unwrap();
194 cursor = Cursor { x: 0, y: 0 };
195 fbuffy.clear(Rgb565::BLACK).map_err(|_| "Fade to error")?;
198 KeyRaw::Pressed(8) => {
200 Text::new(" ", cursor.pos())
203 .map_err(|_| "bad lcd")?;
206 KeyRaw::Pressed(10) => {
209 KeyRaw::Pressed(k) => {
210 rprintln!("Got key {}", k);
211 if let Ok(s) = core::str::from_utf8(&[k]) {
212 Text::new(s, cursor.pos())
215 .map_err(|_| "bad lcd")?;
221 if let Some(buf) = fbuffy.inner() {
222 // timer.start(1_000_000u32);
223 lcd.draw_raw(0, 0, 319, 239, buf).map_err(|_| "bad buffy")?;
224 // let done = timer.read();
225 // rprintln!("Drew in {}ms.", done / 1000);
227 delay.delay_ms(38u8);
267 fn right(&mut self) {
275 fn enter(&mut self) {
282 fn pos(&self) -> Point {
283 Point::new(self.x * 8, self.y * 16)