estim

package
v0.1.0-alpha.64 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 18, 2026 License: GPL-3.0 Imports: 8 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package estim models DG-LAB Coyote 3.0 electrical stimulation output: the documented Bluetooth packet formats, the user-owned strength handshake, and the conversion profile that turns the shared motion frame into two-channel pulse frames. It performs no I/O; links and dispatch owners live elsewhere.

Protocol reference: https://github.com/dungeonlab-open/dglab-bluetooth-protocol (coyote/v3).

Index

Constants

View Source
const (
	LinkNotification = ble.LinkNotification
	LinkBattery      = ble.LinkBattery
	LinkLost         = ble.LinkLost
	LinkWriteFailed  = ble.LinkWriteFailed
)

Link event kinds retain source compatibility for the Coyote driver.

View Source
const (
	// DeviceName is the advertised name of a Coyote 3.0 in normal mode. The
	// recovery-mode name differs ("47L121000_O3") and exposes no pulse service.
	DeviceName                = "47L121000"
	ServiceUUID               = "0000180c-0000-1000-8000-00805f9b34fb"
	WriteCharacteristicUUID   = "0000150a-0000-1000-8000-00805f9b34fb"
	NotifyCharacteristicUUID  = "0000150b-0000-1000-8000-00805f9b34fb"
	BatteryServiceUUID        = "0000180a-0000-1000-8000-00805f9b34fb"
	BatteryCharacteristicUUID = "00001500-0000-1000-8000-00805f9b34fb"
)

Bluetooth identity of a Coyote 3.0 pulse host. UUIDs use the Bluetooth base UUID with DG-LAB's 16-bit service and characteristic values.

View Source
const (
	// MaxStrength is the device's absolute channel strength range, 0..200.
	MaxStrength = 200
	// MaxIntensity is the relative pulse-width range of one waveform frame.
	MaxIntensity = 100
	// MinPeriodMillis and MaxPeriodMillis bound one output unit (pulse plus
	// gap). DG-LAB calls this the waveform frequency: 10 ms is 100 Hz.
	MinPeriodMillis = 10
	MaxPeriodMillis = 1000
	// FrameMillis is the output window of one waveform frame.
	FrameMillis = 25
	// FramesPerPacket frames make up the 100 ms each B0 packet carries.
	FramesPerPacket = 4
	// PacketMillis is the B0 write cadence.
	PacketMillis = FrameMillis * FramesPerPacket
	// PacketBytes is the fixed B0 length; LimitsBytes the fixed BF length.
	PacketBytes = 20
	LimitsBytes = 7
	// MaxSequence is the largest 4-bit strength-change sequence number.
	MaxSequence = 15
)

Protocol ranges and timing.

View Source
const (
	// FollowStroke distributes output between the channels by position and
	// sets pulse width from movement speed.
	FollowStroke = "stroke"
	// FollowSpeed sets pulse width from movement speed, equally on both
	// channels.
	FollowSpeed = "speed"
	// FollowPosition distributes a steady pulse width by position.
	FollowPosition = "position"
)

Follow modes of the conversion profile. None of them maps position to channel strength: strength stays where the user set it, and the profile only shapes pulse width, channel distribution and pulse frequency beneath it.

View Source
const (
	StrengthUnknown     = "unknown"
	StrengthReported    = "reported"
	StrengthZeroCommand = "zero_command"
)

Strength sources distinguish a device report from an accepted zero command.

View Source
const (
	ReferencePackets = 16
)

ReferencePackets is the reference pulse length: 1.6 s.

Variables

View Source
var (
	ErrNotFound             = ble.ErrNotFound
	ErrBluetoothUnavailable = ble.ErrBluetoothUnavailable
	ErrUnsupportedDevice    = ble.ErrUnsupportedDevice
	ErrHelperMissing        = ble.ErrHelperMissing
	ErrLinkClosed           = ble.ErrLinkClosed
)

Connection errors are shared with the native Bluetooth client.

