-
Notifications
You must be signed in to change notification settings - Fork 9
/
FancyLED.cpp
242 lines (176 loc) · 5.15 KB
/
FancyLED.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
231
232
233
234
235
236
237
238
239
240
241
242
/*
FancyLED.h - FancyLED for Wiring/Arduino
(cc) 2011 Carlyn Maw, Attribute, Share Alike
Created 06 July 2011
Version 0.1
*/
// include core Wiring API
#include "WProgram.h"
// include this library's description file
#include "FancyLED.h"
// Constructor /////////////////////////////////////////////////////////////////
// Function that handles the creation and setup of instances
//------------------------------------------------------ Using Arduino Pin Num
FancyLED::FancyLED(int myPin, bool myMode)
{
// initialize this instance's variables
this->_myPin = myPin;
pinMode(this->_myPin, OUTPUT);
this->_type = 0;
this->_myBit = this->_myPin;
this->_mode = myMode;
_lastState = 0;
_currentState = 0;
_pinState= 0;
_dutyCycle = 50;
_fullPeriod = 1000;
_fuseTimer = 0;
_goalPulseCount = 1;
_currentPulseCount = 0;
}
//----------------------------------------------------------------- Using Byte
FancyLED::FancyLED(int myBit, bool myMode, unsigned char *myRegister)
{
// initialize this instance's variables
this->_type = 1;
this->_mode = myMode;
this->_myBit = myBit;
this->_myPin = this->_myBit;
this->_myRegister = myRegister;
this->_registerValue = 255;
_lastState = 0;
_currentState = 0;
_pinState= 0;
_dutyCycle = 50;
_fullPeriod = 1000;
_fuseTimer = 0;
_goalPulseCount = 1;
_currentPulseCount = 0;
}
// Public Methods //////////////////////////////////////////////////////////////
// Functions available in Wiring sketches, this library, and other libraries
//---------////////////////////MAIN LOOP / LISTENER ///////////--------------//
void FancyLED::update(void) {
update(millis());
}
void FancyLED::update(unsigned long newCurrentTime) {
_currentTime = newCurrentTime;
if (_fuseTimer > _currentTime) {
return;
}
if (_currentPulseCount < _goalPulseCount) {
_pulseFlag = true;
} else if (_currentPulseCount >= _goalPulseCount) {
}
// if (_pulseFlag && _countFlag) {
if (_pulseFlag) {
//_longHolder = (_fullPeriod * _dutyCycle) / 100;
//_onPeriod = int(_longHolder);
_onPeriod = (_fullPeriod * _dutyCycle) / 100;
_offPeriod = (_fullPeriod - _onPeriod);
//Serial.println(_onPeriod);
//if my state is ready
if (_currentState == 0) {
//get the time
_flipTime = _currentTime;
//set my state to firing
_currentState = 1;
//turn on the pin
_pinState = _mode;
updatePin(_pinState);
}
//if my state is firing
else if (_currentState == 1) {
if ((_onPeriod) < (_currentTime - _flipTime)) {
//if it's time, turn me off
_flipTime = _currentTime;
_currentState = 2;
_pinState = !_mode;
updatePin(_pinState);
}
}
//if my state is resting
else if (_currentState == 2) {
//keep me off
_pinState = !_mode;
updatePin(_pinState);
//check the time and make me ready
if ((_offPeriod) < (_currentTime - _flipTime)) {
_currentState = 0;
_currentPulseCount++;
_pulseFlag = false;
_flipTime = _currentTime;
}
}
}
}
void FancyLED::setCurrentTime(unsigned long newCurrentTime) {
_currentTime = newCurrentTime;
}
int FancyLED::getState(void){
return _currentState;
}
void FancyLED::turnOn(void){
_pinState = _mode;
updatePin(_pinState);
_lastState = _currentState;
_currentState = 1; //firing
_flipTime = _currentTime;
}
void FancyLED::turnOff(void){
_pinState = !_mode;
updatePin(_pinState);
_lastState = _currentState;
_currentState = 2; //waiting
}
void FancyLED::toggle(void) {
if (_currentState == 1) {
turnOff();
} else {
turnOn();
}
}
void FancyLED::pulse(char myPulseTimes) {
_pulseFlag = true;
_goalPulseCount = myPulseTimes;
_currentPulseCount = 0;
}
void FancyLED::pulse(char myPulseTimes, int myPeriod, int myDutyCycle) {
_dutyCycle = myDutyCycle;
_fullPeriod = myPeriod;
pulse(myPulseTimes);
}
void FancyLED::pulse(void) {
pulse(1);
}
void FancyLED::fusedPulse(long myFuseLength, int myPulseTimes){
_fuseTimer = myFuseLength + _currentTime;
pulse(myPulseTimes);
}
int FancyLED::getDutyCycle(void){
return _dutyCycle;
}
void FancyLED::setDutyCycle(int newDC) {
_dutyCycle = newDC;
}
long FancyLED::getFullPeriod(void){
return _fullPeriod;
}
void FancyLED::setFullPeriod(long newFP){
_fullPeriod = newFP;
}
// Private Methods //////////////////////////////////////////////////////////////
// Functions available to the library only.
void FancyLED::updatePin(bool pinValue) {
if(!_type) {
digitalWrite(_myPin, pinValue);
} else {
_registerValue = *_myRegister;
if (pinValue) {
_registerValue |= (1 << _myBit);
} else {
_registerValue &=~ (1 << _myBit);
}
*_myRegister = _registerValue;
}
}