2025-12-05 20:48:00 +01:00
import lvgl as lv
2026-01-12 10:58:05 +01:00
2026-03-12 16:00:34 +01:00
from mpos import Activity , Intent , AppearanceManager , AppManager , SettingActivity , SettingsActivity , TimeZone
2025-06-17 09:23:01 +02:00
2026-01-25 23:37:39 +01:00
from bootloader import ResetIntoBootloader
2025-12-05 20:48:00 +01:00
from calibrate_imu import CalibrateIMUActivity
2026-01-09 20:10:50 +01:00
from check_imu_calibration import CheckIMUCalibrationActivity
2025-12-05 20:48:00 +01:00
2026-03-12 16:00:34 +01:00
class LaunchWiFi ( Activity ) :
def onCreate ( self ) :
2026-03-18 12:53:59 +01:00
AppManager . start_app ( " com.micropythonos.settings.wifi " )
2026-03-12 16:00:34 +01:00
2026-03-17 15:17:38 +01:00
class LaunchHotspot ( Activity ) :
def onCreate ( self ) :
2026-03-18 12:53:59 +01:00
AppManager . start_app ( " com.micropythonos.settings.hotspot " )
2026-03-17 15:17:38 +01:00
2026-03-17 19:30:34 +01:00
class LaunchWebServer ( Activity ) :
def onCreate ( self ) :
2026-03-18 12:53:59 +01:00
AppManager . start_app ( " com.micropythonos.settings.webserver " )
2026-03-17 19:30:34 +01:00
2026-01-12 10:54:13 +01:00
class Settings ( SettingsActivity ) :
2026-01-12 10:58:05 +01:00
2026-01-12 11:09:07 +01:00
""" Override getIntent to provide prefs and settings via Intent extras """
def getIntent ( self ) :
2025-06-24 13:14:11 +02:00
theme_colors = [
2026-01-28 17:26:18 +01:00
( " Amethyst " , " 9966cc " ) ,
2025-06-24 13:14:11 +02:00
( " Aqua Blue " , " 00ffff " ) ,
( " Bitcoin Orange " , " f0a010 " ) ,
( " Burnt Orange " , " cc5500 " ) ,
( " Charcoal Gray " , " 36454f " ) ,
2026-01-28 17:26:18 +01:00
( " Coral Red " , " ff7f50 " ) ,
2025-06-24 13:14:11 +02:00
( " Crimson " , " dc143c " ) ,
2026-01-28 17:26:18 +01:00
( " Dark Slate " , " 2f4f4f " ) ,
2025-06-24 13:14:11 +02:00
( " Emerald " , " 50c878 " ) ,
2026-01-28 17:26:18 +01:00
( " Forest Green " , " 228b22 " ) ,
2025-06-24 13:14:11 +02:00
( " Goldenrod " , " daa520 " ) ,
( " Indigo " , " 4b0082 " ) ,
( " Lime " , " 00ff00 " ) ,
2026-01-28 17:26:18 +01:00
( " Matrix Green " , " 03a062 " ) ,
( " Midnight Blue " , " 191970 " ) ,
( " Nostr Purple " , " ff00ff " ) ,
( " Piggy Pink " , " ff69b4 " ) ,
( " Saddle Brown " , " 8b4513 " ) ,
( " Sky Blue " , " 87ceeb " ) ,
( " Solarized Yellow " , " b58900 " ) ,
2025-06-24 13:14:11 +02:00
( " Teal " , " 008080 " ) ,
2026-01-28 17:26:18 +01:00
( " Turquoise " , " 40e0d0 " ) ,
( " Vivid Violet " , " 9f00ff " )
2025-06-24 13:14:11 +02:00
]
2026-01-12 11:15:48 +01:00
intent = Intent ( )
2026-01-23 22:36:54 +01:00
from mpos import SharedPreferences
intent . putExtra ( " prefs " , SharedPreferences ( " com.micropythonos.settings " ) )
2026-01-12 11:15:48 +01:00
intent . putExtra ( " settings " , [
2026-03-18 11:40:41 +01:00
{
" title " : " Wi-Fi " ,
" key " : " wifi_settings " ,
" ui " : " activity " ,
" activity_class " : LaunchWiFi ,
" placeholder " : " Scan and connect to Wi-Fi " ,
} ,
{
" title " : " Hotspot " ,
" key " : " hotspot_settings " ,
" ui " : " activity " ,
" activity_class " : LaunchHotspot ,
" placeholder " : " Standalone Wi-Fi access point " ,
} ,
{
" title " : " WebServer " ,
" key " : " webserver_settings " ,
" ui " : " activity " ,
" activity_class " : LaunchWebServer ,
" placeholder " : " WebREPL, password, port etc " ,
} ,
2025-12-07 16:42:41 +01:00
# Basic settings, alphabetically:
2026-01-12 09:40:53 +01:00
{ " title " : " Light/Dark Theme " , " key " : " theme_light_dark " , " ui " : " radiobuttons " , " ui_options " : [ ( " Light " , " light " ) , ( " Dark " , " dark " ) ] , " changed_callback " : self . theme_changed } ,
2026-01-28 17:26:18 +01:00
{ " title " : " Theme Color " , " key " : " theme_primary_color " , " placeholder " : " HTML hex color, like: EC048C " , " ui " : " dropdown " , " ui_options " : theme_colors , " changed_callback " : self . theme_changed , " default_value " : AppearanceManager . DEFAULT_PRIMARY_COLOR } ,
2026-01-25 15:42:38 +01:00
{ " title " : " Timezone " , " key " : " timezone " , " ui " : " dropdown " , " ui_options " : [ ( tz , tz ) for tz in TimeZone . get_timezones ( ) ] , " changed_callback " : lambda * args : TimeZone . refresh_timezone_preference ( ) } ,
2025-11-12 11:56:16 +01:00
# Advanced settings, alphabetically:
2026-01-25 00:19:38 +01:00
{ " title " : " Auto Start App " , " key " : " auto_start_app " , " ui " : " radiobuttons " , " ui_options " : [ ( app . name , app . fullname ) for app in AppManager . get_app_list ( ) ] } ,
2026-01-12 09:40:53 +01:00
{ " title " : " Check IMU Calibration " , " key " : " check_imu_calibration " , " ui " : " activity " , " activity_class " : CheckIMUCalibrationActivity } ,
{ " title " : " Calibrate IMU " , " key " : " calibrate_imu " , " ui " : " activity " , " activity_class " : CalibrateIMUActivity } ,
2025-12-07 16:42:41 +01:00
# Expert settings, alphabetically
2026-01-12 09:40:53 +01:00
{ " title " : " Restart to Bootloader " , " key " : " boot_mode " , " dont_persist " : True , " ui " : " radiobuttons " , " ui_options " : [ ( " Normal " , " normal " ) , ( " Bootloader " , " bootloader " ) ] , " changed_callback " : self . reset_into_bootloader } ,
{ " title " : " Format internal data partition " , " key " : " format_internal_data_partition " , " dont_persist " : True , " ui " : " radiobuttons " , " ui_options " : [ ( " No, do not format " , " no " ) , ( " Yes, erase all settings, files and non-builtin apps " , " yes " ) ] , " changed_callback " : self . format_internal_data_partition } ,
2025-06-17 11:23:45 +02:00
# This is currently only in the drawer but would make sense to have it here for completeness:
2026-01-12 09:40:53 +01:00
#{"title": "Display Brightness", "key": "display_brightness", "placeholder": "A value from 0 to 100."},
2025-06-17 10:45:29 +02:00
# Maybe also add font size (but ideally then all fonts should scale up/down)
2026-01-12 11:15:48 +01:00
] )
return intent
2025-06-17 09:23:01 +02:00
2026-01-12 09:40:53 +01:00
# Change handlers:
2026-01-09 19:41:29 +01:00
def reset_into_bootloader ( self , new_value ) :
if new_value is not " bootloader " :
return
intent = Intent ( activity_class = ResetIntoBootloader )
self . startActivity ( intent )
2025-10-07 22:42:30 +02:00
2026-01-09 19:41:29 +01:00
def format_internal_data_partition ( self , new_value ) :
if new_value is not " yes " :
2026-03-20 23:59:38 +01:00
return # user picked "no" - abort
2026-01-09 19:41:29 +01:00
# Inspired by lvgl_micropython/lib/micropython/ports/esp32/modules/inisetup.py
# Note: it would be nice to create a "FormatInternalDataPartition" activity with some progress or confirmation
try :
import vfs
from flashbdev import bdev
except Exception as e :
print ( f " Could not format internal data partition because: { e } " )
return
if bdev . info ( ) [ 4 ] == " vfs " :
print ( f " Formatting { bdev } as LittleFS2 " )
vfs . VfsLfs2 . mkfs ( bdev )
fs = vfs . VfsLfs2 ( bdev )
elif bdev . info ( ) [ 4 ] == " ffat " :
print ( f " Formatting { bdev } as FAT " )
vfs . VfsFat . mkfs ( bdev )
fs = vfs . VfsFat ( bdev )
print ( f " Mounting { fs } at / " )
vfs . mount ( fs , " / " )
print ( " Done formatting, (re)mounting /builtin " )
try :
import freezefs_mount_builtin
except Exception as e :
# This will throw an exception if there is already a "/builtin" folder present
print ( " settings.py: WARNING: could not import/run freezefs_mount_builtin: " , e )
print ( " Done mounting, refreshing apps " )
2026-01-25 00:19:38 +01:00
AppManager . refresh_apps ( )
2026-01-09 19:41:29 +01:00
def theme_changed ( self , new_value ) :
2026-01-23 22:36:54 +01:00
from mpos import AppearanceManager
AppearanceManager . init ( self . prefs )