diff --git a/examples-raspi/.github/workflows/build-raspi.yaml b/examples-raspi/.github/workflows/build-raspi.yaml new file mode 100644 index 00000000..479114d9 --- /dev/null +++ b/examples-raspi/.github/workflows/build-raspi.yaml @@ -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 \ No newline at end of file diff --git a/examples-raspi/.gitignore b/examples-raspi/.gitignore new file mode 100644 index 00000000..53eaa219 --- /dev/null +++ b/examples-raspi/.gitignore @@ -0,0 +1,2 @@ +/target +**/*.rs.bk diff --git a/examples-raspi/Cargo.lock b/examples-raspi/Cargo.lock new file mode 100644 index 00000000..96c030b7 --- /dev/null +++ b/examples-raspi/Cargo.lock @@ -0,0 +1,241 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "bitflags" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "bitflags" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "bitflags" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "byteorder" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "cast" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "cc" +version = "1.0.47" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "cfg-if" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "ctrlc" +version = "3.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "nix 0.17.0 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "embedded-graphics" +version = "0.6.0-alpha.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "embedded-hal" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "nb 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", + "void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "i2cdev" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.65 (registry+https://github.com/rust-lang/crates.io-index)", + "nix 0.14.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "libc" +version = "0.2.65" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "linux-embedded-hal" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cast 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "embedded-hal 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", + "i2cdev 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", + "spidev 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "sysfs_gpio 0.5.4 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "machine-ip" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "nb" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "nix" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bitflags 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.65 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc_version 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", + "semver 0.1.20 (registry+https://github.com/rust-lang/crates.io-index)", + "void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "nix" +version = "0.14.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "cc 1.0.47 (registry+https://github.com/rust-lang/crates.io-index)", + "cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.65 (registry+https://github.com/rust-lang/crates.io-index)", + "void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "nix" +version = "0.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "cc 1.0.47 (registry+https://github.com/rust-lang/crates.io-index)", + "cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.65 (registry+https://github.com/rust-lang/crates.io-index)", + "void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "raspi-oled" +version = "0.1.0" +dependencies = [ + "ctrlc 3.1.6 (registry+https://github.com/rust-lang/crates.io-index)", + "embedded-graphics 0.6.0-alpha.2 (registry+https://github.com/rust-lang/crates.io-index)", + "linux-embedded-hal 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "machine-ip 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "ssd1306 0.3.0-alpha.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "rustc_version" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "semver 0.1.20 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "semver" +version = "0.1.20" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "spidev" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bitflags 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.65 (registry+https://github.com/rust-lang/crates.io-index)", + "nix 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "ssd1306" +version = "0.3.0-alpha.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "embedded-graphics 0.6.0-alpha.2 (registry+https://github.com/rust-lang/crates.io-index)", + "embedded-hal 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "sysfs_gpio" +version = "0.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "nix 0.14.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "void" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[metadata] +"checksum bitflags 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "32866f4d103c4e438b1db1158aa1b1a80ee078e5d77a59a2f906fd62a577389c" +"checksum bitflags 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8dead7461c1127cf637931a1e50934eb6eee8bff2f74433ac7909e9afcee04a3" +"checksum bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" +"checksum byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "a7c3dd8985a7111efc5c80b44e23ecdd8c007de8ade3b96595387e812b957cf5" +"checksum cast 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "926013f2860c46252efceabb19f4a6b308197505082c609025aa6706c011d427" +"checksum cc 1.0.47 (registry+https://github.com/rust-lang/crates.io-index)" = "aa87058dce70a3ff5621797f1506cb837edd02ac4c0ae642b4542dce802908b8" +"checksum cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)" = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" +"checksum ctrlc 3.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "d0b676fa23f995faf587496dcd1c80fead847ed58d2da52ac1caca9a72790dd2" +"checksum embedded-graphics 0.6.0-alpha.2 (registry+https://github.com/rust-lang/crates.io-index)" = "5e784539ee7c085de51439a0a584d11d0f4ab20baf86aa83de6bb8d0f401e6b4" +"checksum embedded-hal 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ee4908a155094da7723c2d60d617b820061e3b4efcc3d9e293d206a5a76c170b" +"checksum i2cdev 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "db7cf03500911132bacb44430234aa58dba843bb6aa2820154e5abb696e3e3d9" +"checksum libc 0.2.65 (registry+https://github.com/rust-lang/crates.io-index)" = "1a31a0627fdf1f6a39ec0dd577e101440b7db22672c0901fe00a9a6fbb5c24e8" +"checksum linux-embedded-hal 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "795096c0eecb0622afb591c3e1a97b1ba046688a7689957b70b8eee9cac8fb84" +"checksum machine-ip 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "8197ad28c61566fb74b2167563865906215fbc32c61fd866bd54e7c195eb84ed" +"checksum nb 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "b1411551beb3c11dedfb0a90a0fa256b47d28b9ec2cdff34c25a2fa59e45dbdc" +"checksum nix 0.14.1 (registry+https://github.com/rust-lang/crates.io-index)" = "6c722bee1037d430d0f8e687bbdbf222f27cc6e4e68d5caf630857bb2b6dbdce" +"checksum nix 0.17.0 (registry+https://github.com/rust-lang/crates.io-index)" = "50e4785f2c3b7589a0d0c1dd60285e1188adac4006e8abd6dd578e1567027363" +"checksum nix 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7a7bb1da2be7da3cbffda73fc681d509ffd9e665af478d2bee1907cee0bc64b2" +"checksum rustc_version 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "c5f5376ea5e30ce23c03eb77cbe4962b988deead10910c372b226388b594c084" +"checksum semver 0.1.20 (registry+https://github.com/rust-lang/crates.io-index)" = "d4f410fedcf71af0345d7607d246e7ad15faaadd49d240ee3b24e5dc21a820ac" +"checksum spidev 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1ba01d3ef92a37e898fecac76cd3e1b33c999395e2d70787608d9678c4293e04" +"checksum ssd1306 0.3.0-alpha.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6137d88b742ccf36714a54bbff82d278f4a427e062a3095771c77d86d9783f62" +"checksum sysfs_gpio 0.5.4 (registry+https://github.com/rust-lang/crates.io-index)" = "24961a55846623d8e4f6cec38718945116fed8d6970336a7110710a07aa9b5d1" +"checksum void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d" +"checksum winapi 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)" = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +"checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +"checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/examples-raspi/Cargo.toml b/examples-raspi/Cargo.toml new file mode 100644 index 00000000..d1ed7840 --- /dev/null +++ b/examples-raspi/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "raspi-oled" +version = "0.1.0" +authors = ["chux0519 "] +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"] } \ No newline at end of file diff --git a/examples-raspi/README.md b/examples-raspi/README.md new file mode 100644 index 00000000..56136571 --- /dev/null +++ b/examples-raspi/README.md @@ -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) diff --git a/examples-raspi/images/01.jpg b/examples-raspi/images/01.jpg new file mode 100644 index 00000000..f56a1ff6 Binary files /dev/null and b/examples-raspi/images/01.jpg differ diff --git a/examples-raspi/images/02.jpg b/examples-raspi/images/02.jpg new file mode 100644 index 00000000..fe5bc2cc Binary files /dev/null and b/examples-raspi/images/02.jpg differ diff --git a/examples-raspi/rust.raw b/examples-raspi/rust.raw new file mode 100644 index 00000000..cbb43ec9 Binary files /dev/null and b/examples-raspi/rust.raw differ diff --git a/examples-raspi/src/main.rs b/examples-raspi/src/main.rs new file mode 100644 index 00000000..e837531a --- /dev/null +++ b/examples-raspi/src/main.rs @@ -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 = + 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(); +}