megamorse

Crates.iomegamorse
lib.rsmegamorse
version1.0.1
sourcesrc
created_at2024-04-05 18:09:07.010789
updated_at2024-04-06 13:19:09.351325
descriptionA simple morse code translator
homepagehttps://github.com/cloone8/megamorse
repositoryhttps://github.com/cloone8/megamorse
max_upload_size
id1197598
size12,184
Wouter de Bruijn (cloone8)

documentation

README

megamorse

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

crate documentation license

Usage

See the documentation for usage information.

An example for the SparkFun Pro Micro board:

#![no_std]
#![no_main]

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));
        self.led.set_high();

        Ok(())
    }

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

        Ok(())
    }
}

#[arduino_hal::entry]
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
    led.set_high();

    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() {
       player.play_word(word).unwrap();
   }

    loop {}
}
Commit count: 22

cargo fmt