|
| 1 | +#![no_std] |
| 2 | +#![no_main] |
| 3 | + |
| 4 | +extern crate panic_halt; |
| 5 | + |
| 6 | +use cortex_m_rt::entry; |
| 7 | + |
| 8 | +use stm32f4xx_hal as hal; |
| 9 | + |
| 10 | +use crate::hal::{pac, prelude::*, spi::Spi}; |
| 11 | + |
| 12 | +use hal::spi::{Mode, NoMiso, Phase, Polarity}; |
| 13 | + |
| 14 | +use display_interface_spi::SPIInterface; |
| 15 | +use ist7920::Ist7920; |
| 16 | + |
| 17 | +#[entry] |
| 18 | +fn main() -> ! { |
| 19 | + let dp = pac::Peripherals::take().unwrap(); |
| 20 | + let cp = cortex_m::peripheral::Peripherals::take().unwrap(); |
| 21 | + |
| 22 | + let gpioa = dp.GPIOA.split(); |
| 23 | + let gpiob = dp.GPIOB.split(); |
| 24 | + let rcc = dp.RCC.constrain(); |
| 25 | + |
| 26 | + let clocks = rcc.cfgr.freeze(); |
| 27 | + |
| 28 | + let mut led = gpioa.pa5.into_push_pull_output(); |
| 29 | + led.set_low(); |
| 30 | + |
| 31 | + let sck = gpiob.pb3.into_alternate(); |
| 32 | + let miso = NoMiso {}; |
| 33 | + let mosi = gpiob.pb5.into_alternate(); |
| 34 | + |
| 35 | + let dc = gpiob.pb4.into_push_pull_output(); |
| 36 | + let mut res = gpiob.pb10.into_push_pull_output(); |
| 37 | + let cs = gpiob.pb13.into_push_pull_output(); |
| 38 | + |
| 39 | + let mut delay = hal::delay::Delay::new(cp.SYST, &clocks); |
| 40 | + |
| 41 | + let mode = Mode { |
| 42 | + polarity: Polarity::IdleLow, |
| 43 | + phase: Phase::CaptureOnFirstTransition, |
| 44 | + }; |
| 45 | + |
| 46 | + // Change spi transfer mode to Bidi for more efficient operations. |
| 47 | + let spi = |
| 48 | + Spi::new(dp.SPI1, (sck, miso, mosi), mode, 8_000_000.hz(), clocks).to_bidi_transfer_mode(); |
| 49 | + |
| 50 | + let iface = SPIInterface::new(spi, dc, cs); |
| 51 | + |
| 52 | + let mut display = Ist7920::new(iface); |
| 53 | + |
| 54 | + display.reset(&mut res, &mut delay).ok(); |
| 55 | + |
| 56 | + display.init(&mut delay).ok(); |
| 57 | + |
| 58 | + let mut select_figure = 0; |
| 59 | + loop { |
| 60 | + delay.delay_ms(500_u16); |
| 61 | + let (begin, end) = match select_figure { |
| 62 | + 0 => { |
| 63 | + select_figure = 1; |
| 64 | + ((48, 48), (48 + 31, 48 + 31)) |
| 65 | + } |
| 66 | + |
| 67 | + 1 => { |
| 68 | + select_figure = 2; |
| 69 | + ((32, 32), (32 + 63, 32 + 63)) |
| 70 | + } |
| 71 | + _ => { |
| 72 | + select_figure = 0; |
| 73 | + ((24, 24), (24 + 79, 24 + 79)) |
| 74 | + } |
| 75 | + }; |
| 76 | + display.clear().ok(); |
| 77 | + display.set_draw_area(begin, end).ok(); |
| 78 | + for _ in 0..((end.1 - begin.1 + 1) / 16) { |
| 79 | + for _ in 0..((end.0 - begin.0 + 1) / 16) { |
| 80 | + display |
| 81 | + .draw(&[ |
| 82 | + 0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xff, 0xff, 0xff, 0xff, |
| 83 | + 0xff, 0xff, 0xff, 0xff, |
| 84 | + ]) |
| 85 | + .ok(); |
| 86 | + } |
| 87 | + for _ in 0..((end.0 - begin.0 + 1) / 16) { |
| 88 | + display |
| 89 | + .draw(&[ |
| 90 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, |
| 91 | + 0x00, 0x00, 0x00, 0x00, |
| 92 | + ]) |
| 93 | + .ok(); |
| 94 | + } |
| 95 | + } |
| 96 | + led.toggle(); |
| 97 | + } |
| 98 | +} |
0 commit comments