-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStateChangeInt.h
More file actions
85 lines (66 loc) · 2.6 KB
/
StateChangeInt.h
File metadata and controls
85 lines (66 loc) · 2.6 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
#ifndef StateChange_h
#define StateChange_h
#include <Arduino.h>
#include <functional> //typedef std::function<void(int)> callback_t;
#include <Utyx.h>
#include <Runnable.h>
//StateControll
class StateChange : public Runnable {
private:
int changed = 0;
int state = 0;
int stateOld = 0;
ulong tChanged = 0;
ulong tChangedOld = 0;
// void setChanged() { }
typedef std::function<void(int)> callback_t;
callback_t callback;
const char* const * stateNames;
//char* [] stateNames;
public:
StateChange() {} // Constructor
void setup() {}
void loop() {
// var cambiate a metà loop devon restare 1.5 loop
if( changed ) changed--; else changed = false;
} //EndOf loop
/*
void changeTest() {
changed = false;
} //EndOf loop
*/
int get() { return( state ); }
int getOld() { return( stateOld ); }
bool isChanged() { return( changed ); } // 1CicleTimeMemory
ulong getChangeT() { return( tChanged ); } //Quando
ulong getElapsedT() { return( millis() -tChanged ); } //DaQuanto
ulong getPrevStateDT() { return( tChanged -tChangedOld ); } //DurataPrec
void set( int stateNew ) {
if( state != stateNew ) {
changed = 2;
stateOld = state;
state = stateNew;
tChangedOld = tChanged;
tChanged = millis();
//printChange();
callback( state );
}
}
void print() {
Serial.print( "St:" ); Serial.print( state );
Serial.print( "=" ); Serial.print( stateNames[ state ] );
}
void printChange() {
Serial.print( " StCh:" );
Serial.print( stateOld ); Serial.print( "->" ); Serial.print( state );
Serial.print( "=" ); Serial.print( stateNames[ state ] );
Serial.print( "\n" );
}
void setCallBack( callback_t callback_ ) {
callback = callback_;
}
void setStateNames( const char* const stateNames_[] ) {
stateNames = stateNames_;
}
};
#endif