//! RMT Transmit Example -- Morse Code //! //! To try this out, connect a piezo buzzer via a resistor into pin 17. It should repeat "HELLO" //! in morse code. //! //! Set pin 16 to low to stop the signal and make the buzzer sound "GOODBYE" in morse code. //! //! Example loosely based off which has a circuit you can follow. //! https://github.com/espressif/esp-idf/tree/master/examples/peripherals/rmt/morse_code //! //! This example demonstrates: //! * A carrier signal. //! * Looping. //! * Background sending. //! * Taking a [`Pin`] and [`Channel`] by ref mut, so that they can be used again later. //! #[cfg(any(feature = "rmt-legacy", esp_idf_version_major = "4"))] fn main() -> anyhow::Result<()> { example::main() } #[cfg(not(any(feature = "rmt-legacy", esp_idf_version_major = "4")))] fn main() -> anyhow::Result<()> { println!("This example requires feature `rmt-legacy` enabled or using ESP-IDF v4.4.X"); loop { std::thread::sleep(std::time::Duration::from_millis(1000)); } } #[cfg(any(feature = "rmt-legacy", esp_idf_version_major = "4"))] mod example { use esp_idf_hal::units::FromValueType; use esp_idf_hal::{ delay::Ets, gpio::{OutputPin, PinDriver}, peripheral::Peripheral, prelude::Peripherals, rmt::{ config::{CarrierConfig, DutyPercent, Loop, TransmitConfig}, PinState, Pulse, PulseTicks, RmtChannel, TxRmtDriver, VariableLengthSignal, }, }; pub fn main() -> anyhow::Result<()> { esp_idf_hal::sys::link_patches(); let peripherals = Peripherals::take()?; let mut channel = peripherals.rmt.channel0; let mut led = peripherals.pins.gpio17; let stop = peripherals.pins.gpio16; let carrier = CarrierConfig::new() .duty_percent(DutyPercent::new(50)?) .frequency(611.Hz()); let mut config = TransmitConfig::new() .carrier(Some(carrier)) .looping(Loop::Endless) .clock_divider(255); let tx = send_morse_code(&mut channel, &mut led, &config, "HELLO ")?; let stop = PinDriver::input(stop)?; println!("Keep sending until pin {} is set low.", stop.pin()); while stop.is_high() { Ets::delay_ms(100); } println!("Pin {} set to low. Stopped.", stop.pin()); // Release pin and channel so we can use them again. drop(tx); // Wait so the messages don't get garbled. Ets::delay_ms(3000); // Now send a single message and stop. println!("Saying GOODBYE!"); config.looping = Loop::None; send_morse_code(channel, led, &config, "GOODBYE")?; Ok(()) } fn send_morse_code<'d>( channel: impl Peripheral
+ 'd, led: impl Peripheral
+ 'd,
config: &TransmitConfig,
message: &str,
) -> anyhow::Result