forked from ghztomash/MIDIElements
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RGBLed.cpp
190 lines (178 loc) · 3.88 KB
/
RGBLed.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
// MIDI Elements RGBLed class
// Library to simplify handling of components for MIDI controllers
// Created by Tomash Ghz
// www.tomashg.com
#include "RGBLed.h"
//-----------------------------------------------------------------------------------
// constructor
RGBLed::RGBLed(byte pa, byte pb, byte pc){
RGBLed(pa,pb,0,0,false);
}
RGBLed::RGBLed(byte pa, byte pb, byte pc, byte c, byte n){
RGBLed(pa,pb,c,n,false);
}
RGBLed::RGBLed(byte pa, byte pb, byte pc, byte c, byte n, bool a){
pinA=pa;
pinB=pb;
pinC=pc;
channel=c;
number=n;
pwm=a;
state=0;
red=0;
green=128;
blue=0;
intensity=128;
pinMode(pinA,OUTPUT);
pinMode(pinB,OUTPUT);
pinMode(pinC,OUTPUT);
}
// destructor
RGBLed::~RGBLed(){
}
void RGBLed::setOn(byte c, byte n, byte v){
if((channel==c)&&(number==n)){
if(v==0) {// velocity 0 turn RGBLed off
analogWrite(pinA,0);
analogWrite(pinB,0);
analogWrite(pinC,0);
}else if(v==127){ // turn on selected color
analogWrite(pinA,red);
analogWrite(pinB,green);
analogWrite(pinC,blue);
}else // turn RGBLed on
{
if(pwm){ // analog write
analogWrite(pinA,map(v,0,127,0,255));
analogWrite(pinB,map(v,0,127,0,255));
analogWrite(pinC,map(v,0,127,0,255));
}
else{
switch(v%7){
case 0:
analogWrite(pinA,intensity);
analogWrite(pinB,0);
analogWrite(pinC,0);
break;
case 1:
analogWrite(pinA,0);
analogWrite(pinB,intensity);
analogWrite(pinC,0);
break;
case 2:
analogWrite(pinA,0);
analogWrite(pinB,0);
analogWrite(pinC,intensity);
break;
case 3:
analogWrite(pinA,intensity);
analogWrite(pinB,intensity);
analogWrite(pinC,0);
break;
case 4:
analogWrite(pinA,intensity);
analogWrite(pinB,0);
analogWrite(pinC,intensity);
break;
case 5:
analogWrite(pinA,0);
analogWrite(pinB,intensity);
analogWrite(pinC,intensity);
break;
case 6:
analogWrite(pinA,intensity);
analogWrite(pinB,intensity);
analogWrite(pinC,intensity);
break;
}
}
state=v;
}
}
}
void RGBLed::setOff(byte c, byte n, byte v){
if((channel==c)&&(number==n)){ // note off, turn RGBLed off
analogWrite(pinA,0);
analogWrite(pinB,0);
analogWrite(pinC,0);
state=0;
}
}
void RGBLed::setOnSilent(byte c, byte n, byte v){
if((channel==c)&&(number==n)){
if(v==0){ // velocity 0 turn RGBLed off
state=0;
}
else // turn RGBLed on
{
state=v;
}
}
}
void RGBLed::setOffSilent(byte c, byte n, byte v){
if((channel==c)&&(number==n)){ // note off, turn RGBLed off
state=0;
}
}
void RGBLed::set(byte s){
state=s;
}
void RGBLed::set(){
if(state==0){
analogWrite(pinA,0);
analogWrite(pinB,0);
analogWrite(pinC,0);
}
else if(state==127){ // turn on selected color
analogWrite(pinA,red);
analogWrite(pinB,green);
analogWrite(pinC,blue);
}
else{
switch(state%3){
case 0:
analogWrite(pinA,intensity);
analogWrite(pinB,0);
analogWrite(pinC,0);
break;
case 1:
analogWrite(pinA,0);
analogWrite(pinB,intensity);
analogWrite(pinC,0);
break;
case 2:
analogWrite(pinA,0);
analogWrite(pinB,0);
analogWrite(pinC,intensity);
break;
case 3:
analogWrite(pinA,intensity);
analogWrite(pinB,intensity);
analogWrite(pinC,0);
break;
case 4:
analogWrite(pinA,intensity);
analogWrite(pinB,0);
analogWrite(pinC,intensity);
break;
case 5:
analogWrite(pinA,0);
analogWrite(pinB,intensity);
analogWrite(pinC,intensity);
break;
case 6:
analogWrite(pinA,intensity);
analogWrite(pinB,intensity);
analogWrite(pinC,intensity);
break;
}
}
}
// set the on colors manually. on color is enabled if velocity 127 is recieved.
void RGBLed::setColor(byte r, byte g, byte b, byte i){
red=r;
green=g;
blue=b;
intensity=i;
}