View Source
var (
	// ErrStrengthUnconfirmed rejects an increase while the device's actual
	// strength is unknown.
	ErrStrengthUnconfirmed = errors.New("the Coyote has not confirmed its current strength yet")
	// ErrStrengthCeiling rejects an increase at the configured ceiling.
	ErrStrengthCeiling = errors.New("this channel is at its strength ceiling")
)
View Source
var SilentFrame = Frame{
	A: ChannelFrame{PeriodMillis: MinPeriodMillis},
	B: ChannelFrame{PeriodMillis: MinPeriodMillis},
}

SilentFrame is a valid frame with no pulse width on either channel.

Functions

func PeriodByte

func PeriodByte(periodMillis int) (byte, error)

PeriodByte converts an output-unit period of 10..1000 ms to the compressed 10..240 wire value with DG-LAB's published integer formula.

func PeriodForHz

func PeriodForHz(hz int) int

PeriodForHz converts a pulse rate to the nearest valid period.

func PeriodFromByte

func PeriodFromByte(value byte) (int, error)

PeriodFromByte inverts PeriodByte for a valid 10..240 wire value, returning the shortest period that encodes to it.

Types

type Channel

type Channel int

Channel identifies one of the two independent output channels.

const (
	ChannelA Channel = iota
	ChannelB
)

Output channels in device order.

type ChannelFrame

type ChannelFrame struct {
	PeriodMillis int
	Intensity    int
}

ChannelFrame is 25 ms of one channel: the output-unit period and the relative pulse width. Intensity 0 produces no pulses.

type ConnectRequest

type ConnectRequest = ble.ConnectRequest

ConnectRequest selects a remembered device and connection deadlines.

type Connector

type Connector = ble.Connector

Connector supplies a Coyote link, including simulated implementations.

type DeviceInfo

type DeviceInfo = ble.DeviceInfo

DeviceInfo contains the private connection identity.

type Frame

type Frame struct {
	A ChannelFrame
	B ChannelFrame
}

Frame is one 25 ms output window for both channels.

func ReferencePacket

func ReferencePacket(profile Profile, channel Channel, index int, total int) [FramesPerPacket]Frame

ReferencePacket renders the explicit channel test while motion is paused. Only the selected channel pulses, at full width: the first half at the band's lowest rate and the second at its highest. The opening frames ramp in. This reference is not a guarantee of the strongest possible physical sensation.

type Limits

type Limits struct {
	StrengthA         int
	StrengthB         int
	FrequencyBalanceA int
	FrequencyBalanceB int
	IntensityBalanceA int
	IntensityBalanceB int
}

Limits is the BF configuration: per-channel strength ceilings the device enforces against every source, including its own switches, plus the two balance parameters. The device keeps these values through power-off.

func DecodeLimits

func DecodeLimits(payload []byte) (Limits, error)

DecodeLimits parses a BF payload.

func (Limits) Encode

func (l Limits) Encode() ([LimitsBytes]byte, error)

Encode returns the 7-byte BF payload.

type Link = ble.Link

Link is the Coyote driver's shared native byte channel.

type LinkEvent

type LinkEvent = ble.LinkEvent

LinkEvent is a byte-link event interpreted by the Coyote driver.

type LinkEventKind

type LinkEventKind = ble.LinkEventKind

LinkEventKind preserves the Coyote connector event contract.

type Packet

type Packet struct {
	Sequence int
	A        StrengthChange
	B        StrengthChange
	Frames   [FramesPerPacket]Frame
	SilenceA bool
	SilenceB bool
}

Packet is one B0 write: an optional strength change and 100 ms of waveform. SilenceA and SilenceB encode DG-LAB's documented single-channel form: an out-of-range value that makes the device discard that channel's waveform for the packet while its strength field still applies.

func DecodePacket

func DecodePacket(payload []byte) (Packet, error)

DecodePacket parses a B0 payload the way the device reads it. A channel with any out-of-range waveform value is reported as silenced, matching the device discarding that channel's four frames.

func (Packet) Encode

func (p Packet) Encode() ([PacketBytes]byte, error)

