Skip to content

Commit 3ea8617

Browse files
committed
added screensaver feature and cmd (pr3y#64), fixed mp3 playback
1 parent 6c977d6 commit 3ea8617

6 files changed

+81
-10
lines changed

src/display.cpp

+38
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ void loopOptions(const std::vector<std::pair<std::string, std::function<void()>>
7676
int index = 0;
7777
while(1){
7878
if (redraw) {
79+
reset_screensaver_timer();
7980
if(submenu) drawSubmenu(index, options, subText);
8081
else drawOptions(index, options, FGCOLOR, BGCOLOR);
8182
if(bright){
@@ -489,3 +490,40 @@ void drawOther(int x, int y) {
489490
tft.drawArc(40+x,40+y,32,29,240,360,FGCOLOR,BGCOLOR);
490491
}
491492

493+
494+
esp_timer_handle_t screensaver_timer;
495+
496+
static void screensaver_timer_callback(void* arg) {
497+
// turn off TFT backlight
498+
analogWrite(BACKLIGHT, 0);
499+
// TODO: add an option to show a big clock instead
500+
}
501+
502+
void init_screensaver_timer() {
503+
// setup screensaver timer
504+
const esp_timer_create_args_t screensaver_timer_args = {
505+
.callback = &screensaver_timer_callback,
506+
/* argument specified here will be passed to timer callback function */
507+
.arg = (void*) screensaver_timer,
508+
/* name is optional, but may help identify the timer when debugging */
509+
.name = "screensaver"
510+
};
511+
esp_timer_create(&screensaver_timer_args, &screensaver_timer);
512+
esp_timer_start_once(screensaver_timer, 1000000 * SCREENSAVER_TIMEOUT_IN_SECONDS); // timer timeout, in microseconds relative to the current moment
513+
514+
}
515+
516+
void reset_screensaver_timer() {
517+
// if a key was pressed in prev loop
518+
if(esp_timer_is_active(screensaver_timer)) {
519+
// screen is already on, reset the screensaver timer
520+
//MISSING IN OLD SDK: esp_timer_restart(screensaver_timer, 1000000 * SCREENSAVER_TIMEOUT_IN_SECONDS) // Restart a currently running timer.
521+
esp_timer_stop(screensaver_timer);
522+
} else {
523+
// screen is off, reinit brightness
524+
getBrightness();
525+
}
526+
//else
527+
esp_timer_start_once(screensaver_timer, 1000000 * SCREENSAVER_TIMEOUT_IN_SECONDS); // restart a stopped/not running timer
528+
529+
}

src/display.h

+8
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@
66

77
void initDisplay(int i = 0); // Início da função e mostra bootscreen
88

9+
10+
#include "esp_timer.h"
11+
const int SCREENSAVER_TIMEOUT_IN_SECONDS = 60;
12+
extern esp_timer_handle_t screensaver_timer;
13+
void init_screensaver_timer();
14+
void reset_screensaver_timer();
15+
16+
917
//Funções para economizar linhas nas outras funções
1018
void resetTftDisplay(int x = 0, int y = 0, uint16_t fc = FGCOLOR, int size = FM, uint16_t bg = BGCOLOR, uint16_t screen = BGCOLOR);
1119
void setTftDisplay(int x = 0, int y = 0, uint16_t fc = tft.textcolor, int size = tft.textsize, uint16_t bg = tft.textbgcolor);

src/evil_portal.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ void startEvilPortal(String tssid, uint8_t channel, bool deauth) {
119119
redraw=true;
120120
while(1) {
121121
if(redraw) {
122+
reset_screensaver_timer();
122123
drawMainBorder();
123124

124125
tft.setTextSize(FM);

src/main.cpp

+5-3
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,8 @@ void setup() {
119119
#if defined(BACKLIGHT)
120120
pinMode(BACKLIGHT, OUTPUT);
121121
#endif
122-
122+
123+
init_screensaver_timer();
123124
getBrightness();
124125
gsetIrTxPin();
125126
gsetIrRxPin();
@@ -184,13 +185,14 @@ void loop() {
184185
tft.fillScreen(BGCOLOR); //fix any problem with the mainMenu screen when coming back from submenus or functions
185186
redraw=true;
186187
}
187-
188+
188189
if (redraw) {
190+
reset_screensaver_timer();
189191
drawMainMenu(index);
190192
redraw = false;
191193
delay(200);
192194
}
193-
195+
194196
if(checkPrevPress()) {
195197
if(index==0) index = opt - 1;
196198
else if(index>0) index--;

src/sd_functions.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,7 @@ String loopSD(FS &fs, bool filePicker) {
392392
if(returnToMenu) break; // stop this loop and retur to the previous loop
393393

394394
if(redraw) {
395+
reset_screensaver_timer();
395396
if(strcmp(PreFolder.c_str(),Folder.c_str()) != 0 || reload){
396397
index=0;
397398
readFs(fs, Folder, fileList);

src/serialcmds.cpp

+28-7
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#include <ESP8266Audio.h>
1111
#include <ESP8266SAM.h>
1212
#include "sd_functions.h"
13+
#include "settings.h"
14+
#include "display.h"
1315

1416

1517
void SerialPrintHexString(uint64_t val) {
@@ -219,7 +221,7 @@ void handleSerialCommands() {
219221
source = new AudioFileSourcePROGMEM( song.c_str(), song.length() );
220222
} else if(song.indexOf(".") != -1) {
221223
// try to open "song" as a file
222-
// e.g. music_player music/Axel-F.txt
224+
// e.g. music_player audio/Axel-F.txt
223225
if(!song.startsWith("/")) song = "/" + song; // add "/" if missing
224226
// try opening on SD
225227
//if(setupSdCard()) source = new AudioFileSourceFS(SD, song.c_str());
@@ -236,21 +238,25 @@ void handleSerialCommands() {
236238
// switch on extension
237239
song.toLowerCase(); // case-insensitive match
238240
if(song.endsWith(".txt") || song.endsWith(".rtttl")) generator = new AudioGeneratorRTTTL();
239-
/*
241+
/* 2FIX: compilation issues
240242
if(song.endsWith(".mid")) {
241243
// need to load a soundfont
242244
AudioFileSource* sf2 = NULL;
243-
if(setupSdCard()) sf2 = new AudioFileSourceFS(SD, "1mgm.sf2"); // TODO: make configurable
244-
if(!sf2) sf2 = new AudioFileSourceFS(LittleFS, "1mgm.sf2"); // TODO: make configurable
245+
if(setupSdCard()) sf2 = new AudioFileSourceSD("audio/1mgm.sf2"); // TODO: make configurable
246+
if(!sf2) sf2 = new AudioFileSourceLittleFS("audio/1mgm.sf2"); // TODO: make configurable
245247
if(sf2) {
246248
// a soundfount was found
247-
generator = new AudioGeneratorMIDI();
249+
AudioGeneratorMIDI* midi = new AudioGeneratorMIDI();
248250
generator->SetSoundfont(sf2);
251+
generator = midi;
249252
}
250253
}*/
251254
if(song.endsWith(".wav")) generator = new AudioGeneratorWAV();
252255
if(song.endsWith(".mod")) generator = new AudioGeneratorMOD();
253-
if(song.endsWith(".mp3")) generator = new AudioGeneratorMP3();
256+
if(song.endsWith(".mp3")) {
257+
generator = new AudioGeneratorMP3();
258+
source = new AudioFileSourceID3(source);
259+
}
254260
if(song.endsWith(".opus")) generator = new AudioGeneratorOpus();
255261
// TODO: more formats
256262
}
@@ -266,7 +272,10 @@ void handleSerialCommands() {
266272
}*/
267273

268274
//TODO: tone
269-
// https://github.com/earlephilhower/ESP8266Audio/blob/master/examples/PlayWAVFromFunction/PlayWAVFromFunction.ino
275+
// https://github.com/earlephilhower/ESP8266Audio/issues/643
276+
277+
//TODO: webradio
278+
// https://github.com/earlephilhower/ESP8266Audio/tree/master/examples/WebRadio
270279

271280
if(cmd_str.startsWith("tts " ) || cmd_str.startsWith("say " )) {
272281
// https://github.com/earlephilhower/ESP8266SAM/blob/master/examples/Speak/Speak.ino
@@ -292,6 +301,18 @@ void handleSerialCommands() {
292301
// https://github.com/earlephilhower/ESP8266Audio/issues/70
293302
// https://github.com/earlephilhower/ESP8266Audio/pull/118
294303

304+
if(cmd_str.startsWith("lcd " ) || cmd_str.startsWith("tft" ) ) {
305+
String new_status = cmd_str.substring(strlen("lcd "), cmd_str.length());
306+
if(new_status=="off") {
307+
analogWrite(BACKLIGHT, 0);
308+
esp_timer_stop(screensaver_timer);
309+
} else if(new_status=="on") {
310+
getBrightness(); // reinit brightness
311+
reset_screensaver_timer();
312+
}
313+
return;
314+
}
315+
295316
Serial.println("unsupported serial command: " + cmd_str);
296317

297318

0 commit comments

Comments
 (0)