Skip to content

Commit ec608a7

Browse files
authored
Add raw random noise example (#108)
* Add raw random noise example * Changelog entry
1 parent 8d7b3e5 commit ec608a7

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
- [#107](https://github.com/jamwaffles/ssd1306/pull/107) Migrate from Travis to CircleCI
1212
- [#105](https://github.com/jamwaffles/ssd1306/pull/105) Reduce flash usage by around 400 bytes by replacing some internal `unwrap()`s with `as` coercions.
1313
- [#106](https://github.com/jamwaffles/ssd1306/pull/106) Optimise internals by using iterators to elide bounds checks. Should also speed up `GraphicsMode` (and `embedded-graphics` operations) with a cleaned-up `set_pixel`.
14+
- [#108](https://github.com/jamwaffles/ssd1306/pull/108) Add an example using `DisplayProperties.draw()` to send a raw buffer of random bytes to the display over I2C.
1415

1516
## [0.3.0-alpha.4] - 2020-02-07
1617

Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ cortex-m = "0.6.1"
3030
cortex-m-rt = "0.6.11"
3131
panic-semihosting = "0.5.3"
3232

33+
[dependencies.rand]
34+
version = "0.7.3"
35+
default-features = false
36+
features = [ "small_rng" ]
37+
3338
# Turn on `bmp` feature for examples
3439
[dev-dependencies.embedded-graphics]
3540
version = "=0.6.0-alpha.3"

examples/noise_i2c.rs

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
//! Send random raw data to the display, emulating an old untuned TV. This example `release()`s the
2+
//! underlying display properties struct and allows calling of the low-level `draw()` method,
3+
//! sending a 1024 byte buffer straight to the display.
4+
//!
5+
//! This example is for the STM32F103 "Blue Pill" board using I2C1.
6+
//!
7+
//! Wiring connections are as follows for a CRIUS-branded display:
8+
//!
9+
//! ```
10+
//! Display -> Blue Pill
11+
//! (black) GND -> GND
12+
//! (red) +5V -> VCC
13+
//! (yellow) SDA -> PB9
14+
//! (green) SCL -> PB8
15+
//! ```
16+
//!
17+
//! Run on a Blue Pill with `cargo run --example noise_i2c`. Best results when using `--release`.
18+
19+
#![no_std]
20+
#![no_main]
21+
22+
extern crate cortex_m;
23+
extern crate cortex_m_rt as rt;
24+
extern crate panic_semihosting;
25+
extern crate stm32f1xx_hal as hal;
26+
27+
use cortex_m_rt::{entry, exception, ExceptionFrame};
28+
use embedded_graphics::{
29+
pixelcolor::BinaryColor,
30+
prelude::*,
31+
primitives::{Circle, Rectangle, Triangle},
32+
style::PrimitiveStyleBuilder,
33+
};
34+
use hal::{
35+
i2c::{BlockingI2c, DutyCycle, Mode},
36+
prelude::*,
37+
stm32,
38+
};
39+
use rand::prelude::*;
40+
use ssd1306::{mode::displaymode::DisplayModeTrait, prelude::*, Builder};
41+
42+
#[entry]
43+
fn main() -> ! {
44+
let dp = stm32::Peripherals::take().unwrap();
45+
46+
let mut flash = dp.FLASH.constrain();
47+
let mut rcc = dp.RCC.constrain();
48+
49+
let clocks = rcc.cfgr.freeze(&mut flash.acr);
50+
51+
let mut afio = dp.AFIO.constrain(&mut rcc.apb2);
52+
53+
let mut gpiob = dp.GPIOB.split(&mut rcc.apb2);
54+
55+
let scl = gpiob.pb8.into_alternate_open_drain(&mut gpiob.crh);
56+
let sda = gpiob.pb9.into_alternate_open_drain(&mut gpiob.crh);
57+
58+
let i2c = BlockingI2c::i2c1(
59+
dp.I2C1,
60+
(scl, sda),
61+
&mut afio.mapr,
62+
Mode::Fast {
63+
frequency: 400_000.hz(),
64+
duty_cycle: DutyCycle::Ratio2to1,
65+
},
66+
clocks,
67+
&mut rcc.apb1,
68+
1000,
69+
10,
70+
1000,
71+
1000,
72+
);
73+
74+
let mut disp: GraphicsMode<_> = Builder::new().connect_i2c(i2c).into();
75+
76+
disp.init().unwrap();
77+
78+
let mut props = disp.release();
79+
80+
let mut buf = [0x00u8; 1024];
81+
82+
let mut rng = SmallRng::seed_from_u64(0xdead_beef_cafe_d00d);
83+
84+
loop {
85+
rng.fill_bytes(&mut buf);
86+
87+
props.draw(&buf);
88+
}
89+
}
90+
91+
#[exception]
92+
fn HardFault(ef: &ExceptionFrame) -> ! {
93+
panic!("{:#?}", ef);
94+
}

0 commit comments

Comments
 (0)