Skip to content

Commit

Permalink
Merge pull request #8 from phip1611/dev
Browse files Browse the repository at this point in the history
Release 2.0.0 with no_std-support
  • Loading branch information
phip1611 authored Apr 2, 2021
2 parents c075202 + 9ff4794 commit 784e4e6
Show file tree
Hide file tree
Showing 23 changed files with 352 additions and 277 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/target
.idea
.DS_STORE
Cargo.lock
11 changes: 10 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
language: rust
rust:
- stable
- stable

script:
- rustup target add thumbv7em-none-eabihf # some embedded ARM chip
- rustup target add armv7-unknown-linux-gnueabihf # Raspberry Pi
- rustup target add x86_64-unknown-linux-gnu # regular Linux
- cargo check --target thumbv7em-none-eabihf --no-default-features # some embedded ARM chip; no_std
- cargo check --target armv7-unknown-linux-gnueabihf # raspberry Pi
- cargo build --target x86_64-unknown-linux-gnu # regular linux; also in CI
- cargo test --all-targets --target x86_64-unknown-linux-gnu # regular linux; also in CI
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# 2.0.0
This release is breaking.
* Changed main branch from `master` to `main`
* `#[no_std]`-support

# 1.0.1
Updated to SpiDev 0.4.1.

Expand Down
62 changes: 0 additions & 62 deletions Cargo.lock

This file was deleted.

13 changes: 9 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
[package]
name = "ws2818-rgb-led-spi-driver"
description = "Simple, stripped down, educational driver for WS28XX (WS2811) RGB LED (chains). It uses SPI device for timing/clock. Needs Linux and works definitely on Raspberry Pi."
version = "1.0.1"
description = "Simple, stripped down, educational, no_std-compatible driver for WS28XX (WS2811/12) RGB LEDs. Uses SPI device for timing/clock, and works definitely on Linux/Raspberry Pi."
version = "2.0.0"
authors = ["Philipp Schuster <[email protected]>"]
edition = "2018"
exclude = [
"examples",
".travis.yml",
]
keywords = ["spi", "ws2811", "ws2812", "ws2818", "neopixel"]
categories = ["hardware-support"]
categories = ["hardware-support", "no-std"]
readme = "README.md"
license = "MIT"
homepage = "https://github.com/phip1611/ws2818-rgb-led-spi-driver"
Expand All @@ -18,5 +18,10 @@ documentation = "https://docs.rs/ws2818-rgb-led-spi-driver/"

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

[features]
# by default this crate needs "std" and uses "spidev" to access SPI device on Linux
default = ["adapter_spidev"]
adapter_spidev = ["spidev"]

[dependencies]
spidev = "0.4.1"
spidev = { version = "0.4.1", optional = true }
24 changes: 20 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,30 @@ Have a look into the examples/code for further explications. :)
### Examples
See https://github.com/phip1611/ws2818-rgb-led-spi-driver/tree/master/examples.

#### Cargo.toml
```toml
[dependencies]
ws2818-rgb-led-spi-driver = "<latest version>"
# or if you need no_std
ws2818-rgb-led-spi-driver = { version = "<latest version>", default-features = false }
```

#### Code
```rust
use ws2818_rgb_led_spi_driver::encoding::{encode_rgb};
use ws2818_rgb_led_spi_driver::adapter::WS28xxAdapter;
//! Example that definitely works on Raspberry Pi.
//! Make sure you have "SPI" on your Pi enabled and that MOSI-Pin is connected
//! with DIN-Pin of the LEDs. You just need DIN pin, no clock. WS2818 uses an one-wire-protocol.
//! See the specification for details.

use ws2818_rgb_led_spi_driver::adapter_gen::WS28xxAdapter;
use ws2818_rgb_led_spi_driver::adapter_spi::WS28xxSpiAdapter;
use ws2818_rgb_led_spi_driver::encoding::encode_rgb;

fn main() {
println!("make sure you have \"SPI\" on your Pi enabled and that MOSI-Pin is connected with DIN-Pin!");
let mut adapter = WS28xxAdapter::new("/dev/spidev0.0").unwrap();
let mut adapter = WS28xxSpiAdapter::new("/dev/spidev0.0").unwrap();

// Method 1: encode first and write in two step (prefered way; better performance)
// Method 1: encode first and write in two step (preferred way; better performance)
{
let mut spi_encoded_rgb_bits = vec![];
// set first three pixels to bright red, bright green and bright blue
Expand All @@ -64,6 +79,7 @@ fn main() {
adapter.write_rgb(&rgb_values).unwrap();
}
}

```

##### Links
Expand Down
7 changes: 4 additions & 3 deletions examples/src/bin/clear.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
//! with DIN-Pin. You just need DIN pin, no clock. WS2818 uses one-wire-protocol.
//! See the specification for details
use ws2818_examples::{get_led_num_from_args};
use ws2818_rgb_led_spi_driver::adapter::WS28xxAdapter;
use ws2818_examples::get_led_num_from_args;
use ws2818_rgb_led_spi_driver::adapter_gen::WS28xxAdapter;
use ws2818_rgb_led_spi_driver::adapter_spi::WS28xxSpiAdapter;

