Skip to content

Commit

Permalink
add: basic status led peripheral
Browse files Browse the repository at this point in the history
  • Loading branch information
daystram committed Sep 5, 2024
1 parent 629b2d6 commit 483f295
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/keyboard/default/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ impl Configurator for Keyboard {
heartbeat_led,
rgb_matrix,
oled_display: None,
status_led: None,
},
None,
)
Expand Down
1 change: 1 addition & 0 deletions src/keyboard/kb_dev/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ impl Configurator for Keyboard {
heartbeat_led,
rgb_matrix,
oled_display: None,
status_led: None,
},
None,
)
Expand Down
2 changes: 2 additions & 0 deletions src/keyboard/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::{
processor::{events::rgb::RGBMatrix, mapper::InputMap},
remote::transport::uart::{UartReceiver, UartSender},
rotary::RotaryEncoder,
status::StatusLED,
};

pub use selected_keyboard::Keyboard;
Expand Down Expand Up @@ -74,6 +75,7 @@ pub struct Configuration {
>,
>,
>,
pub status_led: Option<StatusLED>,
}

impl Configuration {
Expand Down
15 changes: 13 additions & 2 deletions src/keyboard/quadax_rift/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
pub mod layout;

use core::cell::RefCell;

use alloc::{boxed::Box, rc::Rc};
use core::cell::RefCell;
use hal::{
fugit::{HertzU32, RateExtU32},
gpio, pac, pio, pwm, uart,
Expand All @@ -20,13 +19,15 @@ use crate::{
remote::transport::uart::{UartReceiver, UartSender},
rotary::{Mode, RotaryEncoder},
split::{self, SideDetector},
status::StatusLED,
};

const ENABLE_HEARTBEAT_LED: bool = true;
const ENABLE_KEY_MATRIX: bool = true;
const ENABLE_ROTARY_ENCODER: bool = true;
const ENABLE_RGB_MATRIX: bool = true;
const ENABLE_OLED_SCREEN: bool = true;
const ENABLE_STATUS_LED: bool = true;

pub struct Keyboard {}

Expand Down Expand Up @@ -125,6 +126,15 @@ impl Configurator for Keyboard {
None
};

let status_led = if ENABLE_STATUS_LED {
Some(StatusLED::new(
Box::new(pins.gpio24.into_push_pull_output()),
Box::new(pins.gpio25.into_push_pull_output()),
))
} else {
None
};

let mut uart_peripheral = uart::UartPeripheral::new(
uart0,
(pins.gpio0.into_function(), pins.gpio1.into_function()),
Expand Down Expand Up @@ -154,6 +164,7 @@ impl Configurator for Keyboard {
heartbeat_led,
rgb_matrix,
oled_display,
status_led,
},
Some((uart_sender, uart_receiver)),
)
Expand Down
19 changes: 18 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ mod processor;
mod remote;
mod rotary;
mod split;
mod status;
mod util;

#[macro_use]
Expand Down Expand Up @@ -87,6 +88,7 @@ mod kb {
},
rotary::RotaryEncoder,
split,
status::StatusLED,
};

rp2040_timer_monotonic!(Mono);
Expand Down Expand Up @@ -303,6 +305,10 @@ mod kb {
split::get_self_side()
);

if let Some(ref mut status_led) = config.status_led {
status_led.set_link(true);
}

if let Some(ref mut display) = config.oled_display {
display.clear();
display
Expand All @@ -322,7 +328,13 @@ mod kb {
input_sender,
)
.ok();
master_processor::spawn(input_receiver, keys_sender, frame_sender).ok();
master_processor::spawn(
input_receiver,
keys_sender,
frame_sender,
config.status_led,
)
.ok();
rgb_matrix_renderer::spawn(config.rgb_matrix, frame_receiver).ok();
}
split::Mode::Slave => {
Expand Down Expand Up @@ -470,6 +482,7 @@ mod kb {
>,
mut keys_sender: Sender<'static, Vec<Key>, KEYS_CHANNEL_BUFFER_SIZE>,
frame_sender: Sender<'static, Box<dyn FrameIterator>, 1>,
mut status_led: Option<StatusLED>,
) {
defmt::info!("master_processor()");
let input_processors: &mut [&mut dyn InputProcessor<
Expand Down Expand Up @@ -509,6 +522,10 @@ mod kb {
});
}

if let Some(ref mut status_led) = status_led {
status_led.update_activity(!events.is_empty());
}

if events_processors
.iter_mut()
.try_for_each(|p| p.process(&mut events))
Expand Down
73 changes: 73 additions & 0 deletions src/status.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
use alloc::boxed::Box;
use embedded_hal::digital::OutputPin;
use hal::gpio;
use rtic_monotonics::rp2040::prelude::*;

use crate::kb::Mono;

const ACTIVITY_UPDATE_PERIOD_TICK: u64 = 5_000;
const ACTIVITY_DELAY_PERIOD_TICK: u64 = 300_000;

pub struct StatusLED {
link_led: Box<dyn OutputPin<Error = gpio::Error> + Sync + Send>,
activity_led: Box<dyn OutputPin<Error = gpio::Error> + Sync + Send>,
activity_led_state: gpio::PinState,
activity_last_update_tick: u64,
activity_last_active_tick: u64,
counter: u16,
}

impl StatusLED {
pub fn new(
mut link_led: Box<dyn OutputPin<Error = gpio::Error> + Send + Sync>,
mut activity_led: Box<dyn OutputPin<Error = gpio::Error> + Send + Sync>,
) -> Self {
link_led.set_low().unwrap();
activity_led.set_low().unwrap();

StatusLED {
link_led,
activity_led,
activity_led_state: gpio::PinState::High,
activity_last_update_tick: 0,
activity_last_active_tick: 0,
counter: 0,
}
}

pub fn set_link(&mut self, enable: bool) {
self.link_led
.set_state(if enable {
gpio::PinState::High
} else {
gpio::PinState::Low
})
.unwrap();
}

pub fn update_activity(&mut self, active: bool) {
let now_tick = Mono::now().ticks();
if now_tick - self.activity_last_update_tick < ACTIVITY_UPDATE_PERIOD_TICK {
return;
}
self.activity_last_update_tick = now_tick;
self.counter = self.counter.wrapping_add(1);
if active {
self.activity_last_active_tick = now_tick;
}

self.activity_led_state =
if active || now_tick - self.activity_last_active_tick < ACTIVITY_DELAY_PERIOD_TICK {
if self.counter % 12 < 6 {
gpio::PinState::Low
} else {
gpio::PinState::High
}
} else {
gpio::PinState::High
};
self.activity_led
.set_state(self.activity_led_state)
.unwrap();
}
}

0 comments on commit 483f295

Please sign in to comment.