4 // Panic provider crate
8 // Used to set the program entry point
9 use cortex_m_rt::entry;
11 // Provides definitions for our development board
14 target::{Peripherals, CorePeripherals},
20 Frequency as SpimFrequency,
36 use embedded_graphics::{
37 fonts::{Font8x16, Text},
40 style::{TextStyle, TextStyleBuilder},
45 use ili9341::{Ili9341, Orientation};
47 const TEXT_SAMPLE: &[&str] = &[
50 " let rand: u16 = rng.random_u16();",
51 " buffy.iter_mut().for_each(|px| {",
57 " (32 * (x + 1)) - 1,",
58 " (24 * (y + 1)) - 1,",
65 const TEXT_SAMPLE2: &[&[(i32, Rgb565, &str)]] = &[
66 // "for x in 0..10 {",
67 &[(0, Rgb565::RED, "for "), (4, Rgb565::WHITE, "x "), (6, Rgb565::RED, "in "), (9, Rgb565::MAGENTA, "0"), (10, Rgb565::RED, ".."), (12, Rgb565::MAGENTA, "10"), (14, Rgb565::WHITE, " {")],
68 // " for y in 0..10 {",
69 &[(2, Rgb565::RED, "for "), (6, Rgb565::WHITE, "y "), (8, Rgb565::RED, "in "), (11, Rgb565::MAGENTA, "0"), (12, Rgb565::RED, ".."), (14, Rgb565::MAGENTA, "10"), (16, Rgb565::WHITE, " {")],
70 // " let rand: u16 = rng.random_u16();",
71 &[(4, Rgb565::CYAN, "let "), (8, Rgb565::WHITE, "rand: "), (14, Rgb565::CYAN, "u16 "), (18, Rgb565::RED, "= "), (20, Rgb565::WHITE, "rng."), (24, Rgb565::CYAN, "random_u16"), (34, Rgb565::WHITE, "();")],
72 // " buffy.iter_mut().for_each(|px| {",
73 &[(4, Rgb565::WHITE, "buffy."), (10, Rgb565::CYAN, "iter_mut"), (18, Rgb565::WHITE, "()."), (21, Rgb565::CYAN, "for_each"), (29, Rgb565::WHITE, "(|"), (31, Rgb565::YELLOW, "px"), (33, Rgb565::WHITE, "| {")],
74 // " *px = swap(rand)",
75 &[(6, Rgb565::RED, "*"), (7, Rgb565::WHITE, "px "), (10, Rgb565::RED, "= "), (12, Rgb565::CYAN, "swap"), (16, Rgb565::WHITE, "(rand)")],
77 &[(4, Rgb565::WHITE, "});")],
79 &[(4, Rgb565::WHITE, "lcd."), (8, Rgb565::CYAN, "draw_raw"), (16, Rgb565::WHITE, "(")],
81 &[(6, Rgb565::MAGENTA, "32 "), (9, Rgb565::RED, "* "), (11, Rgb565::WHITE, "x,")],
83 &[(6, Rgb565::MAGENTA, "24 "), (9, Rgb565::RED, "* "), (11, Rgb565::WHITE, "y,")],
84 // " (32 * (x + 1)) - 1,",
85 &[(6, Rgb565::WHITE, "("), (7, Rgb565::MAGENTA,"32 "), (10, Rgb565::RED, "* "), (12, Rgb565::WHITE, "(x "), (15, Rgb565::RED, "+ "), (17, Rgb565::MAGENTA, "1"), (18, Rgb565::WHITE, ")) "), (21, Rgb565::RED, "- "), (23, Rgb565::MAGENTA, "1"), (24, Rgb565::WHITE, ",")],
86 // " (24 * (y + 1)) - 1,",
87 &[(6, Rgb565::WHITE, "("), (7, Rgb565::MAGENTA,"24 "), (10, Rgb565::RED, "* "), (12, Rgb565::WHITE, "(y "), (15, Rgb565::RED, "+ "), (17, Rgb565::MAGENTA, "1"), (18, Rgb565::WHITE, ")) "), (21, Rgb565::RED, "- "), (23, Rgb565::MAGENTA, "1"), (24, Rgb565::WHITE, ",")],
89 &[(6, Rgb565::RED, "&"), (7, Rgb565::WHITE, "buffy,")],
91 &[(4, Rgb565::WHITE, ")."), (6, Rgb565::CYAN, "unwrap"), (12, Rgb565::WHITE, "();")],
93 &[(2, Rgb565::WHITE, "}")],
95 &[(0, Rgb565::WHITE, "}")],
101 Ok(()) => cortex_m::peripheral::SCB::sys_reset(),
106 fn inner_main() -> Result<(), &'static str> {
107 let mut board = Peripherals::take().ok_or("Error getting board!")?;
108 let mut corep = CorePeripherals::take().ok_or("Error")?;
109 let mut timer = Timer::new(board.TIMER0);
110 let mut delay = Timer::new(board.TIMER1);
111 let mut rng = Rng::new(board.RNG);
112 let mut toggle = false;
113 let _clocks = Clocks::new(board.CLOCK).enable_ext_hfosc();
115 // use ChannelMode::NoBlockS
116 rtt_init_print!(NoBlockSkip, 4096);
118 if let Some(msg) = panic_persist::get_panic_message_utf8() {
119 rprintln!("{}", msg);
121 rprintln!("Clean boot!");
125 let p0 = P0Parts::new(board.P0);
126 let p1 = P1Parts::new(board.P1);
128 let kbd_lcd_reset = p1.p1_08; // GPIO5, D5
129 let stm_cs = p0.p0_07; // GPIO6, D6,
130 let lcd_cs = p0.p0_26; // GPIO9, D9,
131 let lcd_dc = p0.p0_27; // GPIO10, D10
133 // Pull the neopixel line low so noise doesn't make it turn on spuriously
134 let keywing_neopixel = p0.p0_06.into_push_pull_output(Level::Low); // GPIO11, D11
136 let spim = Spim::new(
139 sck: p0.p0_14.into_push_pull_output(Level::Low).degrade(),
140 miso: Some(p0.p0_15.into_floating_input().degrade()),
141 mosi: Some(p0.p0_13.into_push_pull_output(Level::Low).degrade()),
148 let mut lcd = Ili9341::new_spi(
150 lcd_cs.into_push_pull_output(Level::High),
151 lcd_dc.into_push_pull_output(Level::High),
152 kbd_lcd_reset.into_push_pull_output(Level::High),
156 lcd.set_orientation(Orientation::Landscape).unwrap();
158 let mut buffy = [0u16; 24 * 32];
159 let mut buffy2 = [[0u16; 320]; 240];
161 let mut fbuffy = buffer::FrameBuffer::new(&mut buffy2);
163 // // rrrrr gggggg bbbbb
164 // buffy.iter_mut().for_each(|px| *px = 0b11111_000000_00000);
166 let style = TextStyleBuilder::new(Font8x16)
167 .text_color(Rgb565::WHITE)
168 .background_color(Rgb565::BLACK)
173 rprintln!("Start colors raw");
177 let rand: u16 = rng.random_u16();
178 buffy.iter_mut().for_each(|px| {
192 rprintln!("Done.\n");
194 timer.delay_ms(1000u16);
196 rprintln!("Start colors raw");
201 buffy.iter_mut().for_each(|px| {
215 rprintln!("Done.\n");
218 timer.delay_ms(1000u16);
220 // buffy2.iter_mut().for_each(|px| *px = swap(0b00000_000000_00000));
221 // rprintln!("Start black");
222 // lcd.draw_raw(0, 0, 319, 239, &buffy2).unwrap();
223 // rprintln!("Done.\n");
225 rprintln!("text start");
229 TEXT_SAMPLE[row as usize],
230 Point::new(0, row * 16)
236 rprintln!("text done");
238 timer.delay_ms(3000u16);
240 rprintln!("Start colors raw");
245 buffy.iter_mut().for_each(|px| {
259 rprintln!("Done.\n");
262 timer.delay_ms(1000u16);
264 // buffy2.iter_mut().for_each(|px| *px = swap(0b00000_000000_00000));
265 // rprintln!("Start black");
266 // lcd.draw_raw(0, 0, 319, 239, &buffy2).unwrap();
267 // rprintln!("Done.\n");
269 rprintln!("text2 start");
271 for (i, row) in TEXT_SAMPLE2.iter().enumerate() {
272 for (offset, color, text) in row.iter() {
273 let styled = TextStyleBuilder::new(Font8x16)
275 .background_color(Rgb565::BLACK)
280 Point::new(*offset * 8, i as i32 * 16)
281 ).into_styled(styled)
287 rprintln!("text2 done");
289 timer.delay_ms(3000u16);
291 rprintln!("Start colors raw");
296 buffy.iter_mut().for_each(|px| {
310 rprintln!("Done.\n");
312 timer.delay_ms(1000u16);
314 // buffy2.iter_mut().for_each(|px| *px = swap(0b00000_000000_00000));
315 // rprintln!("Start black");
316 // lcd.draw_raw(0, 0, 319, 239, &buffy2).unwrap();
317 // rprintln!("Done.\n");
319 timer.start(1_000_000u32);
321 let start: u32 = timer.read();
325 TEXT_SAMPLE[row as usize],
326 Point::new(0, row * 16)
332 let middle: u32 = timer.read();
334 lcd.draw_raw(0, 0, 319, 239, fbuffy.inner()).unwrap();
336 let end: u32 = timer.read();
338 rprintln!("text buffered done");
339 rprintln!("start: 0x{:08X}, middle: 0x{:08X}, end: 0x{:08X}", start, middle, end);
340 rprintln!("render: {} cycs", middle - start);
341 rprintln!("draw: {} cycs", end - middle);
347 timer.delay_ms(3000u16);
349 rprintln!("Start colors raw");
354 buffy.iter_mut().for_each(|px| {
368 rprintln!("Done.\n");
371 timer.delay_ms(1000u16);
373 rprintln!("text2 buffered middle");
375 for (i, row) in TEXT_SAMPLE2.iter().enumerate() {
376 for (offset, color, text) in row.iter() {
377 let styled = TextStyleBuilder::new(Font8x16)
379 .background_color(Rgb565::BLACK)
384 Point::new(*offset * 8, i as i32 * 16)
385 ).into_styled(styled)
391 rprintln!("text2 buffered middle");
393 lcd.draw_raw(0, 0, 319, 239, fbuffy.inner()).unwrap();
395 rprintln!("text2 buffered done");
397 timer.delay_ms(3000u16);
403 // // rrrrr gggggg bbbbb
404 // buffy2.iter_mut().for_each(|px| *px = swap(0b11111_000000_00000));
405 // rprintln!("Start red");
406 // lcd.draw_raw(0, 0, 319, 239, &buffy2).unwrap();
407 // rprintln!("Done.\n");
409 // timer.delay_ms(250u16);
411 // buffy2.iter_mut().for_each(|px| *px = swap(0b00000_111111_00000));
412 // rprintln!("Start green");
413 // lcd.draw_raw(0, 0, 319, 239, &buffy2).unwrap();
414 // rprintln!("Done.\n");
416 // timer.delay_ms(250u16);
418 // buffy2.iter_mut().for_each(|px| *px = swap(0b00000_000000_11111));
419 // rprintln!("Start blue");
420 // lcd.draw_raw(0, 0, 319, 239, &buffy2).unwrap();
421 // rprintln!("Done.\n");
423 // buffy2.iter_mut().for_each(|px| *px = swap(0b00000_000000_00000));
424 // rprintln!("Start black");
425 // lcd.draw_raw(0, 0, 319, 239, &buffy2).unwrap();
426 // rprintln!("Done.\n");
431 let text = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
433 let mut textiter = text.chars().cycle();
437 let mut buf = [0u8; 4];
438 let txt = textiter.next().unwrap().encode_utf8(&mut buf);
441 Point::new(col * 8, row * 16)
446 timer.delay_ms(500u16);
449 timer.delay_ms(1000u16);
452 // buffy2.iter_mut().for_each(|px| *px = swap(0b00000_000000_00000));
453 // rprintln!("Start black");
454 // lcd.draw_raw(0, 0, 319, 239, &buffy2).unwrap();
455 // rprintln!("Done.\n");
457 rprintln!("Starting Text Fill...");
459 let mut text = Text::new(
460 "1234567890123456789012345678901234567890",
462 ).into_styled(style);
465 text.draw(&mut lcd).unwrap();
466 text = text.translate(Point::new(0, 16));
469 rprintln!("Finished Text Fill.");
472 timer.delay_ms(1000u16);
480 const fn swap(inp: u16) -> u16 {
481 (inp & 0x00FF) << 8 |