From dbf0137e36010e66d793be5a90fbc902cd1925ef Mon Sep 17 00:00:00 2001 From: MightyPork Date: Fri, 1 May 2015 00:38:26 +0200 Subject: [PATCH] added rot13 uart example --- examples/Makefile | 2 +- examples/uart_echo_rot13.c | 49 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 examples/uart_echo_rot13.c diff --git a/examples/Makefile b/examples/Makefile index cdca97f..f4a8c24 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -11,7 +11,7 @@ EFUSE = 0x05 ## === Source files === # Main C file -MAIN = uart_keyhandler.c +MAIN = uart_echo_rot13.c # Extra C files in this folder LOCAL_SOURCE = diff --git a/examples/uart_echo_rot13.c b/examples/uart_echo_rot13.c new file mode 100644 index 0000000..f86f46c --- /dev/null +++ b/examples/uart_echo_rot13.c @@ -0,0 +1,49 @@ +#include +#include +#include + +#include "lib/meta.h" +#include "lib/uart.h" + +// +// This example receives asynchronously characters over UART +// and prints them back encrypted with ROT13 cipher (the one used for +// hints in GeoCaching) +// + + +// Echo, but with ROT13. +ISR(USART_RX_vect) +{ + char c = uart_rx(); + + if (c == 13) { + // print CRLF on enter + uart_tx(10); + uart_tx(13); + return; + } + + // do the ROT13 + if ((c >= 'a' && c <= 'm') || (c >= 'A' && c <= 'M')) { + c += 13; + } else + if ((c >= 'n' && c <= 'z') || (c >= 'N' && c <= 'Z')) { + c -= 13; + } + + // Print the result + uart_tx(c); +} + + +void main() +{ + uart_init(9600); + uart_isr_rx(1); + sei(); + + uart_puts_P(PSTR("ROT13 UART echo example\r\n")); + + while(1); +}