Skip to content

effectors — API Reference

Effector plugin packages. The base class and registry live in core.effector_base; the types below are the concrete plugins in effectors/.

effector_base (core)

core.effector_base.EffectorBase(effector_id, config)

Bases: ABC

Base for output devices. Subclasses set effector_type/lanes and implement the lane handler(s) their type uses.

Source code in core/effector_base.py
def __init__(self, effector_id: str, config: "EffectorConfig") -> None:
    self.id = effector_id
    self.config = config
    self.channels = config.channels
    self._device: "Device | None" = None
    self._state: dict[str, Any] = {}      # last-commanded value per channel name

attach_devices(devices)

Bind the backend device (if this effector drives one).

Source code in core/effector_base.py
def attach_devices(self, devices: dict[str, "Device"]) -> None:
    """Bind the backend device (if this effector drives one)."""
    dev_id = self.config.backend.get("device")
    if dev_id is not None:
        if dev_id not in devices:
            raise ValueError(f"effector '{self.id}': unknown device '{dev_id}'")
        self._device = devices[dev_id]

descriptor()

Type + channels + lanes + value domain — lets a UI render the right control.

Source code in core/effector_base.py
def descriptor(self) -> dict:
    """Type + channels + lanes + value domain — lets a UI render the right control."""
    return {
        "type": self.effector_type,
        "lanes": list(self.lanes),
        "channels": [{"name": c.name, "index": c.index} for c in self.channels],
    }

core.effector_base.create_effector(config)

Source code in core/effector_base.py
def create_effector(config: "EffectorConfig") -> "EffectorBase":
    cls = _registry.get(config.type)
    if cls is None:
        raise ValueError(
            f"Unknown effector type '{config.type}'. Known: {sorted(_registry)}."
        )
    return cls(config.id, config)

pwm

effectors.pwm.effector.PwmEffector(effector_id, config)

Bases: EffectorBase

Generic PWM outputs (LED brightness, unidirectional motor speed, fans).

Request body: {"levels": {channel: 0.0-1.0}} (or {channel: level}); levels are normalized and scaled to the device's 0-255 command. channel may be a name or an index. params.min_duty (0..1) maps a non-zero level into [min_duty, 1] so loads with a minimum start point (e.g. small fans) actually move; level 0 is always fully off.

Source code in core/effector_base.py
def __init__(self, effector_id: str, config: "EffectorConfig") -> None:
    self.id = effector_id
    self.config = config
    self.channels = config.channels
    self._device: "Device | None" = None
    self._state: dict[str, Any] = {}      # last-commanded value per channel name

fan_array

effectors.fan_array.effector.FanArray(effector_id, config)

Bases: PwmEffector

One or more named fans driven over the request lane.

Built on the generic pwm drive (level 0–1 → device set_duty), adding what's fan-specific:

  • per-fan min_duty — fans differ in where they actually start; params.per_fan: {<name>: <min_duty>} overrides the effector-wide params.min_duty for that fan.

RPM stays out here: a paired fan_tach sensor publishes RPM to the relay, and closing the loop (target RPM → trim level) is a policy, not the effector.

Source code in core/effector_base.py
def __init__(self, effector_id: str, config: "EffectorConfig") -> None:
    self.id = effector_id
    self.config = config
    self.channels = config.channels
    self._device: "Device | None" = None
    self._state: dict[str, Any] = {}      # last-commanded value per channel name

servo

effectors.servo.effector.ServoEffector(effector_id, config)

Bases: EffectorBase

Hobby servos — position by angle (or raw µs), over two backends.

Request body, one or both of: {"angles": {channel: degrees}} — mapped through the channel's deg↔µs calibration, soft-limited {"us": {channel: microseconds}} — raw, clamped to [min_us, max_us] channel may be a name or an index.

Backend (config.backend): {kind: mcu, device: } — send_command(CMD_SET_US, [index, us]) through an MCU running servo_out firmware {kind: sbc_pwm, chip: N} — Linux hardware PWM (/sys/class/pwm); channel index = pwm line on that chip

Params (global, each overridable per channel via params.per_channel.): freq_hz (50), min_us (500), max_us (2500) — pulse bounds deg_min (0), deg_max (180) — travel soft limits trim_deg (0) — added to every angle invert (false) — reverse direction

The angle→µs map and limits live HERE, in config — the firmware only moves microseconds (and applies its own absolute safety clamp).

Source code in effectors/servo/effector.py
def __init__(self, effector_id, config) -> None:
    super().__init__(effector_id, config)
    p = config.params or {}
    self._freq = int(p.get("freq_hz", 50))
    self._pwm: SysfsPwm | None = None
    if config.backend.get("kind") == "sbc_pwm":
        self._pwm = SysfsPwm(int(config.backend.get("chip", 0)), self._freq,
                             root=config.backend.get("root", "/sys/class/pwm"))

