forked from mdaffin/PyNeoPixel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPyNeoPixel.ino
103 lines (89 loc) · 2.1 KB
/
PyNeoPixel.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
101
102
#include <Adafruit_NeoPixel.h>
#define PIN 8
Adafruit_NeoPixel strip = Adafruit_NeoPixel(3, PIN, NEO_GRB + NEO_KHZ800);
/*
\r Start of command
\n End of command
s show
c setPixelColor pixelH pixelL red green blue
b setBrightness brightness
g getPixelColor pixelH pixelL
n numPixels
*/
enum Commands {
Show = 's',
SetColor = 'c',
SetBrightness = 'b',
GetColor = 'g',
NumberPixels = 'n'
};
void setup() {
Serial.begin(9600);
strip.begin();
strip.show(); // Initialize all pixels to 'off'
Serial.println("Ok");
}
bool read(char &character, long timeout = 1000) {
long start = millis();
while (!Serial.available()) {
if (millis() - start > timeout) {
return false;
}
}
character = Serial.read();
return true;
}
bool setColor() {
char character;
uint16_t pixel;
uint8_t color[3];
if (!read(character)) { return false; }
pixel = (uint16_t) character << 8;
if (!read(character)) { return false; }
pixel += (uint8_t)character;
if (!read(character)) { return false; }
color[0] = character;
if (!read(character)) { return false; }
color[1] = character;
if (!read(character)) { return false; }
color[2] = character;
Serial.print(" ");
Serial.print(color[0]);
Serial.print(" ");
Serial.print(color[1]);
Serial.print(" ");
Serial.print(color[2]);
Serial.print(" ");
strip.setPixelColor(pixel, color[0], color[1], color[2]);
return true;
}
void loop() {
if (Serial.available()) {
char character;
if (!read(character) || character != ':') { return; }
if (!read(character)) { return; }
uint8_t command_index = (uint8_t)character;
Serial.print(command_index);
if (!read(character)) { return; }
switch(character) {
case Show:
strip.show();
Serial.println("Ok");
break;
case SetColor:
if (setColor()) {
Serial.println("Ok");
} else {
Serial.println("Err");
}
break;
case SetBrightness:
break;
case GetColor:
break;
case NumberPixels:
Serial.println(strip.numPixels());
break;
}
}
}