FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
ESP32-Text-Reader/src/main.cpp at main · UnsignedArduino/ESP32-Text-Reader · GitHub
UnsignedArduino
/
ESP32-Text-Reader
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
3
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
ESP32-Text-Reader
/
src
/
main.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
439 lines (407 loc) · 11.4 KB
Breadcrumbs
ESP32-Text-Reader
/
src
/
main.cpp
Copy path
File metadata and controls
439 lines (407 loc) · 11.4 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
//
.pio\libdeps\esp32doit-devkit-v1\SdFat\src\SdFatConfig.h
#
define
SD_FAT_TYPE
3
#
define
USE_FAT_FILE_FLAG_CONTIGUOUS
1
#
define
SPI_DRIVER_SELECT
1
#
define
USE_SPI_ARRAY_TRANSFER
1
#
define
USE_SEPARATE_FAT_CACHE
1
#
define
USE_EXFAT_BITMAP_CACHE
1
#
define
USE_MULTI_SECTOR_IO
1
#
include
<
Arduino.h
>
#
include
<
SdFat.h
>
#
include
"
utils.h
"
#
include
"
sdUtils.h
"
#
include
"
bookmark.h
"
#
include
"
epaperUtils.h
"
#
include
"
screensaver.h
"
#
include
<
GUI_Paint.h
>
#
include
"
buttonUtils.h
"
#
include
"
battUtils.h
"
#
include
"
textReader.h
"
#
include
"
config.h
"
#
include
"
profiling.h
"
unsigned
long
page =
0
;
unsigned
long
maxPage =
0
;
void
textReader
(
const
char
* path) {
working
();
Serial.
print
(
"
Opening
"
);
Serial.
println
(path);
file = sd.
open
(path,
O_READ
);
if
(!file) {
Serial.
print
(
"
Unable to open
"
);
Serial.
println
(path);
blinkError
(
500
,
1000
);
}
fileSize = file.
size
();
Serial.
print
(
"
File size:
"
);
Serial.
print
(fileSize);
Serial.
println
(
"
bytes
"
);
Serial.
println
(
"
Finding max page
"
);
loadingTextFile
(path);
unsigned
long
crc;
maxPage =
getMaxPage
(maxLines, crc);
Serial.
print
(
"
Max page:
"
);
Serial.
println
(maxPage);
Serial.
print
(
"
CRC32: 0x
"
);
Serial.
println
(crc,
HEX
);
Serial.
println
(
"
First text print
"
);
filePos =
0
;
file.
seek
(filePos);
nextFilePos =
printFromLocation
(maxLines);
unsigned
long
lastPage =
0
;
if
(
getLastPage
(path, fileSize, crc, lastPage) && lastPage >
0
) {
working
();
Serial.
print
(
"
Found last page set to
"
);
Serial.
println
(lastPage);
const
byte linesToStickUp =
7
;
const
byte bufSize = maxCharPerLine +
1
;
char
lastPageBuf[bufSize];
snprintf
(lastPageBuf, bufSize,
"
Your were last on page %lu.
"
, lastPage +
1
);
const
char
* lines[linesToStickUp] = {
lastPageBuf,
"
"
,
"
Do you want to go there?
"
,
"
"
,
"
Left: No
"
,
"
Middle: Cancel
"
,
"
Right: Yes
"
};
drawDialog
(lines, linesToStickUp);
updateDisplay
();
notWorking
();
byte pressed =
waitForButtonPress
();
working
();
if
(pressed ==
RIGHT_BUTTON
) {
Serial.
println
(
"
Going to last page
"
);
filePos =
getPosFromPage
(lastPage, maxLines);
page = lastPage;
}
else
if
(pressed ==
CENTER_BUTTON
) {
Serial.
println
(
"
Cancel!
"
);
Serial.
println
(
"
Closing file
"
);
file.
close
();
notWorking
();
return
;
}
else
{
Serial.
println
(
"
Going to first page
"
);
filePos =
0
;
page =
0
;
}
file.
seek
(filePos);
nextFilePos =
printFromLocation
(maxLines);
}
else
{
page =
0
;
}
working
();
updateDisplay
();
notWorking
();
while
(
true
) {
#
if
defined(SET_PAGE_VIA_SERIAL)
if
(Serial.
available
()) {
working
();
//
Flush receive buffer
while
(Serial.
available
()) {
Serial.
read
();
}
Serial.
print
(
"
Current page is
"
);
Serial.
println
(page);
Serial.
println
(
"
Type the page number you want to go to:
"
);
delay
(
1000
);
Serial.
println
(
"
\n
Parsing number
"
);
page = Serial.
parseInt
();
Serial.
print
(
"
Got:
"
);
Serial.
println
(page);
Serial.
print
(
"
Going to page
"
);
Serial.
println
(page);
filePos =
getPosFromPage
(page, maxLines);
file.
seek
(filePos);
nextFilePos =
printFromLocation
(maxLines);
updateDisplay
();
notWorking
();
}
#
endif
if
(
readLeftButton
() && page >
0
) {
working
();
page --;
Serial.
print
(
"
Going to page
"
);
Serial.
println
(page);
//
Have to seek back to beginning and count pages to here
filePos =
getPosFromPage
(page, maxLines);
file.
seek
(filePos);
nextFilePos =
printFromLocation
(maxLines);
updateDisplay
();
notWorking
();
}
if
(
readCenterButton
()) {
working
();
//
Seek back to beginning of page and reprint it
file.
seek
(filePos);
nextFilePos =
printFromLocation
(maxLines);
//
Page is zero indexed
drawTextReaderMenu
(path, page +
1
, maxPage +
1
);
updateDisplay
();
Serial.
println
(
"
Waiting for button press
"
);
notWorking
();
byte pressed =
waitForButtonPress
();
if
(pressed ==
LEFT_BUTTON
) {
Serial.
println
(
"
Left button pressed, exiting!
"
);
setLastPage
(path, fileSize, crc, page);
break
;
}
else
if
(pressed ==
RIGHT_BUTTON
) {
working
();
Serial.
println
(
"
Selecting page
"
);
page =
askForPage
(page, maxPage);
Serial.
print
(
"
Got page:
"
);
Serial.
println
(page);
filePos =
getPosFromPage
(page, maxLines);
notWorking
();
}
working
();
file.
seek
(filePos);
nextFilePos =
printFromLocation
(maxLines);
updateDisplay
();
notWorking
();
waitForButtonRelease
();
}
if
(
readRightButton
() && page < maxPage) {
working
();
page ++;
Serial.
print
(
"
Going to page
"
);
Serial.
println
(page);
//
We can use where we left off
filePos = nextFilePos;
file.
seek
(nextFilePos);
nextFilePos =
printFromLocation
(maxLines);
updateDisplay
();
notWorking
();
}
}
working
();
Serial.
println
(
"
Closing file
"
);
file.
close
();
notWorking
();
}
int
fileSelector
(
char
* pathBuf, byte maxPathLen) {
working
();
unsigned
long
optionIdx =
1
;
unsigned
long
maxFileIdx =
filesInRoot
() -
1
;
unsigned
long
maxOptionIdx = maxFileIdx +
1
;
unsigned
long
optionSkip =
0
;
const
unsigned
long
maxOptionPage =
24
;
FsFile root = sd.
open
(
"
/
"
);
notWorking
();
while
(
true
) {
working
();
Paint_Clear
(
WHITE
);
Serial.
print
(
"
Selected option:
"
);
Serial.
print
(optionIdx);
Serial.
print
(
"
/
"
);
Serial.
println
(maxOptionIdx);
if
(optionIdx > maxOptionPage -
1
) {
optionSkip = (optionIdx / maxOptionPage) * maxOptionPage;
}
else
{
optionSkip =
0
;
}
Serial.
print
(
"
Skipping
"
);
Serial.
print
(optionSkip);
Serial.
println
(
"
options
"
);
const
byte linesToStickUp =
6
;
char
selectedPath[maxPathLen];
root.
rewindDirectory
();
Serial.
println
(
"
Contents:
"
);
char
line[maxCharPerLine +
1
];
const
unsigned
int
maxRow = maxLines - linesToStickUp + optionSkip;
for
(
unsigned
int
row =
0
; row < maxRow; ) {
unsigned
int
yPos = (row - optionSkip) *
13
;
memset
(line,
0
, maxCharPerLine +
1
);
if
(row ==
0
) {
if
(row >= optionSkip) {
if
(row == optionIdx) {
strncat
(line,
"
>
"
, maxCharPerLine +
1
);
}
else
{
strncat
(line,
"
"
, maxCharPerLine +
1
);
}
strncat
(line,
"
Exit
"
, maxPathLen);
Paint_DrawLine
(
0
, yPos +
16
,
EPD_4IN2_HEIGHT
, yPos +
16
,
BLACK
,
DOT_PIXEL_1X1
,
LINE_STYLE_SOLID
);
yPos +=
2
;
}
}
else
{
FsFile entry = root.
openNextFile
();
if
(!entry) {
break
;
}
if
(entry.
isDirectory
()) {
continue
;
}
char
path[maxPathLen];
entry.
getName
(path, maxPathLen -
1
);
if
(row == optionIdx) {
strncat
(line,
"
>
"
, maxCharPerLine +
1
);
strncpy
(selectedPath, path, maxPathLen);
}
else
{
strncat
(line,
"
"
, maxCharPerLine +
1
);
}
if
(
2
+
strlen
(path) > maxCharPerLine) {
strncat
(line, path, maxCharPerLine -
3
);
strncat
(line,
"
...
"
, maxCharPerLine +
1
);
}
else
{
strncat
(line, path, maxCharPerLine +
1
);
}
entry.
close
();
yPos +=
5
;
}
if
(row >= optionSkip) {
Serial.
println
(line);
Paint_DrawString_EN
(
0
, yPos, line, &Font12,
WHITE
,
BLACK
);
}
row ++;
}
drawScrollbar
(
EPD_4IN2_HEIGHT
-
3
,
1
,
3
,
EPD_4IN2_WIDTH
- (
13
* linesToStickUp),
optionSkip,
min
(optionSkip + maxOptionPage, maxOptionIdx), maxOptionIdx);
const
byte bufSize = maxCharPerLine +
1
;
char
battBuf[bufSize];
snprintf
(battBuf, bufSize,
"
Battery: %u%%
"
,
readPercent
());
const
char
*lines[linesToStickUp] = {
"
Select an option
"
,
battBuf,
"
"
,
"
Left: Move up
"
,
"
Center: Select
"
,
"
Right: Move down
"
};
drawDialog
(lines, linesToStickUp);
updateDisplay
();
notWorking
();
byte pressed =
waitForButtonPress
();
if
(pressed ==
CENTER_BUTTON
) {
root.
close
();
if
(optionIdx ==
0
) {
Serial.
println
(
"
Exiting!
"
);
return
1
;
}
else
{
Serial.
print
(
"
Selected path:
"
);
Serial.
println
(selectedPath);
strncpy
(pathBuf, selectedPath, maxPathLen);
return
0
;
}
}
else
{
unsigned
long
startBtnPress =
millis
();
while
(
anyButtonPressed
()) {
unsigned
long
blinkDelay;
if
(
millis
() - startBtnPress >
9000
) {
blinkDelay =
25
;
}
else
if
(
millis
() - startBtnPress >
6000
) {
blinkDelay =
50
;
}
else
if
(
millis
() - startBtnPress >
3000
) {
blinkDelay =
100
;
}
else
{
blinkDelay =
200
;
}
working
();
delay
(blinkDelay);
notWorking
();
delay
(blinkDelay);
}
unsigned
long
endBtnPress =
millis
();
unsigned
long
btnDiff = endBtnPress - startBtnPress;
unsigned
int
changeBy;
if
(btnDiff >
9000
) {
changeBy =
20
;
}
else
if
(btnDiff >
6000
) {
changeBy =
10
;
}
else
if
(btnDiff >
3000
) {
changeBy =
5
;
}
else
{
changeBy =
1
;
}
Serial.
print
(
"
Change by
"
);
Serial.
println
(changeBy);
if
(pressed ==
LEFT_BUTTON
) {
if
(changeBy > optionIdx) {
optionIdx =
0
;
}
else
{
optionIdx -= changeBy;
}
}
else
{
optionIdx =
min
(optionIdx + changeBy, maxOptionIdx);
}
}
}
}
void
setup
() {
pinMode
(
LED_BUILTIN
,
OUTPUT
);
working
();
Serial.
begin
(
9600
);
Serial.
println
();
#
if
defined(SET_PAGE_VIA_SERIAL)
Serial.
println
(
"
Setting page via serial is enabled!
"
);
#
endif
#
if
defined(PROFILING)
Serial.
println
(
"
Enabling profiling
"
);
prepareProfiling
();
Serial.
println
(
"
Profiling enabled!
"
);
#
endif
#
if
defined(ENABLE_SCREENSAVERS)
Serial.
println
(
"
Screensavers are enabled!
"
);
#
endif
#
if
defined(SKIP_TO_SCREENSAVERS)
Serial.
println
(
"
Skipping directly to screensaver code after initalization
"
);
#
endif
beginButtons
();
beginBattReader
();
readBatt
();
if
(
beginEPaper
() !=
0
) {
blinkError
(
250
,
250
);
}
while
(
beginSD
() !=
0
) {
Serial.
println
(
"
No SD card detected!
"
);
noSdScreen
();
Serial.
println
(
"
Waiting for button press to try again
"
);
notWorking
();
waitForButtonPress
();
working
();
Serial.
println
(
"
Trying again
"
);
}
if
(
filesInRoot
() ==
0
) {
Serial.
println
(
"
No files in root directory!
"
);
noTextFilesScreen
();
notWorking
();
Serial.
println
(
"
Infinite looping
"
);
while
(
true
) {
;
}
}
notWorking
();
#
if
!defined(SKIP_TO_SCREENSAVERS)
while
(
true
) {
//
char* selected_path = "/text.txt";
char
selected_path[
MAX_PATH_LEN
];
if
(
fileSelector
(selected_path,
MAX_PATH_LEN
) ==
1
) {
Serial.
println
(
"
Exiting loop!
"
);
break
;
}
Serial.
print
(
"
Path selected:
"
);
Serial.
println
(selected_path);
textReader
(selected_path);
}
#
else
Serial.
println
(
"
Skipping text reader code, going to screensaver!
"
);
#
endif
working
();
Paint_Clear
(
WHITE
);
#
if
defined(ENABLE_SCREENSAVERS)
if
(
drawScreensaver
()) {
Serial.
println
(
"
Successfully drawn random screensaver!
"
);
}
else
{
Serial.
println
(
"
Failed to draw random screensaver!
"
);
Paint_Clear
(
WHITE
);
}
if
(
anyButtonPressed
()) {
Serial.
println
(
"
Skipping screensaver!
"
);
Paint_Clear
(
WHITE
);
}
#
endif
updateDisplay
();
EPD_4IN2_Sleep
();
notWorking
();
}
void
loop
() {
}
Back
|
FazBrowse Home
|
New Git URL