From a40ea0981fb6818641f7212ace9473b8600c6c29 Mon Sep 17 00:00:00 2001 From: Xoaquin Castrelo <xoaquin.cb@gmail.com> Date: Sun, 14 Nov 2021 04:39:25 +0000 Subject: [PATCH] Added support for buttons. --- emb/buttons.cpp | 43 +++++++++++++++++++++++++++++++++++++++++++ emb/buttons.hpp | 15 +++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 emb/buttons.cpp create mode 100644 emb/buttons.hpp diff --git a/emb/buttons.cpp b/emb/buttons.cpp new file mode 100644 index 0000000..7fc1fbd --- /dev/null +++ b/emb/buttons.cpp @@ -0,0 +1,43 @@ +#include "buttons.hpp" +#include "uart.hpp" +#include "time.hpp" +#include <avr/io.h> + +static bool lastStateA = false; +static bool lastStateB = false; +static uint32_t lastMillisA = 0; +static uint32_t lastMillisB = 0; + +void initButtons() +{ + DDRC &= ~(1 << DDC5) & ~(1 << DDC4); // set PC5 and PC4 as inputs + PORTC |= (1 << PORTC5) | (1 << PORTC4); // enable pullups +} + +void updateButtons() +{ + uint32_t currMillis = getCurrentMillis(); + + bool currStateA = (PINC & (1 << PINC5)); + bool currStateB = (PINC & (1 << PINC4)); + + if (currStateA != lastStateA) + { + lastStateA = currStateA; + + if (!currStateA && (currMillis - lastMillisA) > 50) + writeUart('a'); + + lastMillisA = currMillis; + } + + if (currStateB != lastStateB) + { + lastStateB = currStateB; + + if (!currStateB && (currMillis - lastMillisB) > 50) + writeUart('b'); + + lastMillisB = currMillis; + } +} diff --git a/emb/buttons.hpp b/emb/buttons.hpp new file mode 100644 index 0000000..e4f6fcb --- /dev/null +++ b/emb/buttons.hpp @@ -0,0 +1,15 @@ +#ifndef BUTTONS_H +#define BUTTONS_H + +/** + * Initialises the button logic. + */ +void initButtons(); + +/** + * Updates the button logic. + * Should be called periodically. + */ +void updateButtons(); + +#endif /* BUTTONS_H */ -- GitLab