// This example just clears the display. Useful if you don't want to unplug power all the time.
fn main() {
println!("make sure you have \"SPI\" on your Pi enabled and that MOSI-Pin is connected with DIN-Pin!");
let mut adapter = WS28xxAdapter::new("/dev/spidev0.0").unwrap();
let mut adapter = WS28xxSpiAdapter::new("/dev/spidev0.0").unwrap();

let num_leds = get_led_num_from_args();
adapter.clear(num_leds);
Expand Down
8 changes: 4 additions & 4 deletions examples/src/bin/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
//! with DIN-Pin. You just need DIN pin, no clock. WS2818 uses one-wire-protocol.
//! See the specification for details
use ws2818_examples::{get_led_num_and_color_from_args};
use ws2818_rgb_led_spi_driver::adapter::WS28xxAdapter;
use ws2818_examples::get_led_num_and_color_from_args;
use ws2818_rgb_led_spi_driver::adapter_gen::WS28xxAdapter;
use ws2818_rgb_led_spi_driver::adapter_spi::WS28xxSpiAdapter;

pub const FREQUENCY: u64 = 20; // 30 Hz
pub const FREQUENCY_MS: u64 = 1000 / FREQUENCY;
Expand All @@ -13,9 +14,8 @@ pub const FREQUENCY_MS: u64 = 1000 / FREQUENCY;
fn main() {
println!("Sets first n pixels to color x");
println!("make sure you have \"SPI\" on your Pi enabled and that MOSI-Pin is connected with DIN-Pin!");
let mut adapter = WS28xxAdapter::new("/dev/spidev0.0").unwrap();
let mut adapter = WS28xxSpiAdapter::new("/dev/spidev0.0").unwrap();
let (num_leds, r, g, b) = get_led_num_and_color_from_args();
let data = vec![(r, g, b); num_leds];
adapter.write_rgb(&data).unwrap();
}

12 changes: 7 additions & 5 deletions examples/src/bin/middle-to-edges-lights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,21 @@
//! with DIN-Pin. You just need DIN pin, no clock. WS2818 uses one-wire-protocol.
//! See the specification for details
use ws2818_examples::{get_led_num_from_args, darken_rgb, get_random_pixel_val, sleep_busy_waiting_ms};
use ws2818_rgb_led_spi_driver::adapter::WS28xxAdapter;
use std::time::{Instant, Duration};
use std::ops::Add;
use std::time::{Duration, Instant};
use ws2818_examples::{
darken_rgb, get_led_num_from_args, get_random_pixel_val, sleep_busy_waiting_ms,
};
use ws2818_rgb_led_spi_driver::adapter_gen::WS28xxAdapter;
use ws2818_rgb_led_spi_driver::adapter_spi::WS28xxSpiAdapter;

pub const FREQUENCY: u64 = 20; // in Hz
pub const FREQUENCY_MS: u64 = 1000 / FREQUENCY;

// This animations sends animated colored light strips from the middle to the strip to the edges.
fn main() {
println!("make sure you have \"SPI\" on your Pi enabled and that MOSI-Pin is connected with DIN-Pin!");
let mut adapter = WS28xxAdapter::new("/dev/spidev0.0").unwrap();
let mut adapter = WS28xxSpiAdapter::new("/dev/spidev0.0").unwrap();
let num_leds = get_led_num_from_args();
let mut anim = MovingLightStripsAnimation::new(num_leds);

Expand Down Expand Up @@ -111,4 +114,3 @@ impl MovingLightStripsAnimation {
}
}
}

13 changes: 7 additions & 6 deletions examples/src/bin/minimal.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
//! Example that definitely works on Raspberry Pi.
//! Make sure you have "SPI" on your Pi enabled and that MOSI-Pin is connected
//! with DIN-Pin. You just need DIN pin, no clock. WS2818 uses one-wire-protocol.
//! See the specification for details
//! with DIN-Pin of the LEDs. You just need DIN pin, no clock. WS2818 uses an one-wire-protocol.
//! See the specification for details.
use ws2818_rgb_led_spi_driver::encoding::{encode_rgb};
use ws2818_rgb_led_spi_driver::adapter::WS28xxAdapter;
use ws2818_rgb_led_spi_driver::adapter_gen::WS28xxAdapter;
use ws2818_rgb_led_spi_driver::adapter_spi::WS28xxSpiAdapter;
use ws2818_rgb_led_spi_driver::encoding::encode_rgb;

