-
Notifications
You must be signed in to change notification settings - Fork 1
/
lights.ino
180 lines (156 loc) · 4.08 KB
/
lights.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
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
/**
* ReSpeaker Arduino (atmega32u4) is a SPI slave receives commands as strings and controls the NeoPixel lights.
*
* SPI slave based on SeeedStudio from https://github.com/respeaker/spi_bridge/blob/master/arduino_32u4_spi_slave.ino
*/
#include <SPI.h>
// Use the NeoPixel library rather than through ReSpeaker
// can't include <respeaker.h> due to ISR conflict
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
#define PIXEL_PIN 11
#define NUMPIXELS 12
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIXEL_PIN, NEO_GRB + NEO_KHZ800);
char spi_buf[64];
volatile byte spi_buf_index = 0;
volatile byte spi_event = 0;
uint32_t lastColor = pixels.Color(0,0,255);
uint32_t red = pixels.Color(255,0,0);
uint32_t orange = pixels.Color(255,127,0);
uint32_t green = pixels.Color(0,255,0);
uint32_t blue = pixels.Color(0,0,255);
uint32_t yellow = pixels.Color(255,255,0);
uint32_t purple = pixels.Color(128,0,128);
uint32_t pink = pixels.Color(215,72,148);
uint32_t turquoise = pixels.Color(64,224,208);
uint32_t magenta = pixels.Color(255,0,255);
uint32_t white = pixels.Color(255,255,255);
uint32_t black = pixels.Color(0,0,0);
void setup (void)
{
Serial.begin(57600);
Serial1.begin(57600);
spi_slave_init();
pixels.begin();
pixels.setBrightness(10);
}
void spi_slave_init()
{
pinMode(MOSI, INPUT);
pinMode(MISO, OUTPUT);
pinMode(SCK, INPUT);
pinMode(SS, INPUT);
// enable SPI slave mode
SPCR |= _BV(SPE);
SPI.attachInterrupt();
}
// SPI interrupt handler
ISR (SPI_STC_vect)
{
char c = SPDR; // read SPI Data Register
if (spi_buf_index >= sizeof(spi_buf)) {
spi_buf_index = 0;
}
if ('\n' == c) {
spi_event = 1;
spi_buf[spi_buf_index] = '\0';
} else {
spi_buf[spi_buf_index] = c;
spi_buf_index++;
}
}
void loop (void)
{
// USB to UART
while (Serial1.available() && Serial.availableForWrite()) {
Serial.write((char)Serial1.read());
}
while (Serial.available() && Serial1.availableForWrite()) {
Serial1.write((char)Serial.read());
}
if (spi_event) {
spi_event = 0;
spi_buf_index = 0;
Serial.print("spi rx:");
Serial.println(spi_buf);
String command = String(spi_buf);
Serial.print("Command is ");
Serial.println(command);
if (command == "red") {
showColor(red);
} else if (command == "orange") {
showColor(orange);
} else if (command == "green") {
showColor(green);
} else if (command == "blue") {
showColor(blue);
} else if (command == "yellow") {
showColor(yellow);
} else if (command == "purple") {
showColor(purple);
} else if (command == "white") {
showColor(white);
} else if (command == "pink") {
showColor(pink);
} else if (command == "turquoise") {
showColor(turquoise);
} else if (command == "magenta") {
showColor(magenta);
} else if (command == "brighter") {
brighter();
} else if (command == "brightness up") {
brighter();
} else if (command == "brightness down") {
dimmer();
} else if (command == "on") {
lightsOn();
} else if (command == "off") {
lightsOff();
} else {
Serial.print("I don't understand ");
Serial.println(command);
}
}
}
void lightsOff() {
for (int i = 0; i < NUMPIXELS; i++) {
pixels.setPixelColor(i, black);
}
pixels.show();
}
void lightsOn() {
showColor(lastColor);
}
void brighter() {
uint8_t brightness = pixels.getBrightness();
if (brightness >= 205) {
brightness = 255;
} else {
brightness += 50;
}
Serial.print("Setting brightness to ");
Serial.println(brightness);
pixels.setBrightness(brightness);
pixels.show();
}
void dimmer() {
uint8_t brightness = pixels.getBrightness();
if (brightness <= 50) {
brightness = 5;
} else {
brightness -= 50;
}
Serial.print("Setting brightness to ");
Serial.println(brightness);
pixels.setBrightness(brightness);
pixels.show();
}
void showColor(uint32_t color) {
lastColor = color;
for (int i = 0; i < NUMPIXELS; i++) {
pixels.setPixelColor(i, color);
}
pixels.show();
}