|
| 1 | +//! Draw a square, circle and triangle on the screen using the `embedded_graphics` crate. |
| 2 | +//! |
| 3 | +//! This example is for the STM32F103 "Blue Pill" board using I2C1. |
| 4 | +//! |
| 5 | +//! Wiring connections are as follows for a CRIUS-branded display: |
| 6 | +//! |
| 7 | +//! ``` |
| 8 | +//! Display -> Blue Pill |
| 9 | +//! (black) GND -> GND |
| 10 | +//! (red) +5V -> VCC |
| 11 | +//! (yellow) SDA -> PB9 |
| 12 | +//! (green) SCL -> PB8 |
| 13 | +//! ``` |
| 14 | +//! |
| 15 | +//! Run on a Blue Pill with `cargo run --example graphics`. |
| 16 | +
|
| 17 | +#![no_std] |
| 18 | +#![no_main] |
| 19 | + |
| 20 | +use cortex_m_rt::{entry, exception, ExceptionFrame}; |
| 21 | +use embedded_graphics::{ |
| 22 | + pixelcolor::BinaryColor, |
| 23 | + prelude::*, |
| 24 | + primitives::{Circle, Line, PrimitiveStyle, Rectangle}, |
| 25 | +}; |
| 26 | +use panic_semihosting as _; |
| 27 | +use sh1106::{prelude::*, Builder}; |
| 28 | +use stm32f1xx_hal::{ |
| 29 | + i2c::{BlockingI2c, DutyCycle, Mode}, |
| 30 | + prelude::*, |
| 31 | + stm32, |
| 32 | +}; |
| 33 | + |
| 34 | +#[entry] |
| 35 | +fn main() -> ! { |
| 36 | + let dp = stm32::Peripherals::take().unwrap(); |
| 37 | + |
| 38 | + let mut flash = dp.FLASH.constrain(); |
| 39 | + let mut rcc = dp.RCC.constrain(); |
| 40 | + |
| 41 | + let clocks = rcc.cfgr.freeze(&mut flash.acr); |
| 42 | + |
| 43 | + let mut afio = dp.AFIO.constrain(&mut rcc.apb2); |
| 44 | + |
| 45 | + let mut gpiob = dp.GPIOB.split(&mut rcc.apb2); |
| 46 | + |
| 47 | + let scl = gpiob.pb8.into_alternate_open_drain(&mut gpiob.crh); |
| 48 | + let sda = gpiob.pb9.into_alternate_open_drain(&mut gpiob.crh); |
| 49 | + |
| 50 | + let i2c = BlockingI2c::i2c1( |
| 51 | + dp.I2C1, |
| 52 | + (scl, sda), |
| 53 | + &mut afio.mapr, |
| 54 | + Mode::Fast { |
| 55 | + frequency: 100.khz().into(), |
| 56 | + duty_cycle: DutyCycle::Ratio2to1, |
| 57 | + }, |
| 58 | + clocks, |
| 59 | + &mut rcc.apb1, |
| 60 | + 1000, |
| 61 | + 10, |
| 62 | + 1000, |
| 63 | + 1000, |
| 64 | + ); |
| 65 | + |
| 66 | + let mut display: GraphicsMode<_> = Builder::new().connect_i2c(i2c).into(); |
| 67 | + |
| 68 | + display.init().unwrap(); |
| 69 | + display.flush().unwrap(); |
| 70 | + |
| 71 | + Line::new(Point::new(8, 16 + 16), Point::new(8 + 16, 16 + 16)) |
| 72 | + .into_styled(PrimitiveStyle::with_stroke(BinaryColor::On, 1)) |
| 73 | + .draw(&mut display) |
| 74 | + .unwrap(); |
| 75 | + |
| 76 | + Line::new(Point::new(8, 16 + 16), Point::new(8 + 8, 16)) |
| 77 | + .into_styled(PrimitiveStyle::with_stroke(BinaryColor::On, 1)) |
| 78 | + .draw(&mut display) |
| 79 | + .unwrap(); |
| 80 | + |
| 81 | + Line::new(Point::new(8 + 16, 16 + 16), Point::new(8 + 8, 16)) |
| 82 | + .into_styled(PrimitiveStyle::with_stroke(BinaryColor::On, 1)) |
| 83 | + .draw(&mut display) |
| 84 | + .unwrap(); |
| 85 | + |
| 86 | + Rectangle::with_corners(Point::new(48, 16), Point::new(48 + 16, 16 + 16)) |
| 87 | + .into_styled(PrimitiveStyle::with_stroke(BinaryColor::On, 1)) |
| 88 | + .draw(&mut display) |
| 89 | + .unwrap(); |
| 90 | + |
| 91 | + Circle::new(Point::new(88, 16), 16) |
| 92 | + .into_styled(PrimitiveStyle::with_stroke(BinaryColor::On, 1)) |
| 93 | + .draw(&mut display) |
| 94 | + .unwrap(); |
| 95 | + |
| 96 | + for i in 0..30 { |
| 97 | + let thick = 4u32; |
| 98 | + |
| 99 | + Rectangle::new( |
| 100 | + Point::new(i * (thick as i32 + 1), i), |
| 101 | + Size::new(thick, thick), |
| 102 | + ) |
| 103 | + .into_styled(PrimitiveStyle::with_fill(BinaryColor::On)) |
| 104 | + .draw(&mut display) |
| 105 | + .unwrap(); |
| 106 | + |
| 107 | + Rectangle::new( |
| 108 | + Point::new(i * (thick as i32 + 1), i + 20), |
| 109 | + Size::new(thick, 11), |
| 110 | + ) |
| 111 | + .into_styled(PrimitiveStyle::with_fill(BinaryColor::On)) |
| 112 | + .draw(&mut display) |
| 113 | + .unwrap(); |
| 114 | + } |
| 115 | + |
| 116 | + display.flush().unwrap(); |
| 117 | + |
| 118 | + loop {} |
| 119 | +} |
| 120 | + |
| 121 | +#[exception] |
| 122 | +fn HardFault(ef: &ExceptionFrame) -> ! { |
| 123 | + panic!("{:#?}", ef); |
| 124 | +} |
0 commit comments