-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage_browser.cpp
More file actions
199 lines (160 loc) · 5.2 KB
/
image_browser.cpp
File metadata and controls
199 lines (160 loc) · 5.2 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
#include "image_browser.h"
#include "project_config.h"
ImageBrowser imageBrowser;
ImageBrowser::ImageBrowser() : currentIndex(0), initialized(false) {
}
ImageBrowser::~ImageBrowser() {
deinit();
}
bool ImageBrowser::init() {
if (initialized) {
Serial.println("[ImageBrowser] Already initialized");
return true;
}
Serial.println("[ImageBrowser] Initializing...");
// SD card is already initialized by SD Manager, just check if it's available
if (!SD.exists("/")) {
Serial.println("[ImageBrowser] ERROR: SD card not available");
return false;
}
Serial.println("[ImageBrowser] SD card OK, scanning for images...");
scanSDCard();
if (hasImages()) {
Serial.printf("[ImageBrowser] ✅ Successfully initialized with %d images\n", getImageCount());
initialized = true;
return true;
} else {
Serial.println("[ImageBrowser] ❌ No images found - initialization failed");
return false;
}
}
void ImageBrowser::deinit() {
imageList.clear();
currentIndex = 0;
initialized = false;
}
bool ImageBrowser::isImageFile(const String& filename) {
String lower = filename;
lower.toLowerCase();
return lower.endsWith(".png") ||
lower.endsWith(".jpg") ||
lower.endsWith(".jpeg") ||
lower.endsWith(".bmp") ||
lower.endsWith(".raw");
}
void ImageBrowser::scanSDCard() {
imageList.clear();
// First try to scan /images/ directory
File imagesDir = SD.open("/images");
File root;
if (imagesDir && imagesDir.isDirectory()) {
Serial.println("[ImageBrowser] Scanning /images/ directory");
root = imagesDir;
} else {
Serial.println("[ImageBrowser] /images/ directory not found, scanning root directory");
root = SD.open("/");
if (!root) {
Serial.println("[ImageBrowser] ERROR: Cannot open SD card root");
return;
}
}
File file = root.openNextFile();
int imageCount = 0;
int maxFiles = 5000; // Safety limit for file iteration
int fileCounter = 0;
while (file && fileCounter < maxFiles) {
if (!file.isDirectory()) {
String filename = file.name();
if (isImageFile(filename)) {
// If scanning /images/, add full path
if (imagesDir && imagesDir.isDirectory()) {
imageList.push_back("/images/" + filename);
} else {
imageList.push_back("/" + filename);
}
imageCount++;
Serial.printf("[ImageBrowser] Found image: %s\n", filename.c_str());
}
}
file.close(); // Explicitly close file
file = root.openNextFile();
fileCounter++;
}
if (fileCounter >= maxFiles) {
Serial.printf("[ImageBrowser] WARNING: Reached maximum file limit (%d), scan may be incomplete\n", maxFiles);
}
root.close();
Serial.printf("[ImageBrowser] Total images found: %d\n", imageCount);
// Sort the list for consistent order
std::sort(imageList.begin(), imageList.end());
// Set current index to first image or 0 if none
currentIndex = hasImages() ? 0 : -1;
}
bool ImageBrowser::nextImage() {
if (!hasImages()) {
return false;
}
currentIndex = (currentIndex + 1) % imageList.size();
return true;
}
bool ImageBrowser::previousImage() {
if (!hasImages()) {
return false;
}
currentIndex = (currentIndex - 1 + imageList.size()) % imageList.size();
return true;
}
bool ImageBrowser::goToFirst() {
if (!hasImages()) {
return false;
}
currentIndex = 0;
return true;
}
bool ImageBrowser::goToLast() {
if (!hasImages()) {
return false;
}
currentIndex = imageList.size() - 1;
return true;
}
bool ImageBrowser::goToIndex(int index) {
if (!hasImages() || index < 0 || index >= (int)imageList.size()) {
return false;
}
currentIndex = index;
return true;
}
String ImageBrowser::getCurrentImagePath() {
if (!hasImages() || currentIndex < 0 || currentIndex >= (int)imageList.size()) {
Serial.printf("[ImageBrowser] WARNING: Invalid state - hasImages:%s, currentIndex:%d, size:%d\n",
hasImages() ? "true" : "false", currentIndex, (int)imageList.size());
return "";
}
return imageList[currentIndex];
}
String ImageBrowser::getCurrentImageName() {
String path = getCurrentImagePath();
if (path.length() == 0) {
return "";
}
int lastSlash = path.lastIndexOf('/');
if (lastSlash >= 0) {
return path.substring(lastSlash + 1);
}
return path;
}
void ImageBrowser::refreshImageList() {
scanSDCard();
}
void ImageBrowser::printImageList() {
if (!hasImages()) {
Serial.println("No images found on SD card");
return;
}
Serial.printf("Found %d images:\n", imageList.size());
for (int i = 0; i < (int)imageList.size(); i++) {
String marker = (i == currentIndex) ? " > " : " ";
Serial.printf("%s%d: %s\n", marker.c_str(), i + 1, imageList[i].c_str());
}
}