From 0896109fb2a550b981541fb9d5cfcf2463eba0b8 Mon Sep 17 00:00:00 2001 From: Jean-Pierre Martineau Date: Fri, 20 Nov 2026 23:40:29 -0500 Subject: [PATCH] Sound preferences, and which platforms can play a sound Co-Authored-By: Claude Opus 5.5 --- meshterm/core/preferences.py | 130 ++++++++++++++++++++++++++++++++++ meshterm/platforms.py | 15 +++- tests/test_vt_console_font.py | 10 ++- 3 files changed, 152 insertions(+), 3 deletions(-) diff --git a/meshterm/core/preferences.py b/meshterm/core/preferences.py index b8436ac..29f2140 100644 --- a/meshterm/core/preferences.py +++ b/meshterm/core/preferences.py @@ -55,6 +55,7 @@ from .atomicwrite import write_atomically from .courier_store import DONE_CAP from .geo import DEFAULT_VIEW_FRACTION from .models import CLOCK_TOLERANCE_S, NAME_OVERLAP_DAYS +from ..platforms import ALL_PLATFORMS from .watch_store import ALERT_CAP, DEFAULT_SILENCE_HOURS, OFF, SILENCE_CHOICES_H if sys.version_info >= (3, 11): @@ -74,6 +75,7 @@ GROUPS: tuple[str, ...] = ( "Watchtower", "Map", "History", + "Sound", "Display", "Diagnostics", ) @@ -154,6 +156,30 @@ class PrefSpec: #: sees, not the mechanism: an age is how long ago, and a timestamp is when. _CLI_TIME_CHOICES: dict[str, str] = {"relative": "ages", "absolute": "timestamps"} +#: The platforms that can play a sound (:attr:`~meshterm.platforms.Platform.sound`). The +#: Preferences page offers the sound preferences only there. A notification on the other +#: platforms is silent, and only the title bar can flash for it. +_SOUND_PLATFORMS: frozenset[str] = frozenset(p.name for p in ALL_PLATFORMS if p.sound) + +#: The sound themes (refer to :data:`meshterm.services.sound.THEMES`). ``android`` plays the +#: sounds of ``meshterm/assets/sounds/android``. ``bell`` sends the terminal bell (BEL) for +#: each event, which rings on the computer of the user also over SSH. ``none`` plays +#: nothing. +_SOUND_THEME_CHOICES: dict[str, str] = { + "android": "Android", + "bell": "terminal bell", + "none": "no sound", +} + +#: When the title bar flashes for a notification. ``silent`` flashes only when no sound +#: plays for it: sounds are off, the theme is ``none``, the platform has no sound, or the +#: player failed. +_FLASH_CHOICES: dict[str, str] = {"off": "off", "on": "on", "silent": "when silent"} + +#: When a conversation plays its sound. A mention of our node always plays the mention +#: sound, unless the conversation is ``off``. +SOUND_WHEN_CHOICES: dict[str, str] = {"off": "off", "mentions": "mentions", "all": "all messages"} + #: The sources of the place of a node (refer to :mod:`meshterm.services.places`). With #: ``tiles_nominatim``, the map tiles give the first answer at once, and Nominatim corrects it #: later, when MeshTerm is online, because only Nominatim knows the boundaries of a place. @@ -475,6 +501,110 @@ PREFERENCES: tuple[PrefSpec, ...] = ( value_type="str", default="https://nominatim.openstreetmap.org", ), + # --- Sound --------------------------------------------------------------------- + PrefSpec( + key="sounds", + label="Sounds", + help="Play a sound for notifications. ^S turns it on or off", + group="Sound", + value_type="bool", + # Off: a new user hears nothing until they ask for it (the request on Discord, + # 2026-09-26: "Defaulted to off though"). ^S changes this preference from any + # screen, and the header shows its state (🔊 on, 🔇 off). + default=False, + platforms=_SOUND_PLATFORMS, + ), + PrefSpec( + key="sound_theme", + label="Sound theme", + help="The set of sounds that notifications play", + group="Sound", + value_type="enum", + default="android", + choices=_SOUND_THEME_CHOICES, + platforms=_SOUND_PLATFORMS, + ), + PrefSpec( + key="sound_volume_pct", + label="Volume", + help="How loud the sounds play, before the volume of the system", + group="Sound", + value_type="int", + # MeshTerm scales the samples itself (JP, 2026-11-20), because winsound, the only + # player on Windows, has no volume control. Thus the volume is the same with each + # player. 70: the AOSP chimes are loud at full scale. + default=70, + minimum=5, + maximum=100, + unit="%", + platforms=_SOUND_PLATFORMS, + ), + PrefSpec( + key="notify_flash", + label="Flash", + help="Flash the title bar for a notification", + group="Sound", + value_type="enum", + # When silent: the flash replaces a sound that does not play, and it does not add to + # one that does. On the PicoCalc, which has no sound, each notification flashes. + default="silent", + choices=_FLASH_CHOICES, + ), + PrefSpec( + key="dm_sound", + label="DM sound", + help="When a DM plays a sound, if its own Sound is Default", + group="Sound", + value_type="enum", + default="all", + choices=SOUND_WHEN_CHOICES, + platforms=_SOUND_PLATFORMS, + ), + PrefSpec( + key="channel_sound", + label="Channel sound", + help="When a channel plays a sound, if its own Sound is Default", + group="Sound", + value_type="enum", + # Mentions: a public channel can carry many messages each hour. + default="mentions", + choices=SOUND_WHEN_CHOICES, + platforms=_SOUND_PLATFORMS, + ), + PrefSpec( + key="room_sound", + label="Room sound", + help="When a room plays a sound, if its own Sound is Default", + group="Sound", + value_type="enum", + default="mentions", + choices=SOUND_WHEN_CHOICES, + platforms=_SOUND_PLATFORMS, + ), + PrefSpec( + key="packet_tick", + label="Packet tick", + help="A short tick for each packet heard that plays no other sound", + group="Sound", + value_type="bool", + # Off: a busy mesh can send many packets each minute. The service plays at most one + # tick each 250 ms (JP, 2026-11-20). + default=False, + platforms=_SOUND_PLATFORMS, + ), + PrefSpec( + key="battery_low_pct", + label="Battery low", + help="Play the battery sound when the battery goes below this level", + group="Sound", + value_type="int", + # 25: the level where the gauge of the header changes to its low band (batt.low). + default=25, + minimum=5, + maximum=90, + unit="%", + platforms=_SOUND_PLATFORMS, + ), # --- History kept ------------------------------------------------------------ PrefSpec( key="history_days", diff --git a/meshterm/platforms.py b/meshterm/platforms.py index 696e73b..061e36c 100644 --- a/meshterm/platforms.py +++ b/meshterm/platforms.py @@ -169,6 +169,12 @@ class Platform: battery: The source of the battery gauge in the header. ``"companion"`` reads it from the connected MeshCore device. ``"host"`` reads it from the ``power_supply`` driver of sysfs on the PicoCalc itself (confirmed present in P0). Wired in P5. + sound: Whether this platform can play a sound. ``True`` lets MeshTerm play its + notification sounds through the player of the operating system (refer to + :mod:`~meshterm.services.sound`). ``False`` on the PicoCalc: its speaker is silent + on the Lyra, because the speaker pins of the board cannot do hardware PWM. There, + the Preferences page does not offer the sound preferences, and a notification is + silent. The title bar can still flash for it. own_display: Whether MeshTerm itself draws this display (:mod:`meshterm.emulator`) and does not run in a terminal. Then the app is its own terminal, and the terminal that started it is not its business. Examples are a classic Windows @@ -204,6 +210,7 @@ class Platform: tick_s: float spinner_tick_s: float battery: str + sound: bool own_display: bool modifier_watch: str @@ -236,6 +243,7 @@ REGULAR = Platform( tick_s=1.0, spinner_tick_s=0.12, battery="companion", + sound=True, own_display=False, modifier_watch="", ) @@ -268,6 +276,7 @@ PICOCALC_LYRA = Platform( tick_s=2.0, spinner_tick_s=0.5, battery="host", + sound=False, own_display=False, modifier_watch="picocalc", ) @@ -318,11 +327,15 @@ CARDPUTER_ZERO = Platform( tick_s=2.0, spinner_tick_s=0.5, battery="host", + sound=True, own_display=True, modifier_watch="tca8418c", ) -_BY_NAME = {p.name: p for p in (REGULAR, PICOCALC_LYRA, CARDPUTER_ZERO)} +#: Each platform, in a fixed order. +ALL_PLATFORMS: tuple[Platform, ...] = (REGULAR, PICOCALC_LYRA, CARDPUTER_ZERO) + +_BY_NAME = {p.name: p for p in ALL_PLATFORMS} #: The active platform. Do not import this name directly (refer to the module docstring). #: Read it through :func:`get_platform`, and change it only through :func:`set_platform`. diff --git a/tests/test_vt_console_font.py b/tests/test_vt_console_font.py index beefcad..f69407c 100644 --- a/tests/test_vt_console_font.py +++ b/tests/test_vt_console_font.py @@ -267,8 +267,14 @@ def test_the_preference_is_offered_on_the_handheld_only() -> None: spec = get_spec("console_font") assert spec.offered_on("picocalc-lyra") is True assert spec.offered_on("regular") is False - # All other preferences are offered on all platforms. The gate is the exception. - gated = {s.key for _, specs in by_group() for s in specs if s.platforms is not None} + # The other gated preferences are the sound preferences, which the PicoCalc does not + # offer because it has no sound. All other preferences are offered on all platforms. + gated = { + s.key + for group, specs in by_group() + for s in specs + if s.platforms is not None and group != "Sound" + } assert gated == {"console_font"}