/
OctobarSerialApi.pde
158 lines (130 loc) · 3.19 KB
/
OctobarSerialApi.pde
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
#define clockpin 13 // CI
#define enablepin 10 // EI
#define latchpin 9 // LI
#define datapin 11 // DI
#define NumLEDs 8
int LEDChannels[NumLEDs][3] = {0};
int SB_CommandMode;
int SB_RedCommand;
int SB_GreenCommand;
int SB_BlueCommand;
void setup() {
pinMode(datapin, OUTPUT);
pinMode(latchpin, OUTPUT);
pinMode(enablepin, OUTPUT);
pinMode(clockpin, OUTPUT);
SPCR = (1<<SPE)|(1<<MSTR)|(0<<SPR1)|(0<<SPR0);
digitalWrite(latchpin, LOW);
digitalWrite(enablepin, LOW);
Serial.begin(115200);
for (int i = 1; i < 8; i++)
{
setLED(i, 0, 0, 0);
}
WriteLEDArray();
}
void SB_SendPacket() {
if (SB_CommandMode == B01) {
SB_RedCommand = 120;
SB_GreenCommand = 100;
SB_BlueCommand = 100;
}
SPDR = SB_CommandMode << 6 | SB_BlueCommand>>4;
while(!(SPSR & (1<<SPIF)));
SPDR = SB_BlueCommand<<4 | SB_RedCommand>>6;
while(!(SPSR & (1<<SPIF)));
SPDR = SB_RedCommand << 2 | SB_GreenCommand>>8;
while(!(SPSR & (1<<SPIF)));
SPDR = SB_GreenCommand;
while(!(SPSR & (1<<SPIF)));
}
void WriteLEDArray() {
SB_CommandMode = B00; // Write to PWM control registers
for (int h = 0;h<NumLEDs;h++) {
SB_RedCommand = LEDChannels[h][0];
SB_GreenCommand = LEDChannels[h][1];
SB_BlueCommand = LEDChannels[h][2];
SB_SendPacket();
}
delayMicroseconds(15);
digitalWrite(latchpin,HIGH); // latch data into registers
delayMicroseconds(15);
digitalWrite(latchpin,LOW);
SB_CommandMode = B01; // Write to current control registers
for (int z = 0; z < NumLEDs; z++) SB_SendPacket();
delayMicroseconds(15);
digitalWrite(latchpin,HIGH); // latch data into registers
delayMicroseconds(15);
digitalWrite(latchpin,LOW);
}
void setLED(byte LED, int red, int green, int blue)
{
LEDChannels[LED][0] = red;
LEDChannels[LED][1] = green;
LEDChannels[LED][2] = blue;
}
void clearLEDs() {
for (int i = 1; i < 8; i++)
{
setLED(i, 0, 0, 0);
}
WriteLEDArray();
}
byte inByte;
int in[4] = {0,0,0,0};
int pos = 0;
void loop() {
if (Serial.available() > 0) {
inByte = Serial.read();
if ((inByte >= 48) && (inByte <= 57)) { // if ASCII numeric '0' - '9'
in[pos] = in[pos] * 10 + (inByte - 48);
}
else if (inByte == ' ') {
pos++;
if (pos >= 4) pos = 0;
}
else if (inByte == ',' || inByte == 'n') {
setLED(in[0], in[1], in[2], in[3]);
/*
Serial.print('[', BYTE);
Serial.print(in[0], DEC);
Serial.print(',', BYTE);
Serial.print(in[1], DEC);
Serial.print(',', BYTE);
Serial.print(in[2], DEC);
Serial.print(',', BYTE);
Serial.print(in[3], DEC);
Serial.print(']', BYTE);
*/
pos = 0;
in[0] = 0;
in[1] = 0;
in[2] = 0;
in[3] = 0;
}
else if (inByte == 'W') {
WriteLEDArray();
// Serial.print('!', BYTE);
pos = 0;
}
else if (inByte == 'C') {
clearLEDs();
pos = 0;
}
//Serial.print(inByte, BYTE);
}
/*
int i;
static int x, y, z;
for (i = 1; i < 8; i++)
{
setLED(i, 0, 0, 0);
}
setLED(x, 1023, 1023, 1023);
x++;
if (x > 7)
x = 1;
WriteLEDArray();
delay(500);
*/
}