/ firmware / examples / esp32s3 / neopixel.rs
neopixel.rs
 1  // Thanks to: https://github.com/esp-rs/esp-hal-community/blob/main/esp-hal-smartled/examples/hello_rgb_async.rs
 2  #![no_std]
 3  #![no_main]
 4  #![deny(
 5      clippy::mem_forget,
 6      reason = "mem::forget is generally not safe to do with esp_hal types, especially those \
 7      holding buffers for the duration of a data transfer."
 8  )]
 9  #![deny(clippy::large_stack_frames)]
10  
11  use defmt::info;
12  use embassy_executor::Spawner;
13  use embassy_time::{Duration, Timer};
14  use esp_hal::{clock::CpuClock, interrupt::software::SoftwareInterruptControl, rmt::Rmt, time::Rate, timer::timg::TimerGroup};
15  use esp_hal_smartled::{SmartLedsAdapterAsync, buffer_size_async};
16  use panic_rtt_target as _;
17  use smart_leds::{
18      RGB8, SmartLedsWriteAsync, brightness, gamma,
19      hsv::{Hsv, hsv2rgb},
20  };
21  
22  extern crate alloc;
23  
24  esp_bootloader_esp_idf::esp_app_desc!();
25  
26  #[allow(
27      clippy::large_stack_frames,
28      reason = "it's not unusual to allocate larger buffers etc. in main"
29  )]
30  #[esp_rtos::main]
31  async fn main(_spawner: Spawner) -> ! {
32      rtt_target::rtt_init_defmt!();
33  
34      let config = esp_hal::Config::default().with_cpu_clock(CpuClock::max());
35      let peripherals = esp_hal::init(config);
36  
37      esp_alloc::heap_allocator!(#[esp_hal::ram(reclaimed)] size: 73744);
38      esp_alloc::heap_allocator!(size: 64 * 1024);
39  
40      let timg0 = TimerGroup::new(peripherals.TIMG0);
41      let sw_ints = SoftwareInterruptControl::new(peripherals.SW_INTERRUPT);
42      esp_rtos::start(timg0.timer0, sw_ints.software_interrupt0);
43  
44      let rmt: Rmt<'_, esp_hal::Async> = Rmt::new(peripherals.RMT, Rate::from_mhz(80))
45          .unwrap()
46          .into_async();
47      let mut rmt_buffer = [esp_hal::rmt::PulseCode::default(); buffer_size_async(1)];
48      let mut led = SmartLedsAdapterAsync::new(rmt.channel0, peripherals.GPIO38, &mut rmt_buffer);
49  
50      let mut color = Hsv {
51          hue: 0,
52          sat: 255,
53          val: 255,
54      };
55      let mut data: RGB8;
56      let level = 150;
57  
58      loop {
59          info!("neopixel rainbow");
60          for hue in 0..=255 {
61              color.hue = hue;
62              data = hsv2rgb(color);
63              led.write(brightness(gamma([data].into_iter()), level))
64                  .await
65                  .unwrap();
66              Timer::after(Duration::from_millis(10)).await;
67          }
68      }
69  }