-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathsingleLEDLibrary.cpp
193 lines (174 loc) · 4.29 KB
/
singleLEDLibrary.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
#include "singleLEDLibrary.h"
#include "Arduino.h"
#define P_NONE 0
#define P_SINGLE 1
#define P_BREATHE 2
#define P_FLICKER 3
#define P_BLINK 4
#define P_BLINK_RAND 5
sllib::sllib(int pin){
//make sure to use pwm ports here
pinMode(pin,OUTPUT);
digitalWrite(pin,LOW);
_pin = pin;
}
void sllib::update(){
switch(runningFunction){
case P_NONE:
break;
case P_SINGLE:
patternSingle(arrP,speedp);
break;
case P_BREATHE:
breathSingle(speedp);
break;
case P_FLICKER:
flickerSingle();
break;
case P_BLINK:
blinkSingle(speedp);
break;
case P_BLINK_RAND:
blinkRandomSingle(speedp,timep);
break;
}
}
void sllib::setOnSingle(){
runningFunction = P_NONE;
digitalWrite(_pin, HIGH);
}
void sllib::setOffSingle(){
runningFunction = P_NONE;
digitalWrite(_pin, LOW);
}
void sllib::setRandomBlinkSingle(int minTime, int maxTime){
runningFunction = P_BLINK_RAND;
speedp = minTime;
timep = maxTime;
}
void sllib::setPatternSingle(int pattern[], int lengthArray){
if (arrP != 0) {
delete [] arrP;
}
arrP = new int [lengthArray];
for(int i=0; i < lengthArray; i++){
arrP[i] = pattern[i];
}
speedp = lengthArray;
runningFunction = P_SINGLE;
}
void sllib::setBreathSingle(int speed){
runningFunction = P_BREATHE;
speedp = speed;
}
void sllib::setFlickerSingle(){
runningFunction = P_FLICKER;
}
void sllib::setBlinkSingle(int speed){
runningFunction = P_BLINK;
speedp = speed;
}
//function for pattern based blinking, first entry always turns the led high
void sllib::patternSingle(int pattern[], int lengthArray){
if(counter < lengthArray) {
if(milOld == 0){
milOld = millis();
digitalWrite(_pin,HIGH);
}
if((milOld + pattern[counter]) < millis() ){
milOld = millis();
if(counter % 2 == 0){
digitalWrite(_pin, LOW);
}
else{
digitalWrite(_pin,HIGH);
}
counter++;
}
}
else{
counter = 0;
}
}
// simple breating function
void sllib::breathSingle(int speed){
if((milOld + 1) < millis() ){
milOld = millis();
float it = 0;
it = (exp(sin(millis()/(float)speed*PI)) - 0.36787944)*108.0;
analogWrite(_pin, it);
}
}
// simple flickering function
void sllib::flickerSingle(int intMin, int intMax){
if((milOld + 100) < millis()){
milOld = millis();
float it = 0;
it = random(intMin,intMax);
analogWrite(_pin, it);
}
}
//overload for instant use
void sllib::flickerSingle(){
if((milOld + 100) < millis()){
milOld = millis();
float it = 0;
it = random(80,255);
analogWrite(_pin, it);
}
}
//overload for speed controll
void sllib::flickerSingle(int intMin, int intMax, int speed){
if((milOld + speed) < millis()){
milOld = millis();
float it = 0;
it = random(intMin,intMax);
analogWrite(_pin, it);
}
}
// simple blinking function
void sllib::blinkSingle(int speed){
if ( (milOld + speed) < millis() ) {
milOld = millis();
if(ioBlink == false){
digitalWrite(_pin,HIGH);
ioBlink = true;
}
else{
digitalWrite(_pin,LOW);
ioBlink = false;
}
}
}
//overload to allow for async blinking
void sllib::blinkSingle(int timeHigh, int timeLow){
if(ioBlink == false){
if( (milOld + timeHigh) < millis()){
milOld = millis();
digitalWrite(_pin,LOW);
ioBlink = true;
}
}
else{
if((milOld + timeLow) < millis()){
milOld = millis();
digitalWrite(_pin,HIGH);
ioBlink = false;
}
}
}
//blinking with randomised delay
void sllib::blinkRandomSingle(int minTime, int maxTime){
if((milOld + rndTemp) < millis()){
milOld = millis();
rndTemp = random(minTime, maxTime);
if(ioBlink == false){
digitalWrite(_pin,HIGH);
ioBlink = true;
}
else{
digitalWrite(_pin,LOW);
ioBlink = false;
}
}
}