#morse #no-alloc #no-std

no-std megamorse

A simple morse code translator

3 releases (stable)

1.0.1 Apr 6, 2024
0.1.0 Apr 5, 2024

#454 in Embedded development

34 downloads per month

MPL-2.0 license

369 lines


A simple and flexible no_std-by-default morse code library for Rust.

crate documentation license


See the documentation for usage information.

An example for the SparkFun Pro Micro board:


use arduino_hal::port::{mode, Pin, PinOps};
use megamorse::{morse, MorseDecoder, MorsePlayer};
use panic_halt as _;

struct MorseLedDecoder<'a, P: PinOps> {
    timeunit: u16,
    led: &'a mut Pin<mode::Output, P>,

// The magic: We create a decoder
// that listens for commands from the megamorse library,
// and controls the LED on the SparkFun Pro Micro board.
impl<P: PinOps> MorseDecoder for MorseLedDecoder<'_, P> {
    type Error = ();

    fn on(&mut self, timeunits: usize) -> Result<(), Self::Error> {
        self.led.set_low(); // NOTE: This board uses low as on, and high as off
        arduino_hal::delay_ms(self.timeunit * (timeunits as u16));


    fn off(&mut self, timeunits: usize) -> Result<(), Self::Error>{
        arduino_hal::delay_ms(self.timeunit * (timeunits as u16));


fn main() -> ! {
    let dp = arduino_hal::Peripherals::take().unwrap();
    let pins = arduino_hal::pins!(dp);

    let mut led = pins.led_tx.into_output();

    // High is off, low is on

    let decoder = MorseLedDecoder {
        timeunit: 100, // A timeunit is "100". In the decoder, we will choose to interpret this as milliseconds.
        led: &mut led,

    let mut player = MorsePlayer::new(decoder);

    // Will blink "Hello world" in morse code
    player.play_str("Hello world!").unwrap();

    // Or you can construct a literal morse code
    let sos = morse!(... ___ ...);
    for word in sos.into_iter() {

    loop {}