-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSDHandler.cpp
More file actions
726 lines (634 loc) · 20.3 KB
/
SDHandler.cpp
File metadata and controls
726 lines (634 loc) · 20.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
#include "SDHandler.h"
SDHandler::SDHandler(Inkplate *display) : display(display) {}
void SDHandler::init()
{
if (display->sdCardInit())
{
Serial.println("SD Card initialized");
}
else
{
Serial.println("Failed to initialize SD Card");
}
// if (!sd.begin(SdSpiConfig(display->sdGetCs(), SHARED_SPI, SD_SCK_MHZ(10)))) {
// Serial.println("SD Card failed to begin");
// return;
// }
// listFiles("/books");
}
std::vector<String> SDHandler::listFiles(String path, bool no_ext, bool deep, String prefix)
{
SdFile dir;
std::vector<String> list;
// Serial.println("Listing files in "+path);
// Serial.println(no_ext);
// Serial.println(deep);
// Serial.println(prefix);
if (!dir.open(path.c_str(), O_RDONLY))
{
Serial.println("Failed to open directory");
return list;
}
SdFile file;
while (file.openNext(&dir, O_RDONLY))
{
char fileName[150];
file.getName(fileName, sizeof(fileName));
// list += String(fileName) + "\n";
String fileName_str = "";
if (prefix.isEmpty())
{
fileName_str = String(fileName);
}
else
{
fileName_str = prefix + "/" + String(fileName);
}
if (deep && file.isDir())
{
std::vector<String> subList = listFiles(path + "/" + fileName, no_ext, deep, fileName_str);
list.insert(list.end(), subList.begin(), subList.end()); // Append subdirectory files
}
// Serial.println(fileName_str);
if (no_ext)
{
uint8_t lastdot = fileName_str.lastIndexOf(".epub");
fileName_str = fileName_str.substring(0, lastdot);
}
list.push_back(fileName_str);
// Serial.println(fileName_str);
file.close();
}
dir.close();
// Serial.println(list);
return list;
}
bool SDHandler::saveJson(String filename, String keys[], String values[], uint8_t n)
{
// Extract the directory path from the file path (remove the filename)
int lastSlash = filename.lastIndexOf('/');
if (lastSlash == -1)
{
Serial.println("Invalid file path: " + filename);
return false;
}
String folderPath = filename.substring(0, lastSlash); // Get directory part
// Serial.println(folderPath);
// Ensure all parent folders exist
if (!createFolderRecursive(folderPath))
{
Serial.println("Failed to create necessary folders for: " + filename);
return false;
}
SdFile jsonFile;
if (jsonFile.open(filename.c_str(), O_WRITE | O_CREAT | O_TRUNC))
{ // Convert String to const char*
// Increase capacity to handle larger manifests/metadata
StaticJsonDocument<4096> doc;
for (uint8_t i = 0; i < n; i++)
{ // Initialize i correctly
doc[keys[i]] = values[i];
}
serializeJson(doc, jsonFile);
jsonFile.close();
return true;
}
else
{
Serial.println("Failed to open JSON file for writing");
return false;
}
}
bool SDHandler::saveJson(String filename, JsonDocument &doc)
{
// Extract the directory path from the file path (remove the filename)
int lastSlash = filename.lastIndexOf('/');
if (lastSlash == -1)
{
Serial.println("Invalid file path: " + filename);
return false;
}
String folderPath = filename.substring(0, lastSlash); // Get directory part
// Serial.println(folderPath);
// Ensure all parent folders exist
if (!createFolderRecursive(folderPath))
{
Serial.println("Failed to create necessary folders for: " + filename);
return false;
}
SdFile jsonFile;
if (jsonFile.open(filename.c_str(), O_WRITE | O_CREAT | O_TRUNC))
{
serializeJson(doc, jsonFile); // Write the JSON document to the file
jsonFile.close();
return true;
}
else
{
Serial.println("Failed to open JSON file for writing");
return false;
}
}
StaticJsonDocument<4096> SDHandler::loadJson(String filename)
{
StaticJsonDocument<4096> doc; // Increased capacity to reduce truncation of larger JSON files
SdFile jsonFile;
if (jsonFile.open(filename.c_str(), O_RDONLY))
{
DeserializationError error = deserializeJson(doc, jsonFile);
jsonFile.close();
if (error)
{
Serial.print("Failed to parse JSON: ");
Serial.println(error.f_str());
}
}
else
{
Serial.println("Failed to open JSON file for reading");
}
return doc; // Return the JSON document
}
String SDHandler::loadFile(String filename)
{
SdFile file;
if (!file.open(filename.c_str(), O_RDONLY))
{
Serial.println("Failed to open file for reading: " + filename);
return "";
}
size_t fileSize = file.fileSize();
if (fileSize == 0)
{
Serial.println("File is empty: " + filename);
file.close();
return "";
}
char *buffer = (char *)malloc(fileSize + 1); // Allocate memory (+1 for null terminator)
if (!buffer)
{
Serial.println("Memory allocation failed.");
file.close();
return "";
}
// Read whole file robustly in case a single read does not return full size
size_t totalRead = 0;
while (totalRead < fileSize)
{
int r = file.read(buffer + totalRead, fileSize - totalRead);
if (r <= 0)
{
Serial.println("File read error (unexpected EOF) on: " + filename);
break;
}
totalRead += r;
}
if (totalRead != fileSize)
{
Serial.print("Warning: Expected size ");
Serial.print(fileSize);
Serial.print(" but read ");
Serial.println(totalRead);
}
buffer[totalRead] = '\0'; // Null-terminate the string at the actual bytes read
file.close();
String content = String(buffer); // Convert to String
// Serial.print("Loaded file length: ");
// Serial.println(content.length());
free(buffer); // Free allocated memory
return content;
}
bool SDHandler::saveFile(String filename, const char *data, size_t dataSize)
{
// Extract the directory path from the file path (remove the filename)
int lastSlash = filename.lastIndexOf('/');
if (lastSlash == -1)
{
Serial.println("Invalid file path: " + filename);
return false;
}
String folderPath = filename.substring(0, lastSlash); // Get directory part
// Serial.println(folderPath);
// Ensure all parent folders exist
if (!createFolderRecursive(folderPath))
{
Serial.println("Failed to create necessary folders for: " + filename);
return false;
}
// Open the file for writing
SdFile file;
if (!file.open(filename.c_str(), O_WRITE | O_CREAT | O_TRUNC))
{
Serial.println("Failed to open file for writing: " + filename);
return false;
}
file.write((const uint8_t *)data, dataSize);
file.close();
return true;
}
bool SDHandler::folderExists(const String &path)
{
SdFile dir;
return dir.open(path.c_str(), O_READ);
}
bool SDHandler::createFolder(const String &parentPath, const String &folderName)
{
SdFile parentDir;
FatFile newDir;
// if (!parentDir.open(parentPath.c_str(), O_READ)) {
// Serial.println("Parent directory does not exist: " + parentPath);
// return false;
// }
// Open the specified parent directory
if (!parentDir.open(parentPath.c_str(), O_READ))
{
Serial.println("Failed to open parent directory: " + parentPath);
return false;
}
// Create the new directory inside the parent directory
if (!newDir.mkdir(&parentDir, folderName.c_str(), true))
{ // 'true' ensures parent directories are created
Serial.println("Failed to create directory: " + folderName + " in " + parentPath);
return false;
}
Serial.println("Directory created: " + parentPath + "/" + folderName);
return true;
}
bool SDHandler::createFolderRecursive(const String &path)
{
if (folderExists(path))
{
return true; // Folder already exists
}
String subPath = "";
int start = 1;
while (true)
{
int slashIndex = path.indexOf('/', start);
if (slashIndex == -1)
break; // No more slashes, exit loop
String parentPath = path.substring(0, slashIndex);
String folderName = path.substring(slashIndex + 1, path.indexOf('/', slashIndex + 1));
// Serial.println(parentPath);
// Serial.println(folderName);
if (folderName.length() == 0)
break; // Stop if no valid folder name
start = slashIndex + 1;
if (!folderExists(parentPath))
{
Serial.println("Parent folder does not exist: " + parentPath);
return false; // Stop if parent folder doesn't exist
}
if (folderExists(parentPath + "/" + folderName))
{
Serial.println("Folder already exists: " + parentPath + "/" + folderName + ". Continuing...");
continue;
}
if (!createFolder(parentPath, folderName))
{
Serial.println("Failed to create folder: " + folderName + " in " + parentPath);
return false;
}
}
return true;
}
String SDHandler::normalizePath(String path)
{
std::vector<String> stack;
int start = 0;
while (start < path.length())
{
int end = path.indexOf("/", start);
if (end == -1)
end = path.length();
String part = path.substring(start, end);
start = end + 1;
if (part == "..")
{
if (!stack.empty())
stack.pop_back(); // Go up one directory
}
else if (part != "." && part != "")
{
stack.push_back(part);
}
}
String normalizedPath = "/";
for (size_t i = 0; i < stack.size(); i++)
{
normalizedPath += stack[i];
if (i < stack.size() - 1)
normalizedPath += "/";
}
return normalizedPath;
}
bool SDHandler::fileExists(const String &path)
{
SdFile f;
bool ok = f.open(path.c_str(), O_RDONLY);
if (ok)
{
f.close();
return true;
}
return false;
}
bool SDHandler::getImageDimensions(const String &path, int &width, int &height)
{
SdFile file;
if (!file.open(path.c_str(), O_RDONLY))
{
Serial.println("Failed to open image file: " + path);
return false;
}
uint8_t header[30];
file.read(header, sizeof(header));
file.close();
if (header[0] == 'B' && header[1] == 'M')
{
// BMP format (width at offset 18, height at offset 22)
width = *(int *)&header[18];
height = *(int *)&header[22];
return true;
}
else if (header[0] == 0xFF && header[1] == 0xD8)
{
// JPG format
return getJpegDimensions(path, width, height);
}
else if (header[0] == 0x89 && header[1] == 'P' && header[2] == 'N' && header[3] == 'G')
{
// PNG format (IHDR width at offset 16, height at offset 20)
width = (header[16] << 24) | (header[17] << 16) | (header[18] << 8) | header[19];
height = (header[20] << 24) | (header[21] << 16) | (header[22] << 8) | header[23];
return true;
}
Serial.println("Unsupported image format: " + path);
return false;
}
bool SDHandler::getJpegDimensions(const String &path, int &width, int &height)
{
SdFile file;
if (!file.open(path.c_str(), O_RDONLY))
{
Serial.println("Failed to open JPEG file: " + path);
return false;
}
uint8_t marker[2];
// Read the Start of Image (SOI) marker
if (file.read(marker, 2) != 2 || marker[0] != 0xFF || marker[1] != 0xD8)
{
Serial.println("Not a valid JPEG file: " + path);
file.close();
return false;
}
// Scan for the Start of Frame (SOF) marker
while (file.read(marker, 2) == 2)
{
if (marker[0] != 0xFF)
continue; // Not a valid marker
// Check if it's one of the Start of Frame markers
if (marker[1] >= 0xC0 && marker[1] <= 0xC3)
{
file.seekCur(3); // Skip segment length and precision byte
uint8_t sizeData[4];
if (file.read(sizeData, 4) != 4)
{
Serial.println("Failed to read dimensions");
file.close();
return false;
}
height = (sizeData[0] << 8) | sizeData[1];
width = (sizeData[2] << 8) | sizeData[3];
file.close();
return true;
}
// Skip this marker's data segment
uint8_t segmentSize[2];
if (file.read(segmentSize, 2) != 2)
break;
int length = (segmentSize[0] << 8) | segmentSize[1];
file.seekCur(length - 2);
}
file.close();
Serial.println("Failed to find dimensions in: " + path);
return false;
}
// bool SDHandler::ditherImage(const String &inputPath, const String &outputPath) {
// SdFile inputFile;
// // Open the image file
// if (!inputFile.open(inputPath.c_str(), O_RDONLY)) {
// Serial.println("Failed to open input image: " + inputPath);
// return false;
// }
// // Get image dimensions
// int width, height;
// if (!getImageDimensions(inputPath, width, height)) {
// Serial.println("Failed to get image dimensions.");
// inputFile.close();
// return false;
// }
// // Serial.println("Image dimensions: "+ String(width) + "x" + String(height));
// // Allocate memory for the grayscale image
// uint8_t *image = (uint8_t *)malloc(width * height);
// if (!image) {
// Serial.println("Memory allocation failed.");
// inputFile.close();
// return false;
// }
// // Read and convert to grayscale
// for (int y = 0; y < height; y++) {
// for (int x = 0; x < width; x++) {
// uint8_t r, g, b;
// getPixelColor(inputFile, x, y, width, r, g, b); // Pass width to function
// // Serial.println("Pixel Color: ("+ String(r) + "," + String(g) + "," + String(b));
// image[y * width + x] = (uint8_t)(0.299 * r + 0.587 * g + 0.114 * b); // Convert to grayscale
// // Serial.println("Pixel Color: ("+ String(image[y * width + x]) + "," + String(image[y * width + x+1]) + "," + String(image[y * width + x+2]));
// }
// }
// inputFile.close();
// // Apply Floyd-Steinberg dithering
// for (int y = 0; y < height; y++) {
// for (int x = 0; x < width; x++) {
// int oldPixel = image[y * width + x];
// int newPixel = (oldPixel > 127) ? 255 : 0;
// image[y * width + x] = newPixel;
// int quantError = oldPixel - newPixel;
// // Distribute error
// if (x + 1 < width) image[y * width + (x + 1)] = constrain(image[y * width + (x + 1)] + quantError * 7 / 16, 0, 255);
// if (y + 1 < height) {
// if (x > 0) image[(y + 1) * width + (x - 1)] = constrain(image[(y + 1) * width + (x - 1)] + quantError * 3 / 16, 0, 255);
// image[(y + 1) * width + x] = constrain(image[(y + 1) * width + x] + quantError * 5 / 16, 0, 255);
// if (x + 1 < width) image[(y + 1) * width + (x + 1)] = constrain(image[(y + 1) * width + (x + 1)] + quantError * 1 / 16, 0, 255);
// }
// }
// }
// // Save the output image as a black-and-white BMP (for Arduino compatibility)
// if (!savePng(outputPath, image, width, height)) {
// Serial.println("Failed to save dithered image.");
// free(image);
// return false;
// }
// free(image);
// Serial.println("Dithered image saved successfully: " + outputPath);
// return true;
// }
void SDHandler::getPixelColor(SdFile &file, int x, int y, int imageWidth, uint8_t &r, uint8_t &g, uint8_t &b)
{
int pixelPosition = (y * imageWidth + x) * 3; // Each pixel is 3 bytes (R, G, B)
file.seekSet(pixelPosition);
r = file.read();
g = file.read();
b = file.read();
}
// bool SDHandler::savePng(const String &outputPath, uint8_t *image, int width, int height) {
// SdFile outputFile;
// if (!outputFile.open(outputPath.c_str(), O_WRITE | O_CREAT | O_TRUNC)) {
// Serial.println("Failed to open output PNG file: " + outputPath);
// return false;
// }
// // Writing a simple BMP header (PNG would require a library)
// uint8_t bmpHeader[54] = {
// 0x42, 0x4D, // BM
// 0, 0, 0, 0, // File size
// 0, 0, 0, 0, // Reserved
// 54, 0, 0, 0, // Data offset
// 40, 0, 0, 0, // Header size
// 0, 0, 0, 0, // Width
// 0, 0, 0, 0, // Height
// 1, 0, 8, 0, // Planes + BitsPerPixel
// 0, 0, 0, 0, // Compression
// 0, 0, 0, 0, // Image size
// 0x13, 0x0B, 0, 0, // X Pixels per meter
// 0x13, 0x0B, 0, 0, // Y Pixels per meter
// 0, 0, 0, 0, // Color palette
// 0, 0, 0, 0 // Important colors
// };
// // Set width & height in header
// bmpHeader[18] = width & 0xFF;
// bmpHeader[19] = (width >> 8) & 0xFF;
// bmpHeader[22] = height & 0xFF;
// bmpHeader[23] = (height >> 8) & 0xFF;
// outputFile.write(bmpHeader, 54);
// // Write pixel data
// for (int y = height - 1; y >= 0; y--) {
// for (int x = 0; x < width; x++) {
// uint8_t pixel = (image[y * width + x] > 127) ? 255 : 0;
// // uint8_t pixel = image[y * width + x];
// outputFile.write(pixel);
// }
// }
// outputFile.close();
// return true;
// }
std::vector<FileEntry> SDHandler::listFilesWithMeta(const String &path)
{
std::vector<FileEntry> entries;
SdFile dir;
if (!dir.open(path.c_str(), O_RDONLY))
{
Serial.println("Failed to open directory: " + path);
return entries;
}
SdFile file;
while (file.openNext(&dir, O_RDONLY))
{
FileEntry entry;
char fileName[150];
file.getName(fileName, sizeof(fileName));
entry.name = String(fileName);
entry.isDir = file.isDir();
entry.size = file.isDir() ? 0 : file.fileSize();
entries.push_back(entry);
file.close();
}
dir.close();
return entries;
}
bool SDHandler::deletePath(const String &path)
{
SdFile file;
if (!file.open(path.c_str(), O_RDONLY))
{
Serial.println("deletePath: path not found: " + path);
return false;
}
if (file.isDir())
{
file.close();
// Recursively delete contents first
SdFile dir;
if (!dir.open(path.c_str(), O_RDONLY))
return false;
SdFile child;
while (child.openNext(&dir, O_RDONLY))
{
char childName[150];
child.getName(childName, sizeof(childName));
bool isDir = child.isDir();
child.close();
String childPath = path + "/" + String(childName);
if (isDir)
{
if (!deletePath(childPath))
{
dir.close();
return false;
}
}
else
{
SdFile toRemove;
if (toRemove.open(childPath.c_str(), O_WRITE))
{
toRemove.remove();
}
}
}
dir.close();
// Now remove the empty directory
SdFile emptyDir;
if (emptyDir.open(path.c_str(), O_RDONLY))
{
emptyDir.rmdir();
}
return true;
}
else
{
file.close();
SdFile toRemove;
if (toRemove.open(path.c_str(), O_WRITE))
{
return toRemove.remove();
}
return false;
}
}
bool SDHandler::renamePath(const String &oldPath, const String &newPath)
{
SdFile file;
if (!file.open(oldPath.c_str(), O_RDONLY))
{
Serial.println("renamePath: source not found: " + oldPath);
return false;
}
bool result = file.rename(newPath.c_str());
file.close();
return result;
}
bool SDHandler::openFileForRead(const String &path, SdFile &file)
{
return file.open(path.c_str(), O_RDONLY);
}
uint32_t SDHandler::getFileSize(const String &path)
{
SdFile file;
if (!file.open(path.c_str(), O_RDONLY))
{
return 0;
}
uint32_t size = file.fileSize();
file.close();
return size;
}