-
Notifications
You must be signed in to change notification settings - Fork 5
/
TASS.ino
573 lines (498 loc) · 17.1 KB
/
TASS.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
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
// TASS Control Software by John Ackermann N8UR.
// - Features added by Matthew J Wolf, N4MTT
// Copyright (c) 2020 John Ackermann
// Copyright (c) 2018 Matthew J. Wolf
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files
// (the "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
// OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//
// Author: Matthew J. Wolf <[email protected]>
// Date: 18-NOV-2018
//
// - Version 0.7-n4mtt
// = Many changes
// = Added the I command on USB and Ethernet
// = Added support for WIZnet W5500
// -= Renamed source file TASS_W5100.ino to TASS_ethernet.ino
// = Added defines
// -= USE_W5500
// -= SHOW_INFO_USB
// -= SHOW_INFO_ETHERNET
// -= USE_EEPROM
// = No changes to the LED 8x8 display.
char version[] = "v0.80 -- 6 June 2020";
// BOF preprocessor bug prevent - insert me on top of your arduino-code
// From: http://www.a-control.de/arduino-fehler/?lang=en
#if 1
__asm volatile ("nop");
#endif
// uncomment to enable Adafruit 2.8 inch TFT and capacitive touchscreen
#define USE_TS
// uncomment to endable Adafruit 1.2 inch 8x8 LED matrix
//#define USE_LED8X8
// Uncomment to enable Wiznet W5100 ethernet shield.
// Only uncomment USE_W5100 or USE_W5500.
// !! DO NOT UNCOMMENT BOTH !!
//#define USE_W5100
// Uncomment to enable Wiznet W5500 ethernet shield.
// Only uncomment USE_W5100 or USE_W5500.
// !! DO NOT UNCOMMENT BOTH !!
//#define USE_W5500
// Store the S and H masks into EEPROM. Then after a reset
// or power on read the EEPROM to restore the relays.
#define USE_EEPROM
// comment this to set board up as single 8 pole using K9
//#define SPLIT
// uncomment this to display bitmasks on the serial console
#define SHOW_MASKS
// uncomment this to display relay status on the serial console
#define SHOW_INFO_USB
// uncomment this to display relay status on the serial console
//#define SHOW_INFO_ETHERNET
// uncomment to combine boards A and B into a single 16 port switch
// This causes boards A and B to act as one for "S"et commands. Setting
// a relay on either A or B will cause both boards to be cleared with the
// new relay then set. Relay K9 on both boards will follow the relay selection,
// so J16 on either board may be used as the common output.
// - NUM_TASS_BOARDS_DISPLAY_DISPLAY has to be larger than 1 for the affect of
// this define to be included in the displays.
//#define A_B_COMBINE
// uncomment to set board A to double pole mode
// This will cause setting or holding a relay on board D to
// also set its equivalent on the other bank, thus pairing
// 1-5, 2-6, 3-7, 4-8 to create a 4-throw double-pole switch
// - NUM_TASS_BOARDS_DISPLAY_DISPLAY has to be larger than 3 for the affect of
// this define to be included in the displays.
#define A_DOUBLE_POLE
/*****************************************************************************************************************/
#include "TASS_defines.h"
#ifdef USE_TS
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <Adafruit_FT6206.h>
#include <Fonts/FreeSans9pt7b.h>
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
Adafruit_FT6206 ctp = Adafruit_FT6206();
#endif // USE_TS
#ifdef USE_LED8X8
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_LEDBackpack.h>
Adafruit_8x8matrix matrix = Adafruit_8x8matrix();
#define HB_BLINK_MS 1000;
#endif // USE_LED8X8
#ifdef USE_W5100
#include <SPI.h>
#include <EthernetUdp.h>
#include <Dhcp.h>
#include <Dns.h>
#include <util.h>
#include <Ethernet.h>
#include <EthernetServer.h>
#include <EthernetClient.h>
#define USE_ETHERNET
#endif
#ifdef USE_W5500
#include <SPI.h>
#include <EthernetUdp2.h>
#include <Dhcp.h>
#include <Dns.h>
#include <util.h>
#include <Ethernet2.h>
#include <EthernetServer.h>
#include <EthernetClient.h>
#define USE_ETHERNET
#endif
#ifdef USE_EEPROM
#include <EEPROM.h>
#endif
#ifdef USE_ETHERNET
// variable
EthernetServer server = EthernetServer(23);
EthernetClient client = 0;
// ******************************************************
// If using ethernet, set network stuff here:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(10,73,3,177);
IPAddress gateway(10,73,1,1);
IPAddress subnet(255,255,0,0);
// what to echo to the telnet port, if anything
// 0 = no echo, 1 = echo raw command, 2 = interpreted output
int telnet_verbose = 2;
//*******************************************************
#endif
char board = ' ';
int relay = 0;
char cmd = ' ';
byte relay_num = 0;
char relay_char = ' ';
boolean newclient = 1;
boolean execute = 0;
boolean BadCmd = 0;
boolean K9 = 0;
// these keep the relay settings for each board across commands
byte S_mask_A = 0;
byte S_mask_B = 0;
byte S_mask_C = 0;
byte S_mask_D = 0;
// these keep the hold settings for each board across commands
byte H_mask_A = 0;
byte H_mask_B = 0;
byte H_mask_C = 0;
byte H_mask_D = 0;
// these hold the current status (set + hold) for each board
byte cmd_mask_A = 0;
byte cmd_mask_B = 0;
byte cmd_mask_C = 0;
byte cmd_mask_D = 0;
// setup for serial input
const char startChar = '<';
const char endChar = '>';
char dataBuffer[DATABUFFERSIZE+1]; //Add 1 for NULL terminator
// Arrays for status
bool state_a[8];
bool state_b[8];
bool state_c[8];
bool state_d[8];
void setup(void) {
usb_setup();
setup_board();
#ifdef USE_TS
tft_setup();
#endif
#ifdef USE_LED8X8
led8x8_setup();
#endif
#ifdef USE_ETHERNET
ethernet_setup();
#endif
} // setup
void loop() {
BadCmd = 0;
#ifdef USE_LED8X8
heartbeat();
#endif
if (!execute) {
#ifdef USE_TS
get_ts_data();
#endif
#ifdef USE_ETHERNET
get_ethernet_data();
#endif
get_usb_data();
}
if (execute) {
board = toupper(dataBuffer[1]);
relay = atoi(&dataBuffer[2]);
cmd = toupper(dataBuffer[3]);
// set relay K9 to either upper or lower bus depending on relay selected
// K9 = 0 connects to the bus for relays 1-4
if (relay < 5) {
K9 = 0;
} else {
K9 = 1;
} // set K9
byte cmd_mask = 0;
// start of command == "S"
if (cmd == 'S') {
// first, clear mask
#ifndef A_B_COMBINE
switch (board) {
case 'A':
S_mask_A = clear_S_mask(relay,S_mask_A);
break;
case 'B':
S_mask_B = clear_S_mask(relay,S_mask_B);
break;
case 'C':
S_mask_C = clear_S_mask(relay,S_mask_C);
break;
case 'D': S_mask_D = clear_S_mask(relay,S_mask_D); break;
} // switch
#else // if A_B_COMBINE, clear both sides of A and B
if ((board == 'A') || (board == 'B')) {
S_mask_A = clear_mask(S_mask_A);
S_mask_B = clear_mask(S_mask_B);
}
switch (board) {
case 'C':
S_mask_C = clear_S_mask(relay,S_mask_C);
break;
case 'D':
S_mask_D = clear_S_mask(relay,S_mask_D);
break;
} // switch
#endif
// then set the relay mask
#ifndef A_B_COMBINE
switch (board) {
case 'A':
S_mask_A = set_mask(relay,S_mask_A);
break;
case 'B':
S_mask_B = set_mask(relay,S_mask_B);
break;
case 'C':
S_mask_C = set_mask(relay,S_mask_C);
break;
case 'D': S_mask_D = set_mask(relay,S_mask_D);
break;
} // switch
#else // if A_B_COMBINE
switch (board) {
case 'A':
S_mask_A = set_mask(relay,S_mask_A);
set_mask(0,S_mask_B);
break;
case 'B':
S_mask_B = set_mask(relay,S_mask_B);
set_mask(0,S_mask_A);
break;
case 'C':
S_mask_C = set_mask(relay,S_mask_C);
break;
case 'D':
S_mask_D = set_mask(relay,S_mask_D);
break;
} // switch
#endif
// if using board A as double pole, take the final mask and pair relays
// to pair 1&5, 2&6, 3&7, 4&8. Works both ways, so you can select
// either the high or low relay to get the pair
#ifdef A_DOUBLE_POLE
if ((board == 'A') && (relay != 0)) {
if (relay < 5) {
S_mask_A = set_mask(relay + 4,S_mask_A);
} else {
S_mask_A = set_mask(relay - 4,S_mask_A);
}
}
#endif // A_DOUBLE_POLE
}
// end of command == "S"
// start of command == "U"
if (cmd == 'U') {
// clear H mask
switch (board) {
case 'A':
H_mask_A = clear_mask(H_mask_A);
break;
case 'B':
H_mask_B = clear_mask(H_mask_B);
break;
case 'C':
H_mask_C = clear_mask(H_mask_C);
break;
case 'D':
H_mask_D = clear_mask(H_mask_D);
break;
} // switch
// special case - relay 0 means clear both set and hold masks
// don't use "clear_set_mask" because we want to wipe both halves
if (relay == 0) {
// clear S mask
switch(board) {
case 'A':
S_mask_A = clear_mask(S_mask_A);
#ifdef USE_EEPROM
EEPROM.update(0,S_mask_A);
EEPROM.update(4,H_mask_A);
#endif
break;
case 'B':
S_mask_B = clear_mask(S_mask_B);
#ifdef USE_EEPROM
EEPROM.update(1,S_mask_A);
EEPROM.update(5,H_mask_A);
#endif
break;
case 'C':
S_mask_C = clear_mask(S_mask_C);
#ifdef USE_EEPROM
EEPROM.update(2,S_mask_A);
EEPROM.update(6,H_mask_A);
#endif
break;
case 'D':
S_mask_D = clear_mask(S_mask_D);
#ifdef USE_EEPROM
EEPROM.update(1,S_mask_A);
EEPROM.update(7,H_mask_A);
#endif
break;
} // switch
} // if relay == 0
} // if
// end of command == "U"
// start of command == "H"
if (cmd == 'H') {
// set appropriate hold mask
switch (board) {
case 'A':
H_mask_A = set_mask(relay,H_mask_A);
break;
case 'B':
H_mask_B = set_mask(relay,H_mask_B);
break;
case 'C':
H_mask_C = set_mask(relay,H_mask_C);
break;
case 'D':
H_mask_D = set_mask(relay,H_mask_D);
break;
} // switch
// if using board A as double pole, take the final mask and pair relays
// to pair 1&5, 2&6, 3&7, 4&8. Works both ways, so you can select
// either the high or low relay to get the pair
#ifdef A_DOUBLE_POLE
if ((board == 'A') && (relay != 0)) {
if (relay < 5) {
H_mask_A = set_mask(relay + 4,H_mask_A);
} else {
H_mask_A = set_mask(relay - 4,H_mask_A);
}
}
#endif // A_DOUBLE_POLE
} // if
// end of command == "H"
if (cmd == 'I') {
switch (board) {
case 'A':
if (relay == 0) {
info_usb_board(board,A_1,H_mask_A);
#ifdef USE_ETHERNET
info_ethernet_board(board,A_1,H_mask_A);
#endif
}
else {
info_usb_relay(board,relay,A_1,H_mask_A);
#ifdef USE_ETHERNET
info_ethernet_relay(board,relay,A_1,H_mask_A);
#endif
}
break;
case 'B':
if (relay == 0) {
info_usb_board(board,B_1,H_mask_B);
#ifdef USE_ETHERNET
info_ethernet_board(board,B_1,H_mask_B);
#endif
}
else {
info_usb_relay(board,relay,B_1,H_mask_B);
#ifdef USE_ETHERNET
info_ethernet_relay(board,relay,B_1,H_mask_B);
#endif
}
break;
case 'C':
if (relay == 0) {
info_usb_board(board,C_1,H_mask_C);
#ifdef USE_ETHERNET
info_ethernet_board(board,C_1,H_mask_C);
#endif
}
else {
info_usb_relay(board,relay,C_1,H_mask_C);
#ifdef USE_ETHERNET
info_ethernet_relay(board,relay,C_1,H_mask_C);
#endif
}
break;
case 'D':
if (relay == 0) {
info_usb_board(board,D_1,H_mask_D);
#ifdef USE_ETHERNET
info_ethernet_board(board,D_1,H_mask_D);
#endif
}
else {
info_usb_relay(board,relay,D_1,H_mask_D);
#ifdef USE_ETHERNET
info_ethernet_relay(board,relay,D_1,H_mask_D);
#endif
}
break;
}
}
// create the mask that will be written to board by combining hold and set masks
cmd_mask_A = H_mask_A | S_mask_A;
cmd_mask_B = H_mask_B | S_mask_B;
cmd_mask_C = H_mask_C | S_mask_C;
cmd_mask_D = H_mask_D | S_mask_D;
// write to appropriate board
// if A_B_COMBINE, write to both boards
#ifndef A_B_COMBINE
switch (board) {
case 'A':
write_board(A_1,cmd_mask_A,K9);
break;
case 'B':
write_board(B_1,cmd_mask_B,K9);
break;
case 'C':
write_board(C_1,cmd_mask_C,K9);
break;
case 'D':
write_board(D_1,cmd_mask_D,K9);
break;
} // switch
#else
if ((board == 'A') || (board == 'B')) {
write_board(A_1,cmd_mask_A,K9);
write_board(B_1,cmd_mask_B,K9);
}
switch (board) {
case 'C': write_board(C_1,cmd_mask_C,K9); break;
case 'D': write_board(D_1,cmd_mask_D,K9); break;
} // switch
#endif
// work is done, now update status for USB and other installed IO methods
usb_print_status();
// Save the relay masks to EEPROM
#ifdef USE_EEPROM
EEPROM.update(0,S_mask_A);
EEPROM.update(1,S_mask_B);
EEPROM.update(2,S_mask_C);
EEPROM.update(3,S_mask_D);
EEPROM.update(4,H_mask_A);
EEPROM.update(5,H_mask_B);
EEPROM.update(6,H_mask_C);
EEPROM.update(7,H_mask_D);
#endif
#ifdef USE_TS
draw_status();
#endif
#ifdef USE_LED8X8
draw_LED_status();
#endif
#ifdef SHOW_INFO_ETHERNET
ethernet_display_info(NUM_TASS_BOARDS_DISPLAY);
#endif
// Reset Varibles after the command is executed.
board = ' ';
relay = 0;
cmd = ' ';
execute = 0;
} // execute
} // of loop