Skip to content

Commit

Permalink
Merge pull request #130 from eadmaster/esp32s3headless-webui
Browse files Browse the repository at this point in the history
extension-based commands for the file browser
  • Loading branch information
pr3y authored Aug 6, 2024
2 parents 25ff535 + 3e12f7e commit 40f480f
Show file tree
Hide file tree
Showing 10 changed files with 249 additions and 125 deletions.
7 changes: 0 additions & 7 deletions src/core/globals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,3 @@ void updateTimeStr(struct tm timeInfo) {
snprintf(timeStr, sizeof(timeStr), "%02d:%02d", timeInfo.tm_hour, timeInfo.tm_min);
}

void _tone(unsigned int frequency, unsigned long duration = 0UL) {
#if defined(BUZZ_PIN)
tone(BUZZ_PIN, frequency, duration);
//#elif defined(HAS_NS4168_SPKR)
//TODO: alt. implementation using the speaker
#endif
}
2 changes: 1 addition & 1 deletion src/core/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,4 @@ extern String wui_pwd;
extern int tmz;

void setup_gpio();
void _tone(unsigned int frequency, unsigned long duration);

4 changes: 4 additions & 0 deletions src/core/mykeyboard.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#include "mykeyboard.h"
#include "powerSave.h"
#include "sd_functions.h"
#include "modules/ir/TV-B-Gone.h"
#include "modules/rf/rf.h"
#include "modules/others/bad_usb.h"
#include "modules/others/webInterface.h"


