-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSwitch.cpp
executable file
·151 lines (123 loc) · 2.38 KB
/
Switch.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
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
144
145
146
147
148
149
150
/*
* Home I/O Controller Node
*
* <odule: Switch
* Provide I/O (Input) services
*
* 2014-12-01 Sid Young
*
*/
#include "Switch.h"
#include "Config.h"
#include "Arduino.h"
/* ==================== Constructor/Destructors ====================*/
Switch::Switch(char *name, int pin)
{
this->setName(name);
this->setPin(pin);
init();
}
Switch::~Switch() {}
/* ==================== Methods ====================*/
/*----------------------------------------
* init()
*
* Clear counters and set defaults
*/
void Switch::init()
{
pinMode(this->_pin, INPUT);
this->_raw_state = digitalRead(this->_pin);
this->_pin_state = digitalRead(this->_pin);
this->_state_counter = 0;
this->_debounce_counter=0;
}
/*----------------------------------------
*
* Timer1sec()
*
*/
void Switch::Timer1sec()
{
}
/*----------------------------------------
*
* Timer1ms()
*
*/
void Switch::Timer1ms()
{
Debounce();
}
/*----------------------------------------
*
* Debounce()
*
* Called via 1ms timer callback method in main loop
*
* Debouncing:
* - dec debounce counter.
* - zero up time.
* If input still bouncing, reset debounce count to max (start again).
*
* Stable:
* - Inc up time.
* - debounce counter is 0.
* - set pin state to raw state.
*
*/
void Switch::Debounce()
{
if( this->_raw_state==digitalRead(this->_pin) )
{
if(this->_debounce_counter)
this->_debounce_counter--;
else
this->_pin_state = this->_raw_state; // time up - we are stable!
this->_state_counter++; // ms time we are at this state
}
else
{
this->_debounce_counter=DEBOUNCE_DELAY; // reset delay time
this->_raw_state=digitalRead(this->_pin); // set pin as changed
this->_state_counter = 0;
}
}
int Switch::getState() { return this->_pin_state; }
char * Switch::getName() { return this->_name; }
int Switch::getPin() { return this->_pin; }
/*----------------------------------------
*
* setName()
*
* Save the identifier for this IO device
*/
void Switch::setName(char *name)
{
memset(this->_name,0,MAX_NAME_SIZE);
if(strlen(name)>MAX_NAME_SIZE)
{
strncpy(this->_name,name,MAX_NAME_SIZE);
}
else
{
strcpy(this->_name, name);
}
}
/*----------------------------------------
*
* setPin()
*
* set the pin number if its in range
*/
void Switch::setPin(int pin)
{
if(pin<(MAX_PIN_COUNT+1))
{
if((pin >0)||(pin==0))
this->_pin = pin;
}
}
/*
* End of file
*/