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