-
Notifications
You must be signed in to change notification settings - Fork 1
/
PinToggle.cpp
177 lines (155 loc) · 3.48 KB
/
PinToggle.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
/*
PinToggle.cpp - control pin toggling
Created by Bob Burton, 25/03/2018
*/
#include "Arduino.h"
#include "PinToggle.h"
PinToggle::PinToggle(byte pin) //constructor
{
_pinNum = pin;
}
byte PinToggle::begin()
{
static byte _ID = 0;
pinMode(_pinNum, OUTPUT);
return _ID++;
}
//overloaded function to handle optional parameters
//base version
void PinToggle::startToggling(byte startState, unsigned long lowPeriod, unsigned long highPeriod)
{
_periods[0] = lowPeriod;
_periods[1] = highPeriod;
if (_startState == LOW)
{
_periodIndex = 0;
}
else
{
_periodIndex = 1;
}
_startState = startState;
_originalStartState = _startState;
_currentState = _startState;
_writeState();
_startTime = millis();
_toggling = true;
_started = true;
}
//with toggle count parameter
void PinToggle::startToggling(byte startState, unsigned long lowPeriod, unsigned long highPeriod, unsigned int toggleCount)
{
_toggleCount = toggleCount;
_originalToggleCount = _toggleCount;
_counting = true;
startToggling(startState, lowPeriod, highPeriod); //call base version
}
void PinToggle::waitBeforeToggling(byte startState, unsigned long lowPeriod, unsigned long highPeriod, unsigned long waitPeriod)
{
_currentState = startState;
_originalStartState = startState;
_periods[0] = lowPeriod;
_periods[1] = highPeriod;
if (_startState == LOW)
{
_periodIndex = 0;
}
else
{
_periodIndex = 1;
}
_waiting = true;
_waitPeriod = waitPeriod;
_waitStartTime = millis();
_writeState();
}
void PinToggle::restartToggling() //restart toggling with original parameters
{
if (_started)
{
startToggling(_originalStartState, _periods[0], _periods[1], _originalToggleCount);
}
}
void PinToggle::update(callbackFunc func = nullptr) //change state if period ended. callback optional
{
_currentTime = millis();
if (_toggling) //no need to update if not currently toggling
{
if (_currentTime - _startTime >= _periods[_periodIndex])
{
_currentState = !_currentState;
_writeState();
_periodIndex++;
_periodIndex %= 2;
_startTime = _currentTime;
if (_counting)
{
_toggleCount--;
if (func)
{
cb1 = func; //function to execute
cb1(_toggleCount); //execute the callback function. pass back toggle count
}
if (_toggleCount == 0)
{
_toggling = false;
}
}
}
}
if (_waiting)
{
if (_currentTime - _waitStartTime >= _waitPeriod)
{
_waiting = false;
startToggling(_originalStartState, _periods[0], _periods[1]);
}
}
}
unsigned int PinToggle::getToggleCount()
{
return _toggleCount;
}
void PinToggle::stopToggling(byte state)
{
setOutputState(state);
}
void PinToggle::setOutputState(byte state)
{
_currentState = state;
_writeState();
_toggling = false;
}
byte PinToggle::getOutputState()
{
return _currentState;
}
void PinToggle::resumeToggling()
{
if (_started)
{
_toggling = true;
}
}
boolean PinToggle::getTogglingState()
{
return _toggling;
}
void PinToggle::_writeState()
{
digitalWrite(_pinNum, _currentState);
}
void PinToggle::updateLowPeriod(unsigned long newPeriod)
{
if (_toggling)
{
_periods[0] = newPeriod;
}
}
void PinToggle::updateHighPeriod(unsigned long newPeriod)
{
if (_toggling)
{
_periods[1] = newPeriod;
}
}