fn main() {
println!("make sure you have \"SPI\" on your Pi enabled and that MOSI-Pin is connected with DIN-Pin!");
let mut adapter = WS28xxAdapter::new("/dev/spidev0.0").unwrap();
let mut adapter = WS28xxSpiAdapter::new("/dev/spidev0.0").unwrap();

// Method 1: encode first and write in two step (prefered way; better performance)
// Method 1: encode first and write in two step (preferred way; better performance)
{
let mut spi_encoded_rgb_bits = vec![];
// set first three pixels to bright red, bright green and bright blue
Expand Down
11 changes: 5 additions & 6 deletions examples/src/bin/moving-pixel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@
//! with DIN-Pin. You just need DIN pin, no clock. WS2818 uses one-wire-protocol.
//! See the specification for details
use ws2818_examples::{sleep_busy_waiting_ms, get_led_num_from_args};
use ws2818_examples::{get_led_num_from_args, sleep_busy_waiting_ms};
use ws2818_rgb_led_spi_driver::adapter_gen::WS28xxAdapter;
use ws2818_rgb_led_spi_driver::adapter_spi::WS28xxSpiAdapter;
use ws2818_rgb_led_spi_driver::encoding::encode_rgb;
use ws2818_rgb_led_spi_driver::adapter::WS28xxAdapter;

// Example that shows a single moving pixel though the 8x8 led matrix.
fn main() {
println!("make sure you have \"SPI\" on your Pi enabled and that MOSI-Pin is connected with DIN-Pin!");
let mut adapter = WS28xxAdapter::new("/dev/spidev0.0").unwrap();
let mut adapter = WS28xxSpiAdapter::new("/dev/spidev0.0").unwrap();
let num_leds = get_led_num_from_args();

// note we first aggregate all data and write then all at
// once! otherwise timings would be impossible to reach


let mut i = 0;
loop {
let mut data = vec![];
Expand All @@ -31,7 +31,6 @@ fn main() {
adapter.write_encoded_rgb(&data).unwrap();

i = (i + 1) % num_leds;
sleep_busy_waiting_ms(1000/10); // 100ms / 10Hz
sleep_busy_waiting_ms(1000 / 10); // 100ms / 10Hz
}

}
25 changes: 13 additions & 12 deletions examples/src/bin/nxn-colored-display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
//! with DIN-Pin. You just need DIN pin, no clock. WS2818 uses one-wire-protocol.
//! See the specification for details
use ws2818_examples::{sleep_busy_waiting_ms, get_led_square_dim_from_args};
use ws2818_rgb_led_spi_driver::encoding::{encode_rgb_slice};
use ws2818_rgb_led_spi_driver::adapter::WS28xxAdapter;
use ws2818_examples::{get_led_square_dim_from_args, sleep_busy_waiting_ms};
use ws2818_rgb_led_spi_driver::adapter_gen::WS28xxAdapter;
use ws2818_rgb_led_spi_driver::adapter_spi::WS28xxSpiAdapter;
use ws2818_rgb_led_spi_driver::encoding::encode_rgb_slice;

// Some colored animation on a 8x8 led matrix.
fn main() {
println!("make sure you have \"SPI\" on your Pi enabled and that MOSI-Pin is connected with DIN-Pin!");
let mut adapter = WS28xxAdapter::new("/dev/spidev0.0").unwrap();
let mut adapter = WS28xxSpiAdapter::new("/dev/spidev0.0").unwrap();
let dim = get_led_square_dim_from_args();
let rows = dim;
let cols = dim;
Expand All @@ -26,17 +27,17 @@ fn main() {

// calc red
let (r_row, r_col) = ((rows - row - 1) as f64, col as f64);
let r = 255_f64 * ((r_row + 1_f64) * (r_col + 1_f64))/(leds_f64);
let r = 255_f64 * ((r_row + 1_f64) * (r_col + 1_f64)) / (leds_f64);
let r = r.round() as u8;

// calc green
let (g_row, g_col) = (row as f64, col as f64);
let g = 255_f64 * ((g_row + 1_f64) * (g_col + 1_f64))/(leds_f64);
let g = 255_f64 * ((g_row + 1_f64) * (g_col + 1_f64)) / (leds_f64);
let g = g.round() as u8;

// calc blue
let (b_row, b_col) = (row as f64, (cols - col - 1) as f64);
let b = 255_f64 * ((b_row + 1_f64) * (b_col + 1_f64))/(leds_f64);
let b = 255_f64 * ((b_row + 1_f64) * (b_col + 1_f64)) / (leds_f64);
let b = b.round() as u8;

//rgb_data.push((r, g, b));
Expand All @@ -63,16 +64,16 @@ fn main() {
let next = (&rgb_data_current[led_i + 1]).clone();
std::mem::replace(&mut rgb_data_current[led_i], next);
std::mem::replace(&mut rgb_data_current[leds - 1], curr);
}
else if led_i + 1 < leds {
} else if led_i + 1 < leds {
let next = (&rgb_data_current[led_i + 1]).clone();
std::mem::replace(&mut rgb_data_current[led_i], next);
}
}
}
let rgb_data_current_encoded = encode_rgb_slice(&rgb_data_current);
adapter.write_encoded_rgb(&rgb_data_current_encoded).unwrap();
let rgb_data_current_encoded = encode_rgb_slice(&rgb_data_current);
adapter
.write_encoded_rgb(&rgb_data_current_encoded)
.unwrap();
sleep_busy_waiting_ms(50);
}

}
Loading

0 comments on commit 784e4e6

Please sign in to comment.