Encode returns the 20-byte B0 payload. Values are validated rather than clamped: a caller that produced an out-of-range value has a bug, and the device would otherwise silently drop or reinterpret it.

type Point

type Point = companion.Point

Point is one timed semantic position of the shared companion frame copy (internal/companion).

type Profile

type Profile struct {
	Follow       string
	MinHz        int
	MaxHz        int
	Balance      int
	SwapChannels bool
}

Profile is the user's conversion profile. Frequencies are pulse rates in Hz; DG-LAB's period form is derived at the boundary.

func NormalizeProfile

func NormalizeProfile(profile Profile) Profile

NormalizeProfile clamps a profile into its valid ranges.

type Renderer

type Renderer struct {
	// contains filtered or unexported fields
}

Renderer converts the shared frame into pulse frames. It is a pure, deterministic function of the timeline, the profile and its own smoothing state; it never invents motion and never changes channel strength.

func NewRenderer

func NewRenderer(profile Profile) *Renderer

NewRenderer returns a renderer for profile.

func (*Renderer) Packet

func (r *Renderer) Packet(timeline *Timeline, startMillis float64) ([FramesPerPacket]Frame, int)

Packet renders the four frames starting at startMillis and reports how many of them the timeline covered. Uncovered frames are silent.

func (*Renderer) Restart

func (r *Renderer) Restart(streamMillis float64)

Restart begins a new stream: smoothing resets and output fades in from streamMillis, so playback never starts at full pulse width.

func (*Renderer) SetProfile

func (r *Renderer) SetProfile(profile Profile)

SetProfile applies a new profile from the next packet on.

type SimulatedConnector

type SimulatedConnector struct {
	// contains filtered or unexported fields
}

SimulatedConnector opens in-process links to a modeled Coyote 3.0. Each link decodes packets the way the documented device does: ceilings clamp every change, a sequenced change returns a B1 report, and a change made on the device itself reports with sequence 0. It never touches a radio.

func NewSimulatedConnector

func NewSimulatedConnector() *SimulatedConnector

NewSimulatedConnector returns a connector whose devices start at strength 0.

func (*SimulatedConnector) Connect

Connect opens a new simulated link.

func (*SimulatedConnector) FailNextConnect

func (c *SimulatedConnector) FailNextConnect(err error)

FailNextConnect makes the next Connect return err (tests only).

func (c *SimulatedConnector) LastLink() *SimulatedLink

LastLink returns the most recently opened link (tests only).

func (*SimulatedConnector) Simulated

func (c *SimulatedConnector) Simulated() bool

Simulated reports that links never reach a radio.

func (*SimulatedConnector) StartAt

func (c *SimulatedConnector) StartAt(a, b int)

StartAt sets the strength the next simulated device reports before any change, modeling a device left at a level by an earlier session.

type SimulatedLink struct {
	// contains filtered or unexported fields
}

SimulatedLink is one modeled connection.

func (*SimulatedLink) Close

func (l *SimulatedLink) Close() error

Close ends the link.

func (*SimulatedLink) Device

func (l *SimulatedLink) Device() DeviceInfo

Device identifies the simulator.

func (*SimulatedLink) Events

func (l *SimulatedLink) Events() <-chan LinkEvent

Events returns the notification channel.

func (*SimulatedLink) Lose

func (l *SimulatedLink) Lose(cause error)

Lose ends the link as a dropped radio connection would (tests only).

func (*SimulatedLink) ReadBattery

func (l *SimulatedLink) ReadBattery(ctx context.Context) (int, error)

ReadBattery reports a fixed charge.

func (*SimulatedLink) SetLastWill

func (l *SimulatedLink) SetLastWill(payload []byte) error

SetLastWill records the packet a worker would write on core loss.

func (*SimulatedLink) Snapshot

func (l *SimulatedLink) Snapshot() SimulatedState

Snapshot returns the modeled device state.

func (*SimulatedLink) TurnSwitch

func (l *SimulatedLink) TurnSwitch(channel Channel, delta int)

TurnSwitch models the user changing strength on the device itself.

