Skip to content

Commit

Permalink
Autostart option (#2079)
Browse files Browse the repository at this point in the history
* Autostart app on boot
* Settings page
* ext app support
  • Loading branch information
htotoo authored Apr 3, 2024
1 parent 3665b3c commit c48cbb7
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 0 deletions.
57 changes: 57 additions & 0 deletions firmware/application/apps/ui_settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "ui_receiver.hpp"
#include "ui_touch_calibration.hpp"
#include "ui_text_editor.hpp"
#include "ui_external_items_menu_loader.hpp"

#include "portapack_persistent_memory.hpp"
#include "lpc43xx_cpp.hpp"
Expand Down Expand Up @@ -839,6 +840,61 @@ void SetMenuColorView::focus() {
button_save.focus();
}

/* SetAutostartView*/
SetAutostartView::SetAutostartView(NavigationView& nav) {
add_children({&labels,
&button_save,
&button_cancel,
&options});

button_save.on_select = [&nav, this](Button&) {
nav_setting.save();
nav.pop();
};

button_cancel.on_select = [&nav, this](Button&) {
nav.pop();
};

// options
i = 0;
OptionsField::option_t o{"-none-", i};
opts.emplace_back(o);
for (auto& app : NavigationView::appList) {
if (app.id == nullptr) continue;
i++;
o = {app.displayName, i};
opts.emplace_back(o);
full_app_list.emplace(i, app.id);
if (autostart_app == app.id) selected = i;
}
ExternalItemsMenuLoader::load_all_external_items_callback([this](ui::AppInfoConsole& app) {
if (app.appCallName == nullptr) return;
i++;
OptionsField::option_t o = {app.appFriendlyName, i};
opts.emplace_back(o);
full_app_list.emplace(i, app.appCallName);
if (autostart_app == app.appCallName) selected = i;
});

options.set_options(opts);
options.on_change = [this](size_t, OptionsField::value_t v) {
if (v == 0) {
autostart_app = "";
return;
}
auto it = full_app_list.find(v);
if (it != full_app_list.end()) {
autostart_app = it->second;
}
};
options.set_selected_index(selected);
}

void SetAutostartView::focus() {
options.focus();
}

/* SettingsMenuView **************************************/

SettingsMenuView::SettingsMenuView(NavigationView& nav)
Expand Down Expand Up @@ -866,6 +922,7 @@ void SettingsMenuView::on_populate() {
//{"QR Code", ui::Color::dark_cyan(), &bitmap_icon_qr_code, [this]() { nav_.push<SetQRCodeView>(); }},
{"Brightness", ui::Color::dark_cyan(), &bitmap_icon_brightness, [this]() { nav_.push<SetFakeBrightnessView>(); }},
{"Menu Color", ui::Color::dark_cyan(), &bitmap_icon_brightness, [this]() { nav_.push<SetMenuColorView>(); }},
{"Autostart", ui::Color::dark_cyan(), &bitmap_icon_setup, [this]() { nav_.push<SetAutostartView>(); }},
});
}

Expand Down
35 changes: 35 additions & 0 deletions firmware/application/apps/ui_settings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,41 @@ class SetMenuColorView : public View {
};
};

class SetAutostartView : public View {
public:
SetAutostartView(NavigationView& nav);

void focus() override;

std::string title() const override { return "Autostart"; };

private:
int32_t i = 0;
std::string autostart_app{""};
OptionsField::options_t opts;
std::map<int32_t, std::string> full_app_list; // looking table
int32_t selected = 0;
SettingsStore nav_setting{
"nav"sv,
{{"autostart_app"sv, &autostart_app}}};
Labels labels{
{{1 * 8, 1 * 16}, "Select app to start on boot", Color::light_grey()}};

Button button_save{
{2 * 8, 16 * 16, 12 * 8, 32},
"Save"};

OptionsField options{
{0 * 8, 3 * 16},
30,
{}};

Button button_cancel{
{16 * 8, 16 * 16, 12 * 8, 32},
"Cancel",
};
};

class SettingsMenuView : public BtnGridView {
public:
SettingsMenuView(NavigationView& nav);
Expand Down
1 change: 1 addition & 0 deletions firmware/application/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ static void event_loop() {
event_dispatcher.set_display_sleep(true);
}};
portapack::setEventDispatcherToUSBSerial(&event_dispatcher);
system_view.get_navigation_view()->handle_autostart();
event_dispatcher.run();
}

Expand Down
21 changes: 21 additions & 0 deletions firmware/application/ui_navigation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@
#include "file_reader.hpp"
#include "png_writer.hpp"
#include "file_path.hpp"
#include "ff.h"

#include <locale>
#include <codecvt>

using portapack::receiver_model;
using portapack::transmitter_model;
Expand Down Expand Up @@ -708,6 +712,23 @@ bool NavigationView::set_on_pop(std::function<void()> on_pop) {
return true;
}

void NavigationView::handle_autostart() {
std::string autostart_app{""};
SettingsStore nav_setting{
"nav"sv,
{{"autostart_app"sv, &autostart_app}}};
if (!autostart_app.empty()) {
if (StartAppByName(autostart_app.c_str())) return;
// if returned false, check for external apps by that name, and try to start it
std::string appwithpath = "/" + apps_dir.string() + "/";
appwithpath += autostart_app;
appwithpath += ".ppma";
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> conv;
std::filesystem::path pth = conv.from_bytes(appwithpath.c_str());
ui::ExternalItemsMenuLoader::run_external_app(*this, pth);
}
}

/* Helpers **************************************************************/

static void add_apps(NavigationView& nav, BtnGridView& grid, app_location_t loc) {
Expand Down
2 changes: 2 additions & 0 deletions firmware/application/ui_navigation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ class NavigationView : public View {
static const AppList appList;

bool StartAppByName(const char* name); // Starts a View (app) by name stored in appListFC. This is to start apps from console
void handle_autostart();

private:
struct ViewState {
std::unique_ptr<View> view;
Expand Down

0 comments on commit c48cbb7

Please sign in to comment.