11#include " Keypad.hpp"
22#include " debouncedIsr.hpp"
33#include < Arduino-wrapper.h>
4+ #include < algorithm>
5+ #include < array>
46#include < board_pins.hpp>
7+ #include < cassert>
58#include < chrono>
69#include < cstddef>
710#include < functional>
11+ #include < iterator>
12+ #include < optional>
813#include < utility>
914
1015#if __has_include(<FunctionalInterrupt.h>) // specific to Arduino-ESP32
@@ -24,6 +29,34 @@ static HmiHandler callBack;
2429 */
2530static constexpr auto debouncePeriod = 20ms;
2631
32+ /* *
33+ * Maps HMI buttons to events.
34+ */
35+ static constexpr std::pair<board::PinType, KeyId> selectionForPins[] = {
36+ {board::button::pin::task1, KeyId::TASK1},
37+ {board::button::pin::task2, KeyId::TASK2},
38+ {board::button::pin::task3, KeyId::TASK3},
39+ {board::button::pin::task4, KeyId::TASK4},
40+ {board::button::pin::up, KeyId::LEFT},
41+ {board::button::pin::down, KeyId::RIGHT},
42+ {board::button::pin::enter, KeyId::ENTER},
43+ {board::button::pin::back, KeyId::BACK},
44+ };
45+
46+ static std::array<bool , std::size(selectionForPins)> keyPressedState;
47+
48+ static std::optional<std::size_t > getStateIndex (const KeyId keyId)
49+ {
50+ for (std::size_t index = 0 ; index < std::size (selectionForPins); ++index)
51+ {
52+ if (selectionForPins[index].second == keyId)
53+ {
54+ return index;
55+ }
56+ }
57+ return std::nullopt ;
58+ }
59+
2760/* *
2861 * Reacts on a debounced (stabilized) pin change.
2962 *
@@ -40,22 +73,9 @@ static void reactOnPinChange(const board::PinType pin, KeyId keyId)
4073 {
4174 callBack (keyId);
4275 }
76+ keyPressedState.at (getStateIndex (keyId).value ()) = isPressed;
4377}
4478
45- /* *
46- * Maps HMI buttons to events.
47- */
48- static constexpr std::pair<board::PinType, KeyId> selectionForPins[] = {
49- {board::button::pin::task1, KeyId::TASK1},
50- {board::button::pin::task2, KeyId::TASK2},
51- {board::button::pin::task3, KeyId::TASK3},
52- {board::button::pin::task4, KeyId::TASK4},
53- {board::button::pin::up, KeyId::LEFT},
54- {board::button::pin::down, KeyId::RIGHT},
55- {board::button::pin::enter, KeyId::ENTER},
56- {board::button::pin::back, KeyId::BACK},
57- };
58-
5979Keypad::Keypad ()
6080{
6181 // input pins
@@ -73,6 +93,7 @@ Keypad::Keypad()
7393 CHANGE);
7494 index++;
7595 }
96+ std::fill (std::begin (keyPressedState), std::end (keyPressedState), false );
7697}
7798
7899void Keypad::setCallback (std::function<void (KeyId)> callbackFunction)
@@ -82,5 +103,5 @@ void Keypad::setCallback(std::function<void(KeyId)> callbackFunction)
82103
83104bool Keypad::isKeyPressed (const KeyId keyInquiry)
84105{
85- return false ; // TODO
106+ return keyPressedState. at ( getStateIndex (keyInquiry). value ());
86107}
0 commit comments