-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathResetInput.cpp
94 lines (81 loc) · 2.14 KB
/
ResetInput.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
Copyright (c) 2024 Arduino SA
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include "ANetworkConfigurator_Config.h"
#if NETWORK_CONFIGURATOR_COMPATIBLE
#include "ResetInput.h"
#include "Utility/LEDFeedback/LEDFeedback.h"
ResetInput &ResetInput::getInstance() {
static ResetInput instance;
return instance;
}
ResetInput::ResetInput() {
_pin = PIN_RECONFIGURE;
_expired = false;
_startPressed = 0;
_fireEvent = false;
_pressedCustomCallback = nullptr;
}
void ResetInput::begin() {
if(_pin == DISABLE_PIN){
return;
}
#ifdef ARDUINO_OPTA
pinMode(_pin, INPUT);
#else
pinMode(_pin, INPUT_PULLUP);
#endif
pinMode(LED_RECONFIGURE, OUTPUT);
digitalWrite(LED_RECONFIGURE, LED_OFF);
attachInterrupt(digitalPinToInterrupt(_pin),_pressedCallback, CHANGE);
}
bool ResetInput::isEventFired() {
if(_startPressed != 0){
#if defined(ARDUINO_NANO_RP2040_CONNECT) || defined(ARDUINO_SAMD_MKRWIFI1010)
LEDFeedbackClass::getInstance().stop();
#endif
if(micros() - _startPressed > RESET_HOLD_TIME){
digitalWrite(LED_RECONFIGURE, LED_OFF);
_expired = true;
}
}
return _fireEvent;
}
void ResetInput::setPinChangedCallback(ResetInputCallback callback) {
_pressedCustomCallback = callback;
}
void ResetInput::setPin(int pin) {
if(pin < 0){
_pin = DISABLE_PIN;
}else {
_pin = pin;
}
}
void ResetInput::_pressedCallback() {
#if defined(ARDUINO_NANO_RP2040_CONNECT)
if(digitalRead(_pin) == HIGH){
#else
if(digitalRead(_pin) == LOW){
#endif
#if !defined(ARDUINO_NANO_RP2040_CONNECT) && !defined(ARDUINO_SAMD_MKRWIFI1010)
LEDFeedbackClass::getInstance().stop();
#endif
_startPressed = micros();
digitalWrite(LED_RECONFIGURE, LED_ON);
} else {
digitalWrite(LED_RECONFIGURE, LED_OFF);
if(_startPressed != 0 && _expired){
_fireEvent = true;
}else{
LEDFeedbackClass::getInstance().restart();
}
_startPressed = 0;
}
if (_pressedCustomCallback) {
_pressedCustomCallback();
}
}
#endif // NETWORK_CONFIGURATOR_COMPATIBLE