forked from jasoncoon/LightAppliance
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBrowseAnimationsMode.cpp
More file actions
388 lines (314 loc) · 11.5 KB
/
Copy pathBrowseAnimationsMode.cpp
File metadata and controls
388 lines (314 loc) · 11.5 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
/*
* Animated GIF and directory browser for IR Remote Controlled Light Appliance Application for the 32x32 RGB LED Matrix
*
* Written by: Jason Coon
* Copyright (c) 2014 Jason Coon
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "BrowseAnimationsMode.h"
#include "Types.h"
#include "Codes.h"
#include "Colors.h"
#define ANIMATION_DISPLAY_DURATION_SECONDS 10
// Defined in GIFParseFunctions.cpp
extern unsigned long processGIFFile(const char *pathname, unsigned long(*checkForInput)());
// Defined in LightAppliance.ino
extern unsigned long checkForInput();
void BrowseAnimationsMode::run(SmartMatrix matrix, IRrecv irReceiver, SdFat sd) {
BrowseAnimationsMode::matrix = &matrix;
BrowseAnimationsMode::irReceiver = &irReceiver;
BrowseAnimationsMode::sd = &sd;
browseDirectory("/");
}
void BrowseAnimationsMode::browseDirectory(const char *path) {
int index = 0;
Serial.print("browsing directory: ");
Serial.println(path);
int numberOfFiles = countFiles(path);
Serial.print("number of files: ");
Serial.println(numberOfFiles);
while (true) {
// Clear screen
matrix->fillScreen(COLOR_BLACK);
// Fonts are font3x5, font5x7, font6x10, font8x13
matrix->setFont(font5x7);
// Static Mode Selection Text
matrix->drawString(2, 0, COLOR_BLUE, "Select");
matrix->setFont(font3x5);
matrix->drawString(3, 7, COLOR_BLUE, "Pattern");
matrix->drawString(3, 14, COLOR_BLUE, "< use >");
matrix->swapBuffers();
// Setup for scrolling mode
matrix->setScrollMode(wrapForward);
matrix->setScrollSpeed(36); // 10
matrix->setScrollFont(font5x7);
matrix->setScrollColor(COLOR_GREEN);
matrix->setScrollOffsetFromEdge(22);
matrix->scrollText("", 1);
boolean fileSelected = false;
while (!fileSelected) {
// Get current directory or file name
Serial.print("getting file name by index: ");
Serial.println(index);
char name[13];
getNameByIndex(path, index, name, numberOfFiles);
Serial.print("name: ");
Serial.println(name);
if(strcmp(name, "SYSTEM~1") == 0) {
index++;
break;
}
char selectedPath[255];
strcpy(selectedPath, path);
strcat(selectedPath, name);
strcat(selectedPath, "/");
char* longName = getDirectoryLongName(selectedPath);
if (strcmp(longName, "") == 0) {
// Set name selection text
matrix->scrollText(name, 32000);
}
else {
// Set name selection text
matrix->scrollText(longName, 32000);
}
unsigned long irCode = waitForIRCode();
switch (irCode) {
case IRCODE_HOME:
return;
case IRCODE_LEFT:
index--;
if (index < 0) {
index = numberOfFiles - 1;
}
break;
case IRCODE_RIGHT:
index++;
if (index >= numberOfFiles) {
index = 0;
}
break;
case IRCODE_SEL:
// Turn off any text scrolling
matrix->scrollText("", 1);
matrix->setScrollMode(off);
// Clear screen
matrix->fillScreen(COLOR_BLACK);
matrix->swapBuffers();
// open the selected directory, or display the selected animation
Serial.print("selected file: ");
Serial.println(name);
Serial.print("opening selected path: ");
Serial.println(selectedPath);
SdFile file;
if (file.open(selectedPath)) {
if (file.isDir()) {
browseDirectory(selectedPath);
}
else {
runAnimation(path, index, numberOfFiles);
}
file.close();
}
fileSelected = true;
break;
}
}
}
}
char * BrowseAnimationsMode::getDirectoryLongName(const char *directoryName) {
char pathname[255];
strcpy(pathname, directoryName);
strcat(pathname, "index.txt");
String longName;
SdFile indexFile;
if (indexFile.open(pathname) && indexFile.isOpen() && indexFile.isFile()) {
int16_t c;
while ((c = indexFile.read()) > 0) {
char character = (char) c;
if (character == '\r' || character == 0) {
break;
}
longName.concat(character);
}
}
indexFile.close();
char longNameChar[100];
longName.toCharArray(longNameChar, 100);
return longNameChar;
}
void BrowseAnimationsMode::runAnimation(const char* directoryName, int index, int numberOfFiles) {
Serial.print("running animation file: ");
Serial.println(directoryName);
bool timeoutDisabled = true;
while (true) {
if (index < 0) {
index = numberOfFiles - 1;
}
else if (index >= numberOfFiles) {
index = 0;
}
// Clear screen for new animation
matrix->fillScreen(COLOR_BLACK);
matrix->swapBuffers();
char name[13];
getNameByIndex(directoryName, index, name, numberOfFiles);
char pathname[255];
strcpy(pathname, directoryName);
strcat(pathname, name);
// Calculate time in the future to terminate animation
unsigned long timeOut = millis() + (ANIMATION_DISPLAY_DURATION_SECONDS * 1000);
while (timeoutDisabled || timeOut > millis()) {
unsigned long result = processGIFFile(pathname, checkForInput);
// handle user input
if (result == IRCODE_HOME) {
return;
}
else if (result == IRCODE_LEFT) {
index -= 2;
break;
}
else if (result == IRCODE_RIGHT) {
break;
}
else if (result == IRCODE_SEL) {
// toggle timeout on/off
timeoutDisabled = !timeoutDisabled;
}
else if (result != 0) {
break;
}
}
index++;
}
}
// count the number of directories and files
int BrowseAnimationsMode::countFiles(const char* directoryName) {
int number = 0;
// Set the current working directory
if (!sd->chdir(directoryName, true)) {
return 0;
}
sd->vwd()->rewind();
SdFile file;
char fn[13];
while (file.openNext(sd->vwd(), O_READ)) {
file.getFilename(fn);
// If filename not deleted, count it
if (fn[0] != '_') {
number++;
}
file.close();
}
// Set the current working directory
sd->chdir("/", true);
return number;
}
// Get the full path/filename of the GIF file with specified index
void BrowseAnimationsMode::getNameByIndex(const char *directoryName, int index, char *nameBuffer, int numberOfFiles) {
Serial.println("getNameByIndex");
char filename[13];
// Make sure index is in range
Serial.println("Make sure index is in range");
if ((index >= 0) && (index < numberOfFiles)) {
Serial.println("index is in range");
// Set the current working directory
Serial.println("Set the current working directory");
if (!sd->chdir(directoryName, true)) {
Serial.println("Could not change to directory");
sd->errorHalt("Could not change to directory");
}
// Make sure file is closed before starting
SdFile file;
// Rewind the directory to the beginning
Serial.println("Rewind the directory to the beginning");
sd->vwd()->rewind();
Serial.println("opening file");
while ((file.openNext(sd->vwd(), O_READ)) && (index >= 0)) {
Serial.println("getting filename");
file.getFilename(filename);
Serial.print("filename: ");
Serial.println(filename);
// If filename is not marked as deleted, count it
if ((filename[0] != '_') && (filename[0] != '~')) {
Serial.println("file is not marked as deleted");
index--;
}
Serial.println("closing file");
file.close();
}
// Set the current working directory back to root
Serial.println("Setting the current working directory back to root");
if (!sd->chdir("/", true)) {
Serial.println("Could not change to root directory");
sd->errorHalt("Could not change to root directory");
}
// Copy the filename to the name buffer
Serial.println("Copying the filename to the name buffer");
strcpy(nameBuffer, filename);
Serial.println("Copied the filename to the name buffer");
Serial.print("name buffer:");
Serial.println(nameBuffer);
}
}
//// Check for input
//// This can be called by display
//unsigned long BrowseAnimationsMode::checkForInput() {
//
// boolean timeOutCondition = timeOutEnabled && (millis() > timeOut);
// if (timeOutCondition)
// return 0;
//
// return readIRCode();
//}
unsigned long BrowseAnimationsMode::waitForIRCode() {
unsigned long irCode = readIRCode();
while ((irCode == 0) || (irCode == 0xFFFFFFFF)) {
delay(200);
irCode = readIRCode();
}
return irCode;
}
// Read an IR code
// Function will return 0 if no IR code available
unsigned long BrowseAnimationsMode::readIRCode() {
// Is there an IR code to read ?
unsigned long code = _readIRCode();
if (code == 0) {
// No code so return 0
return 0;
}
// Keep reading until code changes
while (_readIRCode() == code) {
;
}
return code;
}
// Low level IR code reading function
// Function will return 0 if no IR code available
unsigned long BrowseAnimationsMode::_readIRCode() {
decode_results results;
results.value = 0;
// Attempt to read an IR code ?
if (irReceiver->decode(&results)) {
delay(20);
// Prepare to receive the next IR code
irReceiver->resume();
}
return results.value;
}