Skip to content

Commit

Permalink
added plugins menu (pr3y#258)
Browse files Browse the repository at this point in the history
  • Loading branch information
eadmaster committed Sep 12, 2024
1 parent 53090da commit 4cd1251
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/core/main_menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ MainMenu::MainMenu() {
&othersMenu,
&clockMenu,
&configMenu,
#if !defined(CORE) && !defined(CORE2)
&scriptsMenu,
#endif
};

_totalItems = _menuItems.size();
Expand Down
2 changes: 2 additions & 0 deletions src/core/main_menu.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "menu_items/RFIDMenu.h"
#include "menu_items/RFMenu.h"
#include "menu_items/WifiMenu.h"
#include "menu_items/ScriptsMenu.h"


class MainMenu {
Expand All @@ -25,6 +26,7 @@ class MainMenu {
RFIDMenu rfidMenu;
RFMenu rfMenu;
WifiMenu wifiMenu;
ScriptsMenu scriptsMenu;

MainMenu();
~MainMenu();
Expand Down
2 changes: 2 additions & 0 deletions src/core/menu_items/OthersMenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,11 @@ void OthersMenu::optionsMenu() {
#ifndef LITE_VERSION
{"Openhaystack", [=]() { openhaystack_setup(); }},
#endif
/*
#if !defined(CORE) && !defined(CORE2)
{"Interpreter", [=]() { run_bjs_script(); }},
#endif
* */
{"Main Menu", [=]() { backToMenu(); }},
};

Expand Down
55 changes: 55 additions & 0 deletions src/core/menu_items/ScriptsMenu.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@

#include "ScriptsMenu.h"
#include "core/display.h"
#include "core/settings.h"
#include "modules/bjs_interpreter/interpreter.h" // for JavaScript interpreter


void ScriptsMenu::optionsMenu() {

String Folder = "/scripts";
FS* fs = NULL;
if(SD.exists(Folder)) fs = &SD;
if(LittleFS.exists(Folder)) fs = &LittleFS;
if(!fs) return; // dir not found

//String fileList[MAXFILES][3];
//readFs(fs, Folder, fileList, "bjs");

options = { };

File root = fs->open(Folder);
if (!root || !root.isDirectory()) return; // not a dir
File file2 = root.openNextFile();

while (file2) {
if (file2.isDirectory()) continue;
String fileName = String(file2.name());
if( ! fileName.endsWith(".js") && ! fileName.endsWith(".bjs")) continue;
// else append to the choices
options.push_back({file2.name(), [=]() { run_bjs_script_headless(*fs, file2.path()); }});
file2 = root.openNextFile();
}
file2.close();
root.close();

options.push_back({"Load...", [=]() { run_bjs_script(); }});
options.push_back({"Main Menu", [=]() { backToMenu(); }});

delay(200);
loopOptions(options,false,true,"Scripts");
}

String ScriptsMenu::getName() {
return _name;
}

void ScriptsMenu::draw() {
// draw the icon
tft.fillRect(iconX,iconY,80,80,BGCOLOR);
int i=0;
for(i=0;i<6;i++) {
tft.drawArc(40+iconX,40+iconY,30,20,15+60*i,45+60*i,FGCOLOR,BGCOLOR,true);
}
tft.drawArc(40+iconX,40+iconY,22,8,0,360,FGCOLOR,BGCOLOR,false);
}
18 changes: 18 additions & 0 deletions src/core/menu_items/ScriptsMenu.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef __SCRIPTS_MENU_H__
#define __SCRIPTS_MENU_H__

#include "MenuItemInterface.h"


class ScriptsMenu : public MenuItemInterface {
public:
void optionsMenu(void);
void draw(void);
String getName(void);

private:
String _name = "Scripts";

};

#endif
7 changes: 5 additions & 2 deletions src/modules/bjs_interpreter/interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -920,6 +920,7 @@ bool interpreter() {
duk_push_c_function(ctx, native_dialogPickFile, 1);
duk_put_global_string(ctx, "dialogPickFile");
// TODO: dialogChoice(choices: array)
// TODO: dialogYesNo()
duk_push_c_function(ctx, native_dialogViewFile, 1);
duk_put_global_string(ctx, "dialogViewFile");
duk_push_c_function(ctx, native_keyboard, 3);
Expand Down Expand Up @@ -991,6 +992,8 @@ bool run_bjs_script_headless(String code) {
}

bool run_bjs_script_headless(FS fs, String filename) {
String code = readScriptFile(fs, filename);
return run_bjs_script_headless(code);
script = readScriptFile(fs, filename);
returnToMenu=true;
interpreter_start=true;
return true;
}

0 comments on commit 4cd1251

Please sign in to comment.