Public Access
M0 step 7: Settings, About, Storage, first-boot setup
- lib/apps_model (host-tested): SettingsMenu (rows, readable values, choices, validation messages) and SetupWizard (names, Region confirmation, timezone; saves only when finished) - AppManager: modal Apps that Home/Back can't leave (setup wizard) - SettingsApp: all settings plus Storage (usage, erase SD behind a dialog) and About (version, node id, battery, memory, uptime) pages, replacing the temporary Diagnostics App - SetupApp: first-boot wizard, opened modally until SetupDone - Node id and default names derived from the MAC like Meshtastic - Widget demo is now hidden (About, then w) - lib_ldf_mode = deep+ so libraries see each other's headers Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EhqxQ49eCju4CzKYNjZzwT
This commit is contained in:
@@ -0,0 +1,143 @@
|
||||
#include "settings_menu.h"
|
||||
|
||||
#include "choices.h"
|
||||
|
||||
namespace roro {
|
||||
|
||||
namespace {
|
||||
|
||||
using Row = SettingsMenu::Row;
|
||||
using Kind = SettingsMenu::Kind;
|
||||
|
||||
struct RowDef {
|
||||
Row row;
|
||||
Kind kind;
|
||||
const char* label;
|
||||
};
|
||||
|
||||
const RowDef kRows[] = {
|
||||
{Row::LongName, Kind::Text, "Long name"}, {Row::ShortName, Kind::Text, "Short name"},
|
||||
{Row::Region, Kind::Choice, "Region"}, {Row::Timezone, Kind::Choice, "Timezone"},
|
||||
{Row::Brightness, Kind::Slider, "Brightness"}, {Row::DimTimeout, Kind::Choice, "Dim after"},
|
||||
{Row::OffTimeout, Kind::Choice, "Screen off after"}, {Row::Sound, Kind::Toggle, "Sound & LED"},
|
||||
{Row::ProbeMacs, Kind::Toggle, "Probe MACs"}, {Row::Storage, Kind::Page, "Storage"},
|
||||
{Row::About, Kind::Page, "About"},
|
||||
};
|
||||
|
||||
const int kDimSeconds[] = {10, 15, 30, 60, 120, 300};
|
||||
const int kOffSeconds[] = {30, 60, 120, 300, 600, 1800};
|
||||
|
||||
std::string formatSeconds(int s) { return s < 60 ? std::to_string(s) + " s" : std::to_string(s / 60) + " min"; }
|
||||
|
||||
template <size_t N>
|
||||
std::vector<std::string> labels(const Choice (&table)[N]) {
|
||||
std::vector<std::string> out;
|
||||
for (auto& c : table) out.push_back(c.label);
|
||||
return out;
|
||||
}
|
||||
|
||||
template <size_t N>
|
||||
std::vector<std::string> durations(const int (&seconds)[N]) {
|
||||
std::vector<std::string> out;
|
||||
for (int s : seconds) out.push_back(formatSeconds(s));
|
||||
return out;
|
||||
}
|
||||
|
||||
template <size_t N>
|
||||
int indexOf(const Choice (&table)[N], const std::string& value) {
|
||||
for (size_t i = 0; i < N; i++)
|
||||
if (value == table[i].value) return static_cast<int>(i);
|
||||
return -1;
|
||||
}
|
||||
|
||||
template <size_t N>
|
||||
int indexOf(const int (&table)[N], int value) {
|
||||
for (size_t i = 0; i < N; i++)
|
||||
if (table[i] == value) return static_cast<int>(i);
|
||||
return -1;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int SettingsMenu::count() const { return sizeof(kRows) / sizeof(kRows[0]); }
|
||||
SettingsMenu::Row SettingsMenu::row(int i) const { return kRows[i].row; }
|
||||
SettingsMenu::Kind SettingsMenu::kind(int i) const { return kRows[i].kind; }
|
||||
std::string SettingsMenu::label(int i) const { return kRows[i].label; }
|
||||
|
||||
std::string SettingsMenu::value(int i) const {
|
||||
switch (row(i)) {
|
||||
case Row::LongName: return settings_.getString(Setting::LongName);
|
||||
case Row::ShortName: return settings_.getString(Setting::ShortName);
|
||||
case Row::Region: return settings_.getString(Setting::Region);
|
||||
case Row::Timezone: {
|
||||
int c = currentChoice(i);
|
||||
return c >= 0 ? kTimezones[c].label : "Custom";
|
||||
}
|
||||
case Row::Brightness: return std::to_string(settings_.getInt(Setting::Brightness)) + "%";
|
||||
case Row::DimTimeout: return formatSeconds(settings_.getInt(Setting::DimTimeoutS));
|
||||
case Row::OffTimeout: return formatSeconds(settings_.getInt(Setting::OffTimeoutS));
|
||||
case Row::Sound: return settings_.getBool(Setting::Sound) ? "On" : "Off";
|
||||
case Row::ProbeMacs: return settings_.getBool(Setting::ProbeMacRaw) ? "Raw" : "Pseudonymised";
|
||||
default: return "";
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<std::string> SettingsMenu::choices(int i) const {
|
||||
switch (row(i)) {
|
||||
case Row::Region: return labels(kRegions);
|
||||
case Row::Timezone: return labels(kTimezones);
|
||||
case Row::DimTimeout: return durations(kDimSeconds);
|
||||
case Row::OffTimeout: return durations(kOffSeconds);
|
||||
default: return {};
|
||||
}
|
||||
}
|
||||
|
||||
int SettingsMenu::currentChoice(int i) const {
|
||||
switch (row(i)) {
|
||||
case Row::Region: return indexOf(kRegions, settings_.getString(Setting::Region));
|
||||
case Row::Timezone: return indexOf(kTimezones, settings_.getString(Setting::Timezone));
|
||||
case Row::DimTimeout: return indexOf(kDimSeconds, settings_.getInt(Setting::DimTimeoutS));
|
||||
case Row::OffTimeout: return indexOf(kOffSeconds, settings_.getInt(Setting::OffTimeoutS));
|
||||
default: return -1;
|
||||
}
|
||||
}
|
||||
|
||||
std::string SettingsMenu::choose(int i, int c) {
|
||||
switch (row(i)) {
|
||||
case Row::Region:
|
||||
settings_.setString(Setting::Region, kRegions[c].value);
|
||||
settings_.setBool(Setting::RegionConfirmed, true);
|
||||
return "";
|
||||
case Row::Timezone: settings_.setString(Setting::Timezone, kTimezones[c].value); return "";
|
||||
case Row::DimTimeout:
|
||||
return settings_.setInt(Setting::DimTimeoutS, kDimSeconds[c]) ? "" : "Dimming must happen before screen off";
|
||||
case Row::OffTimeout:
|
||||
return settings_.setInt(Setting::OffTimeoutS, kOffSeconds[c]) ? "" : "Screen off must come after dimming";
|
||||
default: return "Not a choice";
|
||||
}
|
||||
}
|
||||
|
||||
void SettingsMenu::toggle(int i) {
|
||||
if (row(i) == Row::Sound) settings_.setBool(Setting::Sound, !settings_.getBool(Setting::Sound));
|
||||
if (row(i) == Row::ProbeMacs) settings_.setBool(Setting::ProbeMacRaw, !settings_.getBool(Setting::ProbeMacRaw));
|
||||
}
|
||||
|
||||
void SettingsMenu::adjust(int i, int direction) {
|
||||
if (row(i) != Row::Brightness) return;
|
||||
int next = settings_.getInt(Setting::Brightness) + (direction > 0 ? 10 : -10);
|
||||
if (next < 10) next = 10;
|
||||
if (next > 100) next = 100;
|
||||
settings_.setInt(Setting::Brightness, next);
|
||||
}
|
||||
|
||||
int SettingsMenu::maxBytes(int i) const {
|
||||
return row(i) == Row::ShortName ? 4 : row(i) == Row::LongName ? 39 : 0;
|
||||
}
|
||||
|
||||
std::string SettingsMenu::setText(int i, const std::string& text) {
|
||||
Setting s = row(i) == Row::ShortName ? Setting::ShortName : Setting::LongName;
|
||||
if (settings_.setString(s, text)) return "";
|
||||
return "Must be 1 to " + std::to_string(maxBytes(i)) + " bytes";
|
||||
}
|
||||
|
||||
} // namespace roro
|
||||
@@ -0,0 +1,42 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "settings.h"
|
||||
|
||||
namespace roro {
|
||||
|
||||
// What the Settings App lists: one row per user-facing setting (plus sub-pages), with readable
|
||||
// values, choice lists and validation messages. Rendering and navigation live in the App.
|
||||
class SettingsMenu {
|
||||
public:
|
||||
enum class Row { LongName, ShortName, Region, Timezone, Brightness, DimTimeout, OffTimeout, Sound, ProbeMacs, Storage, About };
|
||||
enum class Kind { Text, Choice, Toggle, Slider, Page };
|
||||
|
||||
explicit SettingsMenu(Settings& settings) : settings_(settings) {}
|
||||
|
||||
int count() const;
|
||||
Row row(int i) const;
|
||||
Kind kind(int i) const;
|
||||
std::string label(int i) const;
|
||||
std::string value(int i) const;
|
||||
|
||||
// Choice rows
|
||||
std::vector<std::string> choices(int i) const;
|
||||
int currentChoice(int i) const; // -1 if the current value isn't one of the choices
|
||||
std::string choose(int i, int choice); // empty on success, otherwise why it was refused
|
||||
|
||||
// Toggle and Slider rows
|
||||
void toggle(int i);
|
||||
void adjust(int i, int direction);
|
||||
|
||||
// Text rows
|
||||
int maxBytes(int i) const;
|
||||
std::string setText(int i, const std::string& text); // empty on success, otherwise why
|
||||
|
||||
private:
|
||||
Settings& settings_;
|
||||
};
|
||||
|
||||
} // namespace roro
|
||||
@@ -0,0 +1,88 @@
|
||||
#include "setup_wizard.h"
|
||||
|
||||
#include "choices.h"
|
||||
|
||||
namespace roro {
|
||||
|
||||
SetupWizard::SetupWizard(Settings& settings, std::string defaultLongName, std::string defaultShortName)
|
||||
: settings_(settings), longName_(std::move(defaultLongName)), shortName_(std::move(defaultShortName)) {}
|
||||
|
||||
int SetupWizard::choiceCount() const {
|
||||
if (step_ == Step::Region) return sizeof(kRegions) / sizeof(kRegions[0]);
|
||||
if (step_ == Step::Timezone) return sizeof(kTimezones) / sizeof(kTimezones[0]);
|
||||
return 0;
|
||||
}
|
||||
|
||||
std::string SetupWizard::choiceLabel(int i) const {
|
||||
return step_ == Step::Region ? kRegions[i].label : kTimezones[i].label;
|
||||
}
|
||||
|
||||
int SetupWizard::selectedChoice() const { return step_ == Step::Region ? region_ : timezone_; }
|
||||
|
||||
void SetupWizard::selectChoice(int i) {
|
||||
if (i < 0 || i >= choiceCount()) return;
|
||||
(step_ == Step::Region ? region_ : timezone_) = i;
|
||||
}
|
||||
|
||||
void SetupWizard::enter(Step step) {
|
||||
step_ = step;
|
||||
error_.clear();
|
||||
if (step == Step::LongName) {
|
||||
editor_ = LineEditor(39);
|
||||
editor_.setText(longName_);
|
||||
} else if (step == Step::ShortName) {
|
||||
editor_ = LineEditor(4);
|
||||
editor_.setText(shortName_);
|
||||
}
|
||||
}
|
||||
|
||||
bool SetupWizard::next() {
|
||||
switch (step_) {
|
||||
case Step::Welcome: enter(Step::LongName); return true;
|
||||
case Step::LongName:
|
||||
if (editor_.text().empty()) {
|
||||
error_ = "Please enter a name";
|
||||
return false;
|
||||
}
|
||||
longName_ = editor_.text();
|
||||
enter(Step::ShortName);
|
||||
return true;
|
||||
case Step::ShortName:
|
||||
if (editor_.text().empty()) {
|
||||
error_ = "Please enter 1 to 4 characters";
|
||||
return false;
|
||||
}
|
||||
shortName_ = editor_.text();
|
||||
enter(Step::Region);
|
||||
return true;
|
||||
case Step::Region: enter(Step::Timezone); return true;
|
||||
case Step::Timezone: enter(Step::Done); return true;
|
||||
case Step::Done:
|
||||
save();
|
||||
finished_ = true;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void SetupWizard::back() {
|
||||
switch (step_) {
|
||||
case Step::LongName: longName_ = editor_.text(); enter(Step::Welcome); break;
|
||||
case Step::ShortName: shortName_ = editor_.text(); enter(Step::LongName); break;
|
||||
case Step::Region: enter(Step::ShortName); break;
|
||||
case Step::Timezone: enter(Step::Region); break;
|
||||
case Step::Done: enter(Step::Timezone); break;
|
||||
case Step::Welcome: break;
|
||||
}
|
||||
}
|
||||
|
||||
void SetupWizard::save() {
|
||||
settings_.setString(Setting::LongName, longName_);
|
||||
settings_.setString(Setting::ShortName, shortName_);
|
||||
settings_.setString(Setting::Region, kRegions[region_].value);
|
||||
settings_.setString(Setting::Timezone, kTimezones[timezone_].value);
|
||||
settings_.setBool(Setting::RegionConfirmed, true);
|
||||
settings_.setBool(Setting::SetupDone, true);
|
||||
}
|
||||
|
||||
} // namespace roro
|
||||
@@ -0,0 +1,46 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "line_editor.h"
|
||||
#include "settings.h"
|
||||
|
||||
namespace roro {
|
||||
|
||||
// The first-boot setup flow: names, Region confirmation, timezone. Nothing is saved until the
|
||||
// last step, so an interrupted setup simply starts again at the next boot.
|
||||
class SetupWizard {
|
||||
public:
|
||||
enum class Step { Welcome, LongName, ShortName, Region, Timezone, Done };
|
||||
|
||||
SetupWizard(Settings& settings, std::string defaultLongName, std::string defaultShortName);
|
||||
|
||||
Step step() const { return step_; }
|
||||
bool finished() const { return finished_; }
|
||||
|
||||
// Name steps edit text; Region and Timezone steps pick a choice.
|
||||
LineEditor& editor() { return editor_; }
|
||||
int choiceCount() const;
|
||||
std::string choiceLabel(int i) const;
|
||||
int selectedChoice() const;
|
||||
void selectChoice(int i);
|
||||
|
||||
bool next(); // validates the step; false (with error()) if it can't advance
|
||||
void back();
|
||||
const std::string& error() const { return error_; }
|
||||
|
||||
private:
|
||||
void enter(Step step);
|
||||
void save();
|
||||
|
||||
Settings& settings_;
|
||||
Step step_ = Step::Welcome;
|
||||
bool finished_ = false;
|
||||
std::string error_;
|
||||
LineEditor editor_{39};
|
||||
std::string longName_, shortName_;
|
||||
int region_ = 0;
|
||||
int timezone_ = 0;
|
||||
};
|
||||
|
||||
} // namespace roro
|
||||
@@ -21,19 +21,41 @@ void AppManager::begin() {
|
||||
redraw_ = true;
|
||||
}
|
||||
|
||||
bool AppManager::open(const char* id) {
|
||||
for (auto& info : apps_) {
|
||||
if (std::strcmp(info.id, id) == 0) {
|
||||
switchTo(*info.app);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
App* AppManager::find(const char* id) const {
|
||||
for (auto& info : apps_)
|
||||
if (std::strcmp(info.id, id) == 0) return info.app;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void AppManager::home() { switchTo(launcher_); }
|
||||
bool AppManager::open(const char* id) {
|
||||
App* app = find(id);
|
||||
if (!app || modal_) return false;
|
||||
switchTo(*app);
|
||||
return true;
|
||||
}
|
||||
|
||||
void AppManager::home() {
|
||||
if (!modal_) switchTo(launcher_);
|
||||
}
|
||||
|
||||
bool AppManager::openModal(const char* id) {
|
||||
App* app = find(id);
|
||||
if (!app) return false;
|
||||
switchTo(*app);
|
||||
modal_ = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
void AppManager::endModal() {
|
||||
modal_ = false;
|
||||
switchTo(launcher_);
|
||||
}
|
||||
|
||||
void AppManager::handleKey(const KeyEvent& event) {
|
||||
if (modal_) {
|
||||
foreground_->onKey(event);
|
||||
return;
|
||||
}
|
||||
if (event.key == Key::Home) {
|
||||
home();
|
||||
return;
|
||||
@@ -42,6 +64,12 @@ void AppManager::handleKey(const KeyEvent& event) {
|
||||
if (!consumed && event.key == Key::Back) home();
|
||||
}
|
||||
|
||||
const char* AppManager::foregroundTitle() const {
|
||||
for (auto& info : apps_)
|
||||
if (info.app == foreground_) return info.title;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
bool AppManager::takeRedraw() {
|
||||
bool r = redraw_ | foreground_->consumeRedraw();
|
||||
redraw_ = false;
|
||||
|
||||
@@ -23,24 +23,32 @@ class AppManager {
|
||||
std::vector<const AppInfo*> visibleApps() const;
|
||||
|
||||
void begin();
|
||||
bool open(const char* id);
|
||||
bool open(const char* id); // refused while a modal App runs
|
||||
void home();
|
||||
|
||||
// A modal App (e.g. the first-boot wizard) can't be left with Home or Back: those keys go to it,
|
||||
// and it ends itself with endModal(), which returns to the Launcher.
|
||||
bool openModal(const char* id);
|
||||
void endModal();
|
||||
|
||||
void handleKey(const KeyEvent& event);
|
||||
void update(uint32_t nowMs) { foreground_->update(nowMs); }
|
||||
|
||||
App& foreground() const { return *foreground_; }
|
||||
const char* foregroundTitle() const; // nullptr for the Launcher
|
||||
|
||||
// True once after the screen needs redrawing (App switch, or the App asked for it).
|
||||
bool takeRedraw();
|
||||
|
||||
private:
|
||||
void switchTo(App& app);
|
||||
App* find(const char* id) const;
|
||||
|
||||
App& launcher_;
|
||||
App* foreground_;
|
||||
std::vector<AppInfo> apps_;
|
||||
bool redraw_ = true;
|
||||
bool modal_ = false;
|
||||
};
|
||||
|
||||
} // namespace roro
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
#pragma once
|
||||
|
||||
namespace roro {
|
||||
|
||||
struct Choice {
|
||||
const char* label;
|
||||
const char* value;
|
||||
};
|
||||
|
||||
// Timezones offered in Settings and the setup wizard (POSIX TZ strings).
|
||||
inline const Choice kTimezones[] = {
|
||||
{"Brussels", "CET-1CEST,M3.5.0,M10.5.0/3"}, // also Paris, Amsterdam, Berlin...
|
||||
{"London", "GMT0BST,M3.5.0/1,M10.5.0"},
|
||||
{"Lisbon", "WET0WEST,M3.5.0/1,M10.5.0"},
|
||||
{"Helsinki", "EET-2EEST,M3.5.0/3,M10.5.0/4"},
|
||||
{"UTC", "UTC0"},
|
||||
};
|
||||
|
||||
// Regions the radio may be used in (see Region in CONTEXT.md).
|
||||
inline const Choice kRegions[] = {
|
||||
{"EU868 (Europe, 869.5 MHz)", "EU868"},
|
||||
};
|
||||
|
||||
} // namespace roro
|
||||
Reference in New Issue
Block a user