-
Notifications
You must be signed in to change notification settings - Fork 0
/
pxdisk_mega.ino
1359 lines (1268 loc) · 34.9 KB
/
pxdisk_mega.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
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
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/////////////////////////////////////////////////
/// pxdisk
/// copyright(c) 2019 William R Cooke
///
/// command line interpreter, blinking lights,
/// SD-card management: 2023, Fred Jan Kraan
/// https://github.com/electrickery/pxdisk_mega
///
/// Use an SD card as multiple disk drives for
/// Epson PX-8 and PX-4 CP/M laptop computers
/////////////////////////////////////////////////
#define MAJOR_VERSION 1
#define MINOR_VERSION 6
#define PATCH 3
#include <SPI.h>
#include <SD.h>
#include "pxdisk.h"
File root;
void(* resetFunc) (void) = 0; // create a standard reset function
//////////////////////////////////////////////////////////////////////////////
/// @fn Setup
///
/// @brief Initializes all I/O and data structures
///
//////////////////////////////////////////////////////////////////////////////
void setup()
{
uint16_t timeOut = 100;
DEBUGPORT.begin(DEBUGBAUDRATE);
while (!DEBUGPORT && timeOut) {
; // wait for serial port to connect. Needed for native USB port only
timeOut--;
if (timeOut % 2) { // toggle debugLED every delay cycle
debugLedOff();
} else {
debugLedOn();
}
delay(100L);
}
if (timeOut) { // if timeout hasn't happened, console is available
console = true;
}
if (console) {
DEBUGPORT.println();
DEBUGPORT.println();
DEBUGPORT.print(F("Beginning PFBDK on "));
DEBUGPORT.print(F(BOARDTEXT));
DEBUGPORT.print(F(" v"));
printVersion();
DEBUGPORT.println("...");
}
debugLedOn();
pinMode(DEBUGLED, OUTPUT);
pinMode(D_LED, OUTPUT);
pinMode(E_LED, OUTPUT);
pinMode(F_LED, OUTPUT);
pinMode(G_LED, OUTPUT);
PXPORT.begin(PXBAUDRATE);
int sd = SD.begin(CS_PIN);
if (!sd) {
if (console) DEBUGPORT.println("No SD-card, halted.");
while(1) {
digitalWrite(DEBUGLED, !digitalRead(DEBUGLED));
delay(250L);
}
}
// TODO: Why in the haydes do we need to do this?
// It appears to be a timing issue. First use of a read
// seems to get everything initialized. If we wait until needed
// maybe it is taking too long and px8 times out?
diskReadSector(0, 0, 0, 1, textBuffer);
diskReadSector(0, 1, 0, 1, textBuffer);
diskReadSector(1, 0, 0, 1, textBuffer);
diskReadSector(1, 1, 0, 1, textBuffer);
root = SD.open("/");
if (console) {
printDirectory();
}
root.close();
//
debugLedOff();
//
driveLedsOff();
for (int i = 0; i < MAX_TEXT; i++) {
textBuffer[i] = 0;
}
}
void printVersion() {
if (console) {
DEBUGPORT.print(MAJOR_VERSION);
DEBUGPORT.print(".");
DEBUGPORT.print(MINOR_VERSION);
DEBUGPORT.print(".");
DEBUGPORT.print(PATCH);
}
}
//////////////////////////////////////////////////////////////////////////////
/// @fn diskReadSector
/// @param[in] uint8_t unit: Disk unit to read from, 0 or 1
/// @param[in] uint8_t disk: Disk in unit to read from, 0 or 1
/// @param[in] uint8_t track: Track to read from, 0 to 39
/// @param[in] uint8_t sector: Sector to read, 1 to 64
/// @param[in] uint8_t* buffer: Pointer to buffer to write into
///
/// @return bool: true if successful, false if not
///
/// @brief Reads a 128 byte sector from given disk, track,sector
///
//////////////////////////////////////////////////////////////////////////////
bool diskReadSector(uint8_t unit, uint8_t disk, uint8_t track, uint8_t sector, uint8_t *buffer)
{
bool rtn = true;
uint8_t device = unit * 2 + disk;
File dsk = SD.open(diskNames[device], FILE_READ);
// if (console) DEBUGPORT.write("R:");
// if (console) DEBUGPORT.println(diskNames[device]);
driveLedOn(device);
if(dsk) {
int result;
rtn = dsk.seek(track * DISK_BYTES_PER_TRACK + (sector-1) * DISK_BYTES_PER_SECTOR);
if(rtn)
{
result = dsk.read(buffer, DISK_BYTES_PER_SECTOR);
if(result < 0)
{
rtn = false;
}
}
dsk.close();
} else {
rtn = false;
if (console) DEBUGPORT.println(F("OPEN & READ FAIL"));
}
return rtn;
}
//////////////////////////////////////////////////////////////////////////////
/// @fn diskWriteSector
/// @param[in] uint8_t unit: Disk unit to write to, 0 or 1
/// @param[in] uint8_t disk: Disk in unit to write to, 0 or 1
/// @param[in] uint8_t track: Track to write to, 0 to 39
/// @param[in] uint8_t sector: Sector to write, 1 to 64
/// @param[in] uint8_t* buffer: Pointer to buffer to read from
///
/// @return bool: true if successful, false if not
///
/// @brief Writes a 128 byte sector to given disk, track,sector
///
//////////////////////////////////////////////////////////////////////////////
bool diskWriteSector(uint8_t unit, uint8_t disk, uint8_t track, uint8_t sector, uint8_t *buffer)
{
bool rtn = true;
uint8_t device = unit * 2 + disk;
// File dsk = SD.open(diskNames[device], FILE_WRITE);
File dsk = SD.open(diskNames[device], O_READ | O_WRITE | O_CREAT);
if(dsk)
{
dsk.seek(track * DISK_BYTES_PER_TRACK + (sector - 1) * DISK_BYTES_PER_SECTOR);
dsk.write(buffer, DISK_BYTES_PER_SECTOR);
dsk.close();
driveLedOn(device);
}
else
{
rtn = false;
if (console) DEBUGPORT.println("OPEN & WRITE FAIL");
}
return rtn;
}
//////////////////////////////////////////////////////////////////////////////
/// @fn hexNibble
/// @param[in] uint8_t n: The nibble to convert (0 to 15)
///
/// @return uint8_t Character HEX(0-9, A-F) representation of n
///
/// @brief Converts nibble to hex character
///
//////////////////////////////////////////////////////////////////////////////
uint8_t hexNibble(uint8_t n)
{
uint8_t rtn = 0;
if(n < 16)
{
rtn = n + 0x30;
if(n > 9)
{
rtn = n + 0x37;
}
}
return rtn;
}
//////////////////////////////////////////////////////////////////////////////
/// @fn showHex
/// @param[in] uint8_t b: Byte to print
///
/// @brief Sends HEX representation of byte to Arduino terminal
///
//////////////////////////////////////////////////////////////////////////////
void showHex(uint8_t b)
{
if (console) {
DEBUGPORT.write(hexNibble( b >> 4));
DEBUGPORT.write(hexNibble( b & 0xf));
DEBUGPORT.write( 0x20);
DEBUGPORT.println();
}
}
//////////////////////////////////////////////////////////////////////////////
/// @fn isValidSID
/// @param[in] uint8_t id: The ID number to check
///
/// @return bool: true if id is valid as a source ID
///
/// @brief Source ID can be from HX20, PX8, or PX4
///
//////////////////////////////////////////////////////////////////////////////
bool isValidSID(uint8_t id)
{
bool rtn = false;
if(id == 0x20 || id == 0x22 || id == 0x23) // 20=HX20, 22=PX8, 23=PX4
{
rtn = true;
}
return rtn;
}
//////////////////////////////////////////////////////////////////////////////
/// @fn isValidDID
/// @param[in] uint8_t id: The ID number to check
///
/// @return bool: true if id is valid as a destination ID
///
/// @brief Destination ID can be to disk unit 1 or 2
///
//////////////////////////////////////////////////////////////////////////////
bool isValidDID(uint8_t id)
{
bool rtn = false;
if(id == 0x31 || id == 0x32) // 31=First Disk (D,E) 32=Second Disk (F,G)
{
rtn = true;
}
return rtn;
}
//////////////////////////////////////////////////////////////////////////////
/// @fn isValidFNC
/// @param[in] uint8_t f: The function code to check
///
/// @return bool: true if this is a valid disk function code
///
/// @brief Checks that function code is valid for disk device
///
//////////////////////////////////////////////////////////////////////////////
bool isValidFNC(uint8_t f)
{
bool rtn = true;
switch(f) {
case FN_DISK_RESET:
case FN_DISK_SELECT:
case FN_DISK_READ_SECTOR:
case FN_DISK_WRITE_SECTOR:
case FN_DISK_WRITE_HST:
case FN_DISK_COPY:
case FN_DISK_FORMAT:
case FN_IMAGE_CMDS:
rtn = true;
break;
default:
break;
}
return rtn;
}
//////////////////////////////////////////////////////////////////////////////
/// @fn receiveByte
///
/// @return uint8_t: Next byte received from Master
///
/// @brief Waits until there is an incoming byte and returns it.
///
//////////////////////////////////////////////////////////////////////////////
uint8_t receiveByte()
{
/// TODO: fix rtn: uint8_t won't work right!
uint8_t rtn;
while( (rtn = PXPORT.read()) < 0);
return rtn;
}
//////////////////////////////////////////////////////////////////////////////
/// @fn sendByte
/// @param[in] uint8_t b: Byte to send to Master
///
/// @brief Sends a raw byte to the Master device
///
//////////////////////////////////////////////////////////////////////////////
void sendByte(uint8_t b)
{
PXPORT.write(b);
}
//////////////////////////////////////////////////////////////////////////////
/// @fn sendHeader
///
/// @brief Sends header to Master based on most recently received data.
///
//////////////////////////////////////////////////////////////////////////////
void sendHeader()
{
uint8_t cks = 0;
sendByte(C_SOH);
cks -= C_SOH;
sendByte(C_FMT_SM);
cks -= C_FMT_SM;
sendByte(latestSID); // Source becomes destination
cks -= latestSID;
sendByte(latestDID); // and vice versa
cks -= latestDID;
sendByte(latestFNC);
cks -= latestFNC;
uint8_t thisSIZ;
switch(latestFNC)
{
case FN_DISK_READ_SECTOR:
thisSIZ = 0x80;
break;
case FN_DISK_WRITE_SECTOR:
thisSIZ = 0;
break;
case FN_DISK_COPY:
thisSIZ = 0x02;
break;
case FN_DISK_FORMAT:
thisSIZ = 0x02;
break;
case FN_IMAGE_CMDS:
thisSIZ = getTxtSize(textBuffer[0]);
break;
default:
thisSIZ = 0x00;
break;
}
sendByte(thisSIZ);
latestSIZ = thisSIZ;
cks -= thisSIZ;
sendByte(cks);
}
//////////////////////////////////////////////////////////////////////////////
/// @fn sendText
///
/// @brief Sends text to Master based on most recently received data.
///
//////////////////////////////////////////////////////////////////////////////
void sendText()
{
uint8_t device;
uint8_t cks = 0;
uint8_t returnCode = 0;
sendByte(C_STX);
cks -= C_STX;
uint8_t i = 0;
bool returnStatus = true;
char managementCommand;
switch(latestFNC)
{
//////////////////////////////////////////////////////////////////////////////
case FN_DISK_RESET:
sendByte(returnCode); // return success
cks -= 0;
break;
//////////////////////////////////////////////////////////////////////////////
case FN_DISK_SELECT: // ??? Where is this? Not in PX8 OSRM ch 15 epsp.html
sendByte(0);
cks -= 0;
break;
//////////////////////////////////////////////////////////////////////////////
case FN_DISK_READ_SECTOR:
returnStatus = diskReadSector(selectedDevice, textBuffer[0] - 1,
textBuffer[1], textBuffer[2], textBuffer);
if(!returnStatus) // error!
{
returnCode = BDOS_RC_READ_ERROR;
}
for(int i = 0; i < MAX_TEXT; i++)
{
uint8_t byt = textBuffer[i];
sendByte(byt);
cks -= byt;
}
sendByte(returnCode); // return code
cks -= 0;
break;
//////////////////////////////////////////////////////////////////////////////
case FN_DISK_WRITE_SECTOR:
device = selectedDevice * 2 + textBuffer[0] - 1;
if (writeProtect[device] == true) {
returnCode = BDOS_RC_WRITE_ERROR; // not the correct code, but the PX is happy.
} else {
returnStatus = diskWriteSector(selectedDevice, textBuffer[0] - 1,
textBuffer[1], textBuffer[2], &textBuffer[4]);
if(!returnStatus) // error!
{
returnCode = BDOS_RC_WRITE_ERROR;
}
}
sendByte(returnCode); // return code
cks -= 0;
break;
//////////////////////////////////////////////////////////////////////////////
case FN_DISK_WRITE_HST: // We always write, so nothing needs to flush
sendByte(0);
break;
//////////////////////////////////////////////////////////////////////////////
case FN_DISK_COPY: // Not currently supported
if (console) DEBUGPORT.println(F("FN_DISK_COPY not supported"));
sendByte(C_NAK);
break;
//////////////////////////////////////////////////////////////////////////////
case FN_DISK_FORMAT: // Not currently supported
if (console) DEBUGPORT.println(F("FN_DISK_FORMAT not supported"));
sendByte(C_NAK);
break;
//////////////////////////////////////////////////////////////////////////////
case FN_IMAGE_CMDS:
managementCommand = latestE0Command;
setBufPointer = receivedSize + 1;
if (console) {
DEBUGPORT.println(F("FN_IMAGE_CMDS not very supported"));
// debug echo
DEBUGPORT.print("textB: ");
for (uint8_t i = 0; i < setBufPointer; i++) {
DEBUGPORT.print(textBuffer[i], HEX); DEBUGPORT.print(" ");
}
DEBUGPORT.println();
DEBUGPORT.print(F("serB: "));
}
for (uint8_t i = 0; i < setBufPointer; i++) {
serialBuffer[i] = textBuffer[i]; // prep the commandInterpreter
if (console) {
if (textBuffer[i] > 0x01F && textBuffer[i] < 0x7F) {
DEBUGPORT.write(textBuffer[i]); DEBUGPORT.print(" ");
} else {
DEBUGPORT.print(textBuffer[i], HEX);
DEBUGPORT.print("h ");
}
}
}
if (console) DEBUGPORT.println();
commandInterpreter(); // execute command
for (uint8_t i = 0; i < getTxtSize(managementCommand); i++) {
sendByte(textBuffer[i]);
cks -= textBuffer[i];
}
sendByte(returnCode);
cks -= 0;
break;
//////////////////////////////////////////////////////////////////////////////
default:
break;
}
sendByte(C_ETX);
cks -= C_ETX;
sendByte(cks);
}
//////////////////////////////////////////////////////////////////////////////
/////////////////////////////// State Machine ////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
/// @fn stateMachine
/// @param[in] uint8_t b: Incoming byte from Master
///
/// @brief Performs action based on current state and incoming byte
///
//////////////////////////////////////////////////////////////////////////////
void stateMachine(uint8_t b)
{
static States state = ST_BEGIN;
static uint8_t textCount = 0;
switch(state)
{
case ST_UNDEFINED: // Should never happen, but you never know
break;
///////////////////////////////////////////////////////////////////////////////
case ST_BEGIN: // Starting state
if(b == C_SEL)
{
state = ST_PS_SEL;
}
else
{
// do nothing. Wait for C_SEL
}
break;
//////////////////////////////////////////////////////////////////////////////
case ST_IDLE:
if(b == C_SEL)
{
state = ST_PS_SEL;
debugLedOn();
}
else if(b == C_SOH)
{
latestCKS = C_SOH;
state = ST_HD_SOH;
debugLedOn();
}
else if(b == C_STX)
{
latestCKS = b;
textCount = 0;
state = ST_TX_STX;
debugLedOn();
}
else if(b == C_EOT)
{
// if (console) DEBUGPORT.println("IDLE got EOT");
}
else
{
// state = ST_ERR; just stay where we are on unrecognized char
// get 00 when px8 turned off
}
break;
//////////////////////////////////////// Select / Poll ///////////////////////
case ST_PS_SEL:
if(isValidDID(b))
{
latestDID = b;
state = ST_PS_DID;
}
else
{
oldState = state;
state = ST_ERR;
}
break;
//////////////////////////////////////////////////////////////////////////////
case ST_PS_DID:
if(isValidSID(b))
{
latestSID = b;
state = ST_PS_SID;
}
else
{
oldState = state;
state = ST_ERR;
}
break;
//////////////////////////////////////////////////////////////////////////////
case ST_PS_SID:
if(b == C_ENQ)
{
sendByte(C_ACK);
selectedDevice = latestDID - MY_ID_1;
state = ST_PS_ENQ;
}
else
{
sendByte(C_NAK);
oldState = state;
state = ST_ERR;
}
break;
//////////////////////////////////////////////////////////////////////////////
case ST_PS_ENQ:
if(b == C_SOH)
{
latestCKS = b;
state = ST_HD_SOH;
}
else
{
oldState = state;
state = ST_ERR;
}
break;
////////////////////////////////// Header /////////////////////////////////
case ST_HD_SOH:
if(b == C_FMT_MS)
{
latestCKS += b;
state = ST_HD_FMT;
}
else
{
oldState = state;
state = ST_ERR;
}
break;
//////////////////////////////////////////////////////////////////////////////
case ST_HD_FMT:
if(isValidDID(b))
{
latestCKS += b;
latestDID = b;
state = ST_HD_DID;
}
break;
//////////////////////////////////////////////////////////////////////////////
case ST_HD_DID:
if(isValidSID(b))
{
latestSID = b;
latestCKS += b;
state = ST_HD_SID;
}
break;
//////////////////////////////////////////////////////////////////////////////
case ST_HD_SID:
if (isValidFNC(b))
{
latestFNC = b;
latestCKS += b;
state = ST_HD_FNC;
}
else
{
oldState = state;
state = ST_ERR;
}
break;
//////////////////////////////////////////////////////////////////////////////
case ST_HD_FNC:
latestSIZ = b;
receivedSize = b;
latestCKS += b;
state = ST_HD_SIZ;
break;
//////////////////////////////////////////////////////////////////////////////
case ST_HD_SIZ:
latestCKS += b;
if(latestCKS == 0)
{
sendByte(C_ACK);
state = ST_HD_CKS;
}
else
{
sendByte(C_NAK);
oldState = state;
state = ST_ERR;
}
break;
//////////////////////////////////////////////////////////////////////////////
case ST_HD_CKS:
if(b == C_STX)
{
latestCKS = b;
textCount = 0;
state = ST_TX_STX;
}
else
{
oldState = state;
state = ST_ERR;
}
break;
//////////////////////////////////////// Text ////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
case ST_TX_STX:
textBuffer[textCount] = b;
if (textCount ==0) latestE0Command = b;
latestCKS += b;
if(textCount == latestSIZ)
{
state = ST_TX_TXT;
}
else
{
textCount++;
}
break;
//////////////////////////////////////////////////////////////////////////////
case ST_TX_TXT:
latestCKS += b;
if(b == C_ETX)
{
state = ST_TX_ETX;
}
else
{
oldState = state;
state = ST_ERR;
}
break;
//////////////////////////////////////////////////////////////////////////////
case ST_TX_ETX:
latestCKS += b;
if(latestCKS == 0)
{
sendByte(C_ACK);
state = ST_TX_CKS;
}
else
{
oldState = state;
state = ST_ERR;
}
break;
//////////////////////////////////////////////////////////////////////////////
case ST_TX_CKS:
if(b == C_EOT)
{
sendHeader();
state = ST_SENT_HDR;
}
else
{
}
break;
//////////////////////////////////////////////////////////////////////////////
case ST_SENT_HDR:
if(b == C_ACK)
{
sendText();
state = ST_SENT_TXT;
}
else
{
oldState = state;
state = ST_ERR;
}
break;
//////////////////////////////////////////////////////////////////////////////
case ST_SENT_TXT:
if(b == C_ACK)
{
sendByte(C_EOT);
state = ST_IDLE;
}
else if(b == C_NAK)
{
sendText();
}
debugLedOff();
break;
//////////////////////////////////////////////////////////////////////////////
case ST_ERR:
if (console) {
DEBUGPORT.print("Err; old state was: ");
DEBUGPORT.print(oldState);
}
state = ST_IDLE;
break;
default:
break;
}
}
//////////////////////////////////////////////////////////////////////////////
/// @fn loop
///
/// @brief Gets next incoming byte and passes it to state machine
///
//////////////////////////////////////////////////////////////////////////////
void loop()
{
int av;
while(true) // TODO PXPORT.available() > 0)
{
if((av = PXPORT.available()) > 32)
{
if (console) {
DEBUGPORT.print('!');
DEBUGPORT.print(av);
}
}
if (PXPORT.available() > 0) { // Wait for something to come in from PX
uint8_t b = receiveByte();
stateMachine(b);
}
if (DEBUGPORT.available() > 0) { // Wait for something to come in from console
consoleCommand = true;
commandCollector();
consoleCommand = false;
}
if (ledTime < millis()) {
driveLedsOff();
}
}
}
void commandCollector() { // only used when console is active
if (console && DEBUGPORT.available() > 0) {
int inByte = DEBUGPORT.read();
switch(inByte) {
case '\n':
commandInterpreter();
clearSerialBuffer();
setBufPointer = 0;
break;
case '\r':
break; // ignore carriage return
default:
serialBuffer[setBufPointer] = inByte;
setBufPointer++;
if (setBufPointer >= SERIALBUFSIZE) {
DEBUGPORT.println(F("Serial buffer overflow. Cleanup."));
clearSerialBuffer();
setBufPointer = 0;
}
}
}
}
void commandInterpreter() {
byte bufByte = serialBuffer[0];
File root; // declaration inside the case doesn't work, why?
switch(bufByte) {
case 'C':
case 'c':
checkName(0);
checkName(1);
checkName(2);
checkName(3);
break;
case 'D':
case 'd':
loadDirectory();
break;
case 'H':
case 'h':
case '?':
DEBUGPORT.println();
DEBUGPORT.print(F("Usage ("));
printVersion();
DEBUGPORT.println(F("):"));
DEBUGPORT.println(F(" C - temp debug for driveNames[][]"));
DEBUGPORT.println(F(" D[p] - SD-card root directory"));
DEBUGPORT.println(F(" H - this help"));
DEBUGPORT.println(F(" M[dnnnnnnnn.eee] - mount file nnnnnnnn.eee on drive d"));
DEBUGPORT.println(F(" Nnnnnnnnn.eee - create an image file nnnnnnnn.eee"));
DEBUGPORT.println(F(" P[dw] - write protect drive d; w=0 RW, w=1 RO"));
DEBUGPORT.println(F(" R - temp reset Arduino"));
break;
case 'M':
case 'm':
mountImage();
break;
case 'N':
case 'n':
newFile();
break;
case 'P':
case 'p':
protect();
break;
case 'R':
case 'r':
resetFunc();
break;
default:
DEBUGPORT.print(bufByte);
DEBUGPORT.println(F(" unsupported"));
return;
}
}
void printDirectory() {
root = SD.open("/");
DEBUGPORT.println(F("Root directory:"));
while (console) {
File entry = root.openNextFile();
if (! entry) {
// no more files
break;
}
DEBUGPORT.print(" ");
String name = entry.name();
DEBUGPORT.print(name);
if (entry.isDirectory()) {
DEBUGPORT.println("/");
// printDirectory();
} else {
// files have sizes, directories do not
DEBUGPORT.print("\t");
if (name.length() < 7) DEBUGPORT.print("\t");
DEBUGPORT.println(entry.size(), DEC);
}
entry.close();
}
root.close();
}
void loadDirectory() { // D[d]
if (consoleCommand) {
printDirectory();
} else {
uint8_t dirOffset = 0;
if (setBufPointer == 2) dirOffset = serialBuffer[1] - '0';
if (dirOffset > 9) dirOffset = 0;
root = SD.open("/");
if (console) {
DEBUGPORT.print(F("Root directory ("));
DEBUGPORT.print(dirOffset);
DEBUGPORT.println("):");
}
uint8_t entryCount = 0;
int entryNameOffset;
int entrySizeOffset;
// skip previously send entries
for (uint8_t dirIndex = 0; dirIndex < (dirOffset * ENTRIESPERMESSAGE); dirIndex++) {
File tmpEntry = root.openNextFile();
if (console) DEBUGPORT.print(".");
tmpEntry.close();
}
if (console) DEBUGPORT.println();
fillTextBuffer(' ');
while (1) {
File entry = root.openNextFile();
if (!entry) {
// no more files
if (console) DEBUGPORT.print(F(" -- no more entries-- "));
break;
}
if (entryCount >= ENTRIESPERMESSAGE) {
break;
}
entryNameOffset = ENTRYSIZE * entryCount;
entrySizeOffset = ENTRYSIZE * entryCount + ENTRYNAMESIZE;
String name = entry.name();
if (entry.isDirectory()) name += '/'; // don't use full 8.3 for directories :-)
uint32_t size = entry.size();
if (console) {
DEBUGPORT.print(" ");
DEBUGPORT.print(name);
}
for (int c = 0; c < ENTRYNAMESIZE; c++) {
if (name.charAt(c) == 0) break;
textBuffer[entryNameOffset + c] = name.charAt(c);
}
if (console) DEBUGPORT.print(" ");
uint8_t sizeByte = long2byte(size, 2);
textBuffer[entrySizeOffset++] = sizeByte;
if (console) {
if (sizeByte < 0x10) DEBUGPORT.print("0");
DEBUGPORT.print(sizeByte, HEX);
}
sizeByte = long2byte(size, 1);
textBuffer[entrySizeOffset++] = sizeByte;
if (console) {