diff options
Diffstat (limited to 'src/main.rs')
| -rw-r--r-- | src/main.rs | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..9bea689 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,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); + } +} |
