use esp_idf_sys as _; // If using the `binstart` feature of `esp-idf-sys`, always keep this module imported
use esp_idf_sys::EspError;
use esp_idf_sys::esp;
use esp_idf_hal::rmt;
use esp_idf_hal::peripherals::Peripherals;
use std::thread::sleep;
use std::time::Duration;
fn led_tx(tx: &mut rmt::TxRmtDriver, data: &Vec<u8>) -> Result<(), EspError> {
use esp_idf_hal::rmt::Signal;
use esp_idf_sys::rmt_item32_t;
let bit1: [rmt::Pulse; 2] = [
rmt::Pulse::new(rmt::PinState::High, rmt::PulseTicks::new(34).unwrap()),
rmt::Pulse::new(rmt::PinState::Low, rmt::PulseTicks::new(16).unwrap()),
];
let bit0: [rmt::Pulse; 2] = [
rmt::Pulse::new(rmt::PinState::High, rmt::PulseTicks::new(16).unwrap()),
rmt::Pulse::new(rmt::PinState::Low, rmt::PulseTicks::new(34).unwrap()),
];
let mut signal = rmt::VariableLengthSignal::with_capacity(data.len() * 16);
for v in data {
for i in 0..u8::BITS {
if v & (0b1000_0000 >> i) != 0 {
signal.push(&bit1).unwrap();
} else {
signal.push(&bit0).unwrap();
}
}
}
tx.start_blocking(&signal)?;
Ok(())
}
fn make_color(idx: u32) -> [u8; 3] {
let clr = (idx % 255) as u8;
let chn = idx / 255 % 3 as u32;
match chn % 3{
0 => [255 - clr, clr, 0],
1 => [0, 255 - clr, clr],
2 => [clr, 0, 255 - clr],
_ => todo!(),
}
}
fn main() {
esp_idf_sys::link_patches();
println!("Hello, world!");
let config = rmt::config::TransmitConfig::new().mem_block_num(3).clock_divider(2);
let peripherals = Peripherals::take().unwrap();
let channel = peripherals.rmt.channel0;
let pin = peripherals.pins.gpio21;
// Create an RMT transmitter.
let mut tx = rmt::TxRmtDriver::new(channel, pin, &config).unwrap();
loop {
for i in 0..9945 {
let i = i * 13;
let led = (i..(i+16)).flat_map(|v| make_color(v + (v - i) * 48)).collect();
led_tx(&mut tx, &led).unwrap();
sleep(Duration::from_micros(100));
}
}
loop {}
}