summaryrefslogtreecommitdiffstats
path: root/src/main.rs
blob: 9bea6893a941573a72641f65cb690deb1f21aca5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#![no_std]
#![no_main]

use core::char;

use arduino_uno::{delay_ms, hal::port::{mode::Output, portb::PB5}, prelude::*};
use panic_halt as _;

const SHORT: u16 = 75;
const LONG:  u16 = SHORT * 3;
const SPACE: u16 = SHORT * 7;

macro_rules! morse {
    ( $led:expr, $( $delay:expr ),* ) => {
        {
            $( 
                $led.set_high().void_unwrap();
                delay_ms($delay);
                $led.set_low().void_unwrap();
                delay_ms(SHORT);
            )*
            delay_ms(LONG - SHORT);
        }
    };
}

fn blink_morse(led: &mut PB5<Output>, c: u8) {
    let c = char::from_u32(c as u32);
    if let Some(c) = c {
        match c {
            'a' => morse!(led, SHORT, LONG),
            'b' => morse!(led, LONG, SHORT, SHORT, SHORT),
            'c' => morse!(led, LONG, SHORT, LONG, SHORT),
            'd' => morse!(led, LONG, SHORT, SHORT),
            'e' => morse!(led, SHORT),
            'f' => morse!(led, SHORT, SHORT, LONG, SHORT),
            'g' => morse!(led, LONG, LONG, SHORT),
            'h' => morse!(led, SHORT, SHORT, SHORT, SHORT),
            'i' => morse!(led, SHORT, SHORT),
            'j' => morse!(led, SHORT, LONG, LONG, LONG),
            'k' => morse!(led, LONG, SHORT, LONG),
            'l' => morse!(led, SHORT, LONG, SHORT, SHORT),
            'm' => morse!(led, LONG, LONG),
            'n' => morse!(led, LONG, SHORT),
            'o' => morse!(led, LONG, LONG, LONG),
            'p' => morse!(led, SHORT, LONG, LONG, SHORT),
            'q' => morse!(led, LONG, LONG, SHORT, LONG),
            'r' => morse!(led, SHORT, LONG, SHORT),
            's' => morse!(led, SHORT, SHORT, SHORT),
            't' => morse!(led, LONG),
            'u' => morse!(led, SHORT, SHORT, LONG),
            'v' => morse!(led, SHORT, SHORT, SHORT, LONG),
            'w' => morse!(led, SHORT, LONG, LONG),
            'x' => morse!(led, LONG, SHORT, SHORT, LONG),
            'y' => morse!(led, LONG, SHORT, LONG, LONG),
            'z' => morse!(led, LONG, LONG, SHORT, SHORT),
            '1' => morse!(led, SHORT, LONG, LONG, LONG, LONG),
            '2' => morse!(led, SHORT, SHORT, LONG, LONG, LONG),
            '3' => morse!(led, SHORT, SHORT, SHORT, LONG, LONG),
            '4' => morse!(led, SHORT, SHORT, SHORT, SHORT, LONG),
            '5' => morse!(led, SHORT, SHORT, SHORT, SHORT, SHORT),
            '6' => morse!(led, LONG, SHORT, SHORT, SHORT, SHORT),
            '7' => morse!(led, LONG, LONG, SHORT, SHORT, SHORT),
            '8' => morse!(led, LONG, LONG, LONG, SHORT, SHORT),
            '9' => morse!(led, LONG, LONG, LONG, LONG, SHORT),
            '0' => morse!(led, LONG, LONG, LONG, LONG, LONG),
            ' ' => delay_ms(SPACE - LONG),
            _ => {},
        }
    }
}

#[arduino_uno::entry]
fn main() -> ! {
    let dp = arduino_uno::Peripherals::take().unwrap();

    let mut pins = arduino_uno::Pins::new(dp.PORTB, dp.PORTC, dp.PORTD);

    let mut led = pins.d13.into_output(&mut pins.ddr);
    led.set_low().void_unwrap();

    let mut serial = arduino_uno::Serial::new(
        dp.USART0,
        pins.d0,
        pins.d1.into_output(&mut pins.ddr),
        57600.into_baudrate(),
    );

    loop {
        let b = nb::block!(serial.read()).void_unwrap();

        blink_morse(&mut led, b);
    }
}