diff --git a/.cargo/config b/.cargo/config index 048d8967..78f141af 100644 --- a/.cargo/config +++ b/.cargo/config @@ -22,5 +22,8 @@ rustflags = [ "-C", "link-arg=-Tlink.x", ] +[target.armv7-unknown-linux-gnueabihf] +linker = "arm-linux-gnueabihf-gcc" + [build] target = "thumbv7m-none-eabi" \ No newline at end of file diff --git a/.circleci/config.yml b/.circleci/config.yml index 12bc36b8..6c92bd46 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -25,6 +25,38 @@ target_steps: &target_steps - ./target - /usr/local/cargo/registry +cross_steps: &cross_steps + docker: + - image: cimg/rust:1.41.0 + steps: + - checkout + - restore_cache: + key: v2-ssd1306-{{ .Environment.CIRCLE_JOB }}-{{ checksum "Cargo.toml" }} + - run: sudo apt update + - run: sudo apt install -y gcc-arm-linux-gnueabihf + - run: rustup self update + - run: rustup default ${RUST_VERSION:-stable} + - run: rustup component add rustfmt + - run: cargo update + - run: | + SYSROOT=$(rustc --print sysroot) + + if [[ ! "$SYSROOT" =~ "$TARGET" ]]; then + rustup target add $TARGET + else + echo "Target $TARGET is already installed" + fi + - run: | + set -e -x + cd $EXAMPLE_PATH + cargo fmt --all -- --check + cargo build --target $TARGET --all-features --release + - save_cache: + key: v2-ssd1306-{{ .Environment.CIRCLE_JOB }}-{{ checksum "Cargo.toml" }} + paths: + - ./target + - /usr/local/cargo/registry + version: 2 jobs: target-arm-unknown-linux-eabi: @@ -73,6 +105,12 @@ jobs: - TARGET: "thumbv7m-none-eabi" <<: *target_steps + example-raspi: + environment: + - TARGET: "armv7-unknown-linux-gnueabihf" + - EXAMPLE_PATH: "examples/raspi" + <<: *cross_steps + build_jobs: &build_jobs jobs: # Raspberry Pi 1 @@ -91,6 +129,9 @@ build_jobs: &build_jobs - target-thumbv7em-none-eabihf - target-thumbv7m-none-eabi + # Samples + - example-raspi + workflows: version: 2 build_all: diff --git a/README.md b/README.md index c9009dd6..b18fdc23 100644 --- a/README.md +++ b/README.md @@ -93,6 +93,10 @@ fn HardFault(ef: &ExceptionFrame) -> ! { ``` +### Raspberry Pi + +A sample project is available under [examples/raspi](./examples/raspi) + ## License Licensed under either of 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..c3d927bf --- /dev/null +++ b/examples/raspi/Cargo.lock @@ -0,0 +1,301 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "bitflags" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "byte-slice-cast" +version = "0.3.5" +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 = "display-interface" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "display-interface-i2c" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "display-interface 0.4.0 (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 = "display-interface-spi" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "byte-slice-cast 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", + "display-interface 0.4.0 (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 = "embedded-graphics" +version = "0.6.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 = "generic-array" +version = "0.14.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "typenum 1.12.0 (registry+https://github.com/rust-lang/crates.io-index)", + "version_check 0.9.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "i2cdev" +version = "0.4.4" +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 = "ioctl-rs" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.65 (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.3.0" +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.4 (registry+https://github.com/rust-lang/crates.io-index)", + "nb 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", + "serial-core 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "serial-unix 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "spidev 0.4.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.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.2 (registry+https://github.com/rust-lang/crates.io-index)", + "linux-embedded-hal 0.3.0 (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.4.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "serial-core" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.65 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "serial-unix" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "ioctl-rs 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.65 (registry+https://github.com/rust-lang/crates.io-index)", + "serial-core 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "termios 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "spidev" +version = "0.4.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)", + "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 = "ssd1306" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "display-interface 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "display-interface-i2c 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "display-interface-spi 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "embedded-graphics 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)", + "embedded-hal 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", + "generic-array 0.14.4 (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 = "termios" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.65 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "typenum" +version = "1.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "version_check" +version = "0.9.2" +source = "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 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" +"checksum byte-slice-cast 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "b0a5e3906bcbf133e33c1d4d95afc664ad37fbdb9f6568d8043e7ea8c27d93d3" +"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 display-interface 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "da12a892db5cce95ae247e8803dc653b1a73da3b0450f7983eb518dd54f583cf" +"checksum display-interface-i2c 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "4895cd4e54e5536ef370d7f1eec787aad8275dd8ad15815aebfa71dd847b4ebf" +"checksum display-interface-spi 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "94d08c3a1682b166cbcbb4fe009a31a1834ba46c16599bf7a9b3cc7bc31b38b0" +"checksum embedded-graphics 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "40a69991ceb896bd4810a0cf2bcc46fc94b7860573c71f965d8e5b3d66942fed" +"checksum embedded-hal 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ee4908a155094da7723c2d60d617b820061e3b4efcc3d9e293d206a5a76c170b" +"checksum generic-array 0.14.4 (registry+https://github.com/rust-lang/crates.io-index)" = "501466ecc8a30d1d3b7fc9229b122b2ce8ed6e9d9223f1138d4babb253e51817" +"checksum i2cdev 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)" = "3c0eb3d9b6b02dc2508ee23439170004e44344bab9d53a490eb1f64c885b5003" +"checksum ioctl-rs 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "f7970510895cee30b3e9128319f2cefd4bde883a39f38baa279567ba3a7eb97d" +"checksum libc 0.2.65 (registry+https://github.com/rust-lang/crates.io-index)" = "1a31a0627fdf1f6a39ec0dd577e101440b7db22672c0901fe00a9a6fbb5c24e8" +"checksum linux-embedded-hal 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e04b2d8e29bb2d949bc9f0ed8acee5033833166731c7e479d0c74d7ef2ab042d" +"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 serial-core 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3f46209b345401737ae2125fe5b19a77acce90cd53e1658cda928e4fe9a64581" +"checksum serial-unix 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f03fbca4c9d866e24a459cbca71283f545a37f8e3e002ad8c70593871453cab7" +"checksum spidev 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "aa5aa93a87c20f4efdf494917ef8fb475522601256ba6bb00ad1e6101f779fe9" +"checksum ssd1306 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c4d9d9c98df2098f81655155550a3ccb1a336abeb31519e8ee6c8b6b594b9353" +"checksum sysfs_gpio 0.5.4 (registry+https://github.com/rust-lang/crates.io-index)" = "24961a55846623d8e4f6cec38718945116fed8d6970336a7110710a07aa9b5d1" +"checksum termios 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "d5d9cf598a6d7ce700a4e6a9199da127e6819a61e64b68609683cc9a01b5683a" +"checksum typenum 1.12.0 (registry+https://github.com/rust-lang/crates.io-index)" = "373c8a200f9e67a0c95e62a4f52fbf80c23b4381c05a17845531982fa99e6b33" +"checksum version_check 0.9.2 (registry+https://github.com/rust-lang/crates.io-index)" = "b5a972e5669d67ba988ce3dc826706fb0a8b01471c088cb0b6110b805cc36aed" +"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..c286085b --- /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" +linux-embedded-hal = "0.3.0" +machine-ip = "0.2.1" +ssd1306 = "0.4.0" +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..9631d483 --- /dev/null +++ b/examples/raspi/README.md @@ -0,0 +1,72 @@ +# SSD1306 demo on Raspberry pi + +This is a sample that uses the built-in I2C bus available in Linux on the Raspberry Pi B/2/3/4. + +## Connections + +This sample relies on the default [Raspberry Pi pinout](https://www.raspberrypi.org/documentation/usage/gpio/) for i2c: + +| Pin | Description | +|-----|-------------------------------------| +| 1 | 3.3 v power | +| 3 | GPIO 2 (SDA) | +| 5 | GPIO 3 (SCL) | +| 6 | ground | + + +Some boards such as the [Adafruit OLED bonnet](https://www.adafruit.com/product/3531) use this same pinout if you want something ready to use without extra wiring or soldering. + +## OS Configuration + +If you're using Raspbian - [raspi-config](https://www.raspberrypi.org/documentation/configuration/raspi-config.md) has an option to enable I2C by default. If you're using another distro - do a quick search and you will probably find a solution to enable it. + +Before running this sample, you can check that your OLED display is detected by installing `i2c-tools` from your distro, then running `i2cdetect -y 1`. The first connected SSD1306 will usually be at address `0x3c`. This sample assumes `0x3c`. If your device is at a different address, then you may need to modify the code for it to work. + +``` +$ sudo i2cdetect -y 1 + 0 1 2 3 4 5 6 7 8 9 a b c d e f +00: -- -- -- -- -- -- -- -- -- -- -- -- -- +10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +30: -- -- -- -- -- -- -- -- -- -- -- -- 3c -- -- -- +40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +70: -- -- -- -- -- -- -- -- +``` + +## Building the code + +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 +``` + +## It works! + +Once the code is running, it will look like this :) + +Commodity SSD1306 boards + +![A Raspberry Pi hooked up to a yellow+blue OLED showing the Rust logo](./images/01.jpg) + +![A Raspberry Pi hooked up to a yellow+blue OLED showing the IP and some shapes](./images/02.jpg) + +Adafruit 128x64 OLED bonnet + +![A Raspberry Pi hooked up to a yellow+blue OLED showing the IP and some shapes](./images/adafruit-oled.jpg) \ No newline at end of file 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/images/adafruit-oled.jpg b/examples/raspi/images/adafruit-oled.jpg new file mode 100644 index 00000000..d2261970 Binary files /dev/null and b/examples/raspi/images/adafruit-oled.jpg differ diff --git a/examples/raspi/src/main.rs b/examples/raspi/src/main.rs new file mode 100644 index 00000000..4ebf452e --- /dev/null +++ b/examples/raspi/src/main.rs @@ -0,0 +1,94 @@ +use embedded_graphics::{ + fonts::{Font6x8, Text}, + image::{Image, ImageRaw}, + pixelcolor::BinaryColor, + prelude::*, + primitives::{Circle, Line, Rectangle}, + style::{PrimitiveStyleBuilder, TextStyleBuilder}, + DrawTarget, +}; +use linux_embedded_hal::I2cdev; +use machine_ip; +use ssd1306::{mode::GraphicsMode, Builder, I2CDIBuilder}; +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 interface = I2CDIBuilder::new().init(i2c); + let mut disp: GraphicsMode<_> = Builder::new().connect(interface).into(); + + disp.init().unwrap(); + disp.flush().unwrap(); + + let style = PrimitiveStyleBuilder::new() + .stroke_color(BinaryColor::On) + .stroke_width(1) + .build(); + + let text_style = TextStyleBuilder::new(Font6x8) + .text_color(BinaryColor::On) + .background_color(BinaryColor::Off) + .build(); + + while running.load(Ordering::SeqCst) { + Line::new(Point::new(8, 16 + 16), Point::new(8 + 16, 16 + 16)) + .into_styled(style) + .into_iter() + .draw(&mut disp); + Line::new(Point::new(8, 16 + 16), Point::new(8 + 8, 16)) + .into_styled(style) + .into_iter() + .draw(&mut disp); + Line::new(Point::new(8 + 16, 16 + 16), Point::new(8 + 8, 16)) + .into_styled(style) + .into_iter() + .draw(&mut disp); + Rectangle::new(Point::new(48, 16), Point::new(48 + 16, 16 + 16)) + .into_styled(style) + .into_iter() + .draw(&mut disp); + Circle::new(Point::new(96, 16 + 8), 8) + .into_styled(style) + .into_iter() + .draw(&mut disp); + + let local_addr = machine_ip::get().unwrap(); + + Text::new( + &format!("IP: {}", local_addr.to_string()), + Point::new(0, 56), + ) + .into_styled(text_style) + .into_iter() + .draw(&mut disp); + + disp.flush().unwrap(); + + sleep(Duration::from_secs(2)); + + disp.clear(); + + let raw: ImageRaw = ImageRaw::new(include_bytes!("./rust.raw"), 64, 64); + + let im = Image::new(&raw, Point::new(32, 0)); + im.draw(&mut disp).unwrap(); + disp.flush().unwrap(); + + sleep(Duration::from_secs(2)); + disp.clear(); + } + disp.clear(); + disp.flush().unwrap(); +} diff --git a/examples/raspi/src/rust.raw b/examples/raspi/src/rust.raw new file mode 100644 index 00000000..cbb43ec9 Binary files /dev/null and b/examples/raspi/src/rust.raw differ