-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubFileManager.cpp
More file actions
174 lines (136 loc) · 4.03 KB
/
Copy pathSubFileManager.cpp
File metadata and controls
174 lines (136 loc) · 4.03 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
//
// Created by codexzier on 22.06.26.
//
#include "SubFileManager.h"
#include <SD.h>
#include <Arduino.h>
#include "FS.h"
uint16_t SubFileManager::GetNextPixelColorValue(File &file) {
const uint8_t highByte = file.read(); // Zweites Byte (hochwertig)
const uint8_t lowByte = file.read(); // Erstes Byte (niederwertig)
const uint16_t color = highByte << 8 | lowByte;
return color;
}
void SubFileManager::readSdCard() {
if (!SD.begin(21)) {
Serial.print("Card Mount Failed");
delay(1000);
return;
}
uint8_t cardType = SD.cardType();
if (cardType == CARD_NONE) {
Serial.print("No SD card attached");
return;
}
Serial.print("SD Card Type: ");
if (cardType == CARD_MMC) {
Serial.print("MMC");
} else if (cardType == CARD_SD) {
Serial.print("SDSC");
} else if (cardType == CARD_SDHC) {
Serial.print("SDHC");
} else {
Serial.print("UNKNOWN");
}
_sdSuccess = true;
}
void SubFileManager::init() {
Serial.println("create instance of the filename list or reset");
if (!_filenames) {
Serial.println("set instance of filename list");
_filenames = std::make_unique<std::vector<std::string>>();
}
readSdCard();
}
// SD card write file
void SubFileManager::writeFile(const char * path, uint8_t * data, size_t len){
Serial.printf("Writing file: %s\n", path);
File file = SD.open(path, FILE_WRITE);
if(!file){
Serial.println("Failed to open file for writing");
delay(1000);
return;
}
if(file.write(data, len) == len){
Serial.println("File written");
} else {
Serial.println("Write failed");
delay(1000);
}
file.close();
}
void SubFileManager::readFiles() {
Serial.println("Get root");
File root = SD.open("/");
if (!root) {
Serial.println("Failed to open directory");
delay(1000);
return;
}
Serial.println("Check directory");
if (!root.isDirectory()) {
Serial.println("Not a directory");
delay(1000);
return;
}
Serial.println("create instance of the filename list or reset");
if (!_filenames) {
Serial.println("set instance of filename list");
_filenames = std::make_unique<std::vector<std::string>>();
}
else {
Serial.println("reset list");
_filenames->clear();
}
Serial.println("read root directory");
File file = root.openNextFile();
while (file) {
if (file.isDirectory()) {
Serial.print(" DIR : ");
Serial.println(file.name());
} else {
std::string filename = file.name();
Serial.print(" FILE: ");
Serial.print(filename.c_str());
Serial.print(" SIZE: ");
Serial.println(file.size());
_filenames->push_back(filename);
}
file = root.openNextFile();
}
file.close();
const int countFiles = static_cast<int>(_filenames->size());
Serial.print("Count files: "); Serial.println(countFiles, DEC);
}
int SubFileManager::GetCountPictures() {
readFiles();
return static_cast<int>(_filenames->size());
}
int SubFileManager::GetCardSizeMb() {
return SD.usedBytes() / 1024 / 1024;
}
uint8_t* SubFileManager::GetPictureData(int index) {
//if (!_filenames) {
Serial.println("Files are not readed! try to read");
readFiles();
if (!_filenames) {
return nullptr;
}
//}
Serial.println("Read file from index.");
const std::string filename = "/" + (*_filenames)[index];
Serial.print("Read file: "); Serial.println(filename.c_str());
File file = SD.open(filename.c_str());
if (!file) {
Serial.println("File picture can not loaded.");
return nullptr;
}
size_t fileSize = file.size();
uint8_t* bitmap = new uint8_t[fileSize];
file.read(bitmap, fileSize);
file.close();
return bitmap;
}
bool SubFileManager::IsSdCardInit() {
return _sdSuccess;
}