Merge branch 'embedded_hal_1_0' into 'main'

Updated to rp2040_hal v0.11 and embedded_hal v1.0

See merge request cm/cmdr-keyboard!5
This commit is contained in:
Christoffer Martinsson 2025-03-09 21:39:17 +01:00
commit ba2a525694
5 changed files with 167 additions and 148 deletions

View File

@ -1,13 +1,9 @@
# #
# Cargo Configuration for the https://github.com/rp-rs/rp-hal.git repository. # Cargo Configuration for the https://github.com/rp-rs/rp-hal.git repository.
# #
# Copyright (c) The RP-RS Developers, 2021
#
# You might want to make a similar file in your own repository if you are # You might want to make a similar file in your own repository if you are
# writing programs for Raspberry Silicon microcontrollers. # writing programs for Raspberry Silicon microcontrollers.
# #
# This file is MIT or Apache-2.0 as per the repository README.md file
#
[build] [build]
# Set the default target to match the Cortex-M0+ in the RP2040 # Set the default target to match the Cortex-M0+ in the RP2040
@ -23,13 +19,12 @@ target = "thumbv6m-none-eabi"
# script. This is usually provided by the cortex-m-rt crate, and by default # script. This is usually provided by the cortex-m-rt crate, and by default
# the version in that crate will include a file called `memory.x` which # the version in that crate will include a file called `memory.x` which
# describes the particular memory layout for your specific chip. # describes the particular memory layout for your specific chip.
# * inline-threshold=5 makes the compiler more aggressive and inlining functions
# * no-vectorize-loops turns off the loop vectorizer (seeing as the M0+ doesn't # * no-vectorize-loops turns off the loop vectorizer (seeing as the M0+ doesn't
# have SIMD) # have SIMD)
rustflags = [ rustflags = [
"-C", "link-arg=--nmagic", "-C", "link-arg=--nmagic",
"-C", "link-arg=-Tlink.x", "-C", "link-arg=-Tlink.x",
"-C", "inline-threshold=5", "-C", "link-arg=-Tdefmt.x",
"-C", "no-vectorize-loops", "-C", "no-vectorize-loops",
] ]
@ -39,4 +34,4 @@ runner = "elf2uf2-rs -d"
# This runner will find a supported SWD debug probe and flash your RP2040 over # This runner will find a supported SWD debug probe and flash your RP2040 over
# SWD: # SWD:
# runner = "probe-run --chip RP2040" # runner = "probe-rs run --chip RP2040"

View File

