forked from farmerkeith/WeMosReadVolts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfarmerkeith_LED.h
134 lines (116 loc) · 3.21 KB
/
farmerkeith_LED.h
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
// tab file for LED object
// created 10 Oct 2017 by farmerkeith
// some of this code has been copied from the standard LED.h file
// include guard
#ifndef LED_H
#define LED_H
class LED{
public:
// constructor
LED(uint8_t ledPin, bool Vbehind);
// functions
bool getState(); // return HIGH for ON, LOW for OFF
void on(); // turns on the LED
void off(); // turns off the LED
void toggle(); // turns on if off, and off if on
void run(); // to be run often (eg every loop())
// controls blink and fade timing
// void blink(unsigned int period, byte flashes); // blinks the LED
// with a period of "period", 50% duty cycle, "flashes" times
// standard implementation uses delay()
// void setValue(unsigned int val); // brightness between ON and OFF using PWM
// fully ON is val=1023, fully OFF is val=0
// void fadeIn(unsigned int time); // increases from 0 to 100% over "time"
// void fadeOut(unsigned int time);// decreases from 100% to 0 over "time"
private:
// variables
bool status; // HIGH (1) for ON, LOW (0) for OFF
uint8_t pin;
bool _Vbehind; // 1 if LED has Vcc behind it; 0 if GND is behind it
unsigned long ledTime; // keep track of time
byte period=1; // ms, interval between executions in LED::run()
public:
// unsigned int _val; // added for debugging
};
// constructor
LED::LED(uint8_t ledPin, bool Vbehind){
pin=ledPin;
_Vbehind = Vbehind;
pinMode(pin,OUTPUT);
off();
}
void LED::run(){
if((long)(millis() - ledTime) >= 0){ // time is expired
ledTime += period; // ms, set time for the following execution
}
}
bool LED::getState() {return status; }
void LED::on(void){
analogWrite(pin,0);
if (_Vbehind) {
digitalWrite(pin,LOW);
} else {
digitalWrite(pin,HIGH);
}
status=true;
}
void LED::off(void){
analogWrite(pin,0);
if (_Vbehind) {
digitalWrite(pin,HIGH);
} else {
digitalWrite(pin,LOW);
}
status=false;
}
void LED::toggle(void){
if (status) off(); else on();
// status ? off() : on();
}
/*
void LED::blink(unsigned int period, byte flashes){
for (byte i=0; i<flashes; i++){
toggle();
delay(period/2);
toggle();
delay(period/2);
}
}
// uses PWM (any ESP8266 GPIO)
void LED::setValue(unsigned int val){
if (val>1008) val=1008; // PWM range is 0 to 1023, 1008 is 1024-16
if (_Vbehind) { // brightness increases with decreasing PWM duty
analogWrite(pin,1008-val);
} else { // brightness increases with increasing PWM duty
analogWrite(pin,val);
}
status = val>>9; // 1 if val >= 512
Serial.print("setValue val(G)=");
Serial.print(val);
Serial.print (" 1008-val(V)=");
Serial.println (1008-val);
_val = val;
// val>=511 ? this->status=false : this->status=true;
}
// uses PWM (any ESP8266 GPIO)
void LED::fadeIn(unsigned int time){
for (int value = 0 ; value <1024; value+=16){
setValue (value);
// analogWrite(pin, value);
delay(time/(64)); // 64 = 1024 / 16
// breaks if time < 64!
}
// analogWrite(pin,0);
on();
}
//assume PWM
void LED::fadeOut(unsigned int time){
for (int value = 0; value <1023; value+=16){
analogWrite(pin, value);
delay(time/(1024/16));
}
analogWrite(pin, 0);
off();
}
*/
#endif