From f01a25df0a409ad733d98f0abcac53a0d08a5825 Mon Sep 17 00:00:00 2001
From: moon2 <moon2protonmail@protonmail.com>
Date: Tue, 19 Sep 2023 16:59:54 +0200
Subject: [PATCH] settings: added restore defaults option

---
 docs/badge/usage.rst                 |  1 +
 python_payload/st3m/settings.py      | 18 +++++++++++++++---
 python_payload/st3m/settings_menu.py |  3 ++-
 3 files changed, 18 insertions(+), 4 deletions(-)

diff --git a/docs/badge/usage.rst b/docs/badge/usage.rst
index 30d3e64f3d..90a0d84cc7 100644
--- a/docs/badge/usage.rst
+++ b/docs/badge/usage.rst
@@ -203,6 +203,7 @@ Menu for setting various system parameters.
 - ``Show FPS``: Displays FPS overlay.
 - ``Debug: ftop``: Prints a task cpu load and memory report every 5 seconds on the USB serial port.
 - ``Touch Overlay``: If a petal is pressed the positional output is displayed in an overlay.
+- ``Restore Defaults``: Restores default settings.
 
 A settings file with more options including headphone and speaker max/min volume and volume
 adjust step is on the flash filesystem at ``/settings.json``.
diff --git a/python_payload/st3m/settings.py b/python_payload/st3m/settings.py
index 26c7d8a545..ea7c058ddb 100644
--- a/python_payload/st3m/settings.py
+++ b/python_payload/st3m/settings.py
@@ -95,7 +95,8 @@ class UnaryTunable(Tunable):
         super().__init__()
         self.key = key
         self._name = name
-        self.value: Any = default
+        self._default = default
+        self.value: Any = self._default
 
     def name(self) -> str:
         return self._name
@@ -132,6 +133,8 @@ class UnaryTunable(Tunable):
         d = reduce(_get, path[:-1], d)
         if k in d:
             self.value = d[k]
+        else:
+            self.set_value(self._default)
 
 
 class OnOffTunable(UnaryTunable):
@@ -211,10 +214,10 @@ num_startup_volume_db = StringTunable(
     "Startup Volume dB", "system.audio.startup_volume_dB", -10
 )
 num_headphones_min_db = StringTunable(
-    "Min Headphone Volume dB", "system.audio.headphones_min_dB", -30
+    "Min Headphone Volume dB", "system.audio.headphones_min_dB", -45
 )
 num_speakers_min_db = StringTunable(
-    "Min Speakers Volume dB", "system.audio.speakers_min_dB", -30
+    "Min Speakers Volume dB", "system.audio.speakers_min_dB", -40
 )
 num_headphones_max_db = StringTunable(
     "Max Headphone Volume dB", "system.audio.headphones_max_dB", 3
@@ -262,6 +265,15 @@ def load_all() -> None:
         setting.load(data)
 
 
+def restore_defaults() -> None:
+    """
+    Restore default settings. Relies on save on exit.
+    """
+    data = {}
+    for setting in load_save_settings:
+        setting.load(data)
+
+
 def _update(d: Dict[str, Any], u: Dict[str, Any]) -> Dict[str, Any]:
     """
     Recursive update dictionary.
diff --git a/python_payload/st3m/settings_menu.py b/python_payload/st3m/settings_menu.py
index a16e8ed6c4..c775baacf4 100644
--- a/python_payload/st3m/settings_menu.py
+++ b/python_payload/st3m/settings_menu.py
@@ -11,7 +11,7 @@ from st3m.goose import (
     Union,
     TYPE_CHECKING,
 )
-from st3m.ui.menu import MenuItem, MenuItemBack
+from st3m.ui.menu import MenuItem, MenuItemBack, MenuItemAction
 from st3m.application import BundleMetadata, MenuItemAppLaunch
 from st3m.ui.elements.menus import SimpleMenu
 from st3m.ui.view import ViewManager
@@ -222,6 +222,7 @@ settings_menu_structure: "MenuStructure" = [
     onoff_debug_ftop,
     onoff_debug_touch,
     # onoff_debug,
+    MenuItemAction("Restore Defaults", restore_defaults),
 ]
 
 
-- 
GitLab