From 8cdcdcf7572d08d5def7fddc7a9438826eeed22b Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Fri, 12 Jan 2018 15:07:44 +0100 Subject: add arduino nano stdin driver --- include/arduino-nano/driver/stdin.h | 24 ++++++++++++++++++++++++ src/arch/arduino-nano/Makefile.inc | 4 ++++ src/arch/arduino-nano/arch.cc | 9 ++++++++- src/arch/arduino-nano/driver/stdin.cc | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 include/arduino-nano/driver/stdin.h create mode 100644 src/arch/arduino-nano/driver/stdin.cc diff --git a/include/arduino-nano/driver/stdin.h b/include/arduino-nano/driver/stdin.h new file mode 100644 index 0000000..6462e0c --- /dev/null +++ b/include/arduino-nano/driver/stdin.h @@ -0,0 +1,24 @@ +#ifndef STANDARDINPUT_H +#define STANDARDINPUT_H + +class StandardInput { + private: + StandardInput(const StandardInput ©); + char buffer[8]; + unsigned char write_pos, read_pos; + + public: + StandardInput() : write_pos(0), read_pos(0) {} + void setup(); + bool hasKey(); + char getKey(); + + inline void addKey(char key) { + buffer[write_pos++] = key; + write_pos %= 8; + } +}; + +extern StandardInput kin; + +#endif diff --git a/src/arch/arduino-nano/Makefile.inc b/src/arch/arduino-nano/Makefile.inc index f4dd78e..958eda2 100644 --- a/src/arch/arduino-nano/Makefile.inc +++ b/src/arch/arduino-nano/Makefile.inc @@ -23,6 +23,10 @@ TARGETS += src/arch/arduino-nano/driver/gpio.cc TARGETS += src/arch/arduino-nano/driver/stdout.cc TARGETS += src/arch/arduino-nano/driver/uptime.cc +ifneq ($(findstring stdin,${arch_drivers}), ) + TARGETS += src/arch/arduino-nano/driver/stdin.cc +endif + OBJECTS = ${TARGETS:.cc=.o} .cc.o: diff --git a/src/arch/arduino-nano/arch.cc b/src/arch/arduino-nano/arch.cc index 28fe34b..f35cb1d 100644 --- a/src/arch/arduino-nano/arch.cc +++ b/src/arch/arduino-nano/arch.cc @@ -24,10 +24,14 @@ void Arch::setup(void) sei(); } +#ifdef WITH_WAKEUP +void wakeup(); +#endif + #if defined(WITH_LOOP) || defined(TIMER_S) #include "driver/uptime.h" -extern void loop(); +void loop(); #endif @@ -41,6 +45,9 @@ void Arch::idle_loop(void) #ifdef WITH_LOOP loop(); #endif +#ifdef WITH_WAKEUP + wakeup(); +#endif #ifdef TIMER_S uptime.tick_s(); #endif diff --git a/src/arch/arduino-nano/driver/stdin.cc b/src/arch/arduino-nano/driver/stdin.cc new file mode 100644 index 0000000..ac58a61 --- /dev/null +++ b/src/arch/arduino-nano/driver/stdin.cc @@ -0,0 +1,32 @@ +#include "driver/stdin.h" +#include +#include + +void StandardInput::setup() +{ + UCSR0B |= _BV(RXCIE0); +} + +bool StandardInput::hasKey() +{ + if (write_pos != read_pos) { + return true; + } + return false; +} + +char StandardInput::getKey() +{ + char ret = buffer[read_pos++]; + read_pos %= 8; + return ret; +} + +StandardInput kin; + +ISR(USART_RX_vect) +{ + while (UCSR0A & _BV(RXC0)) { + kin.addKey(UDR0); + } +} -- cgit v1.2.3