@ -1,54 +1,60 @@
[package] [package]
name = "rp2040" name = "cmdr-keyboard-42"
version = "0.1.0" version = "0.2.0"
edition = "2021" edition = "2024"
[dependencies] [dependencies]
# rp2040_hal dependencies copied from v0.11
cortex-m = "0.7.2" cortex-m = "0.7.2"
waveshare-rp2040-zero = "0.6.0" cortex-m-rt = "0.7"
rp2040-boot2 = { version = "0.2.0", optional = true } cortex-m-rtic = "1.1.4"
rp2040-hal = { version = "0.8.0" } critical-section = {version = "1.2.0"}
cortex-m-rt = { version = "0.7", optional = true } defmt = "0.3"
defmt-rtt = "0.4.0"
dht-sensor = "0.2.1"
embedded-alloc = "0.5.1"
embedded-hal = "1.0.0"
embedded-hal-async = "1.0.0"
embedded-hal-bus = {version = "0.2.0", features = ["defmt-03"]}
embedded-io = "0.6.1"
embedded_hal_0_2 = {package = "embedded-hal", version = "0.2.5", features = ["unproven"]}
fugit = "0.3.6"
futures = {version = "0.3.30", default-features = false, features = ["async-await"]}
hd44780-driver = "0.4.0"
nb = "1.0"
panic-halt = "0.2.0" panic-halt = "0.2.0"
embedded-hal ="0.2.5" panic-probe = {version = "0.3.1", features = ["print-defmt"]}
fugit = "0.3.5"
nb = "1.0.0"
smart-leds = "0.3.0"
smart-leds-trait = "0.2.1"
ws2812-pio = "0.6.0"
usbd-human-interface-device = "0.4.2"
usb-device = "0.2"
packed_struct = { version = "0.10", default-features = false }
pio = "0.2.0" pio = "0.2.0"
pio-proc = "0.2.0"
portable-atomic = {version = "1.7.0", features = ["critical-section"]}
rp2040-boot2 = "0.3.0"
rp2040-hal = {version = "0.11.0", features = ["binary-info", "critical-section-impl", "rt", "defmt"]}
static_cell = "2.1.0"
[features] # USB hid dependencies
# This is the set of features we enable by default usbd-human-interface-device = {version = "0.5.1"}
default = ["boot2", "rt", "critical-section-impl", "rom-func-cache"] usb-device = "0.3"
packed_struct = { version = "0.10", default-features = false }
heapless = "0.8"
# critical section that is safe for multicore use # ws2812-pio dependencies
critical-section-impl = ["rp2040-hal/critical-section-impl"] ws2812-pio = "0.9.0"
smart-leds = "0.4.0"
# 2nd stage bootloaders for rp2040 [lints.clippy]
boot2 = ["rp2040-boot2"] too_long_first_doc_paragraph = "allow"
# Minimal startup / runtime for Cortex-M microcontrollers [profile.release]
rt = ["cortex-m-rt","rp2040-hal/rt"] codegen-units = 1
debug = 0
# This enables a fix for USB errata 5: USB device fails to exit RESET state on busy USB bus. debug-assertions = false
# Only required for RP2040 B0 and RP2040 B1, but it doesn't hurt to enable it incremental = false
rp2040-e5 = ["rp2040-hal/rp2040-e5"] lto = 'fat'
opt-level = 3
# Memoize(cache) ROM function pointers on first use to improve performance overflow-checks = false
rom-func-cache = ["rp2040-hal/rom-func-cache"]
# Disable automatic mapping of language features (like floating point math) to ROM functions
disable-intrinsics = ["rp2040-hal/disable-intrinsics"]
# This enables ROM functions for f64 math that were not present in the earliest RP2040s
rom-v2-intrinsics = ["rp2040-hal/rom-v2-intrinsics"]
[[bin]] [[bin]]
name = "rp2040" name = "cmdr-keyboard-42"
test = false test = false
bench = false bench = false

View File

