-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserial-case.ino
100 lines (81 loc) · 2.11 KB
/
serial-case.ino
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
/***************************************************
Audio 2 Led project
Arduino Nano Led controller firmware v0.1
COMMANDS
-----------------------------------------------
00
Displays the frame currently in the buffer
Replies: 00
* *
02
Setup led strip length
Replies: nothing
* *
01 b1 g1 r1 b2 g2 r2 ... bn gn rn
Reads nn pixels into the frame buffer
Replies: nothing
* *
FF
Clears the display and terminates the animation
Replies: nothing
***************************************************/
#include <Adafruit_NeoPixel.h>
#define DATAPIN 4
#define PIXELS 500
byte pixelBuffer[3];
byte countBuffer[3];
int count = 0;
Adafruit_NeoPixel leds = Adafruit_NeoPixel(PIXELS, DATAPIN);
void setup()
{
// Clear LEDs
leds.begin();
leds.clear();
leds.show();
// Open serial port and tell the controller we're ready.
Serial.begin(1000000);
Serial.println("Setup ok");
Serial.write(0x00);
}
int bytesToInt(unsigned int x_high, unsigned int x_low) {
int combined;
combined = x_high;
combined = combined*256;
combined |= x_low;
return combined;
}
void loop()
{
// Read a command
while (Serial.available() == 0);
byte command = Serial.read();
switch (command)
{
// Show frame
case 0x00:
// Update LEDs
leds.show();
// Tell the controller we're ready
// We don't want to receive serial data during leds.show() because data will be dropped
Serial.write(0x00);
break;
// Load frame
case 0x01:
// Read number of pixels
while (Serial.available() == 0);
Serial.readBytes(countBuffer, 2);
count = bytesToInt(countBuffer[0], countBuffer[1]);
// Read and update pixels
for (int i = 0; i < count; i++)
{
Serial.readBytes(pixelBuffer, 3);
leds.setPixelColor(i, pixelBuffer[0], pixelBuffer[1], pixelBuffer[2]);
}
break;
// Clear
case 0xFF:
leds.clear();
leds.show();
break;
}
}