#morse #no-alloc #no-std

no-std megamorse

一个简单的摩尔斯电码翻译器

3个版本 (稳定)

1.0.1 2024年4月6日
0.1.0 2024年4月5日

#562 in 嵌入式开发

每月下载 22

MPL-2.0 许可证

27KB
369

megamorse

一个简单且灵活的默认无_std摩尔斯电码库,适用于Rust。

crate documentation license

使用方法

有关使用信息,请参阅文档

SparkFun Pro Micro板示例

#![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 {}
}

依赖关系