forked from jelinj8/dso203
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFiles.c
1480 lines (1311 loc) · 52.8 KB
/
Files.c
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
/********************* (C) COPYRIGHT 2010 e-Design Co.,Ltd. ********************
File Name : File.c
Version : DS203_APP Ver 2.5x Author : bure
*******************************************************************************/
#include <string.h>
#include "Function.h"
#include "Process.h"
#include "BIOS.h"
#include "File.h"
#include "Menu.h"
#include "Calibrat.h"
#include "Interrupt.h"
u16 SectorSize = 0;
u8 flash_mode = 0;
u8 SecBuff[LARGEST_SECTOR_SIZE];
uc8 DiskDevInfo_8M[]={"8MB Internal"};
void Print_Clk(u8 Phase);
u8 OverWriteWarn(void);
void ProcessConfigName(void);
void FileMessage(u8 i);
u8 WriteFileSec(u8* SecBuff, u16* pCluster, u32* pDirAddr);
s32 CSVdata(u8 Ch,u8 Inv,u16 i,u8 data);
u8 CSVdataModeLogic(void);
u8 SaveShortBuffXpos;
u8 FileBuff[1600];
u16 TempPar[74];
u8 Versions;
u16 ArbtSampleNumber;
u32 UartFileSize=0;
char Label[15][13];
char Ext[4]={'C','F','G',0};
u16 DirRange=0;
char SelectedFileName[9];
char ConfigFileName[9];
char LastAccessedConfig[9]={32,32,32,32,32,32,32,32,0};
u8 Edited=0;
u8 BufferRestore=0;
u32 RootStart=0x00004000;
u32 RootStop =0x00007F00;
uc32 FatStart=0x00001000;
u16 CSVposition;
const char Offset[5]= {" Os="};
const char SPeriod[7]= {" PER="};
const char Sampling[4][14]={ {" ChA: VOLTS"},{" ChB: VOLTS"},{" ChC: 0= "},{" ChD: 0= "} };
const char OSsampling[4][14]={ {" ChA:HI VOLTS"},{" ChA:LO VOLTS"},{" ChB:HI VOLTS"},{" ChB:LO VOLTS"} };
const char SamplingPeriod[22][8]={ {"33.333mS"},{"16.667mS"},{"6.6667mS"},{"3.3333mS"},{"1.6667mS"},{"666.67uS"},{"333.33uS"},
{"166.67uS"},{"66.667uS"},{"33.333uS"},{"16.667uS"},{"6.6667uS"},{"3.3333uS"},{"1.6667uS"},
{"666.67nS"},{"333.33nS"},{"166.67nS"},{"69.444nS"},{"41.667nS"},{"27.778nS"},{"13.889nS"},
{"13.889nS"} };
const char SubSamplingPeriod[13][8]= { {"3.3333mS"},{"6.6667mS"},{"16.667mS"},{"33.333mS"},{"66.667mS"},{"166.7mS"},{"333.33mS"},
{"500.00mS"},{"1.0000 S"},{"2.0000 S"},{"4.0000 S"},{"10.000 S"},{"20.000 S"} };
const char DigSampling[4][12]={ {" ChA X "},{" ChB X "},{" ChC +20=1"},{" ChD +20=1"} };
const char AmplitudeSteps[8][6]= { {"2.00mV"},{"4.00mV"},{"8.00mV"},{"20.0mV"},{"40.0mV"},{"80.0mV"},{"200 mV"},{"400 mV"} };
const char AmplitudeX10Steps[8][6]= { {"20.0mV"},{"40.0mV"},{"80.0mV"},{"200 mV"},{"400 mV"},{"800 mV"},{"2.00 V"},{"4.00 V"} };
const char ErrorCode[5][6]={" OK "," FULL"," NONE"," ERR"," CNCL"};
const char Clk[4][2]={"|","/","-","$"};
uc16 BMP_Color[16] = { WHT, CYAN, CYAN_, YEL, YEL_, PURPL, PURPL_, GRN, //for 16 color BMP's
GRN_, GRAY, ORANGE, BLUE, RED, BLACK, 0x87F0, 0x7BEF};
//16 color BMP file header
uc8 BmpHead16[54] = { 0X42, 0X4D, 0XF8, 0XBB, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X76, 0X0, 0X00, 0X00, 0X28, 0X00,
0X00, 0X00, 0X90, 0X01, 0X00, 0X00, 0XF0, 0X00, 0X00, 0X00, 0X01, 0X0, 0X04, 0X00, 0X00, 0X00,
0X00, 0X00, 0X82, 0XBB, 0X00, 0X00, 0X12, 0X0B, 0X00, 0X00, 0X12, 0XB, 0X00, 0X00, 0X10, 0X00,
0X00, 0X00, 0X00, 0X00, 0X00, 0X00};
//byte 28= 0x04 bits/pixel
//64K color BMP file header
uc8 BmpHead64[66] = { 0X42, 0X4D, 0X42, 0XEE, 0X02, 0X00, 0X00, 0X00, 0X00, 0X00, 0X42, 0X0, 0X00, 0X00, 0X28, 0X00,
0X00, 0X00, 0X90, 0X01, 0X00, 0X00, 0XF0, 0X00, 0X00, 0X00, 0X01, 0X0, 0X10, 0X00, 0X03, 0X00,
0X00, 0X00, 0X00, 0XEE, 0X02, 0X00, 0X12, 0X0B, 0X00, 0X00, 0X12, 0XB, 0X00, 0X00, 0X00, 0X00,
0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X1F, 0X00, 0X00, 0X00, 0XE0, 0X07,0X00, 0X00, 0X00, 0XF8,
0X00, 0X00};
//byte 28= 0x10 bits/pixel
/*******************************************************************************
Initialize the file system return value: 0x00 = successful
Primarily sets variables for flash_mode and SectorSize
*******************************************************************************/
u8 InitFileSystem() {
u32 ptr;
u8 Ver[8];
if (SectorSize!=0) return 0;
ptr=__Get(DFUVER);
memcpy(Ver,(u8*)ptr,5);
ptr=(Ver[1]-'0')*100 +(Ver[3]-'0')*10 +(Ver[4]-'0');
if(ptr<=311){
flash_mode=FLASH_2M;
SectorSize = FLASH_2M_SECTOR_SIZE;
}
else{
ptr=__Get(DEVICEINFO);
if(memcmp((u8*)ptr,DiskDevInfo_8M,3)==0){
flash_mode=FLASH_8M;
SectorSize = FLASH_8M_SECTOR_SIZE;
}
else {
flash_mode=FLASH_2M;
SectorSize = FLASH_2M_SECTOR_SIZE;
}
}
if (SectorSize>LARGEST_SECTOR_SIZE) { //This should never happen
SectorSize = 0;
return 255;
}
if(flash_mode==FLASH_2M){ //root dir starting/ending addresses
RootStart=0x00004000;
RootStop =0x00007F00;
}else{
RootStart=0x00003000;
RootStop =0x00006F00;
}
return 0;
}
/*******************************************************************************
Print_Clk: progress indicator
*******************************************************************************/
void Print_Clk(u8 Phase)
{
if((AutoSaveBuf==0)||(ChartLogic()==0))Print_Str(250,0,0x0405,INV,(char*)Clk[Phase]);
}
/*******************************************************************************
Open the specified file extension input: The file extension return value: 0x00 = successful
*******************************************************************************/
u8 Make_Filename(u8 FileNum, char* FileName)
{
char Num[4];
u8ToDec3(Num, FileNum,0);
FileName[4] = Num[0];
FileName[5] = Num[1];
FileName[6] = Num[2];
return InitFileSystem();
}
/*******************************************************************************
Obtained corresponds to the current color palette number
*******************************************************************************/
u8 Color_Num(u16 Color) //for 16 color BMP's
{
if(Color == WHT) return 0;
//else if(Color== CYAN ) return 1;
else if((Color== CYAN)||(Color==0xEF60)||(Color==0xF7A0)) return 1;
//else if(Color== YEL ) return 3;
else if((Color== YEL) ||(Color==0x077D)||(Color==0x07BE)) return 3;
else if(Color== PURPL ) return 5;
else if(Color== GRN ) return 7;
//else if(Color== CYAN_ ) return 2;
else if((Color==CYAN_)||(Color==0xDEE0))return 2; //include alt fast dims
//else if(Color== YEL_ ) return 4;
else if((Color== YEL_)||(Color==0x06FB))return 4;
else if(Color== PURPL_) return 6;
else if(Color== GRN_ ) return 8;
else if(Color== GRAY ) return 9;
else if(Color== ORANGE) return 10;
else if(Color== BLUE ) return 11;
else if(Color== RED ) return 12;
//else return 13;
else if(Color== BLACK ) return 13; //add black as viable color so background saves properly
else if(Color== 0x87F0) return 14; //bright green used in serial decoding hex values and arrows added
else if(Color== 0x7BEF) return 15; //dark gray for i2c arrows
else return 9; //anything else would be grid background mixed with something so show grid, not black holes
}
u8 Save_Img(void){
u32 i;
u16 j=0,k=0;
char ChkSum=0,RChkSum=0;
u16 pCluster[3];
u32 pDirAddr[1];
if (InitFileSystem()!=0) return 10; //does not use make_filename so may not be initiated
if(__OpenFileWr(SecBuff,"ROM_IMG BIN", pCluster, pDirAddr)!=OK) return DISK_ERR;
for(i=0;i<0x80000;i++){ //SecBuff here will be either 512 or 4096
SecBuff[j]=*(char*)(0x8000000+i);
ChkSum^=SecBuff[j];
if(++j>=SectorSize){
j=WriteFileSec(SecBuff, pCluster, pDirAddr);
if(j)return j;
j=0;
Print_Clk((k++>>1)& 3); // progress indication
}
}
if(__CloseFile(SecBuff,0x80000,pCluster,pDirAddr)!= OK) return WR_ERR;
i=__OpenFileRd(SecBuff,"ROM_IMG BIN",pCluster,pDirAddr); //check file for checksum
if(i != OK) return i;
j=0;
for(i=0;i<0x80000;i++){
if((j>=SectorSize)||(j==0)){
j=0;
if(__ReadFileSec(SecBuff,pCluster)!= OK) return RD_ERR;
Print_Clk((k++>>1)& 3); // progress indication
}
RChkSum^=SecBuff[j++];
}
if(RChkSum!=ChkSum)return WR_ERR;
return 0;
}
/*******************************************************************************
Load_Dat: Load the saved screen image of the original data input: The file number return value: 0x00 = successful
*******************************************************************************/
u8 Load_Dat(u8 FileNum)
{
char Filename[12] = "FILE DAT";
u16 i;
u16 pCluster[3];
u32 pDirAddr[1];
if ((Current==9)&&(_Det==3)){
for(i=0;i<8;i++)Filename[i]=SelectedFileName[i];
}else{
if (Make_Filename(FileNum, Filename)!=0) return 1;
}
i = __OpenFileRd(SecBuff, Filename, pCluster, pDirAddr);
if(i != OK) return i;
if(flash_mode==FLASH_2M){
for(i=0; i<4; i++){
if(__ReadFileSec(SecBuff, pCluster)!= OK) return RD_ERR;
memcpy(&FileBuff[i*400], SecBuff,400);
}
}
else{
if(__ReadFileSec(SecBuff, pCluster)!= OK) return RD_ERR;
for(i=0; i<4; i++)
memcpy(&FileBuff[i*400], &SecBuff[i*512],400);
}
return 0;
}
/*******************************************************************************
Save_Dat: save current screen to display the original image data input: the file number return value: 0x00 = success
*******************************************************************************/
u8 Save_Dat(u8 FileNum)
{
char Filename[13] = "FILE DAT";
u16 i, j;
u16 pCluster[3];
u32 pDirAddr[1];
if ((Current==9)&&(_Det==3)){
for(i=0;i<8;i++)Filename[i]=SelectedFileName[i];
}else{
if (Make_Filename(FileNum, Filename)!=0) return 1;
}
if(__OpenFileWr(SecBuff, Filename, pCluster, pDirAddr)!=OK) return DISK_ERR;
memset(SecBuff, 0, LARGEST_SECTOR_SIZE);//512
if(flash_mode==FLASH_8M){
for(j=0; j<4; j++){
for(i=0; i<397; i++) SecBuff[i+j*512] = TrackBuff[i*4 + j];
SecBuff[397+j*512] = Title[j][POSI].Value;
}
i=WriteFileSec(SecBuff, pCluster, pDirAddr);
if(i)return i;
Print_Clk(j & 3); // progress indication
}
else
{
for(j=0; j<4; j++){
for(i=0; i<397; i++) SecBuff[i]= TrackBuff[i*4 + j];
SecBuff[397] = Title[j][POSI].Value;
i=WriteFileSec(SecBuff, pCluster, pDirAddr);
if(i)return i;
Print_Clk(j & 3); // progress indication
}
}
if(__CloseFile(SecBuff, 0x0800, pCluster, pDirAddr)!= OK) return WR_ERR;
return OK;
}
/*******************************************************************************
Save_Bmp: save the current screen image as BMP input: file number return value: 0x00 = successful
*******************************************************************************/
u8 Save_Bmp(u8 FileNum)
{
u32 FileSize;
char Filename[12] = "IMAG BMP";
u16 k, i,x=0, y=0, j, ColorH, ColorL; //, tmp; // j, ColorH, ColorL;
register u16 tmp;
u16 pCluster[3];
u32 pDirAddr[1];
if ((Current==9)&&(_Det==3)){
for(i=0;i<8;i++)Filename[i]=SelectedFileName[i];
}else{
if (Make_Filename(FileNum, Filename)!=0) return 1;
}
if(__OpenFileRd(SecBuff, Filename, pCluster, pDirAddr)==0){ // original file exists
if(OverWriteWarn())return 200;
if(DeleteFile(SecBuff,Filename))return WR_ERR;
}
if(__OpenFileWr(SecBuff, Filename, pCluster, pDirAddr)!=OK) return DISK_ERR;
if((_4_source==10)||(_4_source==11)){ //only save in 64K color if displaying spectrograph or map
memcpy(SecBuff, BmpHead64, 66); //64K COLOR BMP
i = 0x0042; // the image data stored address
k = 0;
for(y=0; y<240; y++){
for(x=0; x<400 ; x++){
__Point_SCR(x, y);
tmp =__LCD_GetPixl();
SecBuff[i++]=tmp&0xFF; SecBuff[i++]=tmp>>8;
if(i>=SectorSize){ //512
i=WriteFileSec(SecBuff, pCluster, pDirAddr);
if(i)return i;
Print_Clk((k++ >>1)& 3); // progress indicator
i=0;
}
}
}
FileSize=0x2F000;
}else{
memcpy(SecBuff, BmpHead16, 54); //16 COLOR BMP
i = 0x0036; // palette to store the starting address
for(j=0; j<16; ++j){
SecBuff[j*4 +i+0]=(BMP_Color[j]& 0xF800)>>8; // Blue
SecBuff[j*4 +i+1]=(BMP_Color[j]& 0x07E0)>>3; // Green
SecBuff[j*4 +i+2]=(BMP_Color[j]& 0x001F)<<3; // Red
SecBuff[j*4 +i+3]= 0; // Alpha
}
i = 0x0076; // the image data stored address
k = 0;
for(y=0; y<240; y++){
for(x=0; x<400 ; x+=2){
__Point_SCR(x, y);
ColorH =__LCD_GetPixl();
__Point_SCR(x+1, y);
ColorL =__LCD_GetPixl();
SecBuff[i] =(Color_Num(ColorH)<<4)+ Color_Num(ColorL);
i++;
if(i>=SectorSize){
i=WriteFileSec(SecBuff, pCluster, pDirAddr);
if(i)return i;
Print_Clk((k++ >>1)& 3); // progress indicator
i=0;
}
}
}
FileSize=0xBC00;
}
if(i!=0){
i=WriteFileSec(SecBuff, pCluster, pDirAddr);
if(i)return i;
}
if(__CloseFile(SecBuff, FileSize, pCluster, pDirAddr)!= OK) return WR_ERR; //2nd var just seems to be size stamp, alterbios
return 0; //does not seem to care
}
u8 Load_Bmp(u8 FileNum){
u8 i,y,start,FinishFactor;
u16 j,h,x;
char Filename[13] = "IMAG BMP";
u16 pCluster[3];
u32 pDirAddr[1];
if ((Current==9)&&(_Det==3)){
for(i=0;i<8;i++)Filename[i]=SelectedFileName[i];
}else{
if (Make_Filename(FileNum, Filename)!=0) return 1;
}
i = __OpenFileRd(SecBuff, Filename, pCluster, pDirAddr);
if(i != OK) return i;
if(__ReadFileSec(SecBuff, pCluster)!= OK) return RD_ERR;
if(flash_mode==FLASH_8M)FinishFactor=8; else FinishFactor=1;
if(SecBuff[28]==16){ //if file is 64k color type
x=0;
y=0;
for(h=0; h<(376/FinishFactor); h++){
if(h==0)start=66;else start=0;
for(j=start;j<(512*FinishFactor);j+=2){
__Point_SCR(x,y);
__LCD_SetPixl(((u16)(SecBuff[j+1]<<8))|SecBuff[j]);
if(x<399)x++;else {x=0;y++;}
if((x>=399)&&(y>=239))goto EndOfFile;
}
if(__ReadFileSec(SecBuff, pCluster)!= OK) return RD_ERR;
}
}else if(SecBuff[28]==4){ //16 color type
x=0;
y=0;
for(h=0; h<(96/FinishFactor); h++){
if(h==0)start=0x0076;else start=0;
for(j=start;j<(512*FinishFactor);j++){
__Point_SCR(x,y);
__LCD_SetPixl(BMP_Color[SecBuff[j]>>4]);
__Point_SCR(x+1,y);
__LCD_SetPixl(BMP_Color[SecBuff[j]&0x0F]);
if(x<398)x+=2;else {x=0;y++;}
if((x>=398)&&(y>=239))goto EndOfFile;
}
if(__ReadFileSec(SecBuff, pCluster)!= OK) return RD_ERR;
}
}
EndOfFile:
return 0;
}
/*******************************************************************************
Save_Buf: save the data collection buffer in BUF format input: file number return value: 0x00 = successful
*******************************************************************************/
u8 Save_Buf(u8 FileNum)
{
u8 i;
u16 j;
char Filename[12] = "DATA BUF";
u16* p ;
u16 pCluster[3];
u32 pDirAddr[1];
if ((Current==9)&&(_Det==3)){
for(i=0;i<8;i++)Filename[i]=SelectedFileName[i];
}else{
if (Make_Filename(FileNum, Filename)!=0) return 1;
}
if(__OpenFileRd(SecBuff, Filename, pCluster, pDirAddr)==0){ // original file exists
if((AutoSaveBuf==0)||(ChartLogic()==0)){
if(OverWriteWarn())return 200;
}
if(DeleteFile(SecBuff,Filename))return WR_ERR;
}
if(__OpenFileWr(SecBuff, Filename, pCluster, pDirAddr)!=OK) return DISK_ERR;
if(OSBufferLogic()){ //store OS data in unused part of DataBuf
for(i=0;i<2;i++){
for(j=512;j<1024;j++){
DataBuf[j+(i*512)]&=0x00FFFFFF;
DataBuf[j+(i*512)]|=(u32)OsFFTData[i][j-512]<<24;
}
}
for(i=0;i<2;i++){
for(j=1536;j<2048;j++){
DataBuf[j+(i*512)]&=0x00FFFFFF;
DataBuf[j+(i*512)]|=(u32)RMSdata[i][j-1536]<<24;
}
}
}
for(i=0; i<(BUFFER_SIZE/SectorSize); i++){ // < 16384/ 512 or 4096 = 32 or 4
memcpy(SecBuff, &(DataBuf[i*SectorSize/4]), SectorSize);
j=WriteFileSec(SecBuff, pCluster, pDirAddr);
if(j)return j;
Print_Clk((i >>1)& 3); // progress indication
}
memset(SecBuff, 0, 512);
p =(u16*)SecBuff;
for(i=0; i<4; i++){ // save each corresponding value in the display menu
*p++ = Title[i][0].Value;
*p++ = Title[i][1].Value;
*p++ = Title[i][2].Value;
*p++ = Title[i][3].Value;
}
*p++ = Title[6][0].Value;
*p++ = Title[6][1].Value;
*p++ = 0x00FF & Ka1[_A_Range];
*p++ = Ka2[_A_Range];
*p++ = 0x00FF & Kb1[_B_Range];
*p++ = Kb2[_B_Range];
*p++ = OSBuffer;
*p++ = FlagFrameMode;
*p++ = OSAvg;
*p++ = SubIndex;
*p++ = Title[7][0].Value;
*p++ = Title[7][1].Value;
*p++ = ChartMode;
*p++ = FPGAosFlag;
i=WriteFileSec(SecBuff, pCluster, pDirAddr);
if(i)return i;
if(__CloseFile(SecBuff, 0x4200, pCluster, pDirAddr)!= OK) return WR_ERR;
return 0;
}
u8 WriteFileSec(u8* SecBuff, u16* pCluster, u32* pDirAddr){
if(*pCluster>2040){ //if trying to write beyond the end of the drive
__CloseFile(SecBuff, 0, pCluster, pDirAddr); //will result in a 0 length file and use up space, but at least will
return OVER; //not destroy the file allocation table by looping around
}else{
if(__ProgFileSec(SecBuff, pCluster)!= OK) return WR_ERR;
}
return 0;
}
u8 Load_Arbt(u8 FileNum){
u8 *p;
u16 i,Sample=0;
u8 Tmp,j=0,Start=0;
char ParseStr[6];
u16 pCluster[3];
u32 pDirAddr[1];
char Filename[12] = "DATA ARB";
if ((Current==9)&&(_Det==3)){
for(i=0;i<8;i++)Filename[i]=SelectedFileName[i];
}else{
if (Make_Filename(FileNum, Filename)!=0) return 1;
}
i = __OpenFileRd(SecBuff, Filename, pCluster, pDirAddr);
if(i != OK) return i;
if(__ReadFileSec(SecBuff, pCluster)!= OK) return RD_ERR;
p=(u8*)SecBuff;
while(*p==48){p++;Start++;} //strip off any leading 0's
while(*p!=44){
if((*p>47)&&(*p<58))ParseStr[j++]=*p++;else p++; //retrieve "header" containing number of samples to be used
Start++;
if((j>3)||(Start>200))return 1; //return ERR if samples >3 digits or comma not found after 200 chars
} //break out and continue once comma delimiter is found
p++; //move past delimiter
ArbtSampleNumber=AsciiToU16(ParseStr,j);
j=0;
if((ArbtSampleNumber<2)||(ArbtSampleNumber>720)) //return ERR if number of samples out of range
{ArbtSampleNumber=0;return 1;}
while(Sample<720){
for(i=Start;i<SectorSize;i++){
Tmp=*p++;
if(Tmp==44){ //once delimiter found, convert and load value
ATT_DATA[Sample]= AsciiToU16(ParseStr,j);
if(ATT_DATA[Sample]<0)ATT_DATA[Sample]=0;
if(ATT_DATA[Sample]>4095)ATT_DATA[Sample]=4095; //clip if out of range
j=0; Sample++; //reset string position counter and advance sample position
}else{ //start loading string here
if((Tmp>47)&&(Tmp<58))ParseStr[j++]=Tmp; //eliminate white space or other non relevant chars
if(j>4)j=4; //limit to 5 valid characters
}
if(Sample>(ArbtSampleNumber-1))goto EndOfFile; //if sample goes to ArbtSampleNmber, is now past base 0 sample number index
}//for(i=Start
Start=0;
if(__ReadFileSec(SecBuff, pCluster)!= OK) return RD_ERR;
p=(u8*)SecBuff;
}
EndOfFile:
return 0;
}
u8 ReadDir(char *ext){ //Load 10 filenames matching extension in Label, starting with match # DirRange
u16 n,EntryCounter=0;
u8 i;
u32 pDirAddr[1];
u8 pBuffer[266];
u8 LabelIndex=0;
for(n=0;n<15;n++){
for(i=0;i<13;i++)Label[n][i]=0;
}
for(*pDirAddr=RootStart; *pDirAddr<RootStop;){ //scan for filenames in root directory
if(__ReadDiskData(pBuffer, *pDirAddr, 256)!= OK) //return and display error
{FileMessage(0xFF);return 0;}
for(n=0; n<256; n+=32){
if(pBuffer[n]==0)return LabelIndex; //no more entries //A PARTIALLY FILLED LIST FRAME...
if((pBuffer[n+8]==ext[0])&&(pBuffer[n+9]==ext[1]) //look for file extension match
&&(pBuffer[n+10]==ext[2])&&(pBuffer[n]!=0xE5)){ //and exclude deleted files
if(EntryCounter>=DirRange){ //start offset for next files
for(i=0;i<8;i++)Label[LabelIndex][i]=pBuffer[n+i]; //copy first 8 chars of filename
Label[LabelIndex][8]=0x2E; //"."
for(i=0;i<3;i++)Label[LabelIndex][9+i]=ext[i]; //extension
LabelIndex++;
}
EntryCounter++;
}
if(LabelIndex>=15)return LabelIndex; //list is full
*pDirAddr += 32;
}
}
return LabelIndex;
}
u8 DeleteFile(u8* pBuffer, char* FileName){
u32 pDirAddr[1];
u16 pCluster[1];
u16 i=0,n,Offset;
for(*pDirAddr=RootStart; *pDirAddr<RootStop;){ //scan for filename in root directory
if(__ReadDiskData(pBuffer, *pDirAddr, 4096)!= OK)return RD_ERR;
for(n=0; n<4096; n+=32){
for(i=0; i<11; i++){
if(pBuffer[n + i]!= 0){
if(pBuffer[n+i]!=FileName[i]) break;
if(i == 10){
*pCluster=*(u16*)(pBuffer+n+0x1A); //starting cluster in FAT table for selected file
goto FileNameFound; //n represents the offset
}
}
}
}
*pDirAddr+=4096 ;
}
return RD_ERR;
FileNameFound:
pBuffer[n]=0xE5; //write deleted file flag at address offset 0 in pBuffer
if(__ProgDiskPage(pBuffer,*pDirAddr)!=0)return WR_ERR; //write section back to disk (4096 bytes for 8M, 256 for 2M)
if(__ReadDiskData(pBuffer,FatStart,4096)!= OK)return RD_ERR; //load FAT chain table in pBuffer
if(*pCluster<2)return RD_ERR;
do{ //Follow the white rabbit, but don't get lost, or your files will end up cross-linked!
Offset=*pCluster+(*pCluster/2); //scale address to accommodate extra nibbles
if(Offset>=3072)return RD_ERR;
if(i++>2048)return RD_ERR; //all good things MUST come to an end
if(*pCluster&1){
*pCluster=pBuffer[Offset+1]; //Most Significant byte, save value pointing to next link before zeroing
*pCluster=(*pCluster<<4)+(pBuffer[Offset]>>4); //Shift to proper position and add little endian nibble
pBuffer[Offset]&=0x0F; //zero, to release link
pBuffer[Offset+1]=0;
}else{
*pCluster=pBuffer[Offset+1]&0x0F; //Construct 12 bit entry within proper 2 bytes of double entry "triplet",
*pCluster=(*pCluster<<8)+pBuffer[Offset]; //depending on whether the entry number is odd or even.(Convolutions
pBuffer[Offset]=0; //courtesy of Microsoft et al. who just HAD to pack a 12 bit entry into
pBuffer[Offset+1]&=0xF0; //1.5 bytes instead of 2, thus saving a small fraction of 1% of drive
} //space on a typical floppy; bytes were precious in the age of floppies...)
}while(*pCluster<0xFF0);
if(__ProgDiskPage(pBuffer,FatStart)!=0)return WR_ERR; //write section back to disk
//synch FAT2 with FAT1
if(__ReadDiskData(pBuffer,0x1000,0x1000)!= OK) return RD_ERR;
if(__ProgDiskPage(pBuffer,0x2000)!= OK) return WR_ERR;
if(*pCluster<0xFF0)return RD_ERR; //end of file to be deleted not found, return error
return 0;
}
void FileMessage(u8 i){
if((i==3)||(i==4))i-=2; else{
if(i==200)i=4;else if(i>0)i=3;
}
Print_Str(230, 0,0x050A, PRN,(char*)ErrorCode[i]);
if((_Curr[2].Value==BUF)&&(_Curr[0].Value == LOAD))Delayms(300);else Delayms(900);
_Curr[1].Flag |= UPDAT;
_Curr[2].Flag |= UPDAT;
}
u8 Load_Uart(u8 FileNum){
u16 n=0;
u8 i;
u16 pCluster[3];
u32 pDirAddr[1];
u8 pBuffer[266];
char Filename[12] = "DATA UAR";
UartFileSize=0;
if ((Current==9)&&(_Det==3)){
for(i=0;i<8;i++)Filename[i]=SelectedFileName[i];
}else{
if (Make_Filename(FileNum, Filename)!=0) return 1;
}
i = __OpenFileRd(SecBuff, Filename, pCluster, pDirAddr);
if(i != OK) return i;
for(*pDirAddr=RootStart; *pDirAddr<RootStop;){ //scan for filename in root directory
if(__ReadDiskData(pBuffer, *pDirAddr, 256)!= OK)return RD_ERR;
for(n=0; n<256; n+=32){
for(i=0; i<11; i++){
if(pBuffer[n + i]!= 0){
if(pBuffer[n + i]!= Filename[i]) break;
if(i == 10)goto access_size; //with complete filename match, hold Directory address
}
}
*pDirAddr += 32;
}
}
return RD_ERR;
access_size:
if(__ReadDiskData(pBuffer, *pDirAddr+28, 4)!= OK)return RD_ERR; //read file size value at offset 28 of FAT12 directory entry
for(n=0;n<4;n++)UartFileSize+=(u32)pBuffer[n]<<(n*8); //assemble u32 value from 4 bytes
if(UartFileSize>4096)UartFileSize=4096; //limit to size of SecBuff
n=0;
while(n<UartFileSize){
if(__ReadFileSec(&SecBuff[n], pCluster)!= OK) return RD_ERR;
n+=SectorSize;
}
return 0;
}
/*******************************************************************************
Load_Buf: load saved data into acquisition buffer input: file number return value: 0x00 = successful
*******************************************************************************/
u8 Load_Buf(u8 FileNum)
{
u8 i,j;
u16 h;
char Filename[13] = "DATA BUF";
u16 *p;
u16 pCluster[3];
u32 pDirAddr[1];
save_parameter();
if ((Current==9)&&(_Det==3)){
for(i=0;i<8;i++)Filename[i]=SelectedFileName[i];
}else{
if (Make_Filename(FileNum, Filename)!=0) return 1;
}
i = __OpenFileRd(SecBuff, Filename, pCluster, pDirAddr);
if(i != OK) return i;
for(i=0; i<((BUFFER_SIZE/SectorSize)); i++){
if(__ReadFileSec(SecBuff, pCluster)!= OK) return RD_ERR;
memcpy(&(DataBuf[i*SectorSize/4]),SecBuff,SectorSize);
}
if(__ReadFileSec(SecBuff, pCluster)!= OK) return RD_ERR;
p =(u16*)SecBuff;
for(i=0; i<4; i++){
for(j=0;j<4;j++){
Title[i][j].Value = *p++; // restore
Title[i][j].Flag = UPDAT;
}
}
Title[6][0].Value = *p++;
Title[6][1].Value = *p++;
Title[6][0].Flag = UPDAT;
Title[6][1].Flag = UPDAT;
Ka1[_A_Range] = (*p++ );
Ka2[_A_Range] = (*p++ );
Kb1[_B_Range] = (*p++);
Kb2[_B_Range] = (*p++);
OSBuffer = *p++;
FlagFrameMode = *p++;
OSAvg = *p++;
SubIndex = *p++;
Title[7][0].Value = *p++;
Title[7][1].Value = *p++;
Title[7][0].Flag = UPDAT;
Title[7][1].Flag = UPDAT;
Title[7][2].Flag = UPDAT;
ChartMode = *p++;
FPGAosFlag = *p++;
if(ChartMode>1)ChartMode=0;
if(FPGAosFlag>1)FPGAosFlag=0;
if(Title[3][0].Value < 11){ //set proper menu sub items for ch 4
if(Detail[3]==2)Detail[3]++;
}else{
if(Detail[3]==3)Detail[3]--;
}
if(OSBufferLogic()){ //restore OS data
for(i=0;i<2;i++){
for(h=512;h<1024;h++){
OsFFTData[i][h-512]=DataBuf[h+(i*512)]>>24;
}
}
for(i=0;i<2;i++){
for(h=1536;h<2048;h++){
RMSdata[i][h-1536]=DataBuf[h+(i*512)]>>24;
}
}
}
Title[RUNNING][STATE].Value = 2; // set to "HOLD" status
Title[RUNNING][STATE].Flag |= UPDAT; // set the update flag
XposRef=GetXposRef(_X_posi.Value);
UpdateFileMenu();
BufferRestore=1;
return 0;
}
void save_parameter(void){
u16 *p;
u8 i,j;
if(TempPar[0]!=0xAA55){ //do not save temp parameters again if already saved, indicates original parms not restored
p = TempPar; //and BUF parameters still active from previous BUF load(s)
*p++ = 0xAA55;
for(i=0; i<4; i++){ // save each corresponding value in the display menu
for(j=0;j<4;j++)*p++ = Title[i][j].Value;
}
*p++ = Title[6][0].Value;
*p++ = Title[6][1].Value;
for(i=0; i<8; i++){
*p++ = 0x00FF & Ka1[i];
*p++ = Ka2[i];
*p++ = 0x00FF & Kb1[i];
*p++ = Kb2[i];
}
*p++ = OSBuffer;
*p++ = FlagFrameMode;
*p++ = OSAvg;
*p++ = Title[6][2].Value; //xpos
*p++ = SubIndex;
*p++ = Title[7][0].Value;
*p++ = Title[7][1].Value;
*p++ = Detail[3]; //sub menu position of ch 4
*p++ = ChartMode;
*p++ = FPGAosFlag;
}
}
void reset_parameter(void)
{
u16* p;
u8 i,j;
p=TempPar;
p++;
if(TempPar[0]!=0xAA55) return;
for(i=0; i<4; i++){
for(j=0;j<4;j++){
Title[i][j].Value = *p++; // restore
Title[i][j].Flag = UPDAT;
}
}
Title[6][0].Value = *p++;
Title[6][1].Value = *p++;
Title[6][0].Flag = UPDAT;
Title[6][1].Flag = UPDAT;
for(i=0; i<8; i++){
Ka1[i] = (s8)(*p++ );//& 0xff);
Ka2[i] = (*p++ );
Kb1[i] = (s8)(*p++);// & 0xff);
Kb2[i] = (*p++);
}
OSBuffer = *p++;
FlagFrameMode = *p++;
OSAvg = *p++;
Title[6][2].Value = *p++;
SubIndex = *p++;
Title[7][0].Value = *p++;
Title[7][1].Value = *p++;
Title[7][0].Flag = UPDAT;
Title[7][1].Flag = UPDAT;
Title[7][2].Flag = UPDAT;
Detail[3] = *p++; //sub menu position of ch 4
ChartMode = *p++;
FPGAosFlag = *p++;
p = TempPar;
*p++ = 0;
}
u8 CSVdataModeLogic(void){
return(((OSBufferLogic()==0)&&(DigChLockout==0)&&((!ChartLogic())||(ChartMode==0)))
&&((_3_source)||(_4_source==1)||(_4_source==4)||(_4_source==5)) );
}
s32 CSVdata(u8 Ch,u8 Inv,u16 i,u8 data){
u8 range=Title[Ch][RANGE].Value;
s32 temp;
s8 *p1[2];u16 *p2[2];
p1[0]=Ka1;p1[1]=Kb1;p2[0]=Ka2;p2[1]=Kb2;
p1[Ch]+=range;temp=*p1[Ch]+((data-ADCoffset)-Title[Ch][POSI].Value);
p2[Ch]+=range;if(CalFlag>0)temp=(*p2[Ch]*temp)/1024;
temp*= Y_Attr[range].SCALE;
if(Title[Ch][SOURCE].Value>2)temp*=10;
if(Inv)temp=-temp;
return temp;
}
/*******************************************************************************
Save_Csv: save the acquisition buffer in CSV format input: file number return value: 0x00 = successful
*******************************************************************************/
u8 Save_Csv(u8 FileNum)
{
char Num[4][6];
char Val[4];
char Filename[12] = "DATA CSV";
u32 i, k = 0,length = SectorSize;
s32 temp;
u8 count, j,h=1,m=1;
u16 n = 0;
u16 pCluster[3];
u32 pDirAddr[1];
if ((Current==9)&&(_Det==3)){
for(i=0;i<8;i++)Filename[i]=SelectedFileName[i];
}else{
if (Make_Filename(FileNum, Filename)!=0) return 1;
}
length = SectorSize;
if(__OpenFileRd(SecBuff, Filename, pCluster, pDirAddr)==0){ // original file exists
if((AutoSaveBuf==0)||(ChartLogic()==0)){
if(OverWriteWarn())return 200;
}
if(DeleteFile(SecBuff,Filename))return WR_ERR;
}
if(__OpenFileWr(SecBuff, Filename, pCluster, pDirAddr)!=OK) return DISK_ERR;
//============================================HEADERS===========================================
memcpy(&SecBuff[k],SPeriod,7);k+=7;
if(ChartLogic()){memcpy(&SecBuff[k],SubSamplingPeriod[SubIndex-1],8);k+=8;}
else {memcpy(&SecBuff[k],SamplingPeriod[_T_base],8);k+=8;}
SecBuff[k++]=0x2C;
for(i=0;i<4;i++){
if(CSVdataModeLogic()){
memcpy(&SecBuff[k],Offset,4);k+=4;
u8ToDec3(Val,Title[i][POSI].Value,0);
memcpy(&SecBuff[k],Val,3);k+=3;
}
if(i<2){
if(CSVdataModeLogic()){
memcpy(&SecBuff[k],DigSampling[i],7);k+=7;
if(Title[i][0].Value>2){memcpy(&SecBuff[k],AmplitudeX10Steps[Title[i][RANGE].Value],6);k+=6;}
else {memcpy(&SecBuff[k],AmplitudeSteps[Title[i][RANGE].Value],6);k+=6;}
}else if((OSBufferLogic())||((ChartLogic())&&(ChartMode))){
memcpy(&SecBuff[k],OSsampling[i],13);k+=13;
}else{
memcpy(&SecBuff[k],Sampling[i],13);k+=13;
}
}else{
if(CSVdataModeLogic()){
memcpy(&SecBuff[k],DigSampling[i],10);k+=10;
}else if((OSBufferLogic())||((ChartLogic())&&(ChartMode))){
memcpy(&SecBuff[k],OSsampling[i],13);k+=13;
}else{
SecBuff[k++]=0x2C;
break;
}
}
SecBuff[k++]=0x2C;
}
SecBuff[k++] = 0x0d;
SecBuff[k++] = 0x0a;
if((OSBufferLogic())&&(!FPGAosFlag)&&(_T_Range==16))m=2;
if(_T_Range>16){
if(CSVdataModeLogic())h=4;else h=2;
m=2;
}else if((OSBufferLogic())&&(FPGAosFlag))h=2;
if((_T_Range>17)&&(OSBufferLogic()==0)&&(FPGAosFlag==0))h=3;
if(_T_Range>19)h=4;
for(i=(discard+h); i<bag_max_buf; i++){
//==========================================TIME FIELD===========================================
CSVposition=(i-(discard+m));
CalculateTvernier(2);
if((ChartLogic())&&(SubIndex>4)){ //HOURS
for(j=0;j<4;j++){
if(MinBypass==2){
if(CursorDisplayStrH[j]>0){
SecBuff[k++] = CursorDisplayStrH[j];
}else SecBuff[k++] = 32;
}else SecBuff[k++] = 32;