Skip to content

Commit

Permalink
main: cleanup code
Browse files Browse the repository at this point in the history
  • Loading branch information
and3rson committed Mar 8, 2024
1 parent 54c6381 commit f679ad7
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 44 deletions.
42 changes: 10 additions & 32 deletions firmware/main/src/apps/launcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ LauncherApp::LauncherApp() : App("Menu") {}

void LauncherApp::run() {
lilka::Menu menu("Головне меню");
menu.addItem("Демо", &demos, lilka::display.color565(255, 200, 200));
menu.addItem("Додатки", &demos, lilka::display.color565(255, 200, 200));
menu.addItem("Браузер SD-карти", &sdcard, lilka::display.color565(255, 255, 200));
menu.addItem("Браузер SPIFFS", &memory, lilka::display.color565(200, 255, 200));
menu.addItem("Розробка", &dev, lilka::display.color565(255, 224, 128));
Expand All @@ -42,7 +42,7 @@ void LauncherApp::run() {
int16_t index = menu.getSelectedIndex();
if (index != -1) {
if (index == 0) {
demosMenu();
appsMenu();
} else if (index == 1) {
sdBrowserMenu("/");
} else if (index == 2) {
Expand All @@ -60,26 +60,20 @@ void LauncherApp::run() {
}
}

void LauncherApp::demosMenu() {
// void (*demo_funcs[])(lilka::Canvas *) = {
// demo_lines, demo_disc, demo_ball, demo_epilepsy, demo_letris, demo_user_spi, demo_scan_i2c, 0,
// };

String demos[] = {
void LauncherApp::appsMenu() {
String titles[] = {
// "Лінії", "Шайба", "М'ячик", "Епілепсія", "Летріс", "Тест SPI", "I2C-сканер", "<< Назад",
"Лінії", "Шайба", "М'ячик", "Епілепсія", "Летріс", "Тест SPI", "I2C-сканер", "<< Назад",
};
// vector of functions
std::vector<std::function<App *()>> demo_funcs = {
[]() { return new DemoLines(); }, []() { return new DiskApp(); }, []() { return new BallApp(); },
[]() { return new EpilepsyApp(); }, []() { return new LetrisApp(); }, []() { return new UserSPIApp(); },
[]() { return new ScanI2CApp(); },
APP_CLASS_LIST classes = {
APP_CLASS(DemoLines), APP_CLASS(DiskApp), APP_CLASS(BallApp), APP_CLASS(EpilepsyApp),
APP_CLASS(LetrisApp), APP_CLASS(UserSPIApp), APP_CLASS(ScanI2CApp),
};
int count = sizeof(demos) / sizeof(demos[0]);
int count = sizeof(titles) / sizeof(titles[0]);
lilka::Menu menu("Демо");
for (int i = 0; i < count; i++) {
// menu.addItem(demos[i], &demos[i], lilka::display.color565(255, 200, 200));
menu.addItem(demos[i]);
menu.addItem(titles[i]);
}
while (1) {
menu.update();
Expand All @@ -90,11 +84,8 @@ void LauncherApp::demosMenu() {
if (index == count - 1) {
break;
}
AppManager::getInstance()->addApp(demo_funcs[index]());
AppManager::getInstance()->addApp(classes[index]());
}
// if (index != -1) {
// demo_funcs[index](canvas);
// }
taskYIELD();
}
}
Expand Down Expand Up @@ -169,10 +160,6 @@ void LauncherApp::sdBrowserMenu(String path) {
if (index == numEntries - 1) {
return;
}
// cursor = lilka::ui_menu(canvas, String("SD: ") + path, filenames, numEntries, cursor, icons, colors);
// if (cursor == numEntries - 1) {
// return;
// }
if (entries[index].type == lilka::EntryType::ENT_DIRECTORY) {
sdBrowserMenu(path + entries[index].name + "/");
} else {
Expand Down Expand Up @@ -272,16 +259,8 @@ void LauncherApp::selectFile(String path) {
}
} else if (path.endsWith(".lua")) {
AppManager::getInstance()->addApp(new LuaFileRunnerApp(path));
// int retCode = lilka::lua_runfile(canvas, path);
// if (retCode) {
// lilka::ui_alert(canvas, "Lua", String("Увага!\nКод завершення: ") + retCode);
// }
} else if (path.endsWith(".js")) {
alert("Помилка", "mJS тимчасово\nне підтримується");
// int retCode = lilka::mjs_run(canvas, path);
// if (retCode) {
// lilka::ui_alert(canvas, "Lua", String("Увага!\nКод завершення: ") + retCode);
// }
} else {
// Get file size
FILE *file = fopen(path.c_str(), "r");
Expand All @@ -292,7 +271,6 @@ void LauncherApp::selectFile(String path) {
fseek(file, 0, SEEK_END);
long size = ftell(file);
fclose(file);
// lilka::ui_alert(canvas, path, String("Розмір:\n") + size + " байт");
alert(path, String("Розмір:\n") + size + " байт");
}
}
Expand Down
5 changes: 4 additions & 1 deletion firmware/main/src/apps/launcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

#include "app.h"

#define APP_CLASS_LIST std::vector<std::function<App *()>>
#define APP_CLASS(className) []() { return new className(); }

class LauncherApp : public App {
public:
LauncherApp();

private:
void run();
void demosMenu();
void appsMenu();
void sdBrowserMenu(String path);
void spiffsBrowserMenu();
void devMenu();
Expand Down
10 changes: 0 additions & 10 deletions firmware/main/src/servicemanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,3 @@ ServiceManager *ServiceManager::getInstance() {
void ServiceManager::addService(Service *service) {
services.push_back(service);
}

template <typename T>
T *ServiceManager::getService() {
for (Service *service : services) {
if (dynamic_cast<T *>(service) != NULL) {
return dynamic_cast<T *>(service);
}
}
return NULL;
}
10 changes: 9 additions & 1 deletion firmware/main/src/servicemanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,15 @@ class ServiceManager {
void addService(Service *service);

template <typename T>
T *getService();
T *getService() {
for (Service *service : services) {
T *t = static_cast<T *>(service);
if (t != nullptr) {
return t;
}
}
return nullptr;
}

static ServiceManager *getInstance();

Expand Down

0 comments on commit f679ad7

Please sign in to comment.