-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathCorePlay.ino
333 lines (277 loc) · 7.39 KB
/
CorePlay.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#include <SD.h>
#include <DmxSimple.h> // http://www.pjrc.com/teensy/td_libs_DmxSimple.html
#include <LiquidCrystal.h>
#include <Entropy.h> // http://code.google.com/p/avr-hardware-random-number-generation/
#define MAX_DMX_CHANNELS 250
const int chipSelect = 0;
char buffer[MAX_DMX_CHANNELS*2+4];
LiquidCrystal lcd(12, 13, 16, 17, 18, 19);
unsigned int numfiles=0;
bool sdok=false;
void setup()
{
//Serial1.begin(115200);
//Serial1.println("******************");
// LCD display to show status
lcd.begin(16, 2);
lcd.clear();
lcd.print("CORE Lighting");
lcd.setCursor(0, 1);
lcd.print("Initializing...");
Serial.begin(115200);
// initialize the random number generator
Entropy.Initialize();
// five PWM pins to show first 5 channels
analogWrite(4, 0);
analogWrite(5, 0);
analogWrite(9, 0);
analogWrite(15, 0);
analogWrite(14, 0);
// DMX library transmits on pin 8
DmxSimple.usePin(8);
DmxSimple.maxChannel(MAX_DMX_CHANNELS);
for (int i=1; i<=MAX_DMX_CHANNELS; i++) {
DmxSimple.write(i, 0);
}
// initialize the SD card
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);
if (SD.begin(chipSelect)) {
sdok = true;
// and count the number of .TXT files
File dir = SD.open("/");
while (dir) {
File f = dir.openNextFile();
if (!f) break;
//Serial.print("File: ");
//Serial.println(f.name());
if (stringEndsWith(f.name(), ".txt") && !f.isDirectory()) {
numfiles = numfiles + 1;
}
f.close();
}
dir.close();
}
//Serial.print("numfiles = ");
//Serial.println(numfiles);
digitalWrite(LED_BUILTIN, LOW);
// unused pins should have pullups
pinMode(6, INPUT_PULLUP);
pinMode(7, INPUT_PULLUP);
pinMode(10, INPUT_PULLUP);
pinMode(20, INPUT_PULLUP);
pinMode(21, INPUT_PULLUP);
pinMode(22, INPUT_PULLUP);
pinMode(23, INPUT_PULLUP);
pinMode(24, INPUT_PULLUP);
Serial1.println("setup end");
}
void loop()
{
char filename[20];
if (sdok) {
// randomly choose the next file to play
if (numfiles < 1) return;
filename[0] = 0;
//int num = random(numfiles);
int num = Entropy.random(numfiles);
//Serial.print("random number = ");
//Serial.println(num);
File dir = SD.open("/");
dir.rewindDirectory();
while (1) {
File f = dir.openNextFile();
if (!f) break;
if (stringEndsWith(f.name(), ".txt") && !f.isDirectory()) {
if (num == 0) {
strcpy(filename, f.name());
f.close();
break;
} else {
num = num - 1;
f.close();
}
}
}
dir.close();
// play the file
if (filename[0]) {
//Serial.print("Play file: ");
//Serial.println(filename);
play(filename);
}
}
// if somebody is sending data, better use it...
if (Serial.available()) {
emulate_enttec_dmx();
}
// TODO: would be nice to detect if the SD card is removed
// and automatically recover, rather than requiring power cycle
}
void play(const char *filename)
{
lcd.clear();
lcd.print(filename);
File f = SD.open(filename);
if (f) {
unsigned long size = f.size();
// read the period so we know how fast to play
long period = f.parseInt();
f.readBytesUntil('\n', buffer, sizeof(buffer));
//Serial.print("Period is ");
//Serial.println(period);
if (period < 10 || period > 2500) return;
lcd.setCursor(0, 1);
lcd.print("Playing: ");
// then read every line and play it
elapsedMillis msec=0;
while (f.available()) {
f.readBytesUntil('\n', buffer, sizeof(buffer));
//Serial.print("Data: ");
//Serial.print(buffer);
int channels = hex2bin(buffer);
//Serial.print(", ");
//Serial.print(channels);
//Serial.println(" channels");
if (channels > 0) {
//transmit all the channels with DMX
DmxSimple.maxChannel(channels);
for (int i=0; i < channels; i++) {
DmxSimple.write(i+1, buffer[i]);
}
// display the first 5 channels on LEDs
analogWrite(4, buffer[0]);
analogWrite(5, buffer[1]);
analogWrite(9, buffer[2]);
analogWrite(15, buffer[3]);
analogWrite(14, buffer[4]);
// update the LCD
lcd.setCursor(9, 1);
lcd.print((float)f.position() * 100.0 / (float)size);
lcd.print("%");
// wait for the required period
while (msec < period) {
// wait
if (Serial.available()) {
// ANY incoming data immediately stops
// the currently playing file.
f.close();
return;
}
}
msec = msec - period;
}
}
f.close();
}
}
byte hexdigit(char c)
{
if (c >= '0' && c <= '9') return c - '0';
if (c >= 'A' && c <= 'F') return c - 'A' + 10;
return 255;
}
int hex2bin(char *buf)
{
byte b1, b2;
int i=0, count=0;
while (1) {
b1 = hexdigit(buf[i++]);
if (b1 > 15) break;
b2 = hexdigit(buf[i++]);
if (b2 > 15) break;
buf[count++] = b1 * 16 + b2;
}
return count;
}
byte stringEndsWith(const char *str, const char *end)
{
int i, len, elen;
len = strlen(str);
elen = strlen(end);
if (len < elen) return 0;
for (i=0; i < elen; i++) {
char c1, c2;
c1 = str[len - elen + i];
c2 = end[i];
if (c1 >= 'a' && c1 <= 'z') c1 = toupper(c1);
if (c2 >= 'a' && c2 <= 'z') c2 = toupper(c2);
if (c1 != c2) return 0;
}
return 1;
}
// emulate a subset of the Entec DMX USB Pro
//
#define STATE_START 0
#define STATE_LABEL 1
#define STATE_LEN_LSB 2
#define STATE_LEN_MSB 3
#define STATE_DATA 4
#define STATE_END 5
elapsedMillis timeout = 0;
void emulate_enttec_dmx(void)
{
byte state = STATE_START;
byte label;
unsigned int index, count;
byte b;
lcd.setCursor(0, 1);
lcd.print("Control from USB");
while (1) {
while (!Serial.available()) {
// 0.5 seconds without data, reset state
if (timeout > 500) state = STATE_START;
// 20 seconds without data, revert to playing files
if (timeout > 20000) return;
}
timeout = 0;
b = Serial.read();
switch (state) {
case STATE_START:
if (b == 0x7E) state = STATE_LABEL;
break;
case STATE_LABEL:
label = b;
state = STATE_LEN_LSB;
break;
case STATE_LEN_LSB:
count = b;
state = STATE_LEN_MSB;
break;
case STATE_LEN_MSB:
count |= (b << 8);
index = 0;
if (count > 0) {
state = STATE_DATA;
} else {
state = STATE_END;
}
break;
case STATE_DATA:
if (index < sizeof(buffer)) {
buffer[index++] = b;
}
count = count - 1;
if (count == 0) state = STATE_END;
break;
case STATE_END:
if (b == 0xE7 && label == 6 && index > 1) {
count = index;
if (count > MAX_DMX_CHANNELS) count = MAX_DMX_CHANNELS;
// display the first 5 channels on LEDs
if (count >= 1) analogWrite(4, buffer[1]);
if (count >= 2) analogWrite(5, buffer[2]);
if (count >= 3) analogWrite(9, buffer[3]);
if (count >= 4) analogWrite(15, buffer[4]);
if (count >= 5) analogWrite(14, buffer[5]);
//Serial1.printf("%02X%02X%02X\r\n", buffer[3]&255, buffer[4]&255, buffer[5]&255);
for (index=1; index <= count; index++) {
DmxSimple.write(index, buffer[index]);
}
}
default:
state = STATE_START;
break;
}
}
}