Skip to content

Commit 9ef509c

Browse files
committed
Merge in chux0519/raspi-oled as sample
2 parents 420fdbf + 0d33a5f commit 9ef509c

File tree

9 files changed

+385
-0
lines changed

9 files changed

+385
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
name: Build It
2+
on: [push]
3+
4+
jobs:
5+
build:
6+
runs-on: ubuntu-latest
7+
steps:
8+
- uses: actions/checkout@v1
9+
- name: Build for Raspberry Pi 2/3/4 32 bit
10+
uses: actions-rs/cargo@v1
11+
with:
12+
use-cross: true
13+
command: build
14+
args: --target armv7-unknown-linux-gnueabihf

examples-raspi/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/target
2+
**/*.rs.bk

examples-raspi/Cargo.lock

+241
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples-raspi/Cargo.toml

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[package]
2+
name = "raspi-oled"
3+
version = "0.1.0"
4+
authors = ["chux0519 <[email protected]>"]
5+
edition = "2018"
6+
7+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8+
9+
[dependencies]
10+
embedded-graphics = "0.6.0-alpha.2"
11+
linux-embedded-hal = "0.2.2"
12+
machine-ip = "0.2.1"
13+
ssd1306 = "0.3.0-alpha.2"
14+
ctrlc = { version = "3.0", features = ["termination"] }

examples-raspi/README.md

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# raspi demo for oled(ssd1306)
2+
3+
in rust
4+
5+
## Quick start
6+
7+
The easiest way to build this sample is with [Cross](https://github.com/rust-embedded/cross).
8+
9+
```
10+
cross build --release --target armv7-unknown-linux-gnueabihf
11+
```
12+
13+
> That assumes Raspberry Pi 2/3/4 running a 32 bit kernel
14+
15+
After the build finishes, copy it to your Raspberry Pi
16+
17+
```
18+
scp target/armv7-unknown-linux-gnueabihf/release/raspi-oled user@ip:/home/user
19+
```
20+
21+
Then SSH to your Pi and run it
22+
23+
```
24+
sudo ./raspi-oled
25+
```
26+
27+
## Example
28+
29+
![picture](./images/01.jpg)
30+
31+
![primitive](./images/02.jpg)

examples-raspi/images/01.jpg

182 KB
Loading

examples-raspi/images/02.jpg

177 KB
Loading

examples-raspi/rust.raw

512 Bytes
Binary file not shown.

examples-raspi/src/main.rs

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
use embedded_graphics::fonts::Font6x8;
2+
use embedded_graphics::image::Image;
3+
use embedded_graphics::pixelcolor::BinaryColor;
4+
use embedded_graphics::prelude::*;
5+
use embedded_graphics::primitives::{Circle, Line, Rectangle};
6+
use embedded_graphics::Drawing;
7+
use linux_embedded_hal::I2cdev;
8+
use machine_ip;
9+
use ssd1306::{mode::GraphicsMode, Builder};
10+
use std::thread::sleep;
11+
use std::time::Duration;
12+
extern crate ctrlc;
13+
use std::sync::atomic::{AtomicBool, Ordering};
14+
use std::sync::Arc;
15+
16+
fn main() {
17+
let running = Arc::new(AtomicBool::new(true));
18+
let r = running.clone();
19+
ctrlc::set_handler(move || {
20+
r.store(false, Ordering::SeqCst);
21+
})
22+
.expect("Error setting Ctrl-C handler");
23+
24+
let i2c = I2cdev::new("/dev/i2c-1").unwrap();
25+
26+
let mut disp: GraphicsMode<_> = Builder::new().connect_i2c(i2c).into();
27+
28+
disp.init().unwrap();
29+
disp.flush().unwrap();
30+
31+
while running.load(Ordering::SeqCst) {
32+
disp.draw(
33+
Line::new(Point::new(8, 16 + 16), Point::new(8 + 16, 16 + 16))
34+
.stroke(Some(BinaryColor::On))
35+
.into_iter(),
36+
);
37+
disp.draw(
38+
Line::new(Point::new(8, 16 + 16), Point::new(8 + 8, 16))
39+
.stroke(Some(BinaryColor::On))
40+
.into_iter(),
41+
);
42+
disp.draw(
43+
Line::new(Point::new(8 + 16, 16 + 16), Point::new(8 + 8, 16))
44+
.stroke(Some(BinaryColor::On))
45+
.into_iter(),
46+
);
47+
48+
disp.draw(
49+
Rectangle::new(Point::new(48, 16), Point::new(48 + 16, 16 + 16))
50+
.stroke(Some(BinaryColor::On))
51+
.into_iter(),
52+
);
53+
54+
disp.draw(
55+
Circle::new(Point::new(96, 16 + 8), 8)
56+
.stroke(Some(BinaryColor::On))
57+
.into_iter(),
58+
);
59+
60+
let local_addr = machine_ip::get().unwrap();
61+
62+
disp.draw(
63+
Font6x8::render_str(&format!("IP: {}", local_addr.to_string()))
64+
.translate(Point::new(0, 56))
65+
.into_iter(),
66+
);
67+
disp.flush().unwrap();
68+
69+
sleep(Duration::from_secs(2));
70+
71+
disp.clear();
72+
73+
let im: Image<BinaryColor> =
74+
Image::new(include_bytes!("../rust.raw"), 64, 64).translate(Point::new(32, 0));
75+
disp.draw(im.into_iter());
76+
disp.flush().unwrap();
77+
78+
sleep(Duration::from_secs(2));
79+
disp.clear();
80+
}
81+
disp.clear();
82+
disp.flush().unwrap();
83+
}

0 commit comments

Comments
 (0)