forked from Blendman974/faceclaw
Autoconnect on reaching main screen; play sound effect on first launch
This commit is contained in:
1 parent
ea80d05607
commit
d97429a21a
5 files changed
+91
No files matched your search
@@ -11,6 +11,8 @@ import { onAndroidNotificationPosted } from "../native/notification-icons";
|
|||||||
import { openEvenAppSettings, readEvenAppNotificationState } from "../native/even-app-conflict";
|
import { openEvenAppSettings, readEvenAppNotificationState } from "../native/even-app-conflict";
|
||||||
import { grayImageToPreviewSource } from "../native/gray-image-preview";
|
import { grayImageToPreviewSource } from "../native/gray-image-preview";
|
||||||
import { firmwareIncompatibilityMessage } from "./firmware-compat";
|
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 { beginRenderPass, endRenderPass } from "../util/render-freshness";
|
||||||
import { voiceControlBridge } from "../native/voice-control";
|
import { voiceControlBridge } from "../native/voice-control";
|
||||||
import { G2_LENS_HEIGHT, G2_LENS_WIDTH, GrayImage } from "../graphics/image";
|
import { G2_LENS_HEIGHT, G2_LENS_WIDTH, GrayImage } from "../graphics/image";
|
||||||
@@ -130,6 +132,9 @@ class DashboardController {
|
|||||||
private batteryOptimizationWarningVisible = false;
|
private batteryOptimizationWarningVisible = false;
|
||||||
private displayPreview: ImageSource | null = createInitialDisplayPreview();
|
private displayPreview: ImageSource | null = createInitialDisplayPreview();
|
||||||
private readonly listeners = new Set<DashboardListener>();
|
private readonly listeners = new Set<DashboardListener>();
|
||||||
|
// 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 communicator: FaceclawCommunicatorBridge | null = null;
|
||||||
private shellRefreshTimer: ReturnType<typeof setInterval> | null = null;
|
private shellRefreshTimer: ReturnType<typeof setInterval> | null = null;
|
||||||
@@ -359,6 +364,7 @@ class DashboardController {
|
|||||||
this.log = "";
|
this.log = "";
|
||||||
this.lastInput = "waiting...";
|
this.lastInput = "waiting...";
|
||||||
this.lastSys = "none yet";
|
this.lastSys = "none yet";
|
||||||
|
this.welcomeSoundArmed = isWelcomeSoundPending();
|
||||||
this.firmwareWarningMessage = "";
|
this.firmwareWarningMessage = "";
|
||||||
this.refreshBatteryOptimizationStatus();
|
this.refreshBatteryOptimizationStatus();
|
||||||
this.refreshEvenAppStatus();
|
this.refreshEvenAppStatus();
|
||||||
@@ -435,6 +441,13 @@ class DashboardController {
|
|||||||
this.offFrameMetrics = communicator.onFrameMetrics((metrics) => {
|
this.offFrameMetrics = communicator.onFrameMetrics((metrics) => {
|
||||||
if (this.phase === "connected") {
|
if (this.phase === "connected") {
|
||||||
this.setStatus(`Connected. Last frame: ${this.formatFrameMetrics(metrics)}.`);
|
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) => {
|
this.offFirmwareInfo = communicator.onFirmwareInfo((info) => {
|
||||||
@@ -1051,6 +1064,21 @@ class DashboardController {
|
|||||||
await this.communicator.playBuzzerSequence(payload);
|
await this.communicator.playBuzzerSequence(payload);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** One-time celebratory jingle on the first connection after onboarding. */
|
||||||
|
private async playWelcomeSound(): Promise<void> {
|
||||||
|
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 {
|
private clearDashboardTimer(): void {
|
||||||
if (this.shellRefreshTimer) {
|
if (this.shellRefreshTimer) {
|
||||||
clearInterval(this.shellRefreshTimer);
|
clearInterval(this.shellRefreshTimer);
|
||||||
|
|||||||
@@ -95,6 +95,9 @@ export function loaded(args: EventData) {
|
|||||||
dashboardController.refreshEvenAppStatus()
|
dashboardController.refreshEvenAppStatus()
|
||||||
|
|
||||||
const model = page.bindingContext as MainViewModel | null
|
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 = [
|
const scrollViews = [
|
||||||
page.getViewById<ScrollView>('logScrollView'),
|
page.getViewById<ScrollView>('logScrollView'),
|
||||||
page.getViewById<ScrollView>('logScrollViewLandscape'),
|
page.getViewById<ScrollView>('logScrollViewLandscape'),
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import { Application, Frame, ImageSource, Observable, Screen } from "@nativescript/core";
|
import { Application, Frame, ImageSource, Observable, Screen } from "@nativescript/core";
|
||||||
import { dashboardController } from "../g2/dashboard-controller";
|
import { dashboardController } from "../g2/dashboard-controller";
|
||||||
|
import { isValidMacAddress, loadDeviceAddresses } from "../g2/device-addresses";
|
||||||
|
|
||||||
type LayoutOrientation = "portrait" | "landscape";
|
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<void> {
|
||||||
|
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 {
|
onConfigureTap(): void {
|
||||||
if (!this.canRun) return;
|
if (!this.canRun) return;
|
||||||
Frame.topmost()?.navigate("phone-ui/config-page");
|
Frame.topmost()?.navigate("phone-ui/config-page");
|
||||||
|
|||||||
@@ -2,13 +2,32 @@ import { ApplicationSettings } from "@nativescript/core";
|
|||||||
|
|
||||||
const ONBOARDING_COMPLETE_KEY = "onboarding.complete";
|
const ONBOARDING_COMPLETE_KEY = "onboarding.complete";
|
||||||
const PREVIEW_ONLY_KEY = "onboarding.previewOnly";
|
const PREVIEW_ONLY_KEY = "onboarding.previewOnly";
|
||||||
|
const WELCOME_SOUND_PENDING_KEY = "onboarding.welcomeSoundPending";
|
||||||
|
|
||||||
export function hasCompletedOnboarding(): boolean {
|
export function hasCompletedOnboarding(): boolean {
|
||||||
return ApplicationSettings.getBoolean(ONBOARDING_COMPLETE_KEY, false);
|
return ApplicationSettings.getBoolean(ONBOARDING_COMPLETE_KEY, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function setOnboardingCompleted(completed: boolean): void {
|
export function setOnboardingCompleted(completed: boolean): void {
|
||||||
|
const wasCompleted = hasCompletedOnboarding();
|
||||||
ApplicationSettings.setBoolean(ONBOARDING_COMPLETE_KEY, completed);
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -53,6 +53,30 @@ export function effectPhrases(effect: Effect): Step[][] {
|
|||||||
return Array.isArray(effect[0]) ? (effect as Step[][]) : [effect as 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> | void,
|
||||||
|
sleep: (ms: number) => Promise<void>,
|
||||||
|
): Promise<void> {
|
||||||
|
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 ===============================
|
// ============================ musical helpers ===============================
|
||||||
// Equal-tempered pitch from a name like "A4", "C#5", "Bb3". A4 = 440 Hz.
|
// Equal-tempered pitch from a name like "A4", "C#5", "Bb3". A4 = 440 Hz.
|
||||||
const SEMI: Record<string, number> = { C: 0, D: 2, E: 4, F: 5, G: 7, A: 9, B: 11 };
|
const SEMI: Record<string, number> = { C: 0, D: 2, E: 4, F: 5, G: 7, A: 9, B: 11 };
|
||||||
|
|||||||
Reference in new issue
Block a user