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
- Variables
- func PeriodByte(periodMillis int) (byte, error)
- func PeriodForHz(hz int) int
- func PeriodFromByte(value byte) (int, error)
- type Channel
- type ChannelFrame
- type ConnectRequest
- type Connector
- type DeviceInfo
- type Frame
- type Limits
- type Link
- type LinkEvent
- type LinkEventKind
- type Packet
- type Point
- type Profile
- type Renderer
- type SimulatedConnector
- type SimulatedLink
- func (l *SimulatedLink) Close() error
- func (l *SimulatedLink) Device() DeviceInfo
- func (l *SimulatedLink) Events() <-chan LinkEvent
- func (l *SimulatedLink) Lose(cause error)
- func (l *SimulatedLink) ReadBattery(ctx context.Context) (int, error)
- func (l *SimulatedLink) SetLastWill(payload []byte) error
- func (l *SimulatedLink) Snapshot() SimulatedState
- func (l *SimulatedLink) TurnSwitch(channel Channel, delta int)
- func (l *SimulatedLink) Write(ctx context.Context, payload []byte, _ bool) error
- type SimulatedState
- type StrengthChange
- type StrengthController
- func (c *StrengthController) CancelPending()
- func (c *StrengthController) Next(now time.Time) (StrengthChange, StrengthChange, int)
- func (c *StrengthController) Pending() bool
- func (c *StrengthController) Report(report StrengthReport)
- func (c *StrengthController) RequestZero()
- func (c *StrengthController) Reset()
- func (c *StrengthController) SetCeilings(ceilingA, ceilingB int)
- func (c *StrengthController) Snapshot() StrengthState
- func (c *StrengthController) Step(channel Channel, delta int) error
- func (c *StrengthController) ZeroWritten(sequence int)
- type StrengthMode
- type StrengthReport
- type StrengthState
- type Timeline
Constants ¶
const ( LinkNotification = ble.LinkNotification LinkBattery = ble.LinkBattery LinkLost = ble.LinkLost LinkWriteFailed = ble.LinkWriteFailed )
Link event kinds retain source compatibility for the Coyote driver.
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.
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.
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.
const ( StrengthUnknown = "unknown" StrengthReported = "reported" StrengthZeroCommand = "zero_command" )
Strength sources distinguish a device report from an accepted zero command.
const (
ReferencePackets = 16
)
ReferencePackets is the reference pulse length: 1.6 s.
Variables ¶
var ( ErrNotFound = ble.ErrNotFound ErrUnsupportedDevice = ble.ErrUnsupportedDevice ErrHelperMissing = ble.ErrHelperMissing ErrLinkClosed = ble.ErrLinkClosed )
Connection errors are shared with the native Bluetooth client.
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") )
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 ¶
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 ¶
PeriodForHz converts a pulse rate to the nearest valid period.
func PeriodFromByte ¶
PeriodFromByte inverts PeriodByte for a valid 10..240 wire value, returning the shortest period that encodes to it.
Types ¶
type ChannelFrame ¶
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 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 ¶
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 ¶
DecodeLimits parses a BF payload.
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 ¶
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.
type 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
SpeedLow float64
SpeedHigh float64
}
Profile is the user's conversion profile. Frequencies are pulse rates in Hz; DG-LAB's period form is derived at the boundary. SpeedLow and SpeedHigh are the averaged carriage speeds, in full spans per second, at the user's slowest and fastest saved Handy speeds; zero keeps the saturating curve.
func NormalizeProfile ¶
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 ¶
NewRenderer returns a renderer for profile.
func (*Renderer) Packet ¶
Packet renders the four frames starting at startMillis and reports how many of them the timeline covered. Uncovered frames are silent.
func (*Renderer) Restart ¶
Restart begins a new stream: smoothing resets and output fades in from streamMillis, so playback never starts at full pulse width.
func (*Renderer) SetProfile ¶
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 ¶
func (c *SimulatedConnector) Connect(ctx context.Context, _ ConnectRequest) (Link, error)
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 (*SimulatedConnector) LastLink ¶
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 ¶
type SimulatedLink struct {
// contains filtered or unexported fields
}
SimulatedLink is one modeled connection.
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.
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 ¶
func (c *StrengthController) Next(now time.Time) (StrengthChange, StrengthChange, int)
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 ¶
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).