diff --git a/Cargo.lock b/Cargo.lock index 83b724a..b95804c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -829,6 +829,17 @@ dependencies = [ "termcolor", ] +[[package]] +name = "esp-hal-smartled" +version = "0.18.0" +dependencies = [ + "document-features", + "esp-hal", + "num-traits", + "smart-leds-trait", + "thiserror", +] + [[package]] name = "esp-metadata-generated" version = "0.4.0" @@ -1739,6 +1750,7 @@ dependencies = [ "esp-backtrace", "esp-bootloader-esp-idf", "esp-hal", + "esp-hal-smartled", "esp-println", "esp-radio", "esp-rtos", diff --git a/Cargo.toml b/Cargo.toml index e02776d..dda914b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -36,11 +36,11 @@ static_cell = "2.1" log = "0.4" # LED colour types, the async write trait, and the gamma/brightness helpers. -# The driver below encodes RMT symbols itself because no upstream RMT driver -# currently targets esp-hal 1.1: ws2812-esp32-rmt-driver is esp-idf only, and -# esp-hal-smartled pins ~1.0 (0.17) or ~1.2 (0.18). smart-leds = "0.4" smart-leds-trait = "0.3" +# vendored upstream: widened to accept the esp-hal 1.1 that esp-rtos and +# esp-radio pin us to, so the fix can go back upstream +esp-hal-smartled = { path = "vendor/esp-hal-community/esp-hal-smartled" } # vendored upstream: the lighting cluster is not implemented there yet, so the # path dependency lets us add it in-tree and upstream it later diff --git a/src/light.rs b/src/light.rs index dde23e2..0a9980f 100644 --- a/src/light.rs +++ b/src/light.rs @@ -19,7 +19,7 @@ use smart_leds::hsv::hsv2rgb; use crate::config::LED_COUNT; use crate::config::MAX_DUTY; -use crate::ws2812::Ws2812; +use crate::strip::Strip; /// What a turn of the encoder adjusts. #[derive(Clone, Copy, PartialEq, Eq, Debug)] @@ -86,7 +86,7 @@ pub fn toggle_mode() -> Mode { } #[embassy_executor::task] -pub async fn render_task(mut strip: Ws2812<'static>) { +pub async fn render_task(mut strip: Strip<'static>) { let mut frame = [RGB8::default(); LED_COUNT]; loop { diff --git a/src/main.rs b/src/main.rs index d6a3691..a87d9f7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,7 +4,7 @@ mod config; mod encoder; mod light; -mod ws2812; +mod strip; mod zigbee_stack; use embassy_embedded_hal::adapter::BlockingAsync; @@ -15,10 +15,9 @@ 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_hal_smartled::WS2812B_TIMING; use esp_radio::ieee802154::Ieee802154; use esp_storage::FlashStorage; use static_cell::StaticCell; @@ -28,7 +27,7 @@ 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::strip::Strip; use crate::zigbee_stack::Stack; esp_bootloader_esp_idf::esp_app_desc!(); @@ -52,17 +51,14 @@ async fn main(spawner: embassy_executor::Spawner) -> ! { 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)) + // the adapter derives its pulse timings from this frequency, so the same + // rate has to be handed to both calls + let rmt_freq = Rate::from_mhz(80); + let rmt = Rmt::new(peripherals.RMT, rmt_freq) .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); + let strip = Strip::new(WS2812B_TIMING, rmt.channel0, peripherals.GPIO2, rmt_freq) + .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 diff --git a/src/strip.rs b/src/strip.rs new file mode 100644 index 0000000..c74247d --- /dev/null +++ b/src/strip.rs @@ -0,0 +1,18 @@ +//! The WS2812B strip, as driven by the upstream `esp-hal-smartled` adapter. +//! +//! This only names the concrete type so the rest of the application does not +//! carry the adapter's four generic parameters around. + +use esp_hal::Async; +use esp_hal_smartled::RmtSmartLeds; +use esp_hal_smartled::buffer_size; +use esp_hal_smartled::color_order::Grb; +use smart_leds::RGB8; + +use crate::config::LED_COUNT; + +/// 24 pulses per pixel plus the reset and end-marker codes. +pub const BUFFER_SIZE: usize = buffer_size::(LED_COUNT); + +/// WS2812B pixels latch green first. +pub type Strip<'d> = RmtSmartLeds<'d, BUFFER_SIZE, Async, RGB8, Grb>; diff --git a/src/ws2812.rs b/src/ws2812.rs deleted file mode 100644 index 7894670..0000000 --- a/src/ws2812.rs +++ /dev/null @@ -1,78 +0,0 @@ -//! WS2812B driver over the RMT peripheral. -//! -//! The RMT clock is divided to 12.5 ns per tick, so each of the 24 colour bits -//! becomes one `PulseCode` whose high and low durations encode a 0 or a 1. -//! -//! Only the symbol encoding lives here; colour types, gamma and brightness -//! come from `smart-leds`, and the strip is driven through its async write -//! trait so an upstream driver can replace this one without touching callers. - -use esp_hal::Async; -use esp_hal::gpio::Level; -use esp_hal::rmt::Channel; -use esp_hal::rmt::PulseCode; -use esp_hal::rmt::Tx; -use smart_leds_trait::RGB8; -use smart_leds_trait::SmartLedsWriteAsync; - -use crate::config::LED_COUNT; - -/// Bit timings in RMT ticks at 80 MHz with a divider of 1 (12.5 ns/tick). -const T0H: u16 = 32; // 0.40 us -const T0L: u16 = 68; // 0.85 us -const T1H: u16 = 64; // 0.80 us -const T1L: u16 = 36; // 0.45 us - -/// Pulse codes for a whole frame, plus the terminating end marker. -const PULSE_COUNT: usize = LED_COUNT * 24 + 1; - -pub struct Ws2812<'d> { - channel: Channel<'d, Async, Tx>, - pulses: [PulseCode; PULSE_COUNT], -} - -impl<'d> Ws2812<'d> { - pub fn new(channel: Channel<'d, Async, Tx>) -> Self { - let mut pulses = [PulseCode::default(); PULSE_COUNT]; - pulses[PULSE_COUNT - 1] = PulseCode::end_marker(); - Self { channel, pulses } - } -} - -impl SmartLedsWriteAsync for Ws2812<'_> { - type Error = esp_hal::rmt::Error; - type Color = RGB8; - - /// An iterator shorter than the strip leaves the remaining pixels dark; a - /// longer one is truncated. - async fn write(&mut self, iterator: T) -> Result<(), Self::Error> - where - T: IntoIterator, - I: Into, - { - let (slots, _) = self.pulses[..LED_COUNT * 24].as_chunks_mut::<24>(); - let mut slots = slots.iter_mut(); - - for colour in iterator { - let Some(slot) = slots.next() else { break }; - encode(colour.into(), slot); - } - for slot in slots { - encode(RGB8::default(), slot); - } - - self.channel.transmit(&self.pulses).await - } -} - -fn encode(colour: RGB8, slot: &mut [PulseCode; 24]) { - // WS2812B latches GRB, most significant bit first - let word = u32::from(colour.g) << 16 | u32::from(colour.r) << 8 | u32::from(colour.b); - for (bit, code) in slot.iter_mut().enumerate() { - *code = if word & (1 << (23 - bit)) != 0 { - PulseCode::new(Level::High, T1H, Level::Low, T1L) - } else { - PulseCode::new(Level::High, T0H, Level::Low, T0L) - }; - } -} diff --git a/vendor/esp-hal-community b/vendor/esp-hal-community new file mode 160000 index 0000000..b6ecebf --- /dev/null +++ b/vendor/esp-hal-community @@ -0,0 +1 @@ +Subproject commit b6ecebf7e7937db52004fd9ad1930f3105fa6854