-
Notifications
You must be signed in to change notification settings - Fork 0
/
RetailAlert.cpp
230 lines (200 loc) · 4.3 KB
/
RetailAlert.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#include "Particle.h"
#include <vector>
#include "VineAlert.h"
// ButtonPressAction
using namespace RetailAlert;
std::vector<ButtonPressAction*> ButtonPressAction::instances;
//std::vector<String> Buzzer::devices;
ButtonPressAction::ButtonPressAction(void(*action)(void))
{
callback = action;
}
void ButtonPressAction::bindToPin(uint8_t buttonPin)
{
pin = buttonPin;
pinMode(pin, INPUT_PULLUP);
instances.push_back(this);
}
void ButtonPressAction::update(void)
{
// for(std::vector<ButtonPressAction*>::iterator i = instances.begin(); i !=instances.end(); i++)
for (auto& i : instances) // not needed dereferencing of iterator!
{
bool currentState = digitalRead(i->pin) == 0? true: false;
if (i->lastState != currentState and millis() - i->lastPressMillis > DEBOUNCE_TIME)
{
if(currentState == true)
{
if (i->callback)
{
i->callback();
}
}
i->lastState = currentState;
i->lastPressMillis = millis();
}
}
}
//IndicatorLED
IndicatorLED::IndicatorLED(uint8_t indicatorLed)
{
pin = indicatorLed;
}
void IndicatorLED::begin(void)
{
pinMode(pin, OUTPUT);
}
void IndicatorLED::on(void)
{
digitalWrite(pin, HIGH);
state = true;
}
void IndicatorLED::off(void)
{
digitalWrite(pin, LOW);
state = false;
}
void IndicatorLED::toggle(void)
{
state = !state;
digitalWrite(pin, state? HIGH : LOW);
}
// ToggleSwitchAction
std::vector<ToggleSwitchAction*> ToggleSwitchAction::instances;
void ToggleSwitchAction::bindToPin(uint8_t togglePin)
{
pin = togglePin;
pinMode(pin, INPUT_PULLUP);
instances.push_back(this);
}
void ToggleSwitchAction::update(void)
{
for(auto& i : instances)
{
//Serial << ("Toggle");
SwitchState currentState = digitalRead(i->pin) == HIGH? OPEN : CLOSED;
if (i->lastState != currentState and millis() - i->lastPressMillis > DEBOUNCE_TIME)
{
Serial << ("toggle");
if (i->lastState == OPEN)
{
i->lastState = CLOSED;
if(i->onOpen)
{
i->onOpen();
}
}
else //(i->state == CLOSED)
{
i->lastState = OPEN;
if (i->onClosed)
{
i->onClosed();
}
}
i->lastState = currentState;
i->lastPressMillis = millis();
}
}
}
// Buzzer
void Buzzer::begin()
{
pinMode(pin, OUTPUT);
}
void Buzzer::alarmOn(const char* deviceID)
{
if (devices.size())
{
for (auto i : devices)
{
if (i == deviceID)
{
return;
}
}
}
devices.push_back(deviceID);
}
void Buzzer::alarmOff(const char* deviceID)
{
if (devices.size())
{
int loc = 0;
for (auto i : devices)
{
if (i == deviceID)
{
devices.erase(devices.begin() + loc);
}
loc++;
}
}
}
void Buzzer::buzz(void)
{
if (devices.size())
{
if (state == BUZZER_OFF)
{
state = BUZZER_ON;
digitalWrite(pin, HIGH);
}
}
else
{
if (state == BUZZER_ON)
{
state = BUZZER_OFF;
digitalWrite(pin, LOW);
}
}
}
// MillisTimer
std::vector<MillisTimer*> MillisTimer::timerArray;
MillisTimer::MillisTimer(uint32_t milliseconds, functionPtr action, bool repeat) {
//timerArray.push_back(this);
delay = milliseconds;
callback = action;
repeats = repeat;
}
void MillisTimer::begin(void) {
timerArray.push_back(this);
Serial << "Timer Added" << "\n";
}
void MillisTimer::processTimers(void) {
uint32_t currentMillis = millis();
int count = 0;
for (auto& i : timerArray)
{
if(currentMillis - i->lastMillis >= i->delay)
{
if (i->state == RUNNING){
if(i->callback){
i->callback();
}
if (!i->repeats){
i->state = STOPPED;
}
else {
i->lastMillis = currentMillis;
}
}
}
}
}
void MillisTimer::start (void){
lastMillis = millis();
state = RUNNING;
Serial << "Timer Start\n";
}
void MillisTimer::start (bool repeat){
repeats = repeat;
lastMillis = millis();
state = RUNNING;
Serial << "Timer Start\n";
}
void MillisTimer::stop(void){
lastMillis = millis();
state = STOPPED;
}