forked from ch570512/Qlockwork
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathAnimation.cpp
275 lines (256 loc) · 7.46 KB
/
Animation.cpp
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
#include "Animation.h"
extern s_myanimation myanimation;
extern String myanimationslist[MAXANIMATION];
extern uint32_t anipalette[];
extern ESP8266WebServer webServer;
// Animationsliste
void getAnimationList()
{
int aidx = 0;
int sidx = 0;
String Aname;
uint8_t lidx = 0;
for ( uint8_t z = 0;z < MAXANIMATION;z++) myanimationslist[z] = "";
Dir dir = LittleFS.openDir("/");
while (dir.next()) {
if (!dir.isDirectory())
{
aidx = dir.fileName().indexOf("ani_");
if ( aidx >= 0 )
{
sidx = dir.fileName().lastIndexOf(".json");
if ( sidx >= 0)
{
Aname = dir.fileName().substring(aidx+4,sidx);
if ( Aname != "NEU" )
{
myanimationslist[lidx] = Aname;
// Serial.println ( myanimationslist[lidx] );
lidx++;
}
}
}
}
yield();
}
// sortieren
for (int i = 0; i < lidx; i++) {
for (int j = i + 1; j < lidx; j++) {
if (myanimationslist[j] < myanimationslist[i]) {
std::swap(myanimationslist[i], myanimationslist[j]);
}
}
}
saveAnimationsListe();
}
void saveAnimationsListe()
{
// String filename = "
String anioutput;
int bytesWritten = 0;
bool fk = false;
File file = LittleFS.open(ANIMATIONSLISTE, "w");
if (!file) {
Serial.println(F("Save Animation: Failed to create file"));
}
anioutput = F("{\n \"Animationsliste\" : [ ");
for ( uint8_t z = 0;z < MAXANIMATION;z++)
{
if ( myanimationslist[z].length() > 2 ) {
if ( fk ) anioutput += F(",");
anioutput+= "\"" + myanimationslist[z] + "\"";
}
fk = true;
}
anioutput+= F(" ]\n}\n");
bytesWritten = file.print(anioutput);
#ifdef DEBUG_ANIMATION
if (bytesWritten > 0)
{
Serial.println(F("File " ANIMATIONSLISTE " was written"));
Serial.println(bytesWritten);
} else {
Serial.println(F("File " ANIMATIONSLISTE " write failed"));
}
#endif
// Close the file
file.close();
}
// Lade die Animation
bool loadAnimation(String aniname )
{
#ifdef DEBUG_ANIMATION
Serial.print(F("Loading Animation "));
Serial.println (aniname);
#endif
String filename = "/ani_" + aniname + ".json";
String filezeile;
String typ;
String wert;
uint32_t farbwert;
uint8_t frame = 0;
uint8_t zeile = 0;
String jsonBuffer;
File file = LittleFS.open(filename,"r");
if(!file)
{
Serial.print(F("There was an error opening the file "));
Serial.print(aniname);
Serial.println(F(" for reading"));
return false;
}
// animation leer initialisieren
for ( uint8_t f = 0;f < MAXFRAMES;f++)
{
myanimation.frame[f].delay = 0;
for ( uint8_t zeile = 0;zeile <= 9;zeile++)
{
for ( uint8_t x = 0;x <= 10;x++)
{
myanimation.frame[f].color[x][zeile] = num_to_color(0);
}
}
}
while (file.available())
{
filezeile = file.readStringUntil('\n');
typ = filezeile.substring(0,filezeile.indexOf(":"));
wert = filezeile.substring(filezeile.indexOf(":")+1);
typ.replace("\"","");
typ.trim();
wert.replace(",","");
wert.replace("\"","");
wert.trim();
// Serial.printf("typ: %s = %s\n" , typ.c_str(), wert.c_str());
if ( typ == "Name" ) aniname.toCharArray(myanimation.name,aniname.length()+1);
if ( typ == "Loops" ) myanimation.loops = wert.toInt();
if ( typ == "Laufmode" ) myanimation.laufmode = wert.toInt();
if ( typ == "Palette")
{
jsonBuffer = filezeile;
jsonBuffer.replace("],","]");
jsonBuffer = '{' + jsonBuffer + "}";
// Serial.print( "jsonBuffer0: " );
// Serial.println (jsonBuffer);
if ( !loadjsonarry(-1,0,jsonBuffer) ) return false; //Load Palette
jsonBuffer="";
}
if ( typ.substring(0,5) == "Frame" ) frame = typ.substring(typ.indexOf("_")+1).toInt();
if ( typ == "Delay") myanimation.frame[frame].delay = wert.toInt();
if ( typ.substring(0,5) == "Zeile" )
{
zeile = typ.substring(typ.indexOf("_")+1).toInt();
jsonBuffer = filezeile;
jsonBuffer.replace("],","]");
jsonBuffer = '{' + jsonBuffer + "}";
if ( !loadjsonarry(frame,zeile,jsonBuffer) ) return false; //Load Zeilen pro Frame
jsonBuffer="";
}
// Serial.printf("AnimationFrames ESP.getMaxFreeBlockSize: %i Codezeile: %u\n", ESP.getMaxFreeBlockSize(), __LINE__);
yield();
}
file.close();
#ifdef DEBUG_ANIMATION
Serial.print( "myanimation.name: " );
Serial.println (myanimation.name);
Serial.print( "myanimation.loops: " );
Serial.println (myanimation.loops);
Serial.print( "myanimation.laufmode: " );
Serial.println (myanimation.laufmode);
Serial.print( "anipalette: " );
for ( uint8_t palidx = 0;palidx <= 9;palidx++)
{
Serial.print(num_to_string(anipalette[palidx]));
Serial.print(" ");
}
Serial.println("");
for ( uint8_t f = 0;f < MAXFRAMES;f++)
{
Serial.print ("myanimation.frame: ");
Serial.println (f);
Serial.print ("myanimation.frame.delay: ");
Serial.println (myanimation.frame[f].delay);
for ( uint8_t zeile = 0;zeile <= 9;zeile++)
{
Serial.print ("myanimation.frame.zeile: ");
Serial.print (zeile);
Serial.print (": ");
for ( uint8_t x = 0;x <= 10;x++)
{
Serial.print (color_to_string(myanimation.frame[f].color[x][zeile]));
Serial.print (" ");
}
Serial.println(" ");
}
}
Serial.printf("AnimationFrames ESP.getMaxFreeBlockSize: %i Codezeile: %u\n", ESP.getMaxFreeBlockSize(), __LINE__);
#endif
return true;
}
bool loadjsonarry( int8_t frame, uint8_t zeile, String &jsonBuffer)
{
String farbwert;
String zeile_json;
JSONVar myObject = JSON.parse(jsonBuffer);
if (JSON.typeof(myObject) == "undefined") { Serial.println(F("Load JSON_ARRAY: Parsing the Animationsfile failed")); return false;}
delay(0);
if ( frame == -1 && zeile == 0 )
{
for ( uint8_t palidx = 0;palidx <= 9;palidx++)
{
farbwert = (const char*)myObject["Palette"][palidx];
anipalette[palidx]=string_to_num(farbwert);
}
}
else
{
zeile_json = "Zeile_" + String(zeile);
for ( uint8_t x = 0;x < myObject[zeile_json].length();x++)
{
farbwert = (const char*)myObject[zeile_json][x];
// Serial.printf ("pixel x/y %i/%i = %s ",x,zeile,farbwert.c_str());
myanimation.frame[frame].color[x][zeile] = string_to_color(farbwert);
}
}
return true;
}
// Farbwert (z.B. #FF00DD ) von HEX nach int wandeln
uint32_t string_to_num(String in_color)
{
char buffer[9];
in_color.substring(1, 7).toCharArray(buffer,7);
return strtol(buffer, 0, 16);
}
// Farbwert (z.B. #FF00DD ) von HEX nach String wandeln
String num_to_string(uint32_t in_color)
{
String farbe;
farbe = "000000" + String(in_color,HEX);
farbe = "#" + farbe.substring( farbe.length()-6,farbe.length());
farbe.toUpperCase();
return farbe;
}
// Farbwert (z.B. red, green, blue ) nach String wandeln
String color_to_string(color_s in_color)
{
String farbe;
uint32_t colbuf = 0;
colbuf = in_color.red<<16;
colbuf = colbuf | in_color.green<<8;
colbuf = colbuf | in_color.blue;
return num_to_string(colbuf);
}
// Farbwert string -> s_color
color_s string_to_color(String in_color)
{
return num_to_color(string_to_num(in_color));
}
// Farbwert num -> s_color (z.B. 0xFF00DD) nach red, green, blue
color_s num_to_color(uint32_t in_color)
{
color_s s_color;
s_color.red = in_color >> 16 & 0xFF;
s_color.green = in_color >> 8 & 0xFF;
s_color.blue = in_color & 0xFF;
return s_color;
}