feat: bootstrap esp32-c6 embassy lighting controller
This commit is contained in:
commit
bb81182daa
13 files changed
+2926
No files matched your search
+107
@@ -0,0 +1,107 @@
|
||||
//! Interrupt-driven rotary encoder with a push button.
|
||||
//!
|
||||
//! Both tasks park on GPIO edge interrupts; nothing here polls. Quadrature is
|
||||
//! decoded from the transition between the previous and current A/B pair, so a
|
||||
//! bounce that returns to the previous state cancels itself out instead of
|
||||
//! registering a step.
|
||||
|
||||
use embassy_futures::select::Either;
|
||||
use embassy_futures::select::select;
|
||||
use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
|
||||
use embassy_sync::channel::Channel;
|
||||
use embassy_time::Duration;
|
||||
use embassy_time::Timer;
|
||||
use embassy_time::with_timeout;
|
||||
use esp_hal::gpio::Input;
|
||||
|
||||
use crate::config::DEBOUNCE_MS;
|
||||
use crate::config::LONG_PRESS_MS;
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
|
||||
pub enum EncoderEvent {
|
||||
/// One detent; positive is clockwise.
|
||||
Rotate(i8),
|
||||
Click,
|
||||
LongPress,
|
||||
}
|
||||
|
||||
/// Depth 8 absorbs a fast spin while the consumer is mid-frame; overflowing
|
||||
/// drops the oldest step, which is preferable to blocking the interrupt task.
|
||||
pub static EVENTS: Channel<CriticalSectionRawMutex, EncoderEvent, 8> = Channel::new();
|
||||
|
||||
/// Valid quadrature transitions, indexed by `(previous << 2) | current` where
|
||||
/// each pair is `(a << 1) | b`. Zero marks either no movement or an illegal
|
||||
/// jump of two states, which means a transition was missed.
|
||||
#[rustfmt::skip]
|
||||
const TRANSITIONS: [i8; 16] = [
|
||||
0, -1, 1, 0,
|
||||
1, 0, 0, -1,
|
||||
-1, 0, 0, 1,
|
||||
0, 1, -1, 0,
|
||||
];
|
||||
|
||||
#[embassy_executor::task]
|
||||
pub async fn encoder_task(mut a: Input<'static>, mut b: Input<'static>) {
|
||||
let mut previous = state(&a, &b);
|
||||
// a full detent is four quadrature transitions; only whole detents are
|
||||
// reported so a resting-point jitter never moves the light
|
||||
let mut substeps: i8 = 0;
|
||||
|
||||
loop {
|
||||
match select(a.wait_for_any_edge(), b.wait_for_any_edge()).await {
|
||||
Either::First(()) | Either::Second(()) => {}
|
||||
}
|
||||
|
||||
let current = state(&a, &b);
|
||||
if current == previous {
|
||||
continue;
|
||||
}
|
||||
|
||||
let delta = TRANSITIONS[usize::from((previous << 2) | current)];
|
||||
previous = current;
|
||||
if delta == 0 {
|
||||
continue;
|
||||
}
|
||||
|
||||
substeps += delta;
|
||||
if substeps.abs() >= 4 {
|
||||
let detents = substeps / 4;
|
||||
substeps -= detents * 4;
|
||||
// full is fine: a dropped step during a spin is invisible
|
||||
let _ = EVENTS.try_send(EncoderEvent::Rotate(detents));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[embassy_executor::task]
|
||||
pub async fn button_task(mut button: Input<'static>) {
|
||||
loop {
|
||||
// active low: the switch shorts to ground when pressed
|
||||
button.wait_for_falling_edge().await;
|
||||
Timer::after(Duration::from_millis(DEBOUNCE_MS)).await;
|
||||
if button.is_high() {
|
||||
continue;
|
||||
}
|
||||
|
||||
let long_press = with_timeout(
|
||||
Duration::from_millis(LONG_PRESS_MS),
|
||||
button.wait_for_rising_edge(),
|
||||
)
|
||||
.await
|
||||
.is_err();
|
||||
|
||||
if long_press {
|
||||
let _ = EVENTS.try_send(EncoderEvent::LongPress);
|
||||
// swallow the release so it is not read as a second press
|
||||
button.wait_for_rising_edge().await;
|
||||
} else {
|
||||
let _ = EVENTS.try_send(EncoderEvent::Click);
|
||||
}
|
||||
|
||||
Timer::after(Duration::from_millis(DEBOUNCE_MS)).await;
|
||||
}
|
||||
}
|
||||
|
||||
fn state(a: &Input<'static>, b: &Input<'static>) -> usize {
|
||||
usize::from(a.is_high()) << 1 | usize::from(b.is_high())
|
||||
}
|
||||
Reference in new issue
Block a user