-
Notifications
You must be signed in to change notification settings - Fork 22
/
ESP32_BadApple.ino
255 lines (226 loc) · 6.49 KB
/
ESP32_BadApple.ino
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
// Bad Apple for ESP32 with OLED SSD1306 | 2018 by Hackerspace-FFM.de | MIT-License.
#include "FS.h"
#include "SPIFFS.h"
#include "SSD1306.h"
#include "heatshrink_decoder.h"
// Hints:
// * Adjust the display pins below
// * After uploading to ESP32, also do "ESP32 Sketch Data Upload" from Arduino
SSD1306 display (0x3c, 4, 15); // For Heltec
//SSD1306 display (0x3c, 5, 4);
#if HEATSHRINK_DYNAMIC_ALLOC
#error HEATSHRINK_DYNAMIC_ALLOC must be false for static allocation test suite.
#endif
static heatshrink_decoder hsd;
// global storage for putPixels
int16_t curr_x = 0;
int16_t curr_y = 0;
// global storage for decodeRLE
int32_t runlength = -1;
int32_t c_to_dup = -1;
void listDir(fs::FS &fs, const char * dirname, uint8_t levels){
Serial.printf("Listing directory: %s\n", dirname);
File root = fs.open(dirname);
if(!root){
Serial.println("Failed to open directory");
return;
}
if(!root.isDirectory()){
Serial.println("Not a directory");
return;
}
File file = root.openNextFile();
while(file){
if(file.isDirectory()){
Serial.print(" DIR : ");
Serial.println(file.name());
if(levels){
listDir(fs, file.name(), levels -1);
}
} else {
Serial.print(" FILE: ");
Serial.print(file.name());
Serial.print(" SIZE: ");
Serial.println(file.size());
}
file = root.openNextFile();
}
}
uint32_t lastRefresh = 0;
void putPixels(uint8_t c, int32_t len) {
uint8_t b = 0;
while(len--) {
b = 128;
for(int i=0; i<8; i++) {
if(c & b) {
display.setColor(WHITE);
} else {
display.setColor(BLACK);
}
b >>= 1;
display.setPixel(curr_x, curr_y);
curr_x++;
if(curr_x >= 128) {
curr_x = 0;
curr_y++;
if(curr_y >= 64) {
curr_y = 0;
display.display();
//display.clear();
// 30 fps target rate
if(digitalRead(0)) while((millis() - lastRefresh) < 33) ;
lastRefresh = millis();
}
}
}
}
}
void decodeRLE(uint8_t c) {
if(c_to_dup == -1) {
if((c == 0x55) || (c == 0xaa)) {
c_to_dup = c;
} else {
putPixels(c, 1);
}
} else {
if(runlength == -1) {
if(c == 0) {
putPixels(c_to_dup & 0xff, 1);
c_to_dup = -1;
} else if((c & 0x80) == 0) {
if(c_to_dup == 0x55) {
putPixels(0, c);
} else {
putPixels(255, c);
}
c_to_dup = -1;
} else {
runlength = c & 0x7f;
}
} else {
runlength = runlength | (c << 7);
if(c_to_dup == 0x55) {
putPixels(0, runlength);
} else {
putPixels(255, runlength);
}
c_to_dup = -1;
runlength = -1;
}
}
}
#define RLEBUFSIZE 4096
#define READBUFSIZE 2048
void readFile(fs::FS &fs, const char * path){
static uint8_t rle_buf[RLEBUFSIZE];
size_t rle_bufhead = 0;
size_t rle_size = 0;
size_t filelen = 0;
size_t filesize;
static uint8_t compbuf[READBUFSIZE];
Serial.printf("Reading file: %s\n", path);
File file = fs.open(path);
if(!file || file.isDirectory()){
Serial.println("Failed to open file for reading");
display.drawStringMaxWidth(0, 10, 128, "File open error. Upload video.hs using ESP32 Sketch Upload."); display.display();
return;
}
filelen = file.size();
filesize = filelen;
Serial.printf("File size: %d\n", filelen);
// init display, putPixels and decodeRLE
display.clear();
display.display();
curr_x = 0;
curr_y = 0;
runlength = -1;
c_to_dup = -1;
lastRefresh = millis();
// init decoder
heatshrink_decoder_reset(&hsd);
size_t count = 0;
uint32_t sunk = 0;
size_t toRead;
size_t toSink = 0;
uint32_t sinkHead = 0;
// Go through file...
while(filelen) {
if(toSink == 0) {
toRead = filelen;
if(toRead > READBUFSIZE) toRead = READBUFSIZE;
file.read(compbuf, toRead);
filelen -= toRead;
toSink = toRead;
sinkHead = 0;
}
// uncompress buffer
HSD_sink_res sres;
sres = heatshrink_decoder_sink(&hsd, &compbuf[sinkHead], toSink, &count);
//Serial.print("^^ sinked ");
//Serial.println(count);
toSink -= count;
sinkHead = count;
sunk += count;
if (sunk == filesize) {
heatshrink_decoder_finish(&hsd);
}
HSD_poll_res pres;
do {
rle_size = 0;
pres = heatshrink_decoder_poll(&hsd, rle_buf, RLEBUFSIZE, &rle_size);
//Serial.print("^^ polled ");
//Serial.println(rle_size);
if(pres < 0) {
Serial.print("POLL ERR! ");
Serial.println(pres);
return;
}
rle_bufhead = 0;
while(rle_size) {
rle_size--;
if(rle_bufhead >= RLEBUFSIZE) {
Serial.println("RLE_SIZE ERR!");
return;
}
decodeRLE(rle_buf[rle_bufhead++]);
}
} while (pres == HSDR_POLL_MORE);
}
file.close();
Serial.println("Done.");
}
void setup(){
Serial.begin(115200);
// Reset for some displays
pinMode(16,OUTPUT); digitalWrite(16, LOW); delay(50); digitalWrite(16, HIGH);
display.init();
display.flipScreenVertically ();
display.clear();
display.setTextAlignment (TEXT_ALIGN_LEFT);
display.setFont(ArialMT_Plain_10);
display.setColor(WHITE);
display.drawString(0, 0, "Mounting SPIFFS... ");
display.display();
if(!SPIFFS.begin()){
Serial.println("SPIFFS mount failed");
display.drawStringMaxWidth(0, 10, 128, "SPIFFS mount failed. Upload video.hs using ESP32 Sketch Upload."); display.display();
return;
}
pinMode(0, INPUT_PULLUP);
Serial.print("totalBytes(): ");
Serial.println(SPIFFS.totalBytes());
Serial.print("usedBytes(): ");
Serial.println(SPIFFS.usedBytes());
listDir(SPIFFS, "/", 0);
readFile(SPIFFS, "/video.hs");
//Serial.print("Format SPIFSS? (enter y for yes): ");
// while(!Serial.available()) ;
//if(Serial.read() == 'y') {
// bool ret = SPIFFS.format();
// if(ret) Serial.println("Success. "); else Serial.println("FAILED! ");
//} else {
// Serial.println("Aborted.");
//}
}
void loop(){
}