-
Notifications
You must be signed in to change notification settings - Fork 137
Expand file tree
/
Copy pathfilesystem.c
More file actions
1403 lines (1233 loc) · 46 KB
/
Copy pathfilesystem.c
File metadata and controls
1403 lines (1233 loc) · 46 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include "logging.h"
#include "fatfs/ff.h"
#include "filesystem.h"
#include "osd.h"
#include "rgb_to_fb.h"
#include "geometry.h"
#include "rgb_to_hdmi.h"
#include "startup.h"
#include "defs.h"
#define USE_LODEPNG
#ifdef USE_LODEPNG
#include "lodepng.h"
#else
#include "tiny_png_out.h"
#endif
static FATFS fsObject;
static int capture_id = -1;
#ifdef USE_LODEPNG
static int generate_png(capture_info_t *capinfo, uint8_t **png, unsigned int *png_len ) {
LodePNGState state;
lodepng_state_init(&state);
if (capinfo->bpp < 16) {
state.info_raw.colortype = LCT_PALETTE;
state.info_raw.bitdepth = 8;
state.info_png.color.colortype = LCT_PALETTE;
state.info_png.color.bitdepth = 8;
for (int i = 0; i < (1 << capinfo->bpp); i++) {
int triplet = osd_get_palette(i);
int r = triplet & 0xff;
int g = (triplet >> 8) & 0xff;
int b = (triplet >> 16) & 0xff;
lodepng_palette_add(&state.info_png.color, r, g, b, 255);
lodepng_palette_add(&state.info_raw, r, g, b, 255);
}
} else {
state.info_raw.colortype = LCT_RGB;
state.info_raw.bitdepth = 8;
state.info_png.color.colortype = LCT_RGB;
state.info_png.color.bitdepth = 8;
}
int width = capinfo->width;
int width43 = width;
int height = capinfo->height;
int capscale = get_parameter(F_SCREENCAP_SIZE);
int hscale = get_hscale();
int vscale = get_vscale();
int hdouble = (hscale & 0x80000000) ? 1 : 0;
int vdouble = (vscale & 0x80000000) ? 1 : 0;
hscale &= 0xff;
vscale &= 0xff;
int png_width = (width >> hdouble) * hscale;
int png_height = (height >> vdouble) * vscale;
if (capinfo->mode7) {
double ratio = (double) (png_width * 16 / 12) / png_height;
if (ratio > 1.34 && (capscale == SCREENCAP_HALF43 || capscale == SCREENCAP_FULL43)) {
width43 = (((int)(((double) width * 4 / 3) / ratio) * 12 / 16) >> 2) << 2;
}
} else {
double ratio = (double) png_width / png_height;
if (ratio > 1.34 && (capscale == SCREENCAP_HALF43 || capscale == SCREENCAP_FULL43)) {
width43 = ((int)(((double) width * 4 / 3) / ratio) >> 2) << 2;
}
}
png_width = (width43 >> hdouble) * hscale;
int leftclip = (width - width43) / 2;
int rightclip = leftclip + width43;
log_info("Scaling is %d x %d x=%d y=%d sx=%d sy=%d px=%d py=%d", hscale, vscale, width, height, width/(hdouble + 1), height/(vdouble + 1), png_width, png_height);
width = (width >> hdouble) << hdouble;
width43 = (width >> hdouble) << hdouble;
height = (height >> vdouble) << vdouble;
if (capinfo->bpp == 16) {
int left;
int right;
int top;
int bottom;
int png_left = 0;
int png_right = 0;
int png_top = 0;
int png_bottom = 0;
get_config_overscan(&left, &right, &top, &bottom);
if (get_startup_overscan() != 0 && (left != 0 || right != 0) && (capscale == SCREENCAP_HALF || capscale == SCREENCAP_FULL)) {
png_left = left * png_width / (get_hdisplay() - left - right);
png_right = right * png_width / (get_hdisplay() - left - right);
png_top = top * png_height / (get_vdisplay() - top - bottom);
png_bottom = bottom * png_height / (get_vdisplay() - top - bottom);
}
uint8_t png_buffer[(png_width + png_left + png_right) *3 * (png_height + png_top + png_bottom)] __attribute__((aligned(32)));
uint8_t *pp = png_buffer;
if (png_top != 0) {
for (int i = 0; i < (png_top * (png_left + png_width + png_right)); i++) {
*pp++ = 0;
*pp++ = 0;
*pp++ = 0;
}
}
for (int y = 0; y < height; y += (vdouble + 1)) {
for (int sy = 0; sy < vscale; sy++) {
uint8_t *fp = capinfo->fb + capinfo->pitch * y;
if (png_left != 0) {
for (int x = 0; x < png_left; x++) {
*pp++ = 0;
*pp++ = 0;
*pp++ = 0;
}
}
for (int x = 0; x < width; x += (hdouble + 1)) {
uint8_t single_pixel_lo = *fp++;
uint8_t single_pixel_hi = *fp++;
int single_pixel = single_pixel_lo | (single_pixel_hi << 8);
uint8_t single_pixel_A = (single_pixel >> 12) & 0x0f;
uint8_t single_pixel_R = (single_pixel >> 8) & 0x0f;
uint8_t single_pixel_G = (single_pixel >> 4) & 0x0f;
uint8_t single_pixel_B = single_pixel & 0x0f;
if (single_pixel_A != 0x0f) {
single_pixel_R = single_pixel_R * single_pixel_A / 15;
single_pixel_G = single_pixel_G * single_pixel_A / 15;
single_pixel_B = single_pixel_B * single_pixel_A / 15;
}
single_pixel_R |= (single_pixel_R << 4);
single_pixel_G |= (single_pixel_G << 4);
single_pixel_B |= (single_pixel_B << 4);
if (hdouble) fp += 2;
if (x >= leftclip && x < rightclip) {
for (int sx = 0; sx < hscale; sx++) {
*pp++ = single_pixel_R;
*pp++ = single_pixel_G;
*pp++ = single_pixel_B;
}
}
}
if (png_right != 0) {
for (int x = 0; x < png_right; x++) {
*pp++ = 0;
*pp++ = 0;
*pp++ = 0;
}
}
}
}
if (png_bottom != 0) {
for (int i = 0; i < (png_bottom * (png_left + png_width + png_right)); i++) {
*pp++ = 0;
*pp++ = 0;
*pp++ = 0;
}
}
//log_info("Encoding png %08X, %08X", png, png_buffer);
unsigned int result = lodepng_encode(png, png_len, png_buffer, (png_width + png_left + png_right), png_height + png_top + png_bottom, &state);
if (result) {
log_warn("lodepng_encode32 failed (result = %d)", result);
return 1;
}
return 0;
} else {
uint8_t png_buffer[png_width * png_height] __attribute__((aligned(32)));
uint8_t *pp = png_buffer;
if (capinfo->bpp == 8) {
for (int y = 0; y < height; y += (vdouble + 1)) {
for (int sy = 0; sy < vscale; sy++) {
uint8_t *fp = capinfo->fb + capinfo->pitch * y;
for (int x = 0; x < width; x += (hdouble + 1)) {
uint8_t single_pixel = *fp++;
if (hdouble) fp++;
if (x >= leftclip && x < rightclip) {
for (int sx = 0; sx < hscale; sx++) {
*pp++ = single_pixel;
}
}
}
}
}
} else {
for (int y = 0; y < height; y += (vdouble + 1)) {
for (int sy = 0; sy < vscale; sy++) {
uint8_t *fp = capinfo->fb + capinfo->pitch * y;
uint8_t single_pixel = 0;
for (int x = 0; x < width; x += (hdouble + 1)) {
if (hdouble) {
single_pixel = *fp++;
if (x >= leftclip && x < rightclip) {
for (int sx = 0; sx < hscale; sx++) {
*pp++ = single_pixel >> 4;
}
}
} else {
if ((x & 1) == 0) {
single_pixel = *fp++;
if (x >= leftclip && x < rightclip) {
for (int sx = 0; sx < hscale; sx++) {
*pp++ = single_pixel >> 4;
}
}
} else {
if (x >= leftclip && x < rightclip) {
for (int sx = 0; sx < hscale; sx++) {
*pp++ = single_pixel & 0x0f;
}
}
}
}
}
}
}
}
//log_info("Encoding png %08X, %08X", png, png_buffer);
unsigned int result = lodepng_encode(png, png_len, png_buffer, png_width, png_height, &state);
if (result) {
log_warn("lodepng_encode32 failed (result = %d)", result);
return 1;
}
return 0;
}
}
static void free_png(uint8_t *png) {
if (png) {
free(png);
}
}
#else
// TODO: Fix hard-coded max H resolution of 4096
static uint8_t pixels[3 * 4096] __attribute__((aligned(32)));
static uint8_t png_buffer[8 * 1024 * 1024] __attribute__((aligned(0x4000)));
static int generate_png(capture_info_t *capinfo, uint8_t **png, unsigned int *png_len ) {
enum TinyPngOut_Status result;
struct TinyPngOut state;
*png = NULL;
*png_len = 0;
result = TinyPngOut_init(&state, capinfo->width, capinfo->height, png_buffer);
if (result != TINYPNGOUT_OK) {
log_warn("TinyPngOut_init failed (result = %d)", result);
return 1;
} else {
// TODO: Take account of current palette
for (int y = 0; y < capinfo->height; y++) {
uint8_t *fp = capinfo->fb + capinfo->pitch * y;
uint8_t *pp = pixels;
if (capinfo->bpp == 8) {
for (int x = 0; x < capinfo->width; x++) {
uint8_t single_pixel = *fp++;
*pp++ = (single_pixel & 0x01) ? 255 : 0;
*pp++ = (single_pixel & 0x02) ? 255 : 0;
*pp++ = (single_pixel & 0x04) ? 255 : 0;
}
} else {
for (int x = 0; x < capinfo->width << 1; x++) {
uint8_t double_pixel = *fp++;
*pp++ = (double_pixel & 0x10) ? 255 : 0;
*pp++ = (double_pixel & 0x20) ? 255 : 0;
*pp++ = (double_pixel & 0x40) ? 255 : 0;
*pp++ = (double_pixel & 0x01) ? 255 : 0;
*pp++ = (double_pixel & 0x02) ? 255 : 0;
*pp++ = (double_pixel & 0x04) ? 255 : 0;
}
}
result = TinyPngOut_write(&state, pixels, capinfo->width);
if (result != TINYPNGOUT_OK) {
log_warn("TinyPngOut_write failed (result = %d)", result);
return 1;
}
}
}
*png = png_buffer;
*png_len = state.output_len;
return 0;
}
static void free_png(uint8_t *png) {
}
#endif
static void initialize_capture_id(char * path) {
FRESULT result;
DIR dir;
FILINFO fno;
// Check if already initialized
// TODO: This would fail if cards were swapped live...
//if (capture_id >= 0) { // doesn't work because of different folders for different profiles
// return;
//}
// Open root directory
result = f_opendir(&dir, path);
if (result != FR_OK) {
log_warn("Failed to open %s", path);
return;
}
// Iterate through files in root directory looking for existing capture files
int len = 0;
int baselen = strlen(CAPTURE_FILE_BASE);
capture_id = -1;
do {
result = f_readdir(&dir, &fno);
if (result != FR_OK) {
log_warn("Failed to read %s", path);
break;
}
len = strlen(fno.fname);
if (len > 0) {
if (strncasecmp(fno.fname, CAPTURE_FILE_BASE, baselen) == 0) {
int id = atoi(fno.fname + baselen);
if (id > capture_id) {
capture_id = id;
}
}
}
} while (len > 0);
// And increment it to the next free one
capture_id++;
// Close root directory
result = f_closedir(&dir);
if (result != FR_OK) {
log_warn("Failed to close %s", path);
}
}
void init_filesystem() {
FRESULT result;
// Mount file system
result = f_mount(&fsObject, "", 1);
if (result != FR_OK) {
log_warn("Failed to initialize file system");
return;
}
}
void close_filesystem() {
FRESULT result;
// UnMount file system
result = f_mount(NULL, "", 1);
if (result != FR_OK) {
log_warn("Failed to close file system");
}
}
void capture_screenshot(capture_info_t *capinfo, char *profile) {
FRESULT result;
char path[200];
char filepath[MAX_STRING_SIZE];
FIL file;
uint8_t *png;
unsigned int png_len;
init_filesystem();
result = f_mkdir(CAPTURE_BASE);
if (result != FR_OK && result != FR_EXIST) {
log_warn("Failed to create dir1 %s (result = %d)",CAPTURE_BASE, result);
}
char *position = strchr(profile, '/');
if (position) {
strcpy(filepath, position + 1);
} else {
strcpy(filepath, profile);
}
sprintf(path, "%s/%s", CAPTURE_BASE, filepath);
result = f_mkdir(path);
if (result != FR_OK && result != FR_EXIST) {
log_warn("Failed to create dir2 %s (result = %d)", path, result);
}
initialize_capture_id(path);
sprintf(filepath, "%s/%s%d.png",path, CAPTURE_FILE_BASE, capture_id);
log_info("Screen capture starting, file = %s", filepath);
result = f_open(&file, filepath, FA_CREATE_NEW | FA_WRITE);
if (result != FR_OK) {
log_warn("Failed to create capture file %s (result = %d)", filepath, result);
return;
}
capture_id++;
if (generate_png(capinfo, &png, &png_len)) {
log_warn("generate_png failed, not writing data");
} else {
osd_clear();
clear_menu_bits();
osd_set_noupdate(0, ATTR_DOUBLE_SIZE, "Screen Capture");
osd_set_clear(2, 0, filepath);
log_info("Screen capture PNG length = %d, writing data...", png_len);
UINT num_written = 0;
result = f_write(&file, png, png_len, &num_written);
if (result != FR_OK) {
log_warn("Failed to write capture file %s (result = %d)", filepath, result);
} else if (num_written != png_len) {
log_warn("Capture file %s incomplete (%d < %d bytes)", filepath, num_written, png_len);
}
}
free_png(png);
result = f_close(&file);
if (result != FR_OK) {
log_warn("Failed to close capture file %s (result = %d)", filepath, result);
}
close_filesystem();
log_info("Screen capture complete");
}
void write_profile_choice(char *profile_name, int saved_config_number, char *cpld_name) {
FRESULT result;
FIL file;
unsigned int num_written = 0;
char path[MAX_STRING_SIZE];
char cmdline[MAX_STRING_SIZE];
init_filesystem();
sprintf(path, "/profile_%s.txt", cpld_name);
log_info("Writing: %s", path);
result = f_open(&file, path, FA_WRITE | FA_CREATE_ALWAYS);
if (result != FR_OK) {
log_warn("Failed to open %s (result = %d)", path, result);
close_filesystem();
return;
} else {
sprintf(cmdline, "profile=%s\r\n", profile_name);
int cmdlength = strlen(cmdline);
sprintf(cmdline + cmdlength, "saved_profile=%d\r\n", saved_config_number);
cmdlength += strlen(cmdline + cmdlength);
result = f_write(&file, cmdline, cmdlength, &num_written);
if (result != FR_OK) {
log_warn("Failed to write %s (result = %d)", path, result);
close_filesystem();
return;
} else if (num_written != cmdlength) {
log_warn("%s is incomplete (%d < %d bytes)", path, num_written, cmdlength);
close_filesystem();
return;
}
result = f_close(&file);
if (result != FR_OK) {
log_warn("Failed to close %s (result = %d)", path, result);
close_filesystem();
return;
}
}
close_filesystem();
}
unsigned int file_read_profile(char *profile_name, int saved_config_number, char *sub_profile_name, int updatecmd, char *command_string, unsigned int buffer_size) {
FRESULT result;
char path[MAX_STRING_SIZE];
FIL file;
unsigned int bytes_read = 0;
command_string[bytes_read] = 0;
if (updatecmd) {
write_profile_choice(profile_name, saved_config_number, (char*)cpld->name);
}
init_filesystem();
if (saved_config_number == 0) {
if (sub_profile_name != NULL) {
sprintf(path, "%s/%s/%s/%s.txt", SAVED_PROFILE_BASE, cpld->name, profile_name, sub_profile_name);
} else {
sprintf(path, "%s/%s/%s.txt", SAVED_PROFILE_BASE, cpld->name, profile_name);
}
} else {
if (sub_profile_name != NULL) {
sprintf(path, "%s/%s/%s/%s_%d.txt", SAVED_PROFILE_BASE, cpld->name, profile_name, sub_profile_name, saved_config_number);
} else {
sprintf(path, "%s/%s/%s_%d.txt", SAVED_PROFILE_BASE, cpld->name, profile_name, saved_config_number);
}
}
result = f_open(&file, path, FA_READ);
if (result != FR_OK) {
if (sub_profile_name != NULL) {
sprintf(path, "%s/%s/%s/%s.txt", PROFILE_BASE, cpld->name, profile_name, sub_profile_name);
} else {
sprintf(path, "%s/%s/%s.txt", PROFILE_BASE, cpld->name, profile_name);
}
result = f_open(&file, path, FA_READ);
if (result != FR_OK) {
log_warn("Failed to open profile %s (result = %d)", path, result);
close_filesystem();
return 0;
}
}
log_info("Loading file: %s", path);
result = f_read (&file, command_string, buffer_size, &bytes_read);
if (result != FR_OK) {
bytes_read = 0;
log_warn("Failed to read profile file %s (result = %d)", path, result);
close_filesystem();
return 0;
} else {
command_string[bytes_read] = 0;
}
result = f_close(&file);
if (result != FR_OK) {
log_warn("Failed to close profile %s (result = %d)", path, result);
close_filesystem();
return 0;
}
close_filesystem();
log_debug("Profile loading complete");
if (bytes_read == 0) {
command_string[0] = 0;
}
return bytes_read;
}
static int string_compare (const void * s1, const void * s2) {
return strcmp (s1, s2);
}
void scan_cpld_filenames(char cpld_filenames[MAX_CPLD_FILENAMES][MAX_FILENAME_WIDTH], char *path, int *count) {
FRESULT res;
DIR dir;
FILINFO fno;
*count = 0;
init_filesystem();
res = f_opendir(&dir, path);
if (res == FR_OK) {
while (1) {
res = f_readdir(&dir, &fno);
if (res != FR_OK || fno.fname[0] == 0 || *count == MAX_CPLD_FILENAMES) {
break;
}
if (!(fno.fattrib & AM_DIR)) {
if (fno.fname[0] != '.' && (strlen(fno.fname) > 5) != 0) {
char* filetype = fno.fname + strlen(fno.fname)-5;
if (strcmp(filetype, ".xsvf") == 0) {
strncpy(cpld_filenames[*count], fno.fname, MAX_FILENAME_WIDTH);
cpld_filenames[*count][strlen(fno.fname) - 5] = 0;
log_info("Found CPLD: %s", cpld_filenames[*count]);
(*count)++;
}
}
}
}
f_closedir(&dir);
qsort(cpld_filenames, *count, sizeof *cpld_filenames, string_compare);
}
close_filesystem();
}
void scan_profiles(char *prefix, char manufacturer_names[MAX_PROFILES][MAX_PROFILE_WIDTH], char profile_names[MAX_PROFILES][MAX_PROFILE_WIDTH], int has_sub_profiles[MAX_PROFILES], char *path, size_t *mcount, size_t *count) {
int initial_count = *count;
FRESULT res;
DIR dir;
FIL file;
char fpath[MAX_STRING_SIZE];
static FILINFO fno;
init_filesystem();
log_info("Reading path: %s", path);
res = f_opendir(&dir, path);
if (res == FR_OK) {
for (;;) {
res = f_readdir(&dir, &fno);
if (res != FR_OK || fno.fname[0] == 0 || *mcount == MAX_PROFILES) break;
if (fno.fattrib & AM_DIR && strcmp(fno.fname, PAXHEADER) != 0) {
fno.fname[MAX_PROFILE_WIDTH - 1] = 0;
if (mono_board_detected() == 0 || (mono_board_detected() == 1 && fno.fname[strlen(fno.fname) - 1] == '_')) {
int duplicate = 0;
if (*mcount != 0) {
for (int k = 0; k < *mcount; k++) {
if (strcmp(fno.fname, manufacturer_names[k]) == 0) {
duplicate = 1;
break;
}
}
}
if (duplicate == 0) {
strcpy(manufacturer_names[*mcount], fno.fname);
(*mcount)++;
} else {
}
}
}
}
f_closedir(&dir);
qsort(manufacturer_names, *mcount, sizeof *manufacturer_names, string_compare);
for (int i = 0; i < *mcount; i++) {
sprintf(fpath, "%s/%s", path, manufacturer_names[i]);
log_info("Scanning folder: %s", fpath);
res = f_opendir(&dir, fpath);
//log_info("result %X", res);
if (res == FR_OK) {
for (;;) {
res = f_readdir(&dir, &fno);
if (res != FR_OK || fno.fname[0] == 0 || *count == MAX_PROFILES) break;
if (fno.fattrib & AM_DIR && strcmp(fno.fname, PAXHEADER) != 0) {
fno.fname[MAX_PROFILE_WIDTH - 1] = 0;
if (mono_board_detected() == 0 || (mono_board_detected() == 1 && fno.fname[strlen(fno.fname) - 1] == '_')) {
sprintf(profile_names[*count], "%s%s/%s", prefix, manufacturer_names[i], fno.fname);
log_info("Found profile: %s", profile_names[*count]);
(*count)++;
}
} else {
if (fno.fname[0] != '.' && strlen(fno.fname) > 4 && strcmp(fno.fname, DEFAULTTXT_STRING) != 0) {
char* filetype = fno.fname + strlen(fno.fname)-4;
if (strcmp(filetype, ".txt") == 0) {
fno.fname[MAX_PROFILE_WIDTH - 1] = 0;
fno.fname[strlen(fno.fname) - 4] = 0;
if (mono_board_detected() == 0 || (mono_board_detected() == 1 && fno.fname[strlen(fno.fname) - 1] == '_')) {
sprintf(profile_names[*count], "%s%s/%s", prefix, manufacturer_names[i], fno.fname);
log_info("Found profile: %s", profile_names[*count]);
(*count)++;
}
}
}
}
}
f_closedir(&dir);
}
}
if (*count > initial_count) {
qsort(profile_names[initial_count], (*count) - initial_count, sizeof *profile_names, string_compare);
}
for (int i = initial_count; i < (*count); i++) {
sprintf(fpath, "%s/%s.txt", path, profile_names[i] + strlen(prefix));
res = f_open(&file, fpath, FA_READ);
if (res == FR_OK) {
f_close(&file);
has_sub_profiles[i] = 0;
} else {
has_sub_profiles[i] = 1;
}
}
}
close_filesystem();
}
void scan_sub_profiles(char sub_profile_names[MAX_SUB_PROFILES][MAX_PROFILE_WIDTH], char *sub_path, size_t *count) {
FRESULT res;
DIR dir;
static FILINFO fno;
char path[100] = "/Profiles/";
strncat(path, cpld->name, 80);
strncat(path, "/", 80);
strncat(path, sub_path, 80);
init_filesystem();
res = f_opendir(&dir, path);
if (res == FR_OK) {
for (;;) {
res = f_readdir(&dir, &fno);
if (res != FR_OK || fno.fname[0] == 0 || *count == MAX_SUB_PROFILES) break;
if (!(fno.fattrib & AM_DIR)) {
if (fno.fname[0] != '.' && strlen(fno.fname) > 4 && strcmp(fno.fname, DEFAULTTXT_STRING) != 0) {
char* filetype = fno.fname + strlen(fno.fname)-4;
if (strcmp(filetype, ".txt") == 0) {
fno.fname[MAX_PROFILE_WIDTH - 1] = 0;
strcpy(sub_profile_names[*count], fno.fname);
sub_profile_names[*count][strlen(fno.fname) - 4] = 0;
(*count)++;
}
}
}
}
f_closedir(&dir);
qsort(sub_profile_names, *count, sizeof *sub_profile_names, string_compare);
}
close_filesystem();
}
void scan_rnames(char names[MAX_NAMES][MAX_NAMES_WIDTH], char *path, char *type, int truncate, size_t *count) {
FRESULT res;
DIR dir;
static FILINFO fno;
init_filesystem();
res = f_opendir(&dir, path);
if (res == FR_OK) {
for (;;) {
res = f_readdir(&dir, &fno);
if (res != FR_OK || fno.fname[0] == 0 || *count == MAX_NAMES) break;
if (!(fno.fattrib & AM_DIR)) {
if (fno.fname[0] != '.' && strlen(fno.fname) > 4) {
char* filetype = fno.fname + strlen(fno.fname) - 4;
if (strcmp(filetype, type) == 0) {
strncpy(names[*count], fno.fname, MAX_NAMES_WIDTH);
names[(*count)++][strlen(fno.fname) - truncate] = 0;
}
}
}
}
f_closedir(&dir);
//mask out bit so numbers starting >5 sort before other numbers so 640, 720 & 800 appear before 1024 1280 etc
for (int i = 0; i < *count; i++) {
if (names[i][0] > '5' && names[i][0] <= '9') {
names[i][0] &= 0xef;
}
}
qsort(names, *count, sizeof *names, string_compare);
//restore masked bit
for (int i = 0; i < *count; i++) {
if (names[i][0] > ('5' & 0xef) && names[i][0] <= ('9' & 0xef)) {
names[i][0] |= 0x10;
}
}
}
close_filesystem();
}
int file_save_raw(char *path, char *buffer, unsigned int buffer_size) {
FRESULT result;
FIL file;
unsigned int bytes_written = 0;
log_info("Saving file %s", path);
result = f_open(&file, path, FA_WRITE | FA_CREATE_ALWAYS);
if (result != FR_OK) {
log_warn("Failed to open %s (result = %d)", path, result);
return 0;
}
result = f_write(&file, buffer, buffer_size, &bytes_written);
if (result != FR_OK) {
log_warn("Failed to read %s (result = %d)", path, result);
return 0;
}
result = f_close(&file);
if (result != FR_OK) {
log_warn("Failed to close %s (result = %d)", path, result);
return 0;
}
//log_info("%s reading complete", path);
return bytes_written;
}
int file_save_bin(char *path, char *buffer, unsigned int buffer_size) {
init_filesystem();
int result = file_save_raw(path, buffer, buffer_size);
close_filesystem();
return result;
}
int file_load_raw(char *path, char *buffer, unsigned int buffer_size) {
FRESULT result;
FIL file;
unsigned int bytes_read = 0;
log_info("Loading file %s", path);
result = f_open(&file, path, FA_READ);
if (result != FR_OK) {
log_warn("Failed to open %s (result = %d)", path, result);
return 0;
}
result = f_read (&file, buffer, buffer_size, &bytes_read);
if (result != FR_OK) {
log_warn("Failed to read %s (result = %d)", path, result);
return 0;
}
result = f_close(&file);
if (result != FR_OK) {
log_warn("Failed to close %s (result = %d)", path, result);
return 0;
}
buffer[bytes_read] = 0;
//log_info("%s reading complete", path);
return bytes_read;
}
int file_load(char *path, char *buffer, unsigned int buffer_size) {
init_filesystem();
int result = file_load_raw(path, buffer, buffer_size);
close_filesystem();
return result;
}
int file_save_custom_profile(char *name, char *buffer, unsigned int buffer_size) {
FRESULT result;
FIL file;
unsigned int num_written = 0;
char path[MAX_STRING_SIZE];
char temp_buffer[MAX_BUFFER_SIZE];
int status = 0;
init_filesystem();
result = f_mkdir(PROFILE_BASE);
if (result != FR_OK && result != FR_EXIST) {
log_warn("Failed to create dir %s (result = %d)",SAVED_PROFILE_BASE, result);
}
sprintf(path, "%s/%s", PROFILE_BASE, cpld->name);
result = f_mkdir(path);
if (result != FR_OK && result != FR_EXIST) {
log_warn("Failed to create dir1 %s (result = %d)",path, result);
}
strcpy(temp_buffer, name);
char *index = strchr(temp_buffer, '/');
if (index) {
*index = 0;
}
sprintf(path, "%s/%s/%s", PROFILE_BASE, cpld->name, temp_buffer);
result = f_mkdir(path);
if (result != FR_OK && result != FR_EXIST) {
log_warn("Failed to create dir2 %s (result = %d)",path, result);
}
sprintf(path, "%s/%s/%s.txt", PROFILE_BASE, cpld->name, name);
log_info("Saving custom file %s", path);
result = f_open(&file, path, FA_WRITE | FA_CREATE_ALWAYS);
if (result != FR_OK) {
log_warn("Failed to open %s (result = %d)", path, result);
close_filesystem();
return result;
}
result = f_write(&file, buffer, buffer_size, &num_written);
if (result != FR_OK) {
log_warn("Failed to read %s (result = %d)", path, result);
close_filesystem();
return result;
}
result = f_close(&file);
if (result != FR_OK) {
log_warn("Failed to close %s (result = %d)", path, result);
close_filesystem();
return result;
}
log_info("%s writing complete", path);
close_filesystem();
return status;
}
int file_save(char *dirpath, char *name, char *buffer, unsigned int buffer_size, int saved_config_number) {
FRESULT result;
FIL file;
unsigned int num_written = 0;
unsigned int bytes_read = 0;
char path[MAX_STRING_SIZE];
char comparison_path[MAX_STRING_SIZE];
char comparison_buffer[MAX_BUFFER_SIZE];
char temp_buffer[MAX_BUFFER_SIZE];
int status = 0;
init_filesystem();
result = f_mkdir(SAVED_PROFILE_BASE);
if (result != FR_OK && result != FR_EXIST) {
log_warn("Failed to create dir %s (result = %d)",SAVED_PROFILE_BASE, result);
}
sprintf(path, "%s/%s", SAVED_PROFILE_BASE, cpld->name);
result = f_mkdir(path);
if (result != FR_OK && result != FR_EXIST) {
log_warn("Failed to create dir1 %s (result = %d)",path, result);
}
if (dirpath != NULL) {
strcpy(temp_buffer, dirpath);
char *index = strchr(temp_buffer, '/');
if (index) {
*index = 0;
}
sprintf(path, "%s/%s/%s", SAVED_PROFILE_BASE, cpld->name, temp_buffer);
result = f_mkdir(path);
if (result != FR_OK && result != FR_EXIST) {
log_warn("Failed to create dir2 %s (result = %d)",path, result);
}
sprintf(path, "%s/%s/%s", SAVED_PROFILE_BASE, cpld->name, dirpath);
result = f_mkdir(path);
if (result != FR_OK && result != FR_EXIST) {
log_warn("Failed to create dir3 %s (result = %d)", dirpath, result);
}
if (saved_config_number == 0) {
sprintf(path, "%s/%s/%s/%s.txt", SAVED_PROFILE_BASE, cpld->name, dirpath, name);
} else {
sprintf(path, "%s/%s/%s/%s_%d.txt", SAVED_PROFILE_BASE, cpld->name, dirpath, name, saved_config_number);
}
sprintf(comparison_path, "%s/%s/%s/%s.txt", PROFILE_BASE, cpld->name, dirpath, name);
} else {
strcpy(temp_buffer, name);
char *index = strchr(temp_buffer, '/');
if (index) {
*index = 0;
}
sprintf(path, "%s/%s/%s", SAVED_PROFILE_BASE, cpld->name, temp_buffer);
result = f_mkdir(path);
if (result != FR_OK && result != FR_EXIST) {
log_warn("Failed to create dir2 %s (result = %d)",path, result);
}
if (saved_config_number == 0) {
sprintf(path, "%s/%s/%s.txt", SAVED_PROFILE_BASE, cpld->name, name);
} else {
sprintf(path, "%s/%s/%s_%d.txt", SAVED_PROFILE_BASE, cpld->name, name, saved_config_number);
}
sprintf(comparison_path, "%s/%s/%s.txt", PROFILE_BASE, cpld->name, name);
}
log_info("Loading comparison file %s", comparison_path);
result = f_open(&file, comparison_path, FA_READ);
if (result != FR_OK) {
log_warn("Failed to open %s (result = %d)", comparison_path, result);
}
comparison_buffer[0] = 0; // if comparison file missing then default to zero length
result = f_read (&file, comparison_buffer, MAX_BUFFER_SIZE - 1, &bytes_read);
if (result != FR_OK) {
log_warn("Failed to read %s (result = %d)", comparison_path, result);
}
result = f_close(&file);
if (result != FR_OK) {
log_warn("Failed to close %s (result = %d)", comparison_path, result);
}
// see if entries in buffer differ from comparison buffer
comparison_buffer[bytes_read] = 0;
strcpy(temp_buffer, buffer);
char *temp_pointer = temp_buffer;
int different = 0;
while (temp_pointer[0] != 0) {
char *eol = strstr(temp_pointer, "\r\n");
int eol_size = 0;
if (eol != NULL) {
eol[0] = 0;
eol[1] = 0;
eol_size = 2;
} else {
eol = strchr(temp_pointer, '\n');
if (eol != NULL) {
eol[0] = 0;
eol_size = 1;
}
}
if (strstr(comparison_buffer, temp_pointer) == NULL) {
different = 1;
}
temp_pointer += strlen(temp_pointer) + eol_size;
}