func (*SimulatedLink) Write

func (l *SimulatedLink) Write(ctx context.Context, payload []byte, _ bool) error

Write applies one B0 or BF payload.

type SimulatedState

type SimulatedState struct {
	StrengthA  int
	StrengthB  int
	CeilingA   int
	CeilingB   int
	Packets    int
	Limits     int
	OutputA    int
	OutputB    int
	LastWill   bool
	Closed     bool
	LastPacket Packet
}

SimulatedState is a test and review view of the modeled device.

type StrengthChange

type StrengthChange struct {
	Mode  StrengthMode
	Value int
}

StrengthChange is one channel's strength instruction inside a B0 packet.

type StrengthController

type StrengthController struct {
	// contains filtered or unexported fields
}

StrengthController owns the user-driven strength handshake. It batches user steps, keeps one change in flight until the device's B1 report confirms it, never asks for more than the ceiling, and gives a zero request priority over every pending step. Automation never calls Step: channel strength changes only on an explicit user action or on the device itself.

func NewStrengthController

func NewStrengthController(ceilingA, ceilingB int) *StrengthController

NewStrengthController starts with an unknown device strength.

func (*StrengthController) CancelPending

func (c *StrengthController) CancelPending()

CancelPending discards user steps that have not reached the wire. A Stop must not turn a queued increase into a later background strength change.

func (*StrengthController) Next

Next returns the strength instructions for the packet being composed. Sequence 0 means the packet carries no strength change.

func (*StrengthController) Pending

func (c *StrengthController) Pending() bool

Pending reports whether a change is queued or still awaiting confirmation.

func (*StrengthController) Report

func (c *StrengthController) Report(report StrengthReport)

Report applies a B1 notification. Every report carries the device's actual values; a matching sequence also completes the change in flight.

func (*StrengthController) RequestZero

func (c *StrengthController) RequestZero()

RequestZero sets both channels to zero on the next packet, ahead of any queued or in-flight change.

func (*StrengthController) Reset

func (c *StrengthController) Reset()

Reset forgets device state after a disconnect.

func (*StrengthController) SetCeilings

func (c *StrengthController) SetCeilings(ceilingA, ceilingB int)

SetCeilings updates the per-channel ceilings and trims pending increases.

func (*StrengthController) Snapshot

func (c *StrengthController) Snapshot() StrengthState

Snapshot returns the current handshake state.

func (*StrengthController) Step

func (c *StrengthController) Step(channel Channel, delta int) error

Step queues a user strength change of delta on channel.

func (*StrengthController) ZeroWritten

func (c *StrengthController) ZeroWritten(sequence int)

ZeroWritten records successful GATT delivery, not a device measurement. A notification received since this zero was composed takes precedence, even if it was a physical-switch report with sequence zero. Matching B1 may also have completed the handshake before the write returned.

type StrengthMode

type StrengthMode uint8

StrengthMode says how the device interprets a channel's strength value.

const (
	StrengthKeep     StrengthMode = 0b00
	StrengthIncrease StrengthMode = 0b01
	StrengthDecrease StrengthMode = 0b10
	StrengthSet      StrengthMode = 0b11
)

Strength interpretation modes, two bits per channel.

type StrengthReport

type StrengthReport struct {
	Sequence int
	A        int
	B        int
}

StrengthReport is a decoded B1 notification: the device's actual channel strengths after a change, tagged with the sequence that caused it (0 for a change made on the device itself).

func DecodeNotification

func DecodeNotification(payload []byte) (report StrengthReport, ok bool, err error)

DecodeNotification parses a 0x150B notification. It reports ok=false for message types other than B1 (the deprecated BE included).

type StrengthState

type StrengthState struct {
	A        int
	B        int
	Known    bool
	Source   string
	CeilingA int
	CeilingB int
	Pending  bool
}

StrengthState is a snapshot of the handshake for status views.

type Timeline

type Timeline = companion.Timeline

Timeline is the shared companion copy of the motion frame; the renderer samples it and never reshapes it.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL