-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSimpleLed.cpp
118 lines (97 loc) · 2.79 KB
/
SimpleLed.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
#include "SimpleLed.h"
SimpleLed::SimpleLed() : ledState(STATE_NORMAL),level(SLED_ON){}
SimpleLed::~SimpleLed() {}
/**
* Inits led
* @param ledPin Arduino pin number
* @param isPWM Say true if pin is PWM
*/
void SimpleLed::initLed(uint8_t ledPin, boolean isPWM){
pinIsPWM = isPWM;
initActuator(ledPin);
off();
}
/** Sets led output level
* @param newLevel Pin set as PWM (0-255), Digital (0,1)*/
void SimpleLed::setLevel(uint8_t newLevel){level = newLevel;}
/** Makes led emit level*/
void SimpleLed::emit(uint8_t level){(pinIsPWM) ? writeAnalog(level) : writeDigital((bool)level) /*? digitalWrite(pin,HIGH) : digitalWrite(pin,LOW)*/;}
/** Turns on writting set level */
void SimpleLed::on(){emit(level);}
/** Turns off writting 0 */
void SimpleLed::off(){emit(SLED_OFF);}
/** Makes led blink blocking
* @param times Number of times to blink
* @param periodo Blinking periodin milliseconds, duty cicle is 50%
*/
void SimpleLed::staticBlink(uint16_t times, uint16_t period){
uint16_t d = period/2;
uint16_t n = 0;
while(n++ < times){
on();
delay(d);
off();
delay(d);
}
}
/** Makes led blink dinamically without blocking. Use update()
* to update led state.
* @param times Number of times to blink
* @param periodo Blinking period in milliseconds, duty cicle is 50%
*/
void SimpleLed::dinamicRepeatedBlink(uint16_t times, uint16_t period){
if(ledState == STATE_NORMAL){
// free(&blinkAtt);
blinkAtt = new BlinkAtt;
blinkAtt->state = SLED_ON;
on();
blinkAtt->lastChange = millis();
ledState = STATE_BLINKING;
}
blinkAtt->times = times;
blinkAtt->counter = 0;
blinkAtt->delay = period/2;
}
/** Makes led blink dinamically.Use update() to update led state.
* @param period Blinking period in milliseconds, duty cicle is 50%
*/
void SimpleLed::dinamicBlink(uint16_t period){
dinamicRepeatedBlink(0,period);
ledState = INFINITE_LOOP;
}
/** Makes led blink dinamically.Use update() to update led state.
* @howLong Time kept blinking.
* @param period Blinking period in milliseconds, duty cicle is 50%
*/
void SimpleLed::dinamicTimedBlink(uint16_t howLong, uint16_t period){
dinamicRepeatedBlink(round(howLong/period),period);
}
/**
* Updates led state. To be used when dinamically blinking.
*/
void SimpleLed::update(){
if(ledState != STATE_NORMAL){
if((millis() - blinkAtt->lastChange) >= blinkAtt->delay) {
if(blinkAtt->state == SLED_ON){
off();
blinkAtt->lastChange = millis();
blinkAtt->state = SLED_OFF;
if((ledState != INFINITE_LOOP) && (++blinkAtt->counter >= (blinkAtt->times-1))){reset();}
}else{
on();
blinkAtt->lastChange = millis();
blinkAtt->state = SLED_ON;
}
}
}
}
/**
* Resets blinking and turns led off.
*/
void SimpleLed::reset(){
if(ledState != STATE_NORMAL){
off();
delete blinkAtt;
ledState = STATE_NORMAL;
}
}