-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLED.cpp
192 lines (146 loc) · 4.46 KB
/
LED.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
/*
LED_h - Library to operate a single RGB LED
Purpose: Operate a single RGB LED, directly to the hardware, with inputs 0 to 255 for each colour
Contract ----------------------------------------------
Preconditions: Each input needs to be between 0 and 255. 255 means on, 0 meean off
Guarantee: Light up the LED according to the three inputs
Finite State Machine: First setPins, then repeatedly (setColour or NextColour, then switch on or shine to illuminate) then off.
INTERNAL ----------------------------------------------
Translation Needed: From Hardware - High as a parameter need to be convered to LOW as an input to the hardware
Workarounds:
In the absence of the three parameter constructor working, need to create bare, then setPins
To Do:
Will Need to code flashing lights, that remember the prior colour
Issues
1. set colour sets the light colour, but dosn't set RGBValue to the right number
2. Does on(duration) stop everything for 2 seconds?
3. Can I make LOW represent off?
4. Can I do colours beyond just HIGH and LOW - with analogWrite(pin, vlaue) where value 0-255
*/
#include "Arduino.h"
#include "LED.h"
LED::LED()
{
_rPin=0;
_gPin=0;
_bPin=0;
_r=_off;
_g=_off;
_b=_off;
_RGBValue = 50;
_isOn = false;
}
LED::LED(int rPin, int gPin, int bPin)
{
_rPin=rPin;
_gPin=gPin;
_bPin=bPin;
pinMode(_rPin, OUTPUT); // set RPin to output mode
pinMode(_gPin, OUTPUT); // set GPin to output mode
pinMode(_bPin, OUTPUT); // set BPin to output mode
_r=HIGH; // HIGH is off
_g=HIGH; // HIGH is off
_b=HIGH; // HIGH is off
_RGBValue = 0;
_isOn = false;
}
void LED::setPins(int rPin, int gPin, int bPin){
_rPin=rPin;
_gPin=gPin;
_bPin=bPin;
pinMode(_rPin, OUTPUT); // set RPin to output mode
pinMode(_gPin, OUTPUT); // set GPin to output mode
pinMode(_bPin, OUTPUT); // set BPin to output mode
_r=_off; // HIGH is off
_g=_off; // HIGH is off
_b=_off; // HIGH is off
//_ledWrite(_r,_g,_b);
digitalWrite(_rPin, 255 - _r);
digitalWrite(_gPin, 255 - _g);
digitalWrite(_bPin, 255 - _b);
_RGBValue = 0;
_isOn = false;
}
void LED::setColour(int r, int g, int b)
{
// Change to a specific colour, but don't automatically turn it on
// Each colour element should be either HIGH or LOW
_r=r;
_g=g;
_b=b;
}
void LED::nextColour()
// Move to next colour, but don't automatically turn it on
{
_RGBValue++;
_RGBValue=_RGBValue%8;
switch(_RGBValue){
case 0: _r=_off;_g=_off;_b=_off;break;
case 1: _r=_off;_g=_off;_b=LOW;break;
case 2: _r=_off;_g=LOW;_b=LOW;break;
case 3: _r=_off;_g=LOW;_b=_off;break;
case 4: _r=LOW;_g=LOW;_b=_off;break;
case 5: _r=LOW;_g=LOW;_b=LOW;break;
case 6: _r=LOW;_g=_off;_b=LOW;break;
case 7: _r=LOW;_g=_off;_b=_off;break;
default: break;
}
}
void LED::on(int duration)
// Switch light on for a number of milliseconds
{
_isOn = true;
//_ledWrite(_r,_g,_b);
digitalWrite(_rPin, 255 - _r);
digitalWrite(_gPin, 255 - _g);
digitalWrite(_bPin, 255 - _b);
delay(duration);
off();
}
void LED::shine()
//Switch light on with current colour until further notice. C
//Could be flashing
{
_isOn = true;
//_ledWrite(_r,_g,_b);
digitalWrite(_rPin, 255 - _r);
digitalWrite(_gPin, 255 - _g);
digitalWrite(_bPin, 255 - _b);
}
void LED::off()
// Turn the light off
{
_isOn = false;
//_ledWrite( _off, _off, _off);
digitalWrite(_rPin, 255 - _off);
digitalWrite(_gPin, 255 - _off);
digitalWrite(_bPin, 255 - _off);
}
/*
void _ledWrite(int r, int g, int b) {
// translate to Hardware LOW = 255
digitalWrite(_rPin, 255 - _r);
digitalWrite(_gPin, 255 - _g);
digitalWrite(_bPin, 255 - _b);
}
*/
void LED::debug()
// Display its inards
{
Serial.print(" Pins: ");
Serial.print(_rPin); Serial.print(" ");
Serial.print(_gPin); Serial.print(" ");
Serial.print(_bPin);
Serial.print(" RGB Value: ");
Serial.print(_RGBValue);
Serial.print(" Colours: R ");
Serial.print(_r); Serial.print(" G "); Serial.print(_g); Serial.print(" B "); Serial.print(_b);
Serial.print(" Lights On: ");
Serial.println(_isOn);
}
/*
void LED::setFlash(bool flashing)
// Set this light to flash when it is turned on
{
_isFlashing = flashing;}
*/