-
Notifications
You must be signed in to change notification settings - Fork 0
/
FizzTerm.ino
532 lines (407 loc) · 12 KB
/
FizzTerm.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
/*
FizzTerm - A Simple Serial File System Manager
Use it to record and play back serial data to an SD card. It's particularly
useful if using a retro computer that communicates over RS232 and you
need to capture program listings to disk.
You can also access the SD card on your computer, edit them there,
and then load them back into your retro system.
Requires an Aduino Mega or compatible, with multiple serial port support.
Assumes that MAX232-type adaptors are connected to the Arduino's default
Serial1 and Serial2 ports, and the SD Card reader is connected in the usual
way (although the Chip Select pin may depend on your hardware).
Note: In order to make LIST command work, you must duplicate the SD library
and change all the references to Serial. to Serial1. If you don't,
when you type LIST you won't see the list of files on your card.
Note: Baud rate is set at 9600. You may need to change this.
v0.3 = Toggle between 9600 and 115200 with the baud keyword
v0.3.1 = The command checking is no longer case-sensitive, and sending
strings to computer slower to avoid chokng it.
*/
#include <SPI.h>
#include <SD.h>
// Styles
#define GREEN "\033[0;32m"
#define WHITE "\033[1;37m"
// Set up variables using the SD utility library functions:
Sd2Card card;
SdVolume volume;
SdFile root;
const int chipSelect = 53; // Might be different on your hardware
// Variables used by this app
long BAUDRATE = 9600;
bool HIDE_ECHO = false;
bool RECORDING = false;
char buffer[80];
char buffer_count = 0;
File myFile;
void setup() {
//Serial.begin(BAUDRATE); // For debugging
Serial1.begin(BAUDRATE); // The Terminal
Serial2.begin(BAUDRATE); // The Computer
Serial1.println(GREEN);
Serial1.println(" ______ _ _______ ");
Serial1.println(" | ____(_) |__ __| ");
Serial1.println(" | |__ _ _______| | ___ _ __ _ __ ___ ");
Serial1.println(" | __| | |_ /_ / |/ _ \\ '__| '_ \\` _ \\ ");
Serial1.println(" | | | |/ / / /| | __/ | | | | | | |");
Serial1.println(" |_| |_/___/___|_|\\___|_| |_| |_| |_|\n");
Serial1.println(" v0.3.1\n");
Serial1.println(WHITE);
if (!card.init(SPI_HALF_SPEED, chipSelect)) {
Serial1.println("Error: SD Card was not found.");
//while (1); // You may not want the terminal to hang with no SD card present.
}
// Now we will try to open the 'volume'/'partition' - it should be FAT16 or FAT32
if (!volume.init(card)) {
Serial1.println("Error: SD Card was found, but could not be read.");
//while (1); // You may not want the terminal to hang with no SD card present.
}
// Clear buffer
for (int i = 0; i < 80; i++)
buffer[i] = 0;
}
// Let the user enter a line of text from the terminal, and echo it back to the terminal.
// If it's a backspace, delete the character previous entered.
// Check for <0 or >80 characters.
// When the user enters NEWLINE, well, then we do stuff.
// If you wanted to add support for cursor up and down to go through a history, then
// this is where you would do it.
void Enter_Line()
{
if (Serial1.available())
{
// Read in and echo back to terminal. Don't send to computer yet.
int inByte = Serial1.read();
Serial1.write(inByte);
buffer[buffer_count] = inByte;
bool found_command = false;
if (inByte == 3 && RECORDING)
{
RECORDING = false;
myFile.close();
Serial1.print(GREEN);
Serial1.println("Saving complete.");
Serial1.print(WHITE);
}
if (inByte == 8) // User pressed delete
{
buffer_count--;
if (buffer_count < 0)
{
buffer_count = 0;
}
}
else
{
buffer_count++;
if (buffer_count >= 80)
{
buffer_count = 79;
}
}
if (inByte == 13) // User pressed return
{
// Eat next char, it will be 10
int extraByte = Serial1.read();
// Check for commmands
buffer[buffer_count - 1] = 0; // Get rid of the newline character in there
if (strncasecmp(buffer, "!HELP", 5) == 0)
{
FS_HELP();
found_command = true;
}
if (strncasecmp(buffer, "!BAUD", 5) == 0)
{
FS_BAUD();
found_command = true;
}
if (strncasecmp(buffer, "!LIST", 5) == 0)
{
FS_LIST();
found_command = true;
}
if (strncasecmp(buffer, "!SAVE", 5) == 0)
{
char filename[13];
for (int i = 6; i < 18; i++)
filename[i - 6] = buffer[i];
filename[12] = 0;
FS_SAVE(filename);
found_command = true;
}
if (strncasecmp(buffer, "!LOAD", 5) == 0)
{
char filename[13];
for (int i = 6; i < 18; i++)
filename[i - 6] = buffer[i];
filename[12] = 0;
FS_LOAD(filename);
found_command = true;
}
if (strncasecmp(buffer, "!WIPE", 4) == 0) // Yes, DEL or ERA would have been better but I'm being lazy and assuming 5 chars
{
char filename[13];
for (int i = 6; i < 18; i++)
filename[i - 6] = buffer[i];
filename[12] = 0;
FS_WIPE(filename);
found_command = true;
}
if (strncasecmp(buffer, "!DUMP", 5) == 0)
{
char filename[13];
for (int i = 6; i < 18; i++)
filename[i - 6] = buffer[i];
filename[12] = 0;
FS_DUMP(filename);
found_command = true;
}
if (strncasecmp(buffer, "!STOP", 5) == 0)
{
if (RECORDING)
{
RECORDING = false;
myFile.close();
Serial1.print(GREEN);
Serial1.println("Saving complete.");
Serial1.println(WHITE);
}
else
{
Serial1.print(GREEN);
Serial1.println("Error: Nothing was being saved.");
Serial1.println(WHITE);
}
found_command = true;
}
if (found_command == true)
{
buffer_count = 0;
for (int i = 0; i < 80; i++)
buffer[i] = 0;
return;
}
// No commands found, so a regular string to pass onto computer
HIDE_ECHO = true; // Don't display the text the computer spits back that includes the line we just sent.
// Why? Sometimes there is a > prompt for example, and that would mess up what the user
// sees.
// Take a new line on the terminal, so the user knows they pressed return
Serial1.write(13);
// Now send the text to the computer, char by char
int i = 0;
do {
Serial2.write(buffer[i++]);
delay(10); // Don't make it choke..
} while(buffer[i]!=0 && i<80);
Serial2.write("\r");
// Some omputers are too slow to be sent the entire buffer in one go, rather than char by char with a delay.
// If your computer is quick, replace the above with this:
// Serial2.write(buffer);
buffer_count = 0;
for (int i = 0; i < 80; i++)
buffer[i] = 0;
}
}
}
// This displays the information coming from the computer. It's also where we save it to the SD
// card if that option is currenly active.
void Display_From_Computer()
{
if (Serial2.available())
{
// Read in and echo back
int inByte = Serial2.read();
if (HIDE_ECHO == false)
{
Serial1.write(inByte);
if (RECORDING) // Save any data from the computer to an open file on the SD card.
{
myFile.write(inByte);
}
}
if (inByte == 13) // Wait to echo text back until we get the CR, so absorb the echo of the sent string
{
HIDE_ECHO = false;
}
}
}
void loop()
{
Enter_Line();
Display_From_Computer();
}
void FS_HELP()
{
Serial1.println(GREEN);
Serial1.println();
Serial1.println("FIZZTERM");
Serial1.println();
Serial1.println("!HELP This helpful text.");
Serial1.println("!BAUD Toggle the bit rate between 9600 and 115200.");
Serial1.println("!LIST List the files stored on the SD card.");
Serial1.println("!LOAD file Load the named file, sending it directly to the connected computer.");
Serial1.println("!DUMP file Display the contents the named file, but only to the terminal.");
Serial1.println("!SAVE file Save incoming data to SD from the computer until !STOP or CTRL C.");
Serial1.println("!STOP Stop saving data to file.");
Serial1.println("!WIPE file Delete the named file from the SD card.");
Serial1.println(WHITE);
}
void FS_DUMP(const char * filename)
{
Serial1.println(GREEN);
if (!SD.begin(chipSelect))
{
Serial1.println("Error: Card failed, or not present");
Serial1.println(WHITE);
return;
}
if (!SD.exists(filename))
{
Serial1.println("Error: File not found");
Serial1.println(WHITE);
return;
}
File dataFile = SD.open(filename);
// if the file is available, read from it
if (dataFile)
{
while (dataFile.available())
{
Serial1.write(dataFile.read());
}
dataFile.close();
}
else
{
Serial1.println("Error: Could not open file");
}
Serial1.println(WHITE);
}
void FS_LOAD(const char* filename)
{
Serial1.println(GREEN);
if (!SD.begin(chipSelect))
{
Serial1.println("Error: Card failed, or not present");
Serial1.println(WHITE);
return;
}
if (!SD.exists(filename))
{
Serial1.println("Error: File not found");
Serial1.println(WHITE);
return;
}
File dataFile = SD.open(filename);
Serial1.write("Loading..");
// if the file is available, read from it
if (dataFile) {
while (dataFile.available()) {
Serial2.write(dataFile.read());
delay(10); // Little pause to give the computer time to digest the data. May need increased if computer is slow
// Ignore any echo'd chars from computer
if (Serial2.available()) {
char inByte = Serial2.read();
}
}
dataFile.close();
}
// if the file isn't open, display error
else {
Serial1.println("Error: Could not open file");
Serial1.println(WHITE);
return;
}
Serial1.write("OK");
Serial1.println(WHITE);
}
void FS_BAUD()
{
//
// Toggle between 9600 and 115200
// But you could change this to cycle between all possible speeds if required.
//
//Serial.flush();
Serial1.flush();
Serial2.flush();
//Serial.end();
Serial1.end();
Serial2.end();
delay(2500);
if (BAUDRATE == 9600)
{
BAUDRATE = 115200;
}
else
{
BAUDRATE = 9600;
}
delay(2500);
setup();
}
void FS_LIST()
{
Serial1.println(GREEN);
if (!SD.begin(chipSelect)) {
Serial1.println("Error: Card failed, or not present");
Serial1.println(WHITE);
return;
}
Serial1.println("Card contents (name, date and size in bytes):");
root.openRoot(volume);
// list all files in the card with date and size
root.ls(LS_R | LS_DATE | LS_SIZE);
// Turn off the green style
Serial1.println(WHITE);
}
void FS_WIPE(const char *filename)
{
Serial1.println(GREEN);
if (!SD.begin(chipSelect)) {
Serial1.println("Error: Card failed, or not present");
Serial1.println(WHITE);
return;
}
if (!SD.exists(filename))
{
Serial1.print("Error: File not found");
Serial1.println(WHITE);
return;
}
bool fail = !SD.remove(filename);
if (fail)
{
Serial1.println("Error: file could not be deleted");
}
else
{
Serial1.println("File deleted");
}
Serial1.println(WHITE);
}
void FS_SAVE(const char* filename)
{
Serial1.println(GREEN);
if (!SD.begin(chipSelect)) {
Serial1.println("Error: Card failed, or not present");
Serial1.println(WHITE);
return;
}
if (strlen(filename) < 1)
{
Serial1.println("Error: missing filename");
Serial1.println(WHITE);
return;
}
myFile = SD.open(filename, O_WRITE | O_CREAT);
// if the file opened okay, write to it:
if (myFile) {
Serial1.println("Saving data to card. Stop with CTRL C or !STOP");
Serial1.println(WHITE);
RECORDING = true;
} else {
// if the file didn't open, print an error:
Serial1.println("Error: Could not create the file");
Serial1.println(WHITE);
}
}