Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cargo: Add sk6812_rpi for neopixel #40

Merged
merged 4 commits into from
Jul 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ linux-embedded-hal = "0.3.2"
log = "0.4.19"
nb = "1.1.0"
pwm-pca9685 = "0.3.1"
sk6812_rpi = "0.1"
30 changes: 30 additions & 0 deletions examples/rainbow.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use navigator_rs::Navigator;
use std::thread::sleep;
use std::time::Duration;

fn color_from_sine(percentage: f32) -> [u8; 3] {
let pi = std::f32::consts::PI;
let red = (percentage * 2.0 * pi).sin() * 0.5 + 0.5;
let green = ((percentage + 0.33) * 2.0 * pi).sin() * 0.5 + 0.5;
let blue = ((percentage + 0.67) * 2.0 * pi).sin() * 0.5 + 0.5;
[
(red * 255.0) as u8,
(green * 255.0) as u8,
(blue * 255.0) as u8,
]
}

fn main() {
let mut nav = Navigator::new();
nav.init();

println!("Creating rainbow effect!");
loop {
let steps = 1000;
for i in 0..=steps {
let ratio = i as f32 / steps as f32;
nav.set_neopixel(&[color_from_sine(ratio)]);
sleep(Duration::from_millis(10));
}
}
}
34 changes: 31 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@ use linux_embedded_hal::{Delay, Pin, Spidev};
use log::{info, warn};
use nb::block;
use pwm_pca9685::{Address as pwm_Address, Pca9685};
use sk6812_rpi::strip::{Bus, Strip};
use std::{
fmt,
ops::{Deref, DerefMut},
};

use std::fmt::Debug;

/// Navigator's default crystal clock for PWM, with a value of 24.5760 MHz
const NAVIGATOR_PWM_XTAL_CLOCK_FREQ: f32 = 24_576_000.0;

Expand Down Expand Up @@ -163,6 +162,7 @@ pub struct Navigator {
imu: ICM20689<SpiInterface<Spidev, Pin>>,
mag: Ak09915<I2cdev>,
led: Led,
neopixel: Strip,
}

impl Deref for Pwm {
Expand Down Expand Up @@ -284,6 +284,8 @@ impl Navigator {
.expect("Error: Failed to build BMP280 device");
bmp.zero().unwrap();

let neopixel = Strip::new(Bus::Spi0, 1).unwrap();

let mut spi = Spidev::open("/dev/spidev1.0").expect("Error: Failed during setting up SPI");
let options = SpidevOptions::new()
.bits_per_word(8)
Expand Down Expand Up @@ -326,6 +328,7 @@ impl Navigator {
mag: (mag),
imu: (imu),
led: (led),
neopixel: (neopixel),
}
}

Expand Down Expand Up @@ -671,6 +674,31 @@ impl Navigator {
self.led.set_led_all(state)
}

/// Set the values of the neopixel LED array.
///
/// # Arguments
///
/// * `array` - A 2D array containing RGB values for each LED.
/// Each inner array is a [u8; 3] representing the Red, Green and Blue from a LED.
///
/// # Example
///
/// ```no_run
/// use navigator_rs::{Navigator};
///
/// let mut nav = Navigator::new();
///
/// nav.init();
/// let mut leds = [[0, 0, 255], [0, 255, 0], [255, 0, 0]];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let mut leds = [[255, 255, 255];

the other indexes will not work, this example only turn on the blue LED.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea is to support LED strips. Right now the function only sets our board LED. Since the only LED that exists is the first, it'll only show blue.

/// nav.set_neopixel(&mut leds);
/// ```
///
/// This will set the first LED to blue, second to green, and third to red.
pub fn set_neopixel(&mut self, array: &[[u8; 3]]) {
self.neopixel.fill(array[0].into());
self.neopixel.update().unwrap();
}

/// Reads the magnetometer Ak09915 of [`Navigator`].
///
/// Measurements in \[µT\]
Expand Down Expand Up @@ -924,7 +952,7 @@ impl Navigator {
}
}

pub fn fmt_debug(&mut self) -> impl Debug {
pub fn fmt_debug(&mut self) -> impl fmt::Debug {
#[allow(dead_code)]
#[derive(Debug)]
struct Bmp {
Expand Down