-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSD-OS_Parser.ino
More file actions
1475 lines (1429 loc) · 50.3 KB
/
Copy pathSD-OS_Parser.ino
File metadata and controls
1475 lines (1429 loc) · 50.3 KB
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
/**************************************************/
/*! \brief fnc_AUTO
\param argument string: pointer of char
\return int- value of token
\ingroup token_parser */
/**************************************************/
int fnc_AUTO(const char* szCmdLn)
{
/* place your code here */
if (bAuto) {
Serial.print(F(": off"));
// timer_ms.stop();
bAuto= false;
} else {
Serial.print(F(": on"));
// timer_ms.start();
bAuto = true;
}
return( eAUTO );
} /* end of fnc_AUTO */
/**************************************************/
/*! \brief fnc_CD
\param argument string: pointer of char
\return int- value of token
\ingroup token_parser */
/**************************************************/
int fnc_CD(const char* szCmdLn)
{
/* place your code here */
char sLine[ILINE]={""};
argPathFn( szCmdLn, &sLine[0]);
Serial.print(" ");
if (SD.begin( SDCRD)) {
digitalWrite(LED_BUILTIN, 1);
File dir = SD.open(sLine);
if (dir) {
strcpy(sPath, sLine);
} else {
Serial.println();
Serial.print(sLine);
Serial.print(F(" not found"));
}
SD.end();
digitalWrite(LED_BUILTIN, 0);
}
return( eCD );
} /* end of fnc_CD */
/**************************************************/
/*! \brief fnc_CLS
\param argument string: pointer of char
\return int- value of token
\ingroup token_parser */
/**************************************************/
int fnc_CLS(const char* szCmdLn)
{
/* place your code here */
Serial.print(F("\e[0H\e[2J"));
return( eCLS );
} /* end of fnc_CLS */
/**************************************************/
/*! \brief fnc_COPY
\param argument string: pointer of char
\return int- value of token
\ingroup token_parser */
/**************************************************/
int fnc_COPY(const char* szCmdLn)
{
/* place your code here */
File FH1, FH2;
char sFnTo[ILINE];
char sFnFrom[ILINE];
char *psFnTo= &sFnTo[0];
char s1[ILINE], s2[ILINE];
#define IBUFFER 256
char cBuff[IBUFFER];
szCmdLn++;
int iRet= sscanf(szCmdLn,"%s %s", &s1, &s2);
if (iRet ==2) {
argPathFn( &s1[0], &sFnFrom[0]);
if (strncmp(s2, ".", 1) ==0) {
strncpy(psFnTo, sPath, ILINE);
strncat(psFnTo, "/", ILINE);
strncat(psFnTo, (strrchr(s1, '/')+1), ILINE);
Serial.println();
Serial.println(psFnTo);
} else {
argPathFn( &s2[0], &sFnTo[0]);
} /* end if */
if (SD.begin( SDCRD)) {
digitalWrite(LED_BUILTIN, 1);
FH1 = SD.open(sFnFrom, FILE_READ);
if (FH1) {
FH2 = SD.open(sFnTo, FILE_WRITE);
if (FH2) {
while (FH1.available() > 0) {
size_t iLen = FH1.readBytes(cBuff, IBUFFER);
FH2.write(cBuff, iLen);
} /* end while */
Serial.print(F(" OK"));
} else {
Serial.print(F(" no Desti."));
} /* end if */
FH2.close();
FH1.close();
} else {
Serial.print(sFnFrom);
Serial.print(F(" no Source"));
} /* end if */
digitalWrite(LED_BUILTIN, 0);
}
SD.end();
} /* end if */
return( eCOPY );
} /* end of fnc_COPY */
/**************************************************/
/*! \brief fnc_CONFIG
\param argument string: pointer of char
\return int- value of token
\ingroup token_parser */
/**************************************************/
int fnc_CONFIG(const char* szCmdLn)
{
/* place your code here */
if (!SD.begin( SDCRD)) {
Serial.println(F(": failed"));
} else {
digitalWrite(LED_BUILTIN, 1);
Serial.println(F(": OK"));
Serial.println(F("SPI-Interface"));
Serial.print(F("MISO : "));
Serial.println(PIN_SPI_MISO);
Serial.print(F("MOSI : "));
Serial.println(PIN_SPI_MOSI);
Serial.print(F("SCK : "));
Serial.println(PIN_SPI_SCK);
Serial.print(F("CS : "));
Serial.println(PIN_SPI_CS);
digitalWrite(LEDR, 0);
delay(500);
digitalWrite(LEDG, 0);
delay(500);
digitalWrite(LEDB, 0);
delay (500);
digitalWrite(LEDR, 1);
delay(500);
digitalWrite(LEDG, 1);
delay(500);
digitalWrite(LEDB, 1);
delay(500);
}
SD.end();
digitalWrite(LED_BUILTIN, 0);
return( eCONFIG );
} /* end of fnc_CONFIG */
/**************************************************/
/*! \brief fnc_DATE
\param argument string: pointer of char
\return int- value of token
\ingroup token_parser */
/**************************************************/
int fnc_DATE(const char* szCmdLn)
{
char sLine[ILINE];
struct tm mytm;
/* place your code here */
RTC.getTime(inRTC);
mytm= inRTC.getTmTime();
if (strlen(szCmdLn) >= 8) {
int16_t iResult= sscanf( szCmdLn,"%02d.%02d.%04d", &day, &mon, &year);
if (iResult == 3) {
mytm.tm_year = year -1900;
mytm.tm_mon = mon -1;
mytm.tm_mday = day;
inRTC.setTM(mytm);
RTC.setTime(inRTC);
if (bRTC){
EXRTC.setDate((uint8_t) day, (uint8_t) mon, (uint16_t) year);
}
}
} else {
Serial.print(F(" : "));
Serial.print(F(" CPU: "));
time_t tiUx= inRTC.getUnixTime();
strftime(sLine, sizeof(sLine), "(%A) %0d.%0m.20%0y", localtime(&tiUx));
Serial.print(sLine);
}
return( eDATE );
} /* end of fnc_DATE */
/**************************************************/
/*! \brief fnc_DEL
\param argument string: pointer of char
\return int- value of token
\ingroup token_parser */
/**************************************************/
int fnc_DEL(const char* szCmdLn)
{
/* place your code here */
char sLine[ILINE]={""};
argPathFn( szCmdLn, &sLine[0]);
Serial.print(F(" : "));
if (SD.begin(SDCRD)) {
if (SD.exists(sLine)) {
digitalWrite(LED_BUILTIN, 1);
int16_t iRes= SD.remove(sLine);
if (iRes>0) {
Serial.print(F("removed"));
} else {
Serial.print(F("not removed!"));
}
} else {
Serial.print(F("not Found"));
}
SD.end();
digitalWrite(LED_BUILTIN, 0);
}
return( eDEL );
} /* end of fnc_DEL */
/**************************************************/
/*! \brief fnc_DIR
\param argument string: pointer of char
\return int- value of token
\ingroup token_parser */
/**************************************************/
void printDirectory(File dir, int numTabs) {
static File entry;
while ( entry = dir.openNextFile()) {
for (uint8_t i = 0; i < numTabs; i++) {
Serial.print("\e[");
Serial.print(i*2);
Serial.print("C");
}
int iLen = strlen(entry.name());
Serial.print(entry.name());
if (entry.isDirectory()) // files have sizes, directories do not
{
Serial.print(F("\\\e["));
if (iLen <= 27) {
Serial.print(28);
} else {
Serial.print(iLen + 2);
} /* end if */
Serial.println(F("G\e[1m<Dir>\e[0m"));
// printDirectory(entry, numTabs + 1); // uncommend for recursive funcion call
} else {
Serial.print(F("\e["));
if (iLen <= 23) {
Serial.print(F("24"));
} else {
Serial.print(iLen + 2);
}
Serial.print(F("G"));
Serial.print(entry.size(), DEC);
Serial.println(F(" Byte"));
}
entry.close();
}
}
int fnc_DIR(const char* szCmdLn)
{
/* place your code here */
char sLine[ILINE];
argPathFn( szCmdLn, &sLine[0]);
Serial.println(F(" : "));
if (SD.begin(SDCRD)) {
digitalWrite(LED_BUILTIN, 1);
File dir = SD.open(sLine);
printDirectory(dir, 0);
SD.end();
digitalWrite(LED_BUILTIN, 0);
}
return( eDIR );
} /* end of fnc_DIR */
/**************************************************/
/*! \brief fnc_ECHO
\param argument string: pointer of char
\return int- value of token
\ingroup token_parser */
/**************************************************/
int fnc_ECHO(const char* szCmdLn)
{
/* place your code here */
File FH1;
Serial.print(F("\r\n"));
if (SD.begin(SDCRD)) {
digitalWrite(LED_BUILTIN, 1);
FH1 = SD.open(sLogFn, FILE_WRITE);
if (FH1) {
Serial.println(szCmdLn+1);
FH1.println(szCmdLn+1);
FH1.close();
} else {
Serial.print(F("no File ..."));
Serial.print(sLogFn);
}
SD.end();
digitalWrite(LED_BUILTIN, 0);
}
return( eECHO );
} /* end of fnc_ECHO */
/**************************************************/
/*! \brief fnc_FORMAT
\param argument string: pointer of char
\return int- value of token
\ingroup token_parser */
/**************************************************/
int fnc_FORMAT(const char* szCmdLn)
{
/* place your code here */
Serial.println(F(" : "));
if (SD.begin(SDCRD)) {
digitalWrite(LED_BUILTIN, 1);
//bool stFS= SD.format(); //
Serial.println(F("A \"format\" function is not implemented"));
SD.end();
digitalWrite(LED_BUILTIN, 0);
}
return( eFORMAT );
} /* end of fnc_FORMAT */
/**************************************************/
/*! \brief fnc_HELP
\param argument string: pointer of char
\return int- value of token
\ingroup token_parser */
/**************************************************/
int fnc_HELP(const char* szCmdLn)
{
/* place your code here */
Serial.println(F(" :"));
Serial.println(F("AUTO"));
Serial.println(F("CLS\t clearscreen"));
Serial.println(F("CONFIG\t display configuration"));
Serial.println(F("COPY\t copy file; <src> <targ>"));
Serial.println(F("DATE\t display/set date; format <DD.MM.YYYY>"));
Serial.println(F("DEL\t delete file"));
Serial.println(F("DIR\t display directory"));
Serial.println(F("ECHO\t copy argument into logfile"));
Serial.println(F("FORMAT\t <func. not available>"));
Serial.println(F("HELP\t this help informations"));
Serial.println(F("PATH\t display actual path"));
Serial.println(F("REN\t rename file; <src> <targ>"));
Serial.println(F("CD\t change directory"));
Serial.println(F("MD\t make directory"));
Serial.println(F("RD\t remove directory"));
Serial.println(F("TIME\t display/set time; format <hh.mm.ss>"));
Serial.println(F("TEMP\t display temperature(s)"));
Serial.println(F("TYPE\t display ASCII-file"));
Serial.println(F("VER\t display SW- Version"));
Serial.println(F("VOL\t display SD-Card informations"));
Serial.println(F("XREC\t XModem- download to uC"));
Serial.println(F("XTRAN\t XModem- upload to Host"));
Serial.println(F("YREC\t YModem- download to uC (1kB and CRC)"));
return( eHELP );
} /* end of fnc_HELP */
/**************************************************/
/*! \brief fnc_MD
\param argument string: pointer of char
\return int- value of token
\ingroup token_parser */
/**************************************************/
int fnc_MD(const char* szCmdLn)
{
/* place your code here */
char sLine[ILINE]={""};
argPathFn( szCmdLn, &sLine[0]);
Serial.print(F(" : "));
digitalWrite(LED_BUILTIN, 1);
if (SD.begin(SDCRD)) {
if (!SD.exists(sLine)) {
int16_t iRes= SD.mkdir(sLine);
if (iRes>0) {
Serial.print(F("Dir. created"));
} else {
Serial.print(F("Dir. not created"));
}
} else {
Serial.print(F("Dir. exist"));
}
SD.end();
digitalWrite(LED_BUILTIN, 0);
}
return( eMD );
} /* end of fnc_MD */
/**************************************************/
/*! \brief fnc_PATH
\param argument string: pointer of char
\return int- value of token
\ingroup token_parser */
/**************************************************/
int fnc_PATH(const char* szCmdLn)
{
/* place your code here */
Serial.print(" : ");
Serial.print(sPath);
return( ePATH );
} /* end of fnc_PROMPT */
/**************************************************/
/*! \brief fnc_RD
\param argument string: pointer of char
\return int- value of token
\ingroup token_parser */
/**************************************************/
int fnc_RD(const char* szCmdLn)
{
/* place your code here */
char sLine[ILINE]={""};
argPathFn( szCmdLn, &sLine[0]);
Serial.print(F(" : "));
digitalWrite(LED_BUILTIN, 1);
if (SD.begin(SDCRD)) {
if (SD.exists(sLine)) {
int16_t iRes= SD.rmdir(sLine);
if (iRes>0) {
Serial.print(F("Dir. removed"));
} else {
Serial.print(F("Dir. not removed"));
}
} else {
Serial.print(F(" Dir. not found"));
}
SD.end();
}
digitalWrite(LED_BUILTIN, 0);
return( eRD );
} /* end of fnc_RD */
/**************************************************/
/*! \brief fnc_REN
\param argument string: pointer of char
\return int- value of token
\ingroup token_parser */
/**************************************************/
int fnc_REN(const char* szCmdLn)
{
/* place your code here */
// char sFrom[ILINE];
// char sTo[ILINE];
// char s1[40];
// char s2[40];
// Serial.print(F(" : "));
// szCmdLn++;
// int iRet= sscanf(szCmdLn,"%s %s", &s1, &s2);
// if (iRet ==2) {
// strcpy(sFrom, sPath);
// strcat(sFrom, "/");
// strcat(sFrom, s1);
// strcpy(sTo, sPath);
// strcat(sTo, "/");
// strcat(sTo, s2);
// if (SD.begin( SDCRD)) {
// digitalWrite(LED_BUILTIN, 1);
// bool stFS= SD.rename(sFrom, sTo);
// if (stFS) {
// Serial.print(F("OK"));
// } else {
// Serial.print(F("Error"));
// }
// SD.end();
// digitalWrite(LED_BUILTIN, 0);
// }
// } /* end if */
return( eREN );
} /* end of fnc_RENAME */
/**************************************************/
/*! \brief fnc_TEMP
\param argument string: pointer of char
\return int- value of token
\ingroup token_parser */
/**************************************************/
int fnc_TEMP(const char* szCmdLn)
{
/* place your code here */
Serial.print(" CPU: ");
//Serial.print(analogReadTemp(3.3f));
return( eTEMP );
} /* end of fnc_TEMP */
/**************************************************/
/*! \brief fnc_TIME
\param argument string: pointer of char
\return int- value of token
\ingroup token_parser */
/**************************************************/
int fnc_TIME(const char* szCmdLn)
{
/* place your code here */
char sLine[ILINE];
struct tm mytm;
RTC.getTime(inRTC);
mytm= inRTC.getTmTime();
if (strlen(szCmdLn) >= 3) {
int iResult= sscanf( szCmdLn, "%02d:%02d:%02d", &hour, &minute, &second);
if (iResult == 3) {
mytm.tm_hour= hour;
mytm.tm_min = minute;
mytm.tm_sec = second;
inRTC.setTM(mytm);
RTC.setTime(inRTC);
if (bRTC){
EXRTC.setTime((uint8_t) hour, (uint8_t) minute, (uint8_t) second);
}
}
} else {
Serial.print(" : ");
Serial.print(F(" CPU: "));
time_t tiUx= inRTC.getUnixTime();
strftime(sLine, sizeof(sLine), "%0H:%0M:%0S", localtime(&tiUx));
Serial.print(sLine);
}
return( eTIME );
} /* end of fnc_TIME */
/**************************************************/
/*! \brief fnc_TYPE
\param argument string: pointer of char
\return int- value of token
\ingroup token_parser */
/**************************************************/
int fnc_TYPE(const char* szCmdLn)
{
/* place your code here */
#define BUFLEN 1024
char sLine[ILINE]= {""};
int16_t iBufLen;
unsigned char ucBuffer[BUFLEN];
argPathFn( szCmdLn, &sLine[0]);
Serial.print(F(" : "));
digitalWrite(LED_BUILTIN, 1);
if (SD.begin(SDCRD)) {
File FH1 = SD.open(sLine, FILE_READ);
if (FH1) {
while (FH1.available()) {
iBufLen= FH1.readBytes(&ucBuffer[0], BUFLEN);
Serial.write(&ucBuffer[0], iBufLen);
}
Serial.print(F("\r\nFilesize: "));
Serial.print(FH1.size(), DEC);
Serial.print(F(" Byte"));
FH1.close();
} else {
Serial.print(sLine);
Serial.println(F(" not found!"));
}
SD.end();
digitalWrite(LED_BUILTIN, 0);
}
return( eTYPE );
} /* end of fnc_TYPE */
/**************************************************/
/*! \brief fnc_VER
\param argument string: pointer of char
\return int- value of token
\ingroup token_parser */
/**************************************************/
int fnc_VER(const char* szCmdLn)
{
/* place your code here */
Serial.print(F(" : "));
Serial.println(USB_NAME);
Serial.print(F("CPU- Frequency: "));
Serial.print(F_CPU/1000000);
Serial.println(F(" MHz"));
Serial.print(F("Softwarebuild "));
Serial.print(__DATE__);
Serial.print(F(" "));
Serial.print(__TIME__);
return( eVER );
} /* end of fnc_VER */
/**************************************************/
/*! \brief fnc_VOL
\param argument string: pointer of char
\return int- value of token
\ingroup token_parser */
/**************************************************/
int fnc_VOL(const char* szCmdLn)
{
/* place your code here */
Sd2Card card;
SdVolume volume;
uint8_t iRet;
Serial.println(F(": "));
if (SD.begin(SDCRD)) {
digitalWrite(LED_BUILTIN, 1);
if ((iRet= card.init(SDCRD)) !=0) {
Serial.print(" ");
Serial.print(iRet);
Serial.print(" ");
Serial.println(F("initialization failed."));
} else {
Serial.println(F("Card is present."));
Serial.print(F("Card type: "));
switch (card.type()) {
case SD_CARD_TYPE_SD1:
Serial.println(F("SD1"));
break;
case SD_CARD_TYPE_SD2:
Serial.println(F("SD2"));
break;
case SD_CARD_TYPE_SDHC:
Serial.println(F("SDHC"));
break;
default:
Serial.println(F("Unknown"));
}
if (!volume.init(card)) {
Serial.println(F("No partition."));
} else {
Serial.print(F("Clusters: "));
Serial.println(volume.clusterCount());
Serial.print(F("Blocks x Cluster: "));
Serial.println(volume.blocksPerCluster());
Serial.print(F("Total Blocks: "));
Serial.println(volume.blocksPerCluster() * volume.clusterCount());
uint32_t volumesize;
Serial.print(F("Volume type is: FAT"));
Serial.println(volume.fatType(), DEC);
volumesize = volume.blocksPerCluster();
volumesize *= volume.clusterCount();
volumesize /= 2;
Serial.print(F("Volume size (Mb): "));
volumesize /= 1024;
Serial.println(volumesize);
}
}
SD.end();
digitalWrite(LED_BUILTIN, 0);
}
return( eVOL );
} /* end of fnc_VOL */
/**************************************************/
/*! \brief fnc_XREC
\param argument string: pointer of char
\return int- value of token
\ingroup token_parser */
/**************************************************/
#define XENTRY 0
#define XBLKNUM 1
#define XBLKNUMI 2
#define XDATABLK 3
#define XCHKCRC 4
#define XCHKEOT 5
int fnc_XREC(const char* szCmdLn)
{
/* place your code here */
#define Y_BSIZE 1024
#define X_BSIZE 128
#define Y_TIOUT 10000
char sLine[ILINE] = {""};
int32_t iByteCnt = 0, iBlkCnt;
int8_t iRecState; // Receiver statemachine
bool bRecEnd=false;
unsigned char inChar;
int16_t iBlkSize;
unsigned char ucBuffer[Y_BSIZE];
uint64_t iStrtTi;
File FH1;
Serial.print(" : ");
if (((strlen(szCmdLn)>1)>=1)&& SD.begin(SDCRD)) {
digitalWrite(PIN_LED, 1);
argPathFn( szCmdLn, &sLine[0]);
iBlkCnt = 0;
iRecState = 0;
Serial.flush();
while (!bRecEnd) {
if (iRecState == XENTRY) {
Serial.write(NAK);
iStrtTi= millis()+Y_TIOUT;
while ( (!Serial.available()) && (millis() < iStrtTi) ) { }
if ((Serial.readBytes(&inChar,1)==1) &&(millis()<iStrtTi)) {
if (SD.exists(sLine)){
SD.remove(sLine);
} /* end if */
FH1 = SD.open(sLine, FILE_WRITE);
if ( (inChar== STX) && (millis()<iStrtTi) ) {
iBlkSize= Y_BSIZE;
iRecState = XBLKNUM; // next is BlockNr. token
} else
if ( (inChar== SOH) && (millis()<iStrtTi) ) {
iBlkSize= X_BSIZE;
iRecState = XBLKNUM; // next is BlockNr. token
} else
if ( (inChar== EOT)&&(millis()<iStrtTi) ) {
Serial.write((uint8_t)NAK); // end of transmision
} else {
bRecEnd= true;
} /* end if */
} else
if ((iBlkCnt < 5)&&(millis()>=iStrtTi) ) {
iBlkCnt++;
} else {
bRecEnd= true;
} /* end if */
} else
if (iRecState == XBLKNUM) {
iStrtTi= millis()+Y_TIOUT;
while ( (!Serial.available()) && (millis() < iStrtTi) ) { }
if ((Serial.readBytes(&inChar,1)==1) &&(millis()<iStrtTi)) {
iBlkCnt= inChar;
iRecState = XBLKNUMI;
} else {
bRecEnd= true;
} /* end if */
} else
if (iRecState == XBLKNUMI) { // inverse block counter - not used!
iStrtTi= millis()+Y_TIOUT;
while ( (!Serial.available()) && (millis() < iStrtTi) ) { }
if ((Serial.readBytes(&inChar,1)==1) &&(millis()<iStrtTi)) {
iRecState = XDATABLK;
} else {
bRecEnd= true;
} /* end if */
} else
if (iRecState == XDATABLK) {
iStrtTi= millis()+Y_TIOUT;
while((Serial.readBytes(&ucBuffer[0],iBlkSize)<iBlkSize)&&(millis()<iStrtTi) ) { }
if (millis()<iStrtTi) {
iByteCnt= iByteCnt + iBlkSize;
FH1.write(&ucBuffer[0], iBlkSize);
iRecState = XCHKCRC;
} else {
bRecEnd= true;
} /* end if */
} else
if (iRecState == XCHKCRC) { // Checksum; Hint: CRC is not supported
iStrtTi= millis()+Y_TIOUT;
while ( (Serial.readBytes(&inChar,1)<1) && (millis() < iStrtTi) ) { }
if (millis()<iStrtTi) {
Serial.write((uint8_t)ACK);
iRecState = XCHKEOT; // found Header token
} else {
bRecEnd= true;
} /* end if */
} /* end if */
if (iRecState == XCHKEOT) {
iStrtTi= millis()+Y_TIOUT;
while ( (!Serial.available()) && (millis() < iStrtTi) ) { }
if ((Serial.readBytes(&inChar,1)==1) &&(millis()<iStrtTi)) {
if ((inChar== STX) && (millis()<iStrtTi)) {
iBlkSize = Y_BSIZE;
iRecState = XBLKNUM; // found token for next data block
} else
if ((inChar== SOH) && (millis()<iStrtTi)) {
iBlkSize = X_BSIZE;
iRecState = XBLKNUM; // found token for next data block
} else
if ((inChar== EOT) && (millis()<iStrtTi)) {
Serial.write((uint8_t)ACK);
bRecEnd = true;
} else {
for (int iL= 1; iL<3; iL++){
Serial.write((uint8_t)CAN);
}
bRecEnd= true;
} /* end if */
} /* end if */
} /* end if */
} /* end while */
if (iRecState== XCHKEOT) {
FH1.close();
Serial.print("\nByte Cnt ");
Serial.print(iByteCnt);
Serial.println("\nDone");
} else {
for (int iL= 1; iL<3; iL++) {
Serial.write((uint8_t)CAN);
} /* end for */
Serial.print("\nstoped at state ");
Serial.print(iRecState);
Serial.print("\nBlock Cnt ");
Serial.print(iBlkCnt);
Serial.print("\nByte Cnt ");
Serial.print(iByteCnt);
} /* end if */
} else {
Serial.println(" missing argument!");
} /* end if */
SD.end();
digitalWrite(PIN_LED, 0);
return( eXREC );
} /* end of fnc_XREC */
/**************************************************/
/*! \brief fnc_XTRAN
\param argument string: pointer of char
\return int- value of token
\ingroup token_parser */
/**************************************************/
int fnc_XTRAN(const char* szCmdLn)
{
/* place your code here */
char sLine[ILINE]={""};
uint64_t iFSize = 0;
uint16_t iCSum, iCrc;
uint16_t iBlkCnt = 1;
uint8_t iReTr = 0;
bool bTiOut= false;
bool bTrans = false;
bool bCrc = true;
char inChar;
unsigned char ucBuffer[Y_BSIZE];
uint64_t iStrtTi;
Serial.print(F(" : "));
if (strlen(szCmdLn) > 1) {
digitalWrite(PIN_LED, 1);
argPathFn( szCmdLn, &sLine[0]);
iStrtTi = millis();
if (SD.begin(SDCRD)) {
File FH1 = SD.open(sLine, FILE_READ);
if (FH1 != 0) {
iFSize = FH1.size();
Serial.print(iFSize);
Serial.print(F(" Bytes"));
while ((FH1.available()!=0)&&(!bTiOut)) {
while ((Serial.readBytes(&inChar,1)==0) && (millis()<(iStrtTi+ (X_TIMEOUT*12)))) { }
if (millis()>=(iStrtTi+(X_TIMEOUT*12))) {
for (int iL= 1; iL<3; iL++) {
Serial.write((uint8_t) CAN);
} /* end for */
bTiOut= true;
} else
// Datablock is CRC 1kByte
if (((inChar=='C')&&(!bTrans))||((inChar==ACK)&&(bTrans)&&bCrc)) {
bCrc = true;
iStrtTi= millis();
bTrans= true;
iReTr = 0;
int iRdLen= FH1.read(ucBuffer, Y_BSIZE);
if (iRdLen > 0) {
if (iRdLen < Y_BSIZE) {
for (int iL= iRdLen; iL<Y_BSIZE; iL++){
ucBuffer[iL]= 0x00;
} /* end for */
} /* end if */
Serial.write((uint8_t) STX);
Serial.write((uint8_t) iBlkCnt);
Serial.write((uint8_t) ~iBlkCnt);
Serial.write(ucBuffer, Y_BSIZE); // block transfer
iCrc = 0;
for (int iL=0; iL<Y_BSIZE; iL++){
iCrc= uicalcCrc(ucBuffer[iL], iCrc);
} /* end for */
Serial.write((uint8_t) ((iCrc &0xff00) >>8));
Serial.write((uint8_t) (iCrc & 0x00ff));
iBlkCnt++;
iBlkCnt= iBlkCnt & 0x00ff;
// } else {
// bCrc = false; // if rest >128 Byte, transfer in 128 byte mode
} /* end if */
} else
// checksum 128 Byte
if (((inChar==NAK)&&(!bTrans))||((inChar==ACK)&&(bTrans))||!bCrc) {
bCrc= false;
iStrtTi= millis();
bTrans= true;
iCSum = 0;
iReTr = 0;
int iRdLen= FH1.read(ucBuffer, X_BSIZE);
if (iRdLen > 0) {
if (iRdLen < X_BSIZE) {
for (int iL= iRdLen; iL<X_BSIZE; iL++){
ucBuffer[iL]= 0x00;
} /* end for */
} /* end if */
Serial.write((uint8_t) SOH);
Serial.write((uint8_t) iBlkCnt);
Serial.write((uint8_t) ~iBlkCnt);
// Blocktransfer vs. single char transfer
Serial.write(ucBuffer, X_BSIZE); // block transfer
for (int iL=0; iL<X_BSIZE; iL++){
iCSum = iCSum + ucBuffer[iL];
iCSum = iCSum & 0x00ff;
} /* end for */
Serial.write((uint8_t) iCSum);
iBlkCnt++;
iBlkCnt= iBlkCnt & 0x00ff;
} /* end if */
} else
if (inChar == CAN){
for (int iL= 1; iL<3; iL++) {
Serial.write((uint8_t) CAN);
} /* end for */
bTiOut= true;
} /* end if */
} /* end while */
Serial.write((uint8_t) EOT);
while ((Serial.readBytes(&inChar,1)==0) && (millis()<(iStrtTi+ (X_TIMEOUT)))) { }
Serial.write((uint8_t) EOT);
while ((Serial.readBytes(&inChar,1)==0) && (millis()<(iStrtTi+ (X_TIMEOUT)))) { }
FH1.close();
} else {
Serial.print(sLine);
Serial.println(F(" not found!"));
} /* end if */
if (!bTiOut){
Serial.println(F("\ndone!"));
} else {
Serial.println(F("\nTimeout!"));
} /* end if */
SD.end();
digitalWrite(PIN_LED,0);
} else {
Serial.println("\nno SD-Card!");
} /* end if */
} else {
Serial.println(" no argument!");
} /* end if */
return( eXTRAN );
} /* end of fnc_XTRAN */
/**************************************************/
/*! \brief fnc_YREC
\param argument string: pointer of char
\return int- value of token
\ingroup token_parser */
/**************************************************/
#define YENTRY 0
#define YBLKNUM 1
#define YBLKNUMI 2
#define YDATABLK 3
#define YFNAMBLK 4
#define YCHKCRC 5
#define YCHKEOT 6
#define YMRET 5
int fnc_YREC(const char* szCmdLn)
{
/* place your code here */
char sLine[ILINE] = {0x00};
int32_t iByteCnt = 0, iByteSum = 0;
uint16_t iBlkCnt=0;
uint8_t uiBlkCnt[2];
int32_t iFileSize= 0;
int16_t iRecState; // Receiver statemachine
uint8_t iChkSum;
bool bRecEnd= false;
unsigned char inChar;
int16_t iBlkSize;
unsigned char ucBuffer[Y_BSIZE];
uint64_t iStrtTi;
File FH1;
Serial.print("\n");
iBlkCnt = 0;
iRecState = YENTRY;
if (SD.begin(SDCRD)) {
digitalWrite(PIN_LED, 1);
Serial.flush();
while (!bRecEnd) {
if (iRecState == YENTRY) {
Serial.write("C"); // "C" request for 1kB/CRC transmission
iStrtTi= millis() + 3000;
while ( (!Serial.available()) && (millis()<iStrtTi) ) { }
if ((Serial.readBytes(&inChar,1)==1) && (millis()<iStrtTi)) {
if ( (inChar== STX) && (millis()<iStrtTi) ) {
iBlkSize = Y_BSIZE;
iRecState = YBLKNUM; // found Header token
} else
if ( (inChar== SOH) && (millis()<iStrtTi) ) {
iBlkSize = X_BSIZE;
iRecState = YBLKNUM; // found Header token
} else
if ( (inChar== EOT)&&(millis()<iStrtTi) ) {
Serial.write((uint8_t)NAK); // end of transmision
delay (10);
Serial.write((uint8_t)ACK);
iRecState = 901;
bRecEnd = true;
} else {
} /* end if */