← Back to the chapter · Next detail →

Technical detail · Firmware Architecture Patterns

Firmware Pattern Snippets

19.6 Firmware Pattern Snippets

Use small examples to prove the boundary before copying a full framework.

enum class NodeMode { Booting, Sampling, Publishing, Sleeping, Fault };

NodeMode nextMode(NodeMode mode, bool sampleReady, bool publishOk, bool fault) {
  if (fault) return NodeMode::Fault;
  switch (mode) {
    case NodeMode::Booting: return NodeMode::Sampling;
    case NodeMode::Sampling: return sampleReady ? NodeMode::Publishing : NodeMode::Sampling;
    case NodeMode::Publishing: return publishOk ? NodeMode::Sleeping : NodeMode::Fault;
    case NodeMode::Sleeping: return NodeMode::Sampling;
    case NodeMode::Fault: return NodeMode::Fault;
  }
  return NodeMode::Fault;
}

An analog-to-digital converter (ADC) turns a voltage into a number for code to process. Pair the state machine with a driver boundary and an event handoff:

Give the driver interface typed readings with units and validity flags instead of raw global variables. Keep ADC conversion, calibration, filtering, threshold checks, and payload formatting in deterministic functions.

Use an event queue or flag to keep interrupts and callbacks short, then process the work in normal context. Limit publish attempts and log each one; convert blocking retry loops into state transitions when responsiveness matters. Validate the sample interval, pins, thresholds, and site settings in a configuration record before treating a run as evidence.

Review rule: procedural loops, state machines, objects, queues, functional transforms, and RTOS tasks are useful only when each one has a named job and a testable boundary.

← Back to the chapter · Next detail →