forked from russp81/LEDLAMP_FASTLEDs
-
Notifications
You must be signed in to change notification settings - Fork 4
/
palettes.h
88 lines (68 loc) · 2.46 KB
/
palettes.h
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
// Copyright (c) 2016 @jake-b, @russp81, @toblum
// Griswold LED Lighting Controller
// Griswold is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation, either version 3 of
// the License, or (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
// Griswold is a fork of the LEDLAMP project at
// https://github.com/russp81/LEDLAMP_FASTLEDs
// The LEDLAMP project is a fork of the McLighting Project at
// https://github.com/toblum/McLighting
int paletteCount = 0;
int getPaletteCount() {
Dir dir = SPIFFS.openDir("/palettes");
int palette_count = 0;
while (dir.next()) {
palette_count++;
}
DBG_OUTPUT_PORT.printf("Palette count: %d\n", palette_count);
return palette_count;
}
bool openPaletteFileWithIndex(int index, File* file) {
Dir dir = SPIFFS.openDir("/palettes");
int palette_count = 0;
while (dir.next()) {
if (palette_count == index) break;
palette_count++;
}
if (palette_count != index) {
DBG_OUTPUT_PORT.println("Error, unable to open palette");
return false;
}
*file = dir.openFile("r");
return true; //success
}
bool loadPaletteFromFile(int index, CRGBPalette16* palette) {
File paletteFile;
if (!openPaletteFileWithIndex(index, &paletteFile)) {
DBG_OUTPUT_PORT.printf("Error loading paletteFile at index %d\n", index);
return false;
}
int paletteFileSize = paletteFile.size();
uint8_t* bytes = new uint8_t[paletteFileSize];
if (!bytes) {
DBG_OUTPUT_PORT.println("Unable to allocate memory for palette");
return false;
}
paletteFile.readBytes((char*)bytes, paletteFileSize);
paletteFile.close();
DBG_OUTPUT_PORT.printf("Load palette named %s (%d bytes)\n", paletteFile.name(), paletteFileSize);
palette->loadDynamicGradientPalette(bytes);
delete[] bytes;
return true;
}
String getPaletteNameWithIndex(int index) {
Dir dir = SPIFFS.openDir("/palettes");
int ndx = 0;
while (dir.next()) {
if (ndx == index) return dir.fileName();
ndx++;
}
return "[unknown]";
}