#if defined(HAS_TOUCH)
struct box_t
{
Expand Down Expand Up @@ -176,6 +178,8 @@ void checkShortcutPress(){
if(Keyboard.isKeyPressed('r') || Keyboard.isKeyPressed('s')) otherRFcodes();
if(Keyboard.isKeyPressed('b')) usb_setup(); // badusb
if(Keyboard.isKeyPressed('w')) loopOptionsWebUi();
if(Keyboard.isKeyPressed('f')) { setupSdCard() ? loopSD(SD) : loopSD(LittleFS); }
if(Keyboard.isKeyPressed('l')) loopSD(LittleFS);
// TODO: other boards?
// TODO: user-configurable
}
Expand Down
48 changes: 38 additions & 10 deletions src/core/sd_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
#include "sd_functions.h"
#include "mykeyboard.h" // usinf keyboard when calling rename
#include "display.h" // using displayRedStripe as error msg
#include "../modules/others/audio.h"
#include "modules/others/audio.h"
#include "modules/rf/rf.h"
#include "modules/ir/TV-B-Gone.h"
#include "modules/others/bad_usb.h"

struct FilePage {
int pageIndex;
Expand All @@ -27,6 +30,10 @@ bool setupSdCard() {
sdcardMounted = false;
return false;
}

// avoid unnecessary remounting
if(sdcardMounted) return true;

#if TFT_MOSI == SDCARD_MOSI
if (!SD.begin(SDCARD_CS))
#else
Expand Down Expand Up @@ -410,12 +417,15 @@ String loopSD(FS &fs, bool filePicker, String allowed_ext) {
tft.drawRoundRect(5,5,WIDTH-10,HEIGHT-10,5,FGCOLOR);
closeSdCard();
setupSdCard();
bool exit = false;
returnToMenu=true; // make sure menu is redrawn when quitting in any point

readFs(fs, Folder, fileList, allowed_ext);

for(int i=0; i<MAXFILES; i++) if(fileList[i][2]!="") maxFiles++; else break;
while(1){
if(returnToMenu) break; // stop this loop and retur to the previous loop
//if(returnToMenu) break; // stop this loop and retur to the previous loop
if(exit) break; // stop this loop and retur to the previous loop

if(redraw) {
if(strcmp(PreFolder.c_str(),Folder.c_str()) != 0 || reload){
Expand Down Expand Up @@ -458,7 +468,7 @@ String loopSD(FS &fs, bool filePicker, String allowed_ext) {
{"New Folder", [=]() { createFolder(fs, Folder); }},
{"Rename", [=]() { renameFile(fs, fileList[index][1], fileList[index][0]); }},
{"Delete", [=]() { deleteFromSd(fs, fileList[index][1]); }},
{"Main Menu", [=]() { backToMenu(); }},
{"Main Menu", [&]() { exit = true; }},
};
delay(200);
loopOptions(options);
Expand All @@ -472,7 +482,7 @@ String loopSD(FS &fs, bool filePicker, String allowed_ext) {
{"New Folder", [=]() { createFolder(fs, Folder); }},
};
if(fileToCopy!="") options.push_back({"Paste", [=]() { pasteFile(fs, Folder); }});
options.push_back({"Main Menu", [=]() { backToMenu(); }});
options.push_back({"Main Menu", [&]() { exit = true; }});
delay(200);
loopOptions(options);
tft.drawRoundRect(5,5,WIDTH-10,HEIGHT-10,5,FGCOLOR);
Expand All @@ -486,25 +496,41 @@ String loopSD(FS &fs, bool filePicker, String allowed_ext) {
redraw=true;
} else if (fileList[index][2]=="file") {
options = {
{"New Folder", [=]() { createFolder(fs, Folder); }},
{"View File", [=]() { viewFile(fs, fileList[index][1]); }},
{"Rename", [=]() { renameFile(fs, fileList[index][1], fileList[index][0]); }},
{"Copy", [=]() { copyFile(fs, fileList[index][1]); }},
{"Delete", [=]() { deleteFromSd(fs, fileList[index][1]); }},
{"New Folder", [=]() { createFolder(fs, Folder); }},
};
if(fileToCopy!="") options.push_back({"Paste", [=]() { pasteFile(fs, Folder); }});
options.push_back({"Delete", [=]() { deleteFromSd(fs, fileList[index][1]); }});
if(&fs == &SD) options.push_back({"Copy->LittleFS", [=]() { copyToFs(SD,LittleFS, fileList[index][1]); }});
if(&fs == &LittleFS && sdcardMounted) options.push_back({"Copy->SD", [=]() { copyToFs(LittleFS, SD, fileList[index][1]); }});

// custom file formats commands added in front
if(fileList[index][1].endsWith(".ir")) options.insert(options.begin(), {"IR Tx SpamAll", [&]() {
delay(200);
txIrFile(&fs, fileList[index][1]);
}});
if(fileList[index][1].endsWith(".sub")) options.insert(options.begin(), {"Subghz Tx", [&]() {
delay(200);
txSubFile(&fs, fileList[index][1]);
}});
#if defined(USB_as_HID)
if(fileList[index][1].endsWith(".txt")) options.insert(options.begin(), {"BadUSB Run", [&]() {
Kb.begin();
USB.begin();
key_input(fs, fileList[index][1]);
}});
#endif
#if defined(HAS_NS4168_SPKR)
if(isAudioFile(fileList[index][1])) options.push_back({"Play Audio", [=]() {
playAudioFile(const_cast<fs::FS*>(&fs), fileList[index][1]);
if(isAudioFile(fileList[index][1])) options.insert(options.begin(), {"Play Audio", [&]() {
delay(200);
playAudioFile(&fs, fileList[index][1]);
setup_gpio(); //TODO: remove after fix select loop

}});
#endif

options.push_back({"Main Menu", [=]() { backToMenu(); }});
options.push_back({"Main Menu", [&]() { exit = true; }});
delay(200);
if(!filePicker) loopOptions(options);
else {
Expand Down Expand Up @@ -592,6 +618,8 @@ void viewFile(FS fs, String filepath) {

file = fs.open(filepath, FILE_READ);
if (!file) return;

// TODO: detect binary file, switch to hex view

while (file.available()) {
fileContent = file.readString();
Expand Down
17 changes: 16 additions & 1 deletion src/core/serialcmds.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#include "modules/ir/TV-B-Gone.h"
#include "modules/others/bad_usb.h"

#if defined(HAS_NS4168_SPKR)
#if defined(HAS_NS4168_SPKR) || defined(BUZZ_PIN)
#include "modules/others/audio.h"
#endif

Expand Down Expand Up @@ -291,6 +291,21 @@ bool processSerialCommand(String cmd_str) {
}
#endif

#if defined(HAS_NS4168_SPKR) || defined(BUZZ_PIN)
if(cmd_str.startsWith("tone" ) || cmd_str.startsWith("beep" )) {
const char* args = cmd_str.c_str() + 4;
unsigned long frequency = 500UL;
unsigned long duration = 500UL; // default to 2 sec
if(strlen(args)>1) sscanf(args, " %lu %lu", &frequency, &duration); // try to read the args, keep the defaults if missing
//Serial.print((int) frequency);
//Serial.print((int) duration);
_tone(frequency, duration);
//delay(1000);
//playTone(frequency, duration, 1); // sine
return true;
}
#endif

#if defined(HAS_NS4168_SPKR) //M5StickCs doesn't have speakers.. they have buzzers on pin 02 that only beeps in different frequencies
if(cmd_str.startsWith("music_player " ) ) { // || cmd_str.startsWith("play " )
String song = cmd_str.substring(13, cmd_str.length());
Expand Down
10 changes: 5 additions & 5 deletions src/modules/ir/TV-B-Gone.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,8 @@ void otherIRcodes() {
FS *fs = NULL;
struct Codes selected_code;

returnToMenu = true; // make sure menu is redrawn when quitting in any point

options = {
{"Recent", [&]() { selected_code = selectRecentIrMenu(); }},
{"LittleFS", [&]() { fs=&LittleFS; }},
Expand All @@ -411,7 +413,7 @@ void otherIRcodes() {
delay(200);
loopOptions(options);
delay(200);

if(fs == NULL) { // recent menu was selected
if(selected_code.filepath!="") { // a code was selected, switch on code type
if(selected_code.type=="raw") sendRawCommand(selected_code.frequency, selected_code.data);
Expand Down Expand Up @@ -453,8 +455,8 @@ void otherIRcodes() {

if (!databaseFile) {
Serial.println("Failed to open database file.");
displayError("Fail to open file");
delay(2000);
//displayError("Fail to open file");
//delay(2000);
return;
}
Serial.println("Opened database file.");
Expand Down Expand Up @@ -499,8 +501,6 @@ void otherIRcodes() {
if(checkEscPress() || exit) break;
delay(200);
}

returnToMenu=true;
} // end of otherIRcodes


Expand Down
Loading

0 comments on commit 40f480f

Please sign in to comment.