|
8 | 8 |
|
9 | 9 | #pragma once
|
10 | 10 |
|
11 |
| -#include "Arduino.h" |
12 |
| -#include <functional> |
| 11 | +#include "ANetworkConfigurator_Config.h" |
| 12 | +#include "ResetInputBase.h" |
| 13 | +#include "LEDFeedback.h" |
13 | 14 |
|
14 |
| -#define DISABLE_PIN -1 |
15 |
| - |
16 |
| -/** |
17 |
| - * @class ResetInput |
18 |
| - * @brief A singleton class to handle input of the reset functionality with interrupt-based monitoring. |
19 |
| - * |
20 |
| - * This class provides methods to configure and monitor a reset input pin. It allows |
21 |
| - * setting up a custom callback function to be executed when the pin status changes. |
22 |
| - */ |
23 |
| -class ResetInput{ |
| 15 | +class ResetInput : public ResetInputBase { |
24 | 16 | public:
|
25 |
| - /** |
26 |
| - * @typedef ResetInput::ResetInputCallback |
27 |
| - * @brief A type definition for the callback function to be executed on pin status change. |
28 |
| - */ |
29 |
| - typedef std::function<void()> ResetInputCallback; |
30 |
| - /** |
31 |
| - * @brief Get the singleton instance of the ResetInput class. |
32 |
| - * @return A reference to the ResetInput instance. |
33 |
| - */ |
34 |
| - static ResetInput& getInstance(); |
35 |
| - /** |
36 |
| - * @brief Initialize the reset input by setting up the interrupt pin. |
37 |
| - */ |
38 |
| - void begin(); |
39 |
| - /** |
40 |
| - * @brief Check if the reset event has been fired. |
41 |
| - * @return True if the event is fired, otherwise false. |
42 |
| - */ |
43 |
| - bool isEventFired(); |
44 |
| - /** |
45 |
| - * @brief Set a custom callback function to be called when the pin status changes. |
46 |
| - * This function must be called before invoking the `begin` method. |
47 |
| - * @param callback The custom callback function to be executed. |
48 |
| - */ |
49 |
| - void setPinChangedCallback(ResetInputCallback callback); |
50 |
| - /** |
51 |
| - * @brief Set the pin to be monitored for reset events. |
52 |
| - * By default, the pin is set as INPUT_PULLUP. |
53 |
| - * Use the value DISABLE_PIN to disable the pin and the reset procedure. |
54 |
| - * This function must be called before invoking the `begin` method. |
55 |
| - * @param pin The pin number to be monitored. The pin must |
56 |
| - * be in the list of digital pins usable for interrupts. |
57 |
| - * Please refer to the Arduino documentation for more details: |
58 |
| - * https://docs.arduino.cc/language-reference/en/functions/external-interrupts/attachInterrupt/ |
59 |
| - */ |
60 |
| - void setPin(int pin); |
| 17 | + static ResetInput &getInstance() { |
| 18 | + static ResetInput instance; |
| 19 | + return instance; |
| 20 | + } |
| 21 | + |
| 22 | + void begin() override { |
| 23 | + if(_pin == DISABLE_PIN){ |
| 24 | + return; |
| 25 | + } |
| 26 | + #ifdef ARDUINO_OPTA |
| 27 | + pinMode(_pin, INPUT); |
| 28 | + #else |
| 29 | + pinMode(_pin, INPUT_PULLUP); |
| 30 | + #endif |
| 31 | + pinMode(LED_RECONFIGURE, OUTPUT); |
| 32 | + digitalWrite(LED_RECONFIGURE, LED_OFF); |
| 33 | + attachInterrupt(digitalPinToInterrupt(_pin),_pressedCallback, CHANGE); |
| 34 | + } |
| 35 | + |
| 36 | + bool isEventFired() override { |
| 37 | + if(_startPressed != 0){ |
| 38 | + #if defined(ARDUINO_NANO_RP2040_CONNECT) || defined(ARDUINO_SAMD_MKRWIFI1010) |
| 39 | + LEDFeedbackClass::getInstance().stop(); |
| 40 | + #endif |
| 41 | + if(micros() - _startPressed > RESET_HOLD_TIME){ |
| 42 | + digitalWrite(LED_RECONFIGURE, LED_OFF); |
| 43 | + _expired = true; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + return _fireEvent; |
| 48 | + } |
| 49 | + |
61 | 50 | private:
|
62 |
| - /** |
63 |
| - * @brief Private constructor to enforce the singleton pattern. |
64 |
| - */ |
65 |
| - ResetInput(); |
66 |
| - static inline ResetInputCallback _pressedCustomCallback; |
67 |
| - static inline int _pin; |
| 51 | + ResetInput() { |
| 52 | + _pin = PIN_RECONFIGURE; |
| 53 | + _expired = false; |
| 54 | + _startPressed = 0; |
| 55 | + _fireEvent = false; |
| 56 | + _pressedCustomCallback = nullptr; |
| 57 | + } |
68 | 58 | static inline volatile bool _expired;
|
69 | 59 | static inline volatile bool _fireEvent;
|
70 | 60 | static inline volatile uint32_t _startPressed;
|
71 | 61 | /**
|
72 | 62 | * @brief Internal callback function to handle pin press events.
|
73 | 63 | */
|
74 |
| - static void _pressedCallback(); |
| 64 | + static void _pressedCallback() { |
| 65 | + #if defined(ARDUINO_NANO_RP2040_CONNECT) |
| 66 | + if(digitalRead(_pin) == HIGH){ |
| 67 | + #else |
| 68 | + if(digitalRead(_pin) == LOW){ |
| 69 | + #endif |
| 70 | + #if !defined(ARDUINO_NANO_RP2040_CONNECT) && !defined(ARDUINO_SAMD_MKRWIFI1010) |
| 71 | + LEDFeedbackClass::getInstance().stop(); |
| 72 | + #endif |
| 73 | + _startPressed = micros(); |
| 74 | + digitalWrite(LED_RECONFIGURE, LED_ON); |
| 75 | + } else { |
| 76 | + digitalWrite(LED_RECONFIGURE, LED_OFF); |
| 77 | + if(_startPressed != 0 && _expired){ |
| 78 | + _fireEvent = true; |
| 79 | + }else{ |
| 80 | + LEDFeedbackClass::getInstance().restart(); |
| 81 | + } |
| 82 | + _startPressed = 0; |
| 83 | + } |
| 84 | + |
| 85 | + if (_pressedCustomCallback) { |
| 86 | + _pressedCustomCallback(); |
| 87 | + } |
| 88 | + } |
75 | 89 | };
|
| 90 | + |
0 commit comments