From d97429a21a7030fab4cb9795c4cda07f5e10c152 Mon Sep 17 00:00:00 2001 From: James Babcock Date: Fri, 10 Jul 2026 23:24:17 -0700 Subject: [PATCH] Autoconnect on reaching main screen; play sound effect on first launch --- app/g2/dashboard-controller.ts | 28 ++++++++++++++++++++++++++++ app/phone-ui/main-page.ts | 3 +++ app/phone-ui/main-view-model.ts | 17 +++++++++++++++++ app/phone-ui/onboarding-state.ts | 19 +++++++++++++++++++ app/ui/apps/sound-effects.ts | 24 ++++++++++++++++++++++++ 5 files changed, 91 insertions(+) diff --git a/app/g2/dashboard-controller.ts b/app/g2/dashboard-controller.ts index d9e9422e..704fd496 100644 --- a/app/g2/dashboard-controller.ts +++ b/app/g2/dashboard-controller.ts @@ -11,6 +11,8 @@ import { onAndroidNotificationPosted } from "../native/notification-icons"; import { openEvenAppSettings, readEvenAppNotificationState } from "../native/even-app-conflict"; import { grayImageToPreviewSource } from "../native/gray-image-preview"; import { firmwareIncompatibilityMessage } from "./firmware-compat"; +import { findSoundEffect, playSoundEffect } from "../ui/apps/sound-effects"; +import { isWelcomeSoundPending, setWelcomeSoundPending } from "../phone-ui/onboarding-state"; import { beginRenderPass, endRenderPass } from "../util/render-freshness"; import { voiceControlBridge } from "../native/voice-control"; import { G2_LENS_HEIGHT, G2_LENS_WIDTH, GrayImage } from "../graphics/image"; @@ -130,6 +132,9 @@ class DashboardController { private batteryOptimizationWarningVisible = false; private displayPreview: ImageSource | null = createInitialDisplayPreview(); private readonly listeners = new Set(); + // Set at connect time from the persisted flag; the one-time post-onboarding + // welcome sound plays on the first rendered frame (proof the session is warm). + private welcomeSoundArmed = false; private communicator: FaceclawCommunicatorBridge | null = null; private shellRefreshTimer: ReturnType | null = null; @@ -359,6 +364,7 @@ class DashboardController { this.log = ""; this.lastInput = "waiting..."; this.lastSys = "none yet"; + this.welcomeSoundArmed = isWelcomeSoundPending(); this.firmwareWarningMessage = ""; this.refreshBatteryOptimizationStatus(); this.refreshEvenAppStatus(); @@ -435,6 +441,13 @@ class DashboardController { this.offFrameMetrics = communicator.onFrameMetrics((metrics) => { if (this.phase === "connected") { this.setStatus(`Connected. Last frame: ${this.formatFrameMetrics(metrics)}.`); + // A rendered frame means the session is warmed up (fixedLayoutCreated), + // so the buzzer won't be dropped. Play the one-time welcome sound now. + if (this.welcomeSoundArmed) { + this.welcomeSoundArmed = false; + setWelcomeSoundPending(false); + void this.playWelcomeSound(); + } } }); this.offFirmwareInfo = communicator.onFirmwareInfo((info) => { @@ -1051,6 +1064,21 @@ class DashboardController { await this.communicator.playBuzzerSequence(payload); } + /** One-time celebratory jingle on the first connection after onboarding. */ + private async playWelcomeSound(): Promise { + const effect = findSoundEffect("questcomplete"); + if (!effect) return; + try { + await playSoundEffect( + effect, + (payload) => this.playBuzzerSequence(payload), + (ms) => new Promise((resolve) => setTimeout(resolve, ms)), + ); + } catch (error) { + this.appendLog(`welcome sound failed: ${this.formatError(error)}`); + } + } + private clearDashboardTimer(): void { if (this.shellRefreshTimer) { clearInterval(this.shellRefreshTimer); diff --git a/app/phone-ui/main-page.ts b/app/phone-ui/main-page.ts index d9804117..6f439080 100644 --- a/app/phone-ui/main-page.ts +++ b/app/phone-ui/main-page.ts @@ -95,6 +95,9 @@ export function loaded(args: EventData) { dashboardController.refreshEvenAppStatus() const model = page.bindingContext as MainViewModel | null + // Automatically connect on reaching the main page (no-op if already + // connected or if no glasses are configured). + void model?.autoConnect() const scrollViews = [ page.getViewById('logScrollView'), page.getViewById('logScrollViewLandscape'), diff --git a/app/phone-ui/main-view-model.ts b/app/phone-ui/main-view-model.ts index 2912ee09..23da0bea 100644 --- a/app/phone-ui/main-view-model.ts +++ b/app/phone-ui/main-view-model.ts @@ -1,5 +1,6 @@ import { Application, Frame, ImageSource, Observable, Screen } from "@nativescript/core"; import { dashboardController } from "../g2/dashboard-controller"; +import { isValidMacAddress, loadDeviceAddresses } from "../g2/device-addresses"; type LayoutOrientation = "portrait" | "landscape"; @@ -327,6 +328,22 @@ export class MainViewModel extends Observable { } } + /** + * Try to connect automatically on reaching the main page. No-op if already + * connecting/connected, or if no glasses are configured (e.g. preview-only + * users, who have nothing to connect to). + */ + async autoConnect(): Promise { + if (this.phase !== "disconnected") return; + const addresses = loadDeviceAddresses(); + if (!isValidMacAddress(addresses.right) || !isValidMacAddress(addresses.left)) return; + try { + await dashboardController.connect(); + } catch { + // The controller surfaces failures via status/log; nothing to add here. + } + } + onConfigureTap(): void { if (!this.canRun) return; Frame.topmost()?.navigate("phone-ui/config-page"); diff --git a/app/phone-ui/onboarding-state.ts b/app/phone-ui/onboarding-state.ts index f1643430..451a5ef4 100644 --- a/app/phone-ui/onboarding-state.ts +++ b/app/phone-ui/onboarding-state.ts @@ -2,13 +2,32 @@ import { ApplicationSettings } from "@nativescript/core"; const ONBOARDING_COMPLETE_KEY = "onboarding.complete"; const PREVIEW_ONLY_KEY = "onboarding.previewOnly"; +const WELCOME_SOUND_PENDING_KEY = "onboarding.welcomeSoundPending"; export function hasCompletedOnboarding(): boolean { return ApplicationSettings.getBoolean(ONBOARDING_COMPLETE_KEY, false); } export function setOnboardingCompleted(completed: boolean): void { + const wasCompleted = hasCompletedOnboarding(); ApplicationSettings.setBoolean(ONBOARDING_COMPLETE_KEY, completed); + // Arm the one-time welcome sound on the first-ever completion (not on the + // idempotent re-completions that happen when installing from the main menu). + if (completed && !wasCompleted) { + setWelcomeSoundPending(true); + } +} + +/** + * True when onboarding just completed and the celebratory sound hasn't played + * yet. Consumed (and cleared) on the first successful glasses connection. + */ +export function isWelcomeSoundPending(): boolean { + return ApplicationSettings.getBoolean(WELCOME_SOUND_PENDING_KEY, false); +} + +export function setWelcomeSoundPending(pending: boolean): void { + ApplicationSettings.setBoolean(WELCOME_SOUND_PENDING_KEY, pending); } /** diff --git a/app/ui/apps/sound-effects.ts b/app/ui/apps/sound-effects.ts index aaa6b13d..091b0861 100644 --- a/app/ui/apps/sound-effects.ts +++ b/app/ui/apps/sound-effects.ts @@ -53,6 +53,30 @@ export function effectPhrases(effect: Effect): Step[][] { return Array.isArray(effect[0]) ? (effect as Step[][]) : [effect as Step[]]; } +/** Look up a catalog effect by name (e.g. "questcomplete"). */ +export function findSoundEffect(name: string): SoundEffect | undefined { + return SOUND_EFFECTS.find((effect) => effect.name === name); +} + +/** + * Play an effect through `play`, pacing phrase-by-phrase (and chunking any + * over-long phrase to the firmware's per-message cap) so each sequencer + * message lands as the previous phrase finishes. + */ +export async function playSoundEffect( + effect: SoundEffect, + play: (payload: Uint8Array) => Promise | void, + sleep: (ms: number) => Promise, +): Promise { + for (const phrase of effectPhrases(effect.make())) { + for (let index = 0; index < phrase.length; index += CFW_SEQ_MAX) { + const chunk = phrase.slice(index, index + CFW_SEQ_MAX); + await play(buildSoundSequencePayload(chunk)); + await sleep(phraseDurationMs(chunk)); + } + } +} + // ============================ musical helpers =============================== // Equal-tempered pitch from a name like "A4", "C#5", "Bb3". A4 = 440 Hz. const SEMI: Record = { C: 0, D: 2, E: 4, F: 5, G: 7, A: 9, B: 11 };