Skip to content

Commit

Permalink
Merge in chux0519/raspi-oled as sample
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrickLang committed Aug 2, 2020
2 parents 420fdbf + 0d33a5f commit 9ef509c
Show file tree
Hide file tree
Showing 9 changed files with 385 additions and 0 deletions.
14 changes: 14 additions & 0 deletions examples-raspi/.github/workflows/build-raspi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: Build It
on: [push]

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- name: Build for Raspberry Pi 2/3/4 32 bit
uses: actions-rs/cargo@v1
with:
use-cross: true
command: build
args: --target armv7-unknown-linux-gnueabihf
2 changes: 2 additions & 0 deletions examples-raspi/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/target
**/*.rs.bk
241 changes: 241 additions & 0 deletions examples-raspi/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions examples-raspi/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "raspi-oled"
version = "0.1.0"
authors = ["chux0519 <[email protected]>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
embedded-graphics = "0.6.0-alpha.2"
linux-embedded-hal = "0.2.2"
machine-ip = "0.2.1"
ssd1306 = "0.3.0-alpha.2"
ctrlc = { version = "3.0", features = ["termination"] }
31 changes: 31 additions & 0 deletions examples-raspi/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# raspi demo for oled(ssd1306)

in rust

## Quick start

The easiest way to build this sample is with [Cross](https://github.com/rust-embedded/cross).

```
cross build --release --target armv7-unknown-linux-gnueabihf
```

> That assumes Raspberry Pi 2/3/4 running a 32 bit kernel
After the build finishes, copy it to your Raspberry Pi

```
scp target/armv7-unknown-linux-gnueabihf/release/raspi-oled user@ip:/home/user
```

Then SSH to your Pi and run it

```
sudo ./raspi-oled
```

## Example

![picture](./images/01.jpg)

![primitive](./images/02.jpg)
Binary file added examples-raspi/images/01.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples-raspi/images/02.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples-raspi/rust.raw
Binary file not shown.
83 changes: 83 additions & 0 deletions examples-raspi/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
use embedded_graphics::fonts::Font6x8;
use embedded_graphics::image::Image;
use embedded_graphics::pixelcolor::BinaryColor;
use embedded_graphics::prelude::*;
use embedded_graphics::primitives::{Circle, Line, Rectangle};
use embedded_graphics::Drawing;
use linux_embedded_hal::I2cdev;
use machine_ip;
use ssd1306::{mode::GraphicsMode, Builder};
use std::thread::sleep;
use std::time::Duration;
extern crate ctrlc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;

fn main() {
let running = Arc::new(AtomicBool::new(true));
let r = running.clone();
ctrlc::set_handler(move || {
r.store(false, Ordering::SeqCst);
})
.expect("Error setting Ctrl-C handler");

let i2c = I2cdev::new("/dev/i2c-1").unwrap();

let mut disp: GraphicsMode<_> = Builder::new().connect_i2c(i2c).into();

disp.init().unwrap();
disp.flush().unwrap();

while running.load(Ordering::SeqCst) {
disp.draw(
Line::new(Point::new(8, 16 + 16), Point::new(8 + 16, 16 + 16))
.stroke(Some(BinaryColor::On))
.into_iter(),
);
disp.draw(
Line::new(Point::new(8, 16 + 16), Point::new(8 + 8, 16))
.stroke(Some(BinaryColor::On))
.into_iter(),
);
disp.draw(
Line::new(Point::new(8 + 16, 16 + 16), Point::new(8 + 8, 16))
.stroke(Some(BinaryColor::On))
.into_iter(),
);

disp.draw(
Rectangle::new(Point::new(48, 16), Point::new(48 + 16, 16 + 16))
.stroke(Some(BinaryColor::On))
.into_iter(),
);

disp.draw(
Circle::new(Point::new(96, 16 + 8), 8)
.stroke(Some(BinaryColor::On))
.into_iter(),
);

let local_addr = machine_ip::get().unwrap();

disp.draw(
Font6x8::render_str(&format!("IP: {}", local_addr.to_string()))
.translate(Point::new(0, 56))
.into_iter(),
);
disp.flush().unwrap();

sleep(Duration::from_secs(2));

disp.clear();

let im: Image<BinaryColor> =
Image::new(include_bytes!("../rust.raw"), 64, 64).translate(Point::new(32, 0));
disp.draw(im.into_iter());
disp.flush().unwrap();

sleep(Duration::from_secs(2));
disp.clear();
}
disp.clear();
disp.flush().unwrap();
}

0 comments on commit 9ef509c

Please sign in to comment.