feat: bootstrap esp32-c6 embassy lighting controller

This commit is contained in:
Matt Spencer committed 2026-10-17 15:21:58 +01:00
commit bb81182daa
13 files changed
+2926

No files matched your search

+116
View File
@@ -0,0 +1,116 @@
#![no_std]
#![no_main]
mod config;
mod encoder;
mod light;
mod ws2812;
mod zigbee_stack;
use embassy_embedded_hal::adapter::BlockingAsync;
use esp_alloc as _;
use esp_backtrace as _;
use esp_hal::gpio::Input;
use esp_hal::gpio::InputConfig;
use esp_hal::gpio::Pull;
use esp_hal::interrupt::software::SoftwareInterruptControl;
use esp_hal::rmt::Rmt;
use esp_hal::rmt::TxChannelConfig;
use esp_hal::rmt::TxChannelCreator;
use esp_hal::time::Rate;
use esp_hal::timer::timg::TimerGroup;
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::ws2812::Ws2812;
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;
/// Degrees of hue per detent.
const HUE_STEP: i16 = 6;
#[esp_rtos::main]
async fn main(spawner: embassy_executor::Spawner) -> ! {
esp_println::logger::init_logger_from_env();
let peripherals = esp_hal::init(esp_hal::Config::default());
let sw_int = SoftwareInterruptControl::new(peripherals.SW_INTERRUPT);
let timg0 = TimerGroup::new(peripherals.TIMG0);
esp_rtos::start(timg0.timer0, sw_int.software_interrupt0);
esp_alloc::heap_allocator!(size: 32 * 1024);
// 80 MHz with a divider of 1 gives the 12.5 ns tick the WS2812B bit
// timings in `ws2812` are expressed in
let rmt = Rmt::new(peripherals.RMT, Rate::from_mhz(80))
.expect("init RMT")
.into_async();
let rmt_channel = rmt
.channel0
.configure_tx(&TxChannelConfig::default().with_clk_divider(1))
.expect("configure RMT TX")
.with_pin(peripherals.GPIO2);
let strip = Ws2812::new(rmt_channel);
// 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
let pull_up = InputConfig::default().with_pull(Pull::Up);
let encoder_a = Input::new(peripherals.GPIO4, pull_up);
let encoder_b = Input::new(peripherals.GPIO5, pull_up);
let button = Input::new(peripherals.GPIO6, pull_up);
// 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(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"));
loop {
match EVENTS.receive().await {
EncoderEvent::Rotate(detents) => {
let steps = i16::from(detents) * STEPS_PER_BRIGHTNESS_UNIT as i16;
match light::mode() {
Mode::Brightness => light::adjust_brightness(steps * BRIGHTNESS_STEP),
Mode::Hue => light::adjust_hue(steps * HUE_STEP),
}
}
EncoderEvent::Click => {
let on = light::toggle();
log::info!("light {}", if on { "on" } else { "off" });
}
EncoderEvent::LongPress => {
let mode = light::toggle_mode();
log::info!("encoder now adjusts {mode:?}");
}
}
}
}