From cb20e5902b7786d2ab84179bc680f16afc9e4341 Mon Sep 17 00:00:00 2001 From: MightyPork Date: Fri, 1 May 2015 00:32:23 +0200 Subject: [PATCH] added keyhandler example --- examples/Makefile | 2 +- examples/uart_keyhandler.c | 51 ++++++++++++++++++++++++++++++++++++++ lib/stream.h | 1 + 3 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 examples/uart_keyhandler.c diff --git a/examples/Makefile b/examples/Makefile index c36a52e..cdca97f 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -11,7 +11,7 @@ EFUSE = 0x05 ## === Source files === # Main C file -MAIN = uart_isr.c +MAIN = uart_keyhandler.c # Extra C files in this folder LOCAL_SOURCE = diff --git a/examples/uart_keyhandler.c b/examples/uart_keyhandler.c new file mode 100644 index 0000000..5c12346 --- /dev/null +++ b/examples/uart_keyhandler.c @@ -0,0 +1,51 @@ +#include +#include +#include +#include + +#include "lib/uart.h" +#include "lib/uart_ansi.h" +#include "lib/stream.h" + +// +// Example of asynchronous UART key handling +// +// It recognizes special keys like arrows and some F keys, +// check the header file for full list. +// +// You need uart_ansi for this. +// + +ISR(USART_RX_vect) +{ + char c = uart_rx(); + vt_handle_key(c); + // we can do anything further with the received byte +} + + +// Our custom key handler function +void key_handler(uint8_t code, bool special) +{ + put_str_P(uart, special ? PSTR("Special: ") : PSTR("Char: ")); + put_char(uart, code); // the actual character + put_char(uart, ' '); // space + put_u8(uart, code); // as number + put_nl(uart); // crlf +} + + +void main() +{ + uart_init(9600); // set BAUD-rate + uart_isr_rx(1); // enable the ISR + vt_init(); // initialize uart_ansi library + + vt_set_key_handler(&key_handler); // assign our custom handler + + sei(); + + put_str_P(uart, PSTR("UART key handler test!\r\n")); + + while(1); +} diff --git a/lib/stream.h b/lib/stream.h index 5c7ca32..8ed7979 100644 --- a/lib/stream.h +++ b/lib/stream.h @@ -38,6 +38,7 @@ void put_str_P(const STREAM *p, const char* str); /** Send signed int8 */ +#define put_char(p, c) (p)->tx((c)) void put_u8(const STREAM *p, const uint8_t num);