Skip to content

Commit

Permalink
lib: Add neopixel
Browse files Browse the repository at this point in the history
Signed-off-by: Patrick José Pereira <[email protected]>
  • Loading branch information
patrickelectric committed Jul 14, 2023
1 parent ac7834c commit e235441
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ 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},
Expand Down Expand Up @@ -161,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 @@ -282,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 @@ -324,6 +328,7 @@ impl Navigator {
mag: (mag),
imu: (imu),
led: (led),
neopixel: (neopixel),
}
}

Expand Down Expand Up @@ -669,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]];
/// 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

0 comments on commit e235441

Please sign in to comment.