-
Notifications
You must be signed in to change notification settings - Fork 0
/
LED.h
52 lines (44 loc) · 1.02 KB
/
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
class LED{
public:
LED (int _pin = -1, int _pos = -1){
pin = _pin;
pos = _pos;
digitalWrite(pin, LOW);
}
void setPinPos(int _pin, int _pos)
{
pin = _pin;
pos = _pos;
lowerBound = 1023 * pos;
if (lowerBound){
lowerBound += 1;
}
upperBound = 1023 * (pos + 1);
}
int getPin(){
return pin;
}
void enable(){
digitalWrite(pin, HIGH);
}
void disable(){
digitalWrite(pin, LOW);
}
void updateBrightness(int rawBrightness){
int convertedBrightness = map(rawBrightness, lowerBound, upperBound, 0, 255);
convertedBrightness = convertedBrightness > 255 ? 255 : convertedBrightness;
if (rawBrightness >= lowerBound){
_updateBrightness(convertedBrightness);
} else {
disable();
}
}
private:
int pin;
int pos;
int lowerBound;
int upperBound;
void _updateBrightness(int brightnessIntensity){
analogWrite(pin, brightnessIntensity);
}
};