@ -1,12 +1,12 @@
//! Project: CMtec CMDR Keyboard 42 //! Project: CMtec CMDR Keyboard 42
//! Date: 2023-07-01 //! Date: 2025-03-09
//! Author: Christoffer Martinsson //! Author: Christoffer Martinsson
//! Email: cm@cmtec.se //! Email: cm@cmtec.se
//! License: Please refer to LICENSE in root directory //! License: Please refer to LICENSE in root directory
use core::convert::Infallible; use core::convert::Infallible;
use cortex_m::delay::Delay; use cortex_m::delay::Delay;
use embedded_hal::digital::v2::*; use embedded_hal::digital::{InputPin, OutputPin};
/// Button matrix driver /// Button matrix driver
/// ///
@ -15,7 +15,7 @@ use embedded_hal::digital::v2::*;
/// let button_matrix: ButtonMatrix<4, 6, 48> = ButtonMatrix::new(row_pins, col_pins, 5); /// let button_matrix: ButtonMatrix<4, 6, 48> = ButtonMatrix::new(row_pins, col_pins, 5);
/// ``` /// ```
pub struct ButtonMatrix<'a, const R: usize, const C: usize, const N: usize> { pub struct ButtonMatrix<'a, const R: usize, const C: usize, const N: usize> {
rows: &'a [&'a dyn InputPin<Error = Infallible>; R], rows: &'a mut [&'a mut dyn InputPin<Error = Infallible>; R],
cols: &'a mut [&'a mut dyn OutputPin<Error = Infallible>; C], cols: &'a mut [&'a mut dyn OutputPin<Error = Infallible>; C],
pressed: [bool; N], pressed: [bool; N],
debounce: u8, debounce: u8,
@ -31,7 +31,7 @@ impl<'a, const R: usize, const C: usize, const N: usize> ButtonMatrix<'a, R, C,
/// * `cols` - An array of references to the column pins. /// * `cols` - An array of references to the column pins.
/// * `debounce` - The debounce time in number of scans. /// * `debounce` - The debounce time in number of scans.
pub fn new( pub fn new(
rows: &'a [&'a dyn InputPin<Error = Infallible>; R], rows: &'a mut [&'a mut dyn InputPin<Error = Infallible>; R],
cols: &'a mut [&'a mut dyn OutputPin<Error = Infallible>; C], cols: &'a mut [&'a mut dyn OutputPin<Error = Infallible>; C],
debounce: u8, debounce: u8,
) -> Self { ) -> Self {
@ -62,10 +62,10 @@ impl<'a, const R: usize, const C: usize, const N: usize> ButtonMatrix<'a, R, C,
pub fn scan_matrix(&mut self, delay: &mut Delay) { pub fn scan_matrix(&mut self, delay: &mut Delay) {
for col_index in 0..self.cols.len() { for col_index in 0..self.cols.len() {
self.cols[col_index].set_low().unwrap(); self.cols[col_index].set_low().unwrap();
delay.delay_us(10); delay.delay_us(1);
self.process_column(col_index); self.process_column(col_index);
self.cols[col_index].set_high().unwrap(); self.cols[col_index].set_high().unwrap();
delay.delay_us(10); delay.delay_us(1);
} }
} }

View File

@ -14,31 +14,33 @@ mod status_led;
use button_matrix::ButtonMatrix; use button_matrix::ButtonMatrix;
use core::convert::Infallible; use core::convert::Infallible;
use cortex_m::delay::Delay; use cortex_m::delay::Delay;
use embedded_hal::digital::v2::*; use embedded_hal::digital::{InputPin, OutputPin};
use embedded_hal::timer::CountDown; use embedded_hal_0_2::timer::CountDown;
use fugit::ExtU32; use fugit::ExtU32;
use panic_halt as _; use panic_halt as _;
use rp2040_hal::{ use rp2040_hal::{
gpio::{Function, FunctionConfig, PinId, ValidPinMode}, Sio,
pio::StateMachineIndex, clocks::{Clock, init_clocks_and_plls},
gpio::{AnyPin, Pins},
pac,
pio::{PIOExt, StateMachineIndex},
timer::Timer,
watchdog::Watchdog,
}; };
use status_led::{StatusMode, Ws2812StatusLed}; use status_led::{StatusMode, Ws2812StatusLed};
use usb_device::class_prelude::*; use usb_device::class_prelude::*;
use usb_device::prelude::*; use usb_device::prelude::*;
use usbd_human_interface_device::page::Keyboard; use usbd_human_interface_device::page::Keyboard;
use usbd_human_interface_device::prelude::*; use usbd_human_interface_device::prelude::*;
use waveshare_rp2040_zero::entry;
use waveshare_rp2040_zero::{ // The linker will place this boot block at the start of our program image. We
hal::{ /// need this to help the ROM bootloader get our code up and running.
clocks::{init_clocks_and_plls, Clock}, #[unsafe(link_section = ".boot2")]
pac, #[unsafe(no_mangle)]
pio::PIOExt, #[used]
timer::Timer, pub static BOOT2_FIRMWARE: [u8; 256] = rp2040_boot2::BOOT_LOADER_W25Q080;
watchdog::Watchdog,
Sio, const XTAL_FREQ_HZ: u32 = 12_000_000u32;
},
Pins, XOSC_CRYSTAL_FREQ,
};
// Public constants // Public constants
pub const KEY_ROWS: usize = 4; pub const KEY_ROWS: usize = 4;
@ -53,7 +55,7 @@ pub struct KeyboardButton {
pub fn_mode: u8, pub fn_mode: u8,
} }
#[entry] #[rp2040_hal::entry]
fn main() -> ! { fn main() -> ! {
// Grab our singleton objects // Grab our singleton objects
let mut pac = pac::Peripherals::take().unwrap(); let mut pac = pac::Peripherals::take().unwrap();
@ -63,7 +65,7 @@ fn main() -> ! {
// Configure clocks and PLLs // Configure clocks and PLLs
let clocks = init_clocks_and_plls( let clocks = init_clocks_and_plls(
XOSC_CRYSTAL_FREQ, XTAL_FREQ_HZ,
pac.XOSC, pac.XOSC,
pac.CLOCKS, pac.CLOCKS,
pac.PLL_SYS, pac.PLL_SYS,
@ -88,68 +90,50 @@ fn main() -> ! {
); );
// Setting up array with pins connected to button rows // Setting up array with pins connected to button rows
let button_matrix_row_pins: &[&dyn InputPin<Error = Infallible>; KEY_ROWS] = &[ let button_matrix_row_pins: &mut [&mut dyn InputPin<Error = Infallible>; KEY_ROWS] = &mut [
&pins.gp0.into_pull_up_input(), &mut pins.gpio0.into_pull_up_input(),
&pins.gp1.into_pull_up_input(), &mut pins.gpio1.into_pull_up_input(),
&pins.gp29.into_pull_up_input(), &mut pins.gpio29.into_pull_up_input(),
&pins.gp28.into_pull_up_input(), &mut pins.gpio28.into_pull_up_input(),
]; ];
// Setting up array with pins connected to button columns // Setting up array with pins connected to button columns
let button_matrix_col_pins: &mut [&mut dyn OutputPin<Error = Infallible>; KEY_COLS] = &mut [ let button_matrix_col_pins: &mut [&mut dyn OutputPin<Error = Infallible>; KEY_COLS] = &mut [
&mut pins.gp12.into_push_pull_output(), &mut pins.gpio12.into_push_pull_output(),
&mut pins.gp13.into_push_pull_output(), &mut pins.gpio13.into_push_pull_output(),
&mut pins.gp14.into_push_pull_output(), &mut pins.gpio14.into_push_pull_output(),
&mut pins.gp15.into_push_pull_output(), &mut pins.gpio15.into_push_pull_output(),
&mut pins.gp26.into_push_pull_output(), &mut pins.gpio26.into_push_pull_output(),
&mut pins.gp27.into_push_pull_output(), &mut pins.gpio27.into_push_pull_output(),
&mut pins.gp7.into_push_pull_output(), &mut pins.gpio7.into_push_pull_output(),
&mut pins.gp8.into_push_pull_output(), &mut pins.gpio8.into_push_pull_output(),
&mut pins.gp6.into_push_pull_output(), &mut pins.gpio6.into_push_pull_output(),
&mut pins.gp9.into_push_pull_output(), &mut pins.gpio9.into_push_pull_output(),
&mut pins.gp10.into_push_pull_output(), &mut pins.gpio10.into_push_pull_output(),
&mut pins.gp11.into_push_pull_output(), &mut pins.gpio11.into_push_pull_output(),
]; ];
// Create button matrix object that scans all the PCB buttons // Create button matrix object that scans all the PCB buttons
let mut button_matrix: ButtonMatrix<KEY_ROWS, KEY_COLS, NUMBER_OF_KEYS> = let mut button_matrix: ButtonMatrix<KEY_ROWS, KEY_COLS, NUMBER_OF_KEYS> =
ButtonMatrix::new(button_matrix_row_pins, button_matrix_col_pins, 5); ButtonMatrix::new(button_matrix_row_pins, button_matrix_col_pins, 5);
// Configure USB
let usb_bus = UsbBusAllocator::new(waveshare_rp2040_zero::hal::usb::UsbBus::new(
pac.USBCTRL_REGS,
pac.USBCTRL_DPRAM,
clocks.usb_clock,
true,
&mut pac.RESETS,
));
let mut keyboard = UsbHidClassBuilder::new()
.add_device(
usbd_human_interface_device::device::keyboard::NKROBootKeyboardConfig::default(),
)
.build(&usb_bus);
let mut usb_dev = UsbDeviceBuilder::new(&usb_bus, UsbVidPid(0x1209, 0x0001))
.manufacturer("CMtec")
.product("CMDR keyboard")
.serial_number("0001")
.build();
// Create status LED // Create status LED
let (mut pio, sm0, _, _, _) = pac.PIO0.split(&mut pac.RESETS); let (mut pio, sm0, _, _, _) = pac.PIO0.split(&mut pac.RESETS);
let mut status_led = Ws2812StatusLed::new( let mut status_led = Ws2812StatusLed::new(
pins.neopixel.into_mode(), pins.gpio16.into_function(),
&mut pio, &mut pio,
sm0, sm0,
clocks.peripheral_clock.freq(), clocks.peripheral_clock.freq(),
); );
// Set red color to statusled indicating error if not reaching assumed state (USB connect)
status_led.update(StatusMode::Error);
// Create keyboard button array // Create keyboard button array
let mut buttons: [KeyboardButton; NUMBER_OF_KEYS] = [KeyboardButton::default(); NUMBER_OF_KEYS]; let mut buttons: [KeyboardButton; NUMBER_OF_KEYS] = [KeyboardButton::default(); NUMBER_OF_KEYS];
// Create timers/delays // Create timers/delays
let timer = Timer::new(pac.TIMER, &mut pac.RESETS); let timer = Timer::new(pac.TIMER, &mut pac.RESETS, &clocks);
let mut delay = Delay::new(core.SYST, clocks.system_clock.freq().to_Hz()); let mut delay = Delay::new(core.SYST, clocks.system_clock.freq().to_Hz());
let mut usb_hid_report_count_down = timer.count_down(); let mut usb_hid_report_count_down = timer.count_down();
@ -161,9 +145,6 @@ fn main() -> ! {
let mut status_led_count_down = timer.count_down(); let mut status_led_count_down = timer.count_down();
status_led_count_down.start(250.millis()); status_led_count_down.start(250.millis());
// let mut start_count_down = timer.count_down();
// start_count_down.start(5000.millis());
// Create variables to track caps lock and fn mode // Create variables to track caps lock and fn mode
let mut caps_lock_active: bool = false; let mut caps_lock_active: bool = false;
let mut fn_mode: u8; let mut fn_mode: u8;
@ -187,24 +168,39 @@ fn main() -> ! {
rp2040_hal::rom_data::reset_to_usb_boot(gpio_activity_pin_mask, disable_interface_mask); rp2040_hal::rom_data::reset_to_usb_boot(gpio_activity_pin_mask, disable_interface_mask);
} }
// Configure USB
let usb_bus = UsbBusAllocator::new(rp2040_hal::usb::UsbBus::new(
pac.USBCTRL_REGS,
pac.USBCTRL_DPRAM,
clocks.usb_clock,
true,
&mut pac.RESETS,
));
let mut keyboard = UsbHidClassBuilder::new()
.add_device(
usbd_human_interface_device::device::keyboard::NKROBootKeyboardConfig::default(),
)
.build(&usb_bus);
let mut usb_dev = UsbDeviceBuilder::new(&usb_bus, UsbVidPid(0x1209, 0x0001))
.strings(&[StringDescriptors::default()
.manufacturer("CMtec")
.product("CMDR Keyboard 42")
.serial_number("0001")])
.unwrap()
.build();
loop { loop {
if status_led_count_down.wait().is_ok() { if status_led_count_down.wait().is_ok() {
update_status_led(&mut status_led, &caps_lock_active, &sticky_state, &started); update_status_led(&mut status_led, &caps_lock_active, &sticky_state, &started);
} }
// if start_count_down.wait().is_ok() && !started {
// started = true;
// }
if usb_hid_report_count_down.wait().is_ok() { if usb_hid_report_count_down.wait().is_ok() {
let pressed_keys = button_matrix.buttons_pressed(); let pressed_keys = button_matrix.buttons_pressed();
fn_mode = get_fn_mode(pressed_keys); fn_mode = get_fn_mode(pressed_keys);
// if !caps_lock_active && sticky_state != 2 {
// update_status_led(&mut status_led, &caps_lock_active, &sticky_state, &started);
// }
for (index, key) in pressed_keys.iter().enumerate() { for (index, key) in pressed_keys.iter().enumerate() {
buttons[index].pressed = *key; buttons[index].pressed = *key;
} }
@ -268,9 +264,8 @@ fn update_status_led<P, SM, I>(
sticky_state: &u8, sticky_state: &u8,
started: &bool, started: &bool,
) where ) where
P: PIOExt + FunctionConfig, I: AnyPin<Function = P::PinFunction>,
I: PinId, P: PIOExt,
Function<P>: ValidPinMode<I>,
SM: StateMachineIndex, SM: StateMachineIndex,
{ {
if *caps_lock_active { if *caps_lock_active {

View File

@ -1,11 +1,11 @@
//! Project: CMtec CMDR Keyboard 42 //! Project: CMtec CMDR Keyboard 42
//! Date: 2023-07-01 //! Date: 2025-03-09
//! Author: Christoffer Martinsson //! Author: Christoffer Martinsson
//! Email: cm@cmtec.se //! Email: cm@cmtec.se
//! License: Please refer to LICENSE in root directory //! License: Please refer to LICENSE in root directory
use rp2040_hal::{ use rp2040_hal::{
gpio::{Function, FunctionConfig, Pin, PinId, ValidPinMode}, gpio::AnyPin,
pio::{PIOExt, StateMachineIndex, UninitStateMachine, PIO}, pio::{PIOExt, StateMachineIndex, UninitStateMachine, PIO},
}; };
use smart_leds::{SmartLedsWrite, RGB8}; use smart_leds::{SmartLedsWrite, RGB8};
@ -25,17 +25,16 @@ use ws2812_pio::Ws2812Direct;
pub enum StatusMode { pub enum StatusMode {
Off = 0, Off = 0,
Normal = 1, Normal = 1,
Activity = 2, NormalFlash = 2,
ActivityFlash = 3, Activity = 3,
Other = 4, ActivityFlash = 4,
OtherFlash = 5, Other = 5,
Warning = 6, OtherFlash = 6,
Error = 7, Warning = 7,
Bootloader = 8, Error = 8,
Bootloader = 9,
} }
#[warn(dead_code)] #[warn(dead_code)]
/// Status LED driver
/// This driver uses the PIO state machine to drive a WS2812 LED /// This driver uses the PIO state machine to drive a WS2812 LED
/// ///
/// # Example /// # Example
@ -50,20 +49,19 @@ pub enum StatusMode {
/// ``` /// ```
pub struct Ws2812StatusLed<P, SM, I> pub struct Ws2812StatusLed<P, SM, I>
where where
I: PinId, I: AnyPin<Function = P::PinFunction>,
P: PIOExt + FunctionConfig, P: PIOExt,
Function<P>: ValidPinMode<I>,
SM: StateMachineIndex, SM: StateMachineIndex,
{ {
ws2812_direct: Ws2812Direct<P, SM, I>, ws2812_direct: Ws2812Direct<P, SM, I>,
state: bool, state: bool,
mode: StatusMode,
} }
impl<P, SM, I> Ws2812StatusLed<P, SM, I> impl<P, SM, I> Ws2812StatusLed<P, SM, I>
where where
I: PinId, I: AnyPin<Function = P::PinFunction>,
P: PIOExt + FunctionConfig, P: PIOExt,
Function<P>: ValidPinMode<I>,
SM: StateMachineIndex, SM: StateMachineIndex,
{ {
/// Creates a new instance of this driver. /// Creates a new instance of this driver.
@ -75,7 +73,7 @@ where
/// * `sm` - PIO state machine /// * `sm` - PIO state machine
/// * `clock_freq` - PIO clock frequency /// * `clock_freq` - PIO clock frequency
pub fn new( pub fn new(
pin: Pin<I, Function<P>>, pin: I,
pio: &mut PIO<P>, pio: &mut PIO<P>,
sm: UninitStateMachine<(P, SM)>, sm: UninitStateMachine<(P, SM)>,
clock_freq: fugit::HertzU32, clock_freq: fugit::HertzU32,
@ -83,28 +81,40 @@ where
// prepare the PIO program // prepare the PIO program
let ws2812_direct = Ws2812Direct::new(pin, pio, sm, clock_freq); let ws2812_direct = Ws2812Direct::new(pin, pio, sm, clock_freq);
let state = false; let state = false;
let mode = StatusMode::Off;
Self { Self {
ws2812_direct, ws2812_direct,
state, state,
mode,
} }
} }
/// Get current status mode
#[allow(dead_code)]
pub fn get_mode(&self) -> StatusMode {
self.mode
}
#[warn(dead_code)]
/// Update status LED /// Update status LED
/// Depending on the mode, the LED will be set to a different colour /// Depending on the mode, the LED will be set to a different colour
/// ///
/// * OFF = off /// * OFF = off
/// * NORMAL = green /// * NORMAL = green
/// * NORMALFLASH = green (flashing)
/// * ACTIVITY = blue /// * ACTIVITY = blue
/// * ACTIVITYFLASH = blue (flashing)
/// * OTHER = orange /// * OTHER = orange
/// * OTHERFLASH = orange (flashing)
/// * WARNING = red (flashing) /// * WARNING = red (flashing)
/// * ERROR = red /// * ERROR = red
/// * BOOTLOADER = purple /// * BOOTLOADER = purple
/// ///
/// Make sure to call this function regularly to keep the LED flashing /// Make sure to call this function regularly to keep the LED flashing
pub fn update(&mut self, mode: StatusMode) { pub fn update(&mut self, mode: StatusMode) {
let colors: [RGB8; 9] = [ let colors: [RGB8; 10] = [
(0, 0, 0).into(), // Off (0, 0, 0).into(), // Off
(10, 7, 0).into(), // Green (10, 7, 0).into(), // Green
(10, 7, 0).into(), // Green
(10, 4, 10).into(), // Blue (10, 4, 10).into(), // Blue
(10, 4, 10).into(), // Blue (10, 4, 10).into(), // Blue
(5, 10, 0).into(), // Orange (5, 10, 0).into(), // Orange
@ -114,18 +124,31 @@ where
(0, 10, 10).into(), // Purple (0, 10, 10).into(), // Purple
]; ];
if (mode == StatusMode::ActivityFlash if mode == StatusMode::Warning
|| mode == StatusMode::NormalFlash
|| mode == StatusMode::ActivityFlash
|| mode == StatusMode::OtherFlash || mode == StatusMode::OtherFlash
|| mode == StatusMode::Warning) || mode != self.mode
{
self.mode = mode;
} else {
return;
}
if (mode == StatusMode::Warning
|| mode == StatusMode::NormalFlash
|| mode == StatusMode::ActivityFlash
|| mode == StatusMode::OtherFlash)
&& !self.state && !self.state
{ {
self.ws2812_direct self.ws2812_direct
.write([colors[mode as usize]].iter().copied()) .write([colors[mode as usize]].iter().copied())
.unwrap(); .unwrap();
self.state = true; self.state = true;
} else if mode == StatusMode::ActivityFlash } else if mode == StatusMode::Warning
|| mode == StatusMode::NormalFlash
|| mode == StatusMode::ActivityFlash
|| mode == StatusMode::OtherFlash || mode == StatusMode::OtherFlash
|| mode == StatusMode::Warning
|| mode == StatusMode::Off || mode == StatusMode::Off
{ {
self.ws2812_direct self.ws2812_direct