Skip to content

Commit

Permalink
Merge pull request #20 from daystram/feat/system-processor
Browse files Browse the repository at this point in the history
Add system processor and U2F flash mode control
  • Loading branch information
daystram committed Sep 7, 2024
2 parents bf042be + f6e4ffc commit 74fc6ea
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 39 deletions.
1 change: 1 addition & 0 deletions src/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ macro_rules! RG {
#[allow(dead_code, clippy::enum_variant_names)]
#[derive(Clone, Copy, Debug, Format, PartialEq)]
pub enum Control {
U2FBootloaderJump,
RGBAnimationNext,
RGBAnimationPrevious,
RGBSpeedUp,
Expand Down
2 changes: 1 addition & 1 deletion src/keyboard/quadax_rift/layout/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ pub fn get_input_map() -> InputMap<
[K(Key::Escape), ___________, ___________, ___________, ___________, ___________, ___________, ___________, ___________, ___________, ___________, ___________, ___________, K(Key::DeleteBackspace)],
[___________, C(Control::RGBAnimationNext),C(Control::RGBSpeedUp),C(Control::RGBBrightnessUp),___________,___________, ___________, ___________, K(Key::F10), K(Key::F1), K(Key::F2), K(Key::F3), ___________, ___________],
[K(Key::LeftControl), C(Control::RGBAnimationPrevious),C(Control::RGBSpeedDown),C(Control::RGBBrightnessDown),___________,___________, ___________, ___________, K(Key::F11), K(Key::F4), K(Key::F5), K(Key::F6), ___________, ___________],
[K(Key::LeftShift), ___________, ___________, ___________, ___________, ___________, ___________, ___________, K(Key::F12), K(Key::F7), K(Key::F8), K(Key::F9), ___________, K(Key::RightShift)],
[K(Key::LeftShift), C(Control::U2FBootloaderJump),___________, ___________, ___________, ___________, ___________, ___________, K(Key::F12), K(Key::F7), K(Key::F8), K(Key::F9), ___________, K(Key::RightShift)],
[___________, K(Key::LeftControl), K(Key::LeftAlt), K(Key::LeftGUI), ___________, K(Key::Space), ___________, ___________, K(Key::Space), ___________, K(Key::RightGUI), K(Key::RightAlt), K(Key::RightControl), ___________],
],
},
Expand Down
1 change: 1 addition & 0 deletions src/keyboard/quadax_rift/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ impl Configurator for Keyboard {
Some(StatusLED::new(
Box::new(pins.gpio24.into_push_pull_output()),
Box::new(pins.gpio25.into_push_pull_output()),
29,
))
} else {
None
Expand Down
18 changes: 12 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,10 @@ mod kb {
keyboard::{Configuration, Configurator, Keyboard},
matrix::{SplitScanner, SplitSwitchMatrix},
processor::{
events::rgb::{FrameIterator, RGBMatrix, RGBProcessor},
events::{
rgb::{FrameIterator, RGBMatrix, RGBProcessor},
system::SystemProcessor,
},
input::debounce::KeyMatrixRisingFallingDebounceProcessor,
mapper::{Input, Mapper},
Event, EventsProcessor, InputProcessor,
Expand Down Expand Up @@ -310,7 +313,7 @@ mod kb {
);

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

if let Some(ref mut display) = config.oled_display {
Expand Down Expand Up @@ -498,9 +501,12 @@ mod kb {
let mut mapper = Mapper::new(<Keyboard as Configurator>::get_input_map());
let events_processors: &mut [&mut dyn EventsProcessor<
<Keyboard as Configurator>::Layer,
>] = &mut [&mut RGBProcessor::<
{ <Keyboard as Configurator>::RGB_MATRIX_LED_COUNT },
>::new(frame_sender)];
>] = &mut [
&mut RGBProcessor::<{ <Keyboard as Configurator>::RGB_MATRIX_LED_COUNT }>::new(
frame_sender,
),
&mut SystemProcessor::new(29),
];

let mut poll_end_time = Mono::now();
let mut n: u64 = 0;
Expand All @@ -527,7 +533,7 @@ mod kb {
}

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

if events_processors
Expand Down
1 change: 1 addition & 0 deletions src/processor/events/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod none;
pub mod replace;
pub mod rgb;
pub mod system;
33 changes: 33 additions & 0 deletions src/processor/events/system.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use alloc::vec::Vec;
use hal::rom_data;

use crate::{
key::{Action, Control, Edge, LayerIndex},
processor::{Event, EventsProcessor, Result},
};

pub struct SystemProcessor {
u2f_activity_pin: u8,
}

#[allow(dead_code)]
impl SystemProcessor {
pub fn new(u2f_activity_pin: u8) -> Self {
SystemProcessor { u2f_activity_pin }
}
}

impl<L: LayerIndex> EventsProcessor<L> for SystemProcessor {
fn process(&mut self, events: &mut Vec<Event<L>>) -> Result {
events.iter_mut().for_each(|e| {
if e.edge == Edge::Rising {
if let Action::Control(c) = e.action {
if c == Control::U2FBootloaderJump {
rom_data::reset_to_usb_boot(1 << self.u2f_activity_pin, 0)
}
}
}
});
Ok(())
}
}
72 changes: 40 additions & 32 deletions src/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,41 @@ 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;
const REMOTE_ACTIVITY_UPDATE_PERIOD_TICK: u64 = 5_000;
const REMOTE_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,
remote_link_led: Box<dyn OutputPin<Error = gpio::Error> + Sync + Send>,
remote_activity_led: Box<dyn OutputPin<Error = gpio::Error> + Sync + Send>,
remote_activity_led_state: gpio::PinState,
remote_activity_last_update_tick: u64,
remote_activity_last_active_tick: u64,
u2f_activity_led_pin: u8,
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>,
mut remote_link_led: Box<dyn OutputPin<Error = gpio::Error> + Send + Sync>,
mut remote_activity_led: Box<dyn OutputPin<Error = gpio::Error> + Send + Sync>,
u2f_activity_led_pin: u8,
) -> Self {
link_led.set_low().unwrap();
activity_led.set_low().unwrap();
remote_link_led.set_low().unwrap();
remote_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,
remote_link_led,
remote_activity_led,
remote_activity_led_state: gpio::PinState::High,
remote_activity_last_update_tick: 0,
remote_activity_last_active_tick: 0,
u2f_activity_led_pin,
counter: 0,
}
}

pub fn set_link(&mut self, enable: bool) {
self.link_led
pub fn set_remote_link(&mut self, enable: bool) {
self.remote_link_led
.set_state(if enable {
gpio::PinState::High
} else {
Expand All @@ -45,29 +48,34 @@ impl StatusLED {
.unwrap();
}

pub fn update_activity(&mut self, active: bool) {
pub fn update_remote_activity(&mut self, active: bool) {
let now_tick = Mono::now().ticks();
if now_tick - self.activity_last_update_tick < ACTIVITY_UPDATE_PERIOD_TICK {
if now_tick - self.remote_activity_last_update_tick < REMOTE_ACTIVITY_UPDATE_PERIOD_TICK {
return;
}
self.activity_last_update_tick = now_tick;
self.remote_activity_last_update_tick = now_tick;
self.counter = self.counter.wrapping_add(1);
if active {
self.activity_last_active_tick = now_tick;
self.remote_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
}
self.remote_activity_led_state = if active
|| now_tick - self.remote_activity_last_active_tick < REMOTE_ACTIVITY_DELAY_PERIOD_TICK
{
if self.counter % 12 < 6 {
gpio::PinState::Low
} else {
gpio::PinState::High
};
self.activity_led
.set_state(self.activity_led_state)
}
} else {
gpio::PinState::High
};
self.remote_activity_led
.set_state(self.remote_activity_led_state)
.unwrap();
}

pub fn get_u2f_activity_led_pin(&mut self) -> u8 {
self.u2f_activity_led_pin
}
}

0 comments on commit 74fc6ea

Please sign in to comment.