feat(encoder): trigger on B and sample A

Waiting on both phases via select drops the pending future each time the
other fires, so B's edges were being lost and a later sample of B always
read it back at rest. Triggering on B's falling edge and sampling A there
is stable on this encoder.

Also moves the encoder to D10/D9/D8 next to the power pads, drops the
strip to a single LED for bring-up, and makes saturation an adjustable
axis so the light defaults to white.
This commit is contained in:
Matt Spencer committed 2026-10-17 18:22:09 +01:00
1 parent b0e3b6f4a0
commit 9793bc62af
6 files changed
+192 -97

No files matched your search

+51 -32
View File
@@ -6,9 +6,9 @@ mod encoder;
mod light;
mod status;
mod strip;
#[cfg(feature = "zigbee")]
mod zigbee_stack;
use embassy_embedded_hal::adapter::BlockingAsync;
use esp_alloc as _;
use esp_backtrace as _;
use esp_hal::gpio::Input;
@@ -21,28 +21,24 @@ use esp_hal::rmt::Rmt;
use esp_hal::time::Rate;
use esp_hal::timer::timg::TimerGroup;
use esp_hal_smartled::WS2812B_TIMING;
use esp_radio::ieee802154::Ieee802154;
use esp_storage::FlashStorage;
use static_cell::StaticCell;
use zigbee_mac::esp::EspMlme;
use crate::config::STEPS_PER_BRIGHTNESS_UNIT;
use crate::encoder::EVENTS;
use crate::encoder::EncoderEvent;
use crate::light::Mode;
use crate::strip::Strip;
use crate::zigbee_stack::Stack;
esp_bootloader_esp_idf::esp_app_desc!();
static STACK: StaticCell<Stack> = StaticCell::new();
/// Brightness units per detent.
const BRIGHTNESS_STEP: i16 = 8;
/// Hue per detent, in 1/256ths of the colour circle (~5.6 degrees).
const HUE_STEP: i16 = 4;
/// Saturation units per detent; 0 is white, 255 fully coloured.
const SATURATION_STEP: i16 = 16;
#[esp_rtos::main]
async fn main(spawner: embassy_executor::Spawner) -> ! {
esp_println::logger::init_logger_from_env();
@@ -65,38 +61,54 @@ async fn main(spawner: embassy_executor::Spawner) -> ! {
.expect("configure WS2812B adapter");
// the encoder's common pin is grounded, so both phases and the switch idle
// high and are driven low; the pull-ups make an open contact well defined
// high and are driven low; the pull-ups make an open contact well defined.
// D10/D9/D8 sit directly under the 3V3 and GND pads, keeping the module's
// five wires on one side of the board.
let pull_up = InputConfig::default().with_pull(Pull::Up);
let encoder_a = Input::new(peripherals.GPIO21, pull_up);
let encoder_b = Input::new(peripherals.GPIO22, pull_up);
let button = Input::new(peripherals.GPIO23, pull_up);
let encoder_a = Input::new(peripherals.GPIO18, pull_up); // D10, CLK
let encoder_b = Input::new(peripherals.GPIO20, pull_up); // D9, DT
let button = Input::new(peripherals.GPIO19, pull_up); // D8, SW
// on-board user LED, starting dark
let led = Output::new(peripherals.GPIO15, Level::High, OutputConfig::default());
// the stack owns persistence: the information bases are restored here and
// written back whenever the stack changes them
let flash = BlockingAsync::new(FlashStorage::new(peripherals.FLASH));
let storage = zigbee::storage::init_with_flash(flash, zigbee_stack::FLASH_RANGE).await;
let ieee802154 = Ieee802154::new(peripherals.IEEE802154);
let mac = EspMlme::new(ieee802154, esp_radio::ieee802154::Config::default());
log::info!("device IEEE address: {:#018x}", mac.ieee_address());
let stack: &'static Stack = STACK.init(Stack::new(
mac,
zigbee_stack::config(),
zigbee_stack::handler(),
storage,
));
spawner.spawn(status::heartbeat_task(led).expect("spawn heartbeat_task"));
spawner.spawn(light::render_task(strip).expect("spawn render_task"));
spawner.spawn(encoder::encoder_task(encoder_a, encoder_b).expect("spawn encoder_task"));
spawner.spawn(encoder::button_task(button).expect("spawn button_task"));
spawner.spawn(zigbee_stack::stack_task(stack).expect("spawn stack_task"));
spawner.spawn(zigbee_stack::on_off_task().expect("spawn on_off_task"));
spawner.spawn(zigbee_stack::identify_task().expect("spawn identify_task"));
#[cfg(feature = "zigbee")]
{
use embassy_embedded_hal::adapter::BlockingAsync;
use esp_radio::ieee802154::Ieee802154;
use esp_storage::FlashStorage;
use static_cell::StaticCell;
use zigbee_mac::esp::EspMlme;
use crate::zigbee_stack::Stack;
static STACK: StaticCell<Stack> = StaticCell::new();
// the stack owns persistence: the information bases are restored here
// and written back whenever the stack changes them
let flash = BlockingAsync::new(FlashStorage::new(peripherals.FLASH));
let storage = zigbee::storage::init_with_flash(flash, zigbee_stack::FLASH_RANGE).await;
let ieee802154 = Ieee802154::new(peripherals.IEEE802154);
let mac = EspMlme::new(ieee802154, esp_radio::ieee802154::Config::default());
log::info!("device IEEE address: {:#018x}", mac.ieee_address());
let stack: &'static Stack = STACK.init(Stack::new(
mac,
zigbee_stack::config(),
zigbee_stack::handler(),
storage,
));
spawner.spawn(zigbee_stack::stack_task(stack).expect("spawn stack_task"));
spawner.spawn(zigbee_stack::on_off_task().expect("spawn on_off_task"));
spawner.spawn(zigbee_stack::identify_task().expect("spawn identify_task"));
}
loop {
match EVENTS.receive().await {
@@ -105,14 +117,21 @@ async fn main(spawner: embassy_executor::Spawner) -> ! {
match light::mode() {
Mode::Brightness => light::adjust_brightness(steps * BRIGHTNESS_STEP),
Mode::Hue => light::adjust_hue(steps * HUE_STEP),
Mode::Saturation => light::adjust_saturation(steps * SATURATION_STEP),
}
log::info!(
"rotate {detents:+} -> brightness {} hue {} saturation {}",
light::brightness(),
light::hue(),
light::saturation()
);
}
EncoderEvent::Click => {
let on = light::toggle();
log::info!("light {}", if on { "on" } else { "off" });
}
EncoderEvent::LongPress => {
let mode = light::toggle_mode();
let mode = light::next_mode();
log::info!("encoder now adjusts {mode:?}");
}
}