-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathArDNC.ino
447 lines (383 loc) · 9.72 KB
/
ArDNC.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
/*
ArDNC2
Created Mar 2015
by Mike van Leeuwen, Dipl. T.
for O'Hara Machine & Tool Inc.
SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 10
The RS232 attaches as follows:
** TX - pin 1 (out)
** RC - pin 0 (in)
** CTS - pin 2 (in)
** RTS - pin 3 (out)
*/
#include <SD.h>
// include the display library code:
#include <Wire.h>
#include <Adafruit_RGBLCDShield.h>
Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield();
// These #defines make it easy to set the backlight color
#define OFF 0x0
#define RED 0x1
#define GREEN 0x2
#define YELLOW 0x3
#define BLUE 0x4
#define VIOLET 0x5
#define TEAL 0x6
#define WHITE 0x7
// XON and XOFF #defines
#define XON 0x11
#define XOFF 0x13
#define XON2 0x12
#define XOFF2 0x14
// CTS and RTS pin
#define CTS 2
#define RTS 3
//Configuration settings
char cfgname[16] = "NO-NAME";
char cfgfilename[16] = "";
char cfgdir[32] = "/";
int cfgbaud = 2400;
int cfgdata = 8;
int cfgpar = 0;
int cfgstop = 1;
int cfgsoft = 1;
int cfghard = 0;
int cfgwait = 0;
int cfgcomdebug = 0;
int cfghead = 0;
int cfghrepeat = 0;
int cfgsendperc = 0;
void setup() {
//Initialize CTS and RTS pins
pinMode(CTS, INPUT);
pinMode(RTS, OUTPUT);
digitalWrite(RTS, LOW);
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
lcd.clear();
lcd.print(F("Initializing... "));
lcd.setBacklight(WHITE);
delay(2000); // Gives time to read the screen.
//Initializing SD Card
pinMode(10, OUTPUT);
if (!SD.begin(10)) {
lcd.clear();
lcd.print(F("SD Card init "));
lcd.setCursor(0, 1);
lcd.print(F("FAILED"));
lcd.setBacklight(RED);
return;
}
readcfg();
}
void loop()
{
choosefile();
}
void choosefile() {
uint8_t buttons;
int index=-1;
int i=0;
File root;
lcd.clear();
lcd.print(F("Select File:"));
lcd.setBacklight(WHITE);
root = SD.open(cfgdir);
while(true) {
File entry = root.openNextFile();
index++;
if(! entry) {
if(index==0){
//Directory is empty
lcd.clear();
lcd.print(F("ERROR. Dir not"));
lcd.setCursor(0, 1);
lcd.print(F("found or empty."));
lcd.setBacklight(RED);
while(true){
}
}
index=-1;
entry.close();
root.close();
return;
}
lcd.setCursor(0, 1);
lcd.print(entry.name());
lcd.print(F(" ")); //cleans rest of the line
//////////////////////////////////////////////////////////
// Available Memory Checker //
//////////////////////////////////////////////////////////
// lcd.setCursor(0,0);
// lcd.print(freeRam());
delay(500);
while(true){
buttons = lcd.readButtons();
if(buttons) {
if(buttons & BUTTON_DOWN){
break;
}
if(buttons & BUTTON_RIGHT){
strcpy(cfgfilename, entry.name());
while (true){
sendfile();
lcd.clear();
lcd.print(F("Select to resend"));
lcd.setCursor(0, 1);
lcd.print(F("Back = file menu"));
lcd.setBacklight(WHITE);
while(true){
buttons = lcd.readButtons();
if(buttons) {
if(buttons & BUTTON_RIGHT){
break;
}
if(buttons & BUTTON_LEFT){
entry.close();
root.close();
return;
}
}
}
}
break;
}
if(buttons & BUTTON_UP){
if(index>0){
entry.close();
root.close();
root = SD.open(cfgdir);
for(i=1; i<index; i++){
File entry = root.openNextFile();
entry.close();
}
index=i-2;
break;
}
}
}
}
entry.close();
}
}
void readcfg()
{
char character;
char desc[16] = "";
char valu[16] = "";
int editvalue = 0;
int i = 0;
File cfg = SD.open("config.cfg");
if(cfg) {
while (cfg.available()) {
character = cfg.read();
if(character == '#') {
//Comment - ignore this line
while(character != '\n' && cfg.available()){
character = cfg.read();
}
i++;
lcd.setCursor(0,1);
lcd.print(i);
//if character is a letter, number, slash, period, or underscore
}else if(isalnum(character) || character == 47 || character == 46 || character == 31) {
if(editvalue == 1){
//add character to the value
append(valu, character);
}else{
//add character to the description
append(desc, character);
}
}else if(character == '='){
//description done now mod value
editvalue = 1;
}else if(character == '\n'){
i++;
lcd.setCursor(0,1);
lcd.print(i);
//end of line, reset editvalue and set proper global
editvalue = 0;
if (strcmp("NAME",desc) == 0){
strcpy(cfgname, valu);
} else if(strcmp("SPEED",desc) == 0){
cfgbaud=atoi(valu);
} else if(strcmp("DATA",desc) == 0){
cfgdata=atoi(valu);
} else if(strcmp("PAR", desc) == 0){
cfgpar=atoi(valu);
} else if(strcmp("STOP",desc) == 0){
cfgstop=atoi(valu);
} else if(strcmp("SOFT",desc) == 0){
cfgsoft=atoi(valu);
} else if(strcmp("HARD",desc) == 0){
cfghard=atoi(valu);
} else if(strcmp("WAIT",desc) == 0){
cfgwait=atoi(valu);
} else if(strcmp("FILE",desc) == 0){
strcpy(cfgfilename, valu);
} else if(strcmp("DIR",desc) == 0){
strcpy(cfgdir, valu);
} else if(strcmp("COMDEBUG",desc) == 0){
cfgcomdebug=atoi(valu);
} else if(strcmp("HEAD",desc) == 0){
cfghead=atoi(valu);
} else if(strcmp("HEAD_REPEAT",desc) == 0){
cfghrepeat=atoi(valu);
} else if(strcmp("SEND_PERCENT",desc) == 0){
cfgsendperc=atoi(valu);
} //ADD MORE SETTINGS HERE.
strcpy(desc, "");
strcpy(valu, "");
} else {
//Ignore anything else
}
}
cfg.close();
lcd.clear();
lcd.print(F("Init done."));
lcd.setCursor(0,1);
lcd.print(F("Using "));
lcd.print(cfgname);
lcd.setBacklight(WHITE);
delay(2000);
} else { //Read error occured (couldn't find file).
lcd.clear();
lcd.print(F("ERROR"));
lcd.setCursor(0,1);
lcd.print(F("Config not found"));
lcd.setBacklight(RED);
// Error stop execution.
while(true) {
}
}
}
void sendfile()
{
uint8_t button;
unsigned int fsize=0;
unsigned int fpos=-1;
unsigned int i=1000;
unsigned int psetting=0;
byte sread=0;
char fullname[47]="";
lcd.clear();
lcd.print(F("Sending..."));
lcd.setBacklight(GREEN);
//Figure out port settings
switch (cfgpar) {
case 0:
psetting = 0;
break;
case 1:
psetting = 48;
break;
case 2:
psetting = 32;
break;
}
psetting=psetting + (cfgdata-5)*2;
psetting=psetting + (cfgstop-1)*8;
// Display Communication Settings
if(cfgcomdebug == 1){
lcd.setCursor(13, 0);
lcd.print(psetting);
lcd.setBacklight(WHITE);
delay(3000);
lcd.setCursor(13, 0);
lcd.print(" ");
lcd.setBacklight(GREEN);
}
Serial.begin(cfgbaud, psetting);
while (!Serial) {
}
strcpy(fullname, cfgdir);
strcat(fullname, cfgfilename);
File dataFile=SD.open(fullname);
if (dataFile) {
do{
button=lcd.readButtons();
} while(button & BUTTON_RIGHT); //Wait until Right button has been released.
//If hardware handshake send Request To Send
if(cfghard == 1){
digitalWrite(RTS, HIGH);
}
//Send header string if required
lcd.setBacklight(GREEN);
for(int headcount=0; headcount<cfghrepeat; headcount++){
Serial.write(cfghead);
}
//Wait for XON if applicable
if(cfgsoft == 1 && cfgwait == 1){
lcd.setBacklight(YELLOW);
while(cfgsoft==1){
sread = Serial.read();
button=lcd.readButtons();
if (sread == XON || sread == XON2 || button & BUTTON_RIGHT){
break;
}
}
lcd.setBacklight(GREEN);
do{
button=lcd.readButtons();
} while(button & BUTTON_RIGHT); //Wait until Right button has been released.
}
if(cfgsendperc == 1){
Serial.write(31); //Send Percent (%) symbol
Serial.write(13); //Send Newline (\n)
}
fsize = dataFile.size()/1000;
while (dataFile.available()) {
if(cfgsoft == 1){
sread = Serial.read();
if(sread == XOFF || sread == XOFF2){
lcd.setBacklight(YELLOW);
while(true){
sread = Serial.read();
if(sread == XON || sread == XON2){
break;
}
}
lcd.setBacklight(GREEN);
}
}
Serial.write(dataFile.read());
i++;
if(i>1023){
fpos++;
lcd.setCursor(0,1);
lcd.print(fpos);
lcd.print(F("k of "));
lcd.print(fsize);
lcd.print("k");
i=0;
}
}
dataFile.close();
//Shut off Request To Send
digitalWrite(RTS, LOW);
} else {
fsize = dataFile.size();
lcd.clear();
lcd.print(F("ERROR"));
lcd.setCursor(0, 1);
lcd.print(F("File Open Error."));
lcd.setBacklight(RED);
while(true){
}
}
}
void append(char* s, char c)
{
int len = strlen(s);
s[len]=c;
s[len+1]='\0';
}
int freeRam () {
extern int __heap_start, *__brkval;
int v;
return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
}