-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInPin.h
More file actions
143 lines (111 loc) · 3.87 KB
/
InPin.h
File metadata and controls
143 lines (111 loc) · 3.87 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#ifndef InPin_h
#define InPin_h
#include <Arduino.h>
#include <vector>
#include <Runnable.h>
//NB: pesa 1us per ogni input dell app
//TODO: usare lettura via interrupt
inline int digitalReadF( uint8_t ulPin ) {
/*
//Specifico per Nordic nrf5x:
ulPin = g_ADigitalPinMap[ulPin];
NRF_GPIO_Type * port = nrf_gpio_pin_port_decode(&ulPin);
uint32_t const bm = (1UL << ulPin);
//return (bm & ((port->DIR & bm) ? port->OUT : port->IN)) ? 1 : 0;
return (bm & (port->IN)) ? 1 : 0;
*/
return digitalRead( ulPin );
}
#define kDebounceTime 20
enum InPinType {
In_ActiveHigh = 0x00,
In_ActiveLow = 0x01,
};
class InPin : public Runnable {
private:
char simbolStr[2];
const char* name;
const byte pin;
const bool activeLow;
bool oldState;
bool actualState;
bool debouncedState;
bool changed;
ulong debounceTime;
ulong debounceStartTime;
ulong prevStateDuration;
ulong changedTime = 0;
public:
static std::vector<InPin*> InPins;
/*
InPin(const char* aName, byte aPin, boolean activeLow) :
simbol('i'),
name(aName),
pin(aPin),
activeLow(activeLow),
debounceTime(kDebounceTime)
{
InPins.push_back( this );
}
*/
InPin(const char aSimbol, const char* aName, byte aPin, boolean activeLow) :
simbolStr("i"),
name(aName),
pin(aPin),
activeLow(activeLow),
debounceTime(kDebounceTime)
{
simbolStr[0] = aSimbol;
InPins.push_back( this );
}
char* getSimbolStr() { return( simbolStr ); }
char getSimbol() { return( simbolStr[0] ); }
// bool getActualState() { return (actualState ^ activeLow); }
// bool getState() { return (state ^ activeLow); }
bool isActiveNow() { return( actualState ); }
bool isActive() { return( debouncedState ); }
bool isChanged() { return( changed ); } // 1CicleTimeMemory
bool isChToActive() { return( changed && isActive() ); }
bool isChToNotActive() { return( changed && (!isActive()) ); }
unsigned long getChangeTime() { return( changedTime ); }
void setDebounceTime( unsigned long t ) { debounceTime = t; }
unsigned long getPrevStateDuration() { return( prevStateDuration ); }
void setup() {
///Serial.println("InPin("+(String)pin+"): Setup");
if( activeLow ) {
pinMode(pin, INPUT_PULLUP);
// actualState = HIGH;
} else {
pinMode(pin, INPUT);
// actualState = LOW;
}
actualState = LOW; // vien corretto in loop
debouncedState = actualState;
}
void loop() { //TODO: sost millis() e leggere via interrupt per lasciar libero loop
changed = false;
oldState = actualState;
actualState = digitalReadF(pin) ^ activeLow; //Inverte la lettura se il pin è attivo Basso
if (actualState != oldState) {
debounceStartTime = millis(); //Reset the debouncing timer
} else { // if==
// if( (debouncedState != actualState) && (pin==2) && (actualState==true) ) { //pStart=2
// Serial.print("Input "); Serial.print(name);
// Serial.print(" (pin"); Serial.print(pin); Serial.print(")" );
// Serial.print(" ActDebTime "); Serial.println( ((millis() -debounceStartTime)) );
// }
if ( (debouncedState != actualState) && ((millis() - debounceStartTime) >= debounceTime) ) {
debouncedState = actualState;
changed = true;
prevStateDuration = millis() -changedTime; // debounceStartTime -changedTime;
changedTime = millis(); //debounceStartTime;
/*
Serial.print("Input "); Serial.print(name);
Serial.print(" (G"); Serial.print(pin); Serial.print(")" );
Serial.print(" changed to "); Serial.println(debouncedState?"1":"0");
*/
}
}
} //EndOf loop
};
#endif