effectors.servo.effector.SysfsPwm(chip, freq_hz, root='/sys/class/pwm')

Minimal Linux hardware-PWM driver (/sys/class/pwm) for the sbc_pwm backend.

One instance per pwmchip; lines are lazily exported. Pulse widths are written as duty_cycle in ns against a fixed period (the servo frame rate). root is injectable for tests.

Source code in effectors/servo/effector.py
def __init__(self, chip: int, freq_hz: int, root: str = "/sys/class/pwm") -> None:
    self._chip = Path(root) / f"pwmchip{chip}"
    self._period_ns = round(1_000_000_000 / freq_hz)
    self._exported: set[int] = set()

power_rail

effectors.power_rail.effector.PowerRail(effector_id, config)

Bases: EffectorBase

One switchable power rail (relay / high-side switch) — gating, not failure.

Request body (request lane): {"on": true|false} — direct switch {"levels": {: 0|1}} — policy lane (PolicyRuntime applies actions as levels; >= 0.5 = on)

Backend (config.backend): {kind: gpio, line: {backend: libgpiod, chip, line, active_low}} — SBC kernel GPIO driving the relay IN pin. Most SRD-05VDC modules energize on LOW: put active_low in the line spec. {kind: mcu, device: , channel: N, active_low: …} — a gpio_out firmware channel via the device's command sink (core/gpio.py mcu backend; e.g. the brainstem RP2040s).

Config keys (params): initial: "on"|"off" (default "on") — state applied at startup members: [device ids] — devices powered THROUGH this rail. While the rail is off those devices are gated: GET /devices reports them gated (not failed), so a deliberate power cut never reads as a fault. The mcu_serial reconnect loop picks them back up when the rail returns.

Rail state is published to the thalamic relay as power.<id> (1/0) so policies and other cortices can observe gating like any other signal.

Source code in effectors/power_rail/effector.py
def __init__(self, effector_id, config) -> None:
    super().__init__(effector_id, config)
    self._line: OutputLine = NullOutputLine("not attached")
    self._relay = None
    self._on: bool | None = None          # unknown until start()

attach_relay(relay)

Optional app.py hook — lets the rail publish its state as a signal.

Source code in effectors/power_rail/effector.py
def attach_relay(self, relay) -> None:
    """Optional app.py hook — lets the rail publish its state as a signal."""
    self._relay = relay

gated_devices()

Device ids currently without power because this rail is off.

Source code in effectors/power_rail/effector.py
def gated_devices(self) -> set[str]:
    """Device ids currently without power because this rail is off."""
    return set(self.members) if self._on is False else set()

speaker

effectors.speaker.effector.Speaker(effector_id, config)

Bases: EffectorBase

Audio playback through ALSA (MAX98357A on the shared-clock I2S DOUT).

Two lanes
  • stream — WS /effectors/<id>/stream: raw interleaved S16_LE audio fed straight to an aplay raw pipe (spawned lazily on first chunk, respawned on a broken pipe). The same format sensors/audio_in captures, so loopback tests are byte-symmetric.
  • request — {"on": true|false}: the amp's SD pin via a core/gpio.py output line (params.sd_line). Gating the amp is a GPIO, not a relay — it costs nothing and kills idle hiss.

Config keys (params): alsa_device ("hw:0,0"), sample_rate (48000), channels (2), sd_line ({backend: libgpiod, …} | absent = always enabled).

Volume/limiting note: the 1 W ADA1313 driver hangs off a ~1.8 W-capable amp — the gain strap is the hardware ceiling; software keeps meaning (what to say) and the amp keeps physics (how loud it can be).

Source code in effectors/speaker/effector.py
def __init__(self, effector_id, config) -> None:
    super().__init__(effector_id, config)
    p = config.params or {}
    self._device = str(p.get("alsa_device", "hw:0,0"))
    self._rate = int(p.get("sample_rate", 48000))
    self._channels = int(p.get("channels", 2))
    self._sd: OutputLine = NullOutputLine("not attached")
    self._proc: subprocess.Popen | None = None
    self._bytes = 0
    self._on = True

stream_sink

effectors.stream_sink.effector.StreamSink(effector_id, config)

Bases: EffectorBase

Reference stream-lane effector (no hardware) — records what it receives.

Proves the continuous-flow lane end to end (WS /effectors/{id}/stream → feed) and is the template a real speaker / addressable-LED-strip effector follows.

Source code in effectors/stream_sink/effector.py
def __init__(self, effector_id: str, config: "EffectorConfig") -> None:
    super().__init__(effector_id, config)
    self._bytes = 0
    self._last_len = 0