Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Principle:78 Xiaozhi esp32 Device State Machine

From Leeroopedia
Knowledge Sources
Domains Software_Architecture, Embedded_Systems, State_Management
Last Updated 2026-10-17 00:00 GMT

Overview

A finite state machine (FSM) that governs the runtime behavior of an embedded voice assistant by validating all state transitions against a predefined transition table, ensuring the device never enters an illegal combination of audio, network, and UI states.

Description

The device state machine is a deterministic finite automaton with 12 states representing the device's operational modes. Every state transition is validated against a static transition table; invalid transitions are rejected and logged. This prevents race conditions where, for example, the device might attempt to play TTS audio while upgrading firmware.

The 12 device states:

  • kDeviceStateUnknown — initial/unset state
  • kDeviceStateStarting — boot and initialization
  • kDeviceStateWifiConfiguring — WiFi provisioning mode
  • kDeviceStateIdle — standby, wake word active
  • kDeviceStateConnecting — opening audio channel to server
  • kDeviceStateListening — capturing user speech
  • kDeviceStateSpeaking — playing TTS response
  • kDeviceStateNotifying — playing notification audio
  • kDeviceStateUpgrading — OTA firmware update in progress
  • kDeviceStateActivating — server activation
  • kDeviceStateAudioTesting — audio hardware test mode
  • kDeviceStateFatalError — unrecoverable error

The state machine uses the observer pattern: registered listeners (AddStateChangeListener) are notified on each successful transition. The main application registers a listener that sets MAIN_EVENT_STATE_CHANGED, causing the event loop to call HandleStateChangedEvent(), which updates display, LED, audio, and power settings based on the new state.

Usage

Use this principle when:

  • A device has distinct operational modes with strict ordering requirements
  • Multiple asynchronous events could cause conflicting state changes
  • UI, audio, and network behavior must be coordinated based on a single source of truth
  • Invalid state combinations must be prevented (e.g., listening while upgrading)

Theoretical Basis

The FSM is defined as a 5-tuple (Q, Σ, δ, q₀, F):

Q = {Unknown, Starting, WifiConfiguring, Idle, Connecting,
     Listening, Speaking, Notifying, Upgrading, Activating,
     AudioTesting, FatalError}

δ = transition function (validated table)

q₀ = Unknown

Key transitions:
  Starting      → Idle, WifiConfiguring, Activating
  Idle          → Connecting, Notifying, Upgrading, AudioTesting
  Connecting    → Listening, Idle
  Listening     → Speaking, Idle
  Speaking      → Listening, Idle, Notifying
  Notifying     → Idle
  Upgrading     → FatalError (on failure → reboot)

The transition validation is a simple lookup table:

bool IsValidTransition(DeviceState from, DeviceState to) const {
    // Returns true only if (from, to) is in the allowed transition set
}

The observer pattern decouples state changes from side effects:

TransitionTo(target):
  if IsValidTransition(current, target):
    old = current
    current = target
    for each listener:
      listener(old, target)   // notified in caller's context
    return true
  return false

Practical Guide

When implementing a device state machine:

  • Route all transitions through one method: Application::SetDeviceState() delegates to TransitionTo(), ensuring validation.
  • Handle side effects in a single handler: HandleStateChangedEvent() updates display, LED, audio, and power based on the new state.
  • Use thread-safe state reads: GetState() uses std::atomic for lock-free reads.
  • Notify listeners synchronously: callbacks run in the context of TransitionTo(); use event bits to defer heavy work to the main loop.

Related Pages

Implemented By

Uses Heuristic

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment