4 // Panic provider crate
5 use panic_persist as _;
7 // Used to set the program entry point
8 use cortex_m_rt::entry;
10 // Provides definitions for our development board
12 gpio::{p0::Parts as P0Parts, p1::Parts as P1Parts, Level},
14 spim::{Frequency as SpimFrequency, Pins as SpimPins, MODE_0},
16 twim::{Frequency as TwimFrequency, Pins as TwimPins},
17 Clocks, Rng, Spim, Timer, Twim,
20 use rtt_target::{rprintln, rtt_init_print};
22 use embedded_graphics::{
23 fonts::{Font8x16, Text},
26 style::TextStyleBuilder,
29 use bbq10kbd::{Bbq10Kbd, KeyRaw};
33 use ili9341::{Ili9341, Orientation};
38 Ok(()) => cortex_m::peripheral::SCB::sys_reset(),
43 fn inner_main() -> Result<(), &'static str> {
44 let board = Peripherals::take().ok_or("Error getting board!")?;
45 let mut timer = Timer::new(board.TIMER0);
46 let mut delay = Timer::new(board.TIMER1);
47 let mut _rng = Rng::new(board.RNG);
48 let _clocks = Clocks::new(board.CLOCK).enable_ext_hfosc();
50 // use ChannelMode::NoBlockS
51 rtt_init_print!(NoBlockSkip, 4096);
53 if let Some(msg) = panic_persist::get_panic_message_utf8() {
56 rprintln!("Clean boot!");
59 let p0 = P0Parts::new(board.P0);
60 let p1 = P1Parts::new(board.P1);
62 let kbd_lcd_reset = p1.p1_08; // GPIO5, D5
63 let _stm_cs = p0.p0_07; // GPIO6, D6,
64 let lcd_cs = p0.p0_26; // GPIO9, D9,
65 let lcd_dc = p0.p0_27; // GPIO10, D10
67 let kbd_sda = p0.p0_12.into_floating_input().degrade();
68 let kbd_scl = p0.p0_11.into_floating_input().degrade();
70 let kbd_i2c = Twim::new(
79 let mut kbd = Bbq10Kbd::new(kbd_i2c);
81 // Pull the neopixel lines low so noise doesn't make it turn on spuriously
82 let _keywing_neopixel = p0.p0_06.into_push_pull_output(Level::Low); // GPIO11, D11
83 let _feather_neopixel = p0.p0_16.into_push_pull_output(Level::Low);
88 sck: p0.p0_14.into_push_pull_output(Level::Low).degrade(),
89 miso: Some(p0.p0_15.into_floating_input().degrade()),
90 mosi: Some(p0.p0_13.into_push_pull_output(Level::Low).degrade()),
97 let mut lcd = Ili9341::new_spi(
99 lcd_cs.into_push_pull_output(Level::High),
100 lcd_dc.into_push_pull_output(Level::High),
101 kbd_lcd_reset.into_push_pull_output(Level::High),
106 lcd.set_orientation(Orientation::Landscape).unwrap();
108 let mut _buffy = [0u16; 24 * 32];
109 let mut buffy2 = [[0u16; 320]; 240];
111 let mut fbuffy = buffer::FrameBuffer::new(&mut buffy2);
113 // // rrrrr gggggg bbbbb
114 // buffy.iter_mut().for_each(|px| *px = 0b11111_000000_00000);
116 let mut style = TextStyleBuilder::new(Font8x16)
117 .text_color(Rgb565::WHITE)
118 .background_color(Rgb565::BLACK)
121 kbd.set_backlight(255).unwrap();
123 let vers = kbd.get_version().unwrap();
125 rprintln!("Vers: {:?}", vers);
127 kbd.sw_reset().unwrap();
128 timer.delay_ms(10u8);
130 let vers = kbd.get_version().unwrap();
132 rprintln!("Vers: {:?}", vers);
134 let mut cursor = Cursor { x: 0, y: 0 };
136 lcd.clear(Rgb565::BLACK).map_err(|_| "Fade to error")?;
137 fbuffy.clear(Rgb565::BLACK).map_err(|_| "Fade to error")?;
140 let key = kbd.get_fifo_key_raw().map_err(|_| "bad fifo")?;
144 KeyRaw::Pressed(6) => {
145 style = TextStyleBuilder::new(Font8x16)
146 .text_color(Rgb565::WHITE)
147 .background_color(Rgb565::BLACK)
151 KeyRaw::Pressed(17) => {
152 style = TextStyleBuilder::new(Font8x16)
153 .text_color(Rgb565::RED)
154 .background_color(Rgb565::BLACK)
158 KeyRaw::Pressed(7) => {
159 style = TextStyleBuilder::new(Font8x16)
160 .text_color(Rgb565::GREEN)
161 .background_color(Rgb565::BLACK)
165 KeyRaw::Pressed(18) => {
166 style = TextStyleBuilder::new(Font8x16)
167 .text_color(Rgb565::BLUE)
168 .background_color(Rgb565::BLACK)
172 KeyRaw::Pressed(1) => {
176 KeyRaw::Pressed(2) => {
180 KeyRaw::Pressed(3) => {
184 KeyRaw::Pressed(4) => {
188 KeyRaw::Pressed(5) => {
189 kbd.sw_reset().unwrap();
190 cursor = Cursor { x: 0, y: 0 };
191 fbuffy.clear(Rgb565::BLACK).map_err(|_| "Fade to error")?;
194 KeyRaw::Pressed(8) => {
196 Text::new(" ", cursor.pos())
199 .map_err(|_| "bad lcd")?;
202 KeyRaw::Pressed(10) => {
205 KeyRaw::Pressed(k) => {
206 rprintln!("Got key {}", k);
207 if let Ok(s) = core::str::from_utf8(&[k]) {
208 Text::new(s, cursor.pos())
211 .map_err(|_| "bad lcd")?;
217 if let Some(buf) = fbuffy.inner() {
218 timer.start(1_000_000u32);
219 lcd.draw_raw(0, 0, 319, 239, buf).map_err(|_| "bad buffy")?;
220 let done = timer.read();
221 rprintln!("Drew in {}ms.", done / 1000);
223 timer.delay_ms(38u8);
263 fn right(&mut self) {
271 fn enter(&mut self) {
278 fn pos(&self) -> Point {
279 Point::new(self.x * 8, self.y * 16)