-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDMX_Tester.c
763 lines (725 loc) · 17.2 KB
/
DMX_Tester.c
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
// DMX Tester
// Author: Tony Keith
// Description: DMX Tester
//
// Supports the following input protocol:
// XXX@III#
// XXX-XXX@III#
// *@*#
// XXX@*#
// XXX-*@III#
// XXX-*@*#
//
// XXX = 1 to 512 Channel
// III = 1 to 256 Intensity
// * = Wildcard value: 512 for channel and 256 for Full intensity
//
// A = @ (at sign)
// D = - (dash)
// C = Clear
// # = Execute
// B = Bump (no supported)
// Comment out for Serial but not DMX
//#define SERIAL_DEBUG_ENABLED
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#include <Wire.h>
// Comment out for Serial - include for DMX
#include <Conceptinetics.h>
// States
#define INITIAL 1
#define START1 2
#define START2 3
#define START3 4
#define END1 5
#define END2 6
#define END3 7
#define ATSIGN 8
#define WILDCARD1 9
#define WILDCARD2 10
#define INTENSITY1 12
#define INTENSITY2 13
#define INTENSITY3 14
#define POUNDSIGN 15
#define EXECUTE 16
#define CLEAR 17
#define BUMP 18
const byte ROWS = 4; // define four rows
const byte COLS = 4; // define four
char keys [ROWS] [COLS] = {
{'1', '2', '3','@'},
{'4', '5', '6','B'},
{'7', '8', '9','C'},
{'*', '0', '#','-'}
};
// 16 switches on PC board keypad
// Pin R/C Port
// 8 C4 13
// 7 C3 12
// 6 C2 11
// 5 C1 10
// 4 R1 6
// 3 R2 7
// 2 R3 8
// 1 R4 9
// 4x4 membrane keypad
// Pin R/C Port
// 8 C4 13
// 7 C3 12
// 6 C2 11
// 5 C1 10
// 4 R4 9
// 3 R3 8
// 2 R2 7
// 1 R1 6
// Connect 4 * 4 keypad row-bit port, the corresponding digital IO ports panel
byte rowPins [ROWS] = {6,7,8,9};
// Connect 4 * 4 buttons faithfully port, the corresponding digital IO ports panel
byte colPins [COLS] = {10,11,12,13};
// Call the function library function Keypad
Keypad keypad = Keypad (makeKeymap (keys), rowPins, colPins, ROWS, COLS);
LiquidCrystal_I2C lcd(0x3F,20,4);
#define MAXCMDBUFFER 10
static char buffer[MAXCMDBUFFER];
static int index=0;
static int pos = 0;
static int startChannel = 0;
static int endChannel = 0;
static int intensity = -1;
static int state=INITIAL;
#define VERSION "V0.30"
#define MIN_START_CHANNEL 1
#define MAX_END_CHANNEL 512
#define MIN_INTENSITY 0
#define MAX_INTENSITY 255
#ifndef SERIAL_DEBUG_ENABLED
#define DMX_MASTER_CHANNELS 512
// Pin number to change read or write mode on the shield
#define RXEN_PIN 2
// Configure a DMX master controller, the master controller
// will use the RXEN_PIN to control its write operation
// on the bus
DMX_Master dmx_master ( DMX_MASTER_CHANNELS, RXEN_PIN );
#endif
void setup () {
lcd.init();
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("DMX Tester ");
lcd.print(VERSION);
lcd.setCursor(0,1);
lcd.print("Enter Cmd:");
// Serial or DMX but not both
#ifdef SERIAL_DEBUG_ENABLED
Serial.begin (9600);
#else
dmx_master.enable ();
#endif
}
void loop () {
int tmpState;
char key = keypad.getKey ();
if (key != NO_KEY) {
// Clear
if(key == 'C') {
state = CLEAR;
}
// Bump - Not Implemented
if(key == 'B') {
tmpState = state;
state = BUMP;
}
displayState(state);
switch(state) {
case INITIAL:
// X
if(validKeys0to9(key)) {
pos = displayKey(key, pos);
storeKey(key);
state = START1;
} else if (validWildCard(key)) {
// *@
pos = displayKey(key, pos);
state = WILDCARD1;
} else {
invalidFormat();
clearAll();
}
break;
case START1:
// XX
if(validKeys0to9(key)) {
pos = displayKey(key, pos);
storeKey(key);
state = START2;
} else if (validDash(key)) {
// X-
state = END1;
pos = displayKey(key, pos);
startChannel = getInt();
if(! validateChannel(startChannel)) {
invalidStartChannel();
clearAll();
} else {
clearBuffer();
}
} else if(validAtSign(key)) {
// X@
state = INTENSITY1;
pos = displayKey(key, pos);
startChannel = getInt();
if(! validateChannel(startChannel)) {
invalidStartChannel();
clearAll();
} else {
endChannel = startChannel;
clearBuffer();
}
} else {
invalidFormat();
clearAll();
}
break;
case START2:
// XXX
if(validKeys0to9(key)) {
pos = displayKey(key, pos);
storeKey(key);
state = START3;
} else if (validDash(key)) {
// XX-
state = END1;
pos = displayKey(key, pos);
startChannel = getInt();
if(! validateChannel(startChannel)) {
invalidStartChannel();
clearAll();
} else {
clearBuffer();
}
} else if(validAtSign(key)) {
// XX@
state = INTENSITY1;
pos = displayKey(key, pos);
startChannel = getInt();
endChannel = startChannel;
clearBuffer();
} else {
invalidFormat();
clearAll();
}
break;
case START3:
if (validDash(key)) {
// XXX-
state = END1;
pos = displayKey(key, pos);
startChannel = getInt();
if(! validateChannel(startChannel)) {
invalidStartChannel();
clearAll();
} else {
clearBuffer();
}
} else if(validAtSign(key)) {
// XXX@
state = INTENSITY1;
pos = displayKey(key, pos);
startChannel = getInt();
if(! validateChannel(startChannel)) {
invalidStartChannel();
clearAll();
} else {
endChannel = startChannel;
clearBuffer();
}
} else {
invalidFormat();
clearAll();
}
break;
case END1:
// X-X, XX-X, XXX-X
if(validKeys0to9(key)) {
pos = displayKey(key, pos);
storeKey(key);
state = END2;
} else if (validWildCard(key)) {
// X-*, XX-*, XXX-*
state = WILDCARD2;
pos = displayKey(key, pos);
endChannel = MAX_END_CHANNEL;
} else {
invalidFormat();
clearAll();
}
break;
case END2:
// X-XX, XX-XX, XXX-XX
if(validKeys0to9(key)) {
pos = displayKey(key, pos);
storeKey(key);
state = END3;
} else if (validAtSign(key)) {
// X-X@, XX-X@, XXX-X@
state = INTENSITY1;
pos = displayKey(key, pos);
// endChannel = X
endChannel = getInt();
// validate endChannel
if(! validateChannel(endChannel) || ! validateChannelRange(startChannel, endChannel)) {
invalidEndChannel();
clearAll();
} else {
clearBuffer();
}
} else {
invalidFormat();
clearAll();
}
break;
case END3:
// X-XXX, XX-XXX, XXX-XXX
if(validKeys0to9(key)) {
pos = displayKey(key, pos);
storeKey(key);
state = ATSIGN;
} else if (validAtSign(key)) {
// X-XX@, XX-XX@, XXX-XX@
state = INTENSITY1;
pos = displayKey(key, pos);
// endChannel = XX
endChannel = getInt();
// validate End Channel
if(! validateChannel(endChannel) || ! validateChannelRange(startChannel, endChannel)) {
invalidEndChannel();
clearAll();
} else {
clearBuffer();
}
} else {
invalidFormat();
clearAll();
}
break;
case ATSIGN:
// X-XXX@, XX-XXX@, XXX-XXX@
if(validAtSign(key)) {
state = INTENSITY1;
pos = displayKey(key, pos);
// endChannel = XX
endChannel = getInt();
// validate End Channel
if(! validateChannel(endChannel)) {
invalidEndChannel();
clearAll();
} else {
clearBuffer();
}
} else {
invalidFormat();
clearAll();
}
break;
case WILDCARD1:
// *@
if(validAtSign(key)) {
state = INTENSITY1;
pos = displayKey(key, pos);
startChannel = MIN_START_CHANNEL;
endChannel = MAX_END_CHANNEL;
} else {
invalidFormat();
clearAll();
}
break;
case WILDCARD2:
// X-*@, XX-*@, XXX-*@
if(validAtSign(key)) {
state = INTENSITY1;
pos = displayKey(key, pos);
endChannel = MAX_END_CHANNEL;
} else {
invalidFormat();
clearAll();
}
break;
case INTENSITY1:
// @X
if(validKeys0to9(key)) {
pos = displayKey(key, pos);
storeKey(key);
state = INTENSITY2;
} else if (validWildCard(key)) {
// X-XX@*, XX-XX@*, XXX-XX@*
state = POUNDSIGN;
pos = displayKey(key, pos);
// intensity = MAX
intensity = MAX_INTENSITY;
// validate Intensity
if(! validateIntensity(intensity)) {
invalidIntensity();
clearAll();
} else {
clearBuffer();
}
} else {
invalidFormat();
clearAll();
}
break;
case INTENSITY2:
// @XX
if(validKeys0to9(key)) {
pos = displayKey(key, pos);
storeKey(key);
state = INTENSITY3;
} else if (validPoundSign(key)) {
// X-XX@XI#, XX-XX@I#, XXX-XX@I#
state = EXECUTE;
pos = displayKey(key, pos);
// intensity = I
intensity = getInt();
// validate Intensity
if(! validateIntensity(intensity)) {
invalidIntensity();
clearAll();
} else {
clearBuffer();
}
} else {
invalidFormat();
clearAll();
}
break;
case INTENSITY3:
// @XXX
if(validKeys0to9(key)) {
state = POUNDSIGN;
pos = displayKey(key, pos);
storeKey(key);
intensity = getInt();
// validate Intensity
if(! validateIntensity(intensity)) {
invalidIntensity();
clearAll();
} else {
clearBuffer();
}
} else if (validPoundSign(key)) {
// @XXX#, @XXX#, @XXX#
state = EXECUTE;
pos = displayKey(key, pos);
// intensity = XXX
intensity = getInt();
// validate Intensity
if(! validateIntensity(intensity)) {
invalidIntensity();
clearAll();
} else {
clearBuffer();
}
} else {
invalidFormat();
clearAll();
}
break;
case POUNDSIGN:
if (validPoundSign(key)) {
state = EXECUTE;
} else {
invalidFormat();
clearAll();
}
break;
case CLEAR:
clearAll();
break;
case BUMP:
state = tmpState;
break;
default:
break;
} // swtich
#ifdef SERIAL_DEBUG_ENABLED
Serial.print("Key:");
Serial.print(key);
Serial.print(" Idx:");
Serial.print(index);
Serial.print(" S:");
Serial.print(startChannel);
Serial.print(" E:");
Serial.print(endChannel);
Serial.print(" I:");
Serial.println(intensity);
#endif
// EXECUTE
if(state == EXECUTE) {
sendDMX(startChannel, endChannel, intensity);
clearAll();
}
} // if
} // loop
// Send DMX
void sendDMX(int start, int end, unsigned char intensity) {
lcd.setCursor(0,2);
lcd.print("S:");
lcd.print(start);
lcd.print(" E:");
lcd.print(end);
lcd.print(" I:");
lcd.print(intensity);
delay(2000);
#ifdef SERIAL_DEBUG_ENABLED
#else
// add DMX control
if(start == end) {
dmx_master.setChannelValue(start, intensity);
} else {
dmx_master.setChannelRange(start, end, intensity );
}
#endif
return;
}
// Clears resets everything
void clearAll(void) {
buffer[0] = '\0';
index=0;
pos=0;
startChannel = 0;
endChannel = 0;
intensity = -1;
state = INITIAL;
clearDisplay();
}
// Display key on Cmd line
int displayKey(char key, int pos) {
lcd.setCursor(pos,2);
lcd.print(key);
pos++;
return(pos);
}
// Store key in buffer
void storeKey(char key) {
buffer[index] = key;
index++;
}
// Clear buffer
void clearBuffer(void) {
buffer[0] = '\0';
index=0;
}
// Convert buffer into Integer
int getInt() {
buffer[index] = '\0';
return(atoi(buffer));
}
// Display state name
void displayState(int state) {
#ifdef SERIAL_DEBUG_ENABLED
char stateName[15];
switch(state) {
case INITIAL:
strcpy(stateName, "INITIAL");
break;
case START1:
strcpy(stateName, "START1");
break;
case START2:
strcpy(stateName, "START2");
break;
case START3:
strcpy(stateName, "START3");
break;
case END1:
strcpy(stateName, "END1");
break;
case END2:
strcpy(stateName, "END2");
break;
case END3:
strcpy(stateName, "END3");
break;
case ATSIGN:
strcpy(stateName, "ATSIGN");
break;
case WILDCARD1:
strcpy(stateName, "WILDCARD1");
break;
case WILDCARD2:
strcpy(stateName, "WILDCARD2");
break;
case INTENSITY1:
strcpy(stateName, "INTENSITY1");
break;
case INTENSITY2:
strcpy(stateName, "INTENSITY2");
break;
case INTENSITY3:
strcpy(stateName, "INTENSITY3");
break;
case POUNDSIGN:
strcpy(stateName, "POUNDSIGN");
break;
case EXECUTE:
strcpy(stateName, "EXECUTE");
break;
case CLEAR:
strcpy(stateName, "CLEAR");
break;
case BUMP:
strcpy(stateName, "BUMP");
break;
default:
strcpy(stateName, "UNKNOWN");
break;
} // switch
Serial.print("State:");
Serial.println(stateName);
#endif
}
// Validate channel
// return true if valid, else false
int validateChannel(int channel) {
int valid=1;
if(channel < MIN_START_CHANNEL || channel > MAX_END_CHANNEL) {
valid = 0;
}
return(valid);
}
// Validate channel range
// retun true if valid, else false
int validateChannelRange(int startCh, int endCh) {
int valid=1;
if(startCh < MIN_START_CHANNEL || startCh > MAX_END_CHANNEL) {
valid = 0;
} else if(endCh <= MIN_START_CHANNEL || endCh > MAX_END_CHANNEL) {
valid = 0;
} else if(startCh > endCh) {
valid = 0;
}
return(valid);
}
// Validate intensity
// return true if valid, else false
int validateIntensity(int intensity) {
int valid=1;
if(intensity < MIN_INTENSITY || intensity > MAX_INTENSITY) {
valid = 0;
}
return(valid);
}
// Clear the Cmd and execute area of the display
void clearDisplay(void) {
lcd.setCursor(0,2);
lcd.print(" ");
lcd.setCursor(0,3);
lcd.print(" ");
}
// Display invalid intensity error
void invalidIntensity(void) {
lcd.setCursor(0,2);
lcd.print("Invalid Intensity:");
lcd.setCursor(0,3);
lcd.print("0-255 or *");
delay(2000);
clearDisplay();
}
// Display invalid start channel error
void invalidStartChannel(void) {
lcd.setCursor(0,2);
lcd.print("Invalid Start Chan:");
lcd.setCursor(0,3);
lcd.print("1-512");
delay(2000);
clearDisplay();
}
// Display invalid end channel error
void invalidEndChannel(void) {
lcd.setCursor(0,2);
lcd.print("Invalid End Chan:");
lcd.setCursor(0,3);
lcd.print("1-512 or *");
delay(2000);
clearDisplay();
}
// Display format error
void invalidFormat(void) {
lcd.setCursor(0,2);
lcd.print("Invalid Format:");
lcd.setCursor(0,3);
lcd.print("CCC@PPP");
delay(1000);
lcd.setCursor(0,3);
lcd.print("CCC-CCC@PPP");
delay(1000);
lcd.setCursor(0,3);
lcd.print("use * for CCC or PPP");
delay(1000);
clearDisplay();
}
// Validate keys: * (asterisk)
// return true if valid, else false
int validWildCard(int key) {
int valid=0;
switch(key) {
case '*':
valid=1;
break;
}
return valid;
}
// Validate keys: @ (at sign)
// return true if valid, else false
int validAtSign(int key) {
int valid=0;
switch(key) {
case '@':
valid=1;
break;
}
return valid;
}
// Validate keys: - (dash)
// return true if valid, else false
int validDash(int key) {
int valid=0;
switch(key) {
case '-':
valid=1;
break;
}
return valid;
}
// Validate keys: # (pound sign)
// return true if valid, else false
int validPoundSign(int key) {
int valid=0;
switch(key) {
case '#':
valid=1;
break;
}
return valid;
}
// Validate keys: 0-9
// return true if valid, else false
int validKeys0to9(int key) {
int valid=0;
switch(key) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
valid=1;
break;
}
return valid;
}
// End of File