buttplug-go
A Go library for controlling sex toys through Intiface Central. It covers the whole of version 4 of the Buttplug protocol, including finding toys, every kind of movement, sensors and stopping.
Requirements
- Go 1.27 or newer
- Intiface Central, running with its engine started
Installation
go get github.com/VictoriqueMoe/buttplug-go
Quick start
This waits for a Lovense Lush to connect, then vibrates it at half strength for two seconds.
package main
import (
"context"
"log"
"time"
"github.com/VictoriqueMoe/buttplug-go/intiface"
"github.com/VictoriqueMoe/buttplug-go/toy"
)
func main() {
ctx := context.Background()
session, err := intiface.Dial(ctx, "ws://localhost:12345", "my app")
if err != nil {
log.Println(err)
return
}
defer session.Close()
lush, err := session.Await(ctx, "Lovense Lush")
if err != nil {
log.Println(err)
return
}
if err := lush.Drive(ctx, toy.Vibration, 0.5); err != nil {
log.Println(err)
return
}
time.Sleep(2 * time.Second)
}
Closing the session disconnects from Intiface, which stops every toy. The session's Ended channel closes when the connection is lost, and Close then returns the reason.
Examples
The examples folder has a small program for each part of the library.
| Example |
What it shows |
| find |
Finding toys and listing what they can do |
| motor |
Drive, Step and Halt on motors |
| stroker |
Glide and Step on strokers |
| sensor |
Read and Watch on sensors |
| halt |
Stopping a motor, a toy or everything |
Except for find, they take the name of a toy.
go run ./examples/motor "Lovense Lush"
Finding toys
Await waits for a toy to connect, scanning for it if needed. The name is the toy's model, or the display name you gave it in Intiface.
Discover scans and gives you every toy as it connects. It stops when you leave the loop, the context ends or Intiface finishes scanning.
for found, err := range session.Discover(ctx) {
if err != nil {
return err
}
fmt.Println(found.Model, "connected")
}
Arrivals does the same without scanning. A toy's Gone channel closes when it disconnects.
for found, err := range session.Arrivals(ctx) {
if err != nil {
return err
}
go func() {
<-found.Gone
fmt.Println(found.Model, "disconnected")
}()
}
Toys returns the toys connected right now.
Moving toys
Drive
Drive takes a motion and a strength from 0 to 1. It moves every motor on the toy that can make that motion, and returns toy.ErrNoMotor if there are none.
if err := lush.Drive(ctx, toy.Vibration, 0.5); err != nil {
return err
}
| Motion |
What the toy does |
toy.Vibration |
Vibrates |
toy.Rotation |
Rotates, or turns the other way below 0 if the toy supports it |
toy.Thrusting |
Thrusts or oscillates at a set speed |
toy.Squeezing |
Squeezes or inflates |
toy.Spraying |
Sprays |
toy.Warming |
Heats, or cools below 0 if the toy supports it |
toy.Lighting |
Sets the brightness of its light |
toy.Position |
Moves straight to a position |
To control one motor at a time, use Motors.
for _, motor := range lush.Motors {
fmt.Println(motor.Label, motor.Motion)
if err := motor.Drive(ctx, 0.3); err != nil {
return err
}
}
Step
Every motor has a fixed number of steps, from Lowest to Highest. Step sets one exactly. This climbs to half power one step at a time.
motor := lush.Motors[0]
for step := range motor.Highest / 2 {
if err := motor.Step(ctx, step+1); err != nil {
return err
}
time.Sleep(300 * time.Millisecond)
}
Glide
Strokers that can move to a position over a set time have Axes. Glide takes a position from 0 to 1 and how long to take. Step does the same with one of the axis's own steps.
axis := stroker.Axes[0]
if err := axis.Glide(ctx, 0.8, 500*time.Millisecond); err != nil {
return err
}
if err := axis.Step(ctx, 20, time.Second); err != nil {
return err
}
Motors, axes and sensors with the same Feature number belong to the same part of the toy.
Stopping toys
Halt works on everything from a single motor to every toy at once.
if err := lush.Halt(ctx); err != nil {
return err
}
| Call |
What stops |
session.Halt(ctx) |
Movement and live sensors on every toy |
lush.Halt(ctx) |
Movement and live sensors on that toy |
lush.Halt(ctx, toy.Movement) |
Only that toy's movement |
lush.Halt(ctx, toy.Sensing) |
Only that toy's live sensors |
motor.Halt(ctx) |
Movement on that part of the toy |
axis.Halt(ctx) |
Movement on that part of the toy |
sensor.Halt(ctx) |
Live sensors on that part of the toy |
Sensors
| Sensor |
What it reports |
toy.Battery |
Battery level, as a percentage |
toy.Signal |
Bluetooth signal strength |
toy.Pressure |
Pressure, in the toy's own units |
toy.Button |
1 while pressed, 0 when released |
Charge returns the battery level.
charge, err := lush.Charge(ctx)
if err != nil {
return err
}
fmt.Println("battery:", charge)
For the rest, use Sensors. Read takes one reading from a sensor that is Readable.
for _, sensor := range lush.Sensors {
if !sensor.Readable {
continue
}
value, err := sensor.Read(ctx)
if err != nil {
return err
}
fmt.Println(sensor.Sense, value)
}
Watch follows a sensor that is Live, giving you each new reading until you leave the loop.
for _, sensor := range stroker.Sensors {
if sensor.Sense != toy.Pressure || !sensor.Live {
continue
}
for pressure, err := range sensor.Watch(ctx) {
if err != nil {
return err
}
fmt.Println(pressure)
}
}
Errors
| Error |
Meaning |
intiface.ErrClosed |
The session has been closed |
intiface.ErrVersion |
Intiface does not use version 4 of the protocol |
intiface.Refusal |
Intiface turned the request down, with its reason |
toy.ErrGone |
The toy has disconnected |
toy.ErrNoMotor |
The toy cannot make that motion |
toy.ErrStrength |
The strength is outside what the motor accepts |
toy.ErrPosition |
The position or step is outside the range |
toy.ErrDuration |
The axis cannot move that quickly or slowly |
toy.ErrNoBattery |
The toy does not report its battery level |
toy.ErrNotReadable |
The sensor cannot be read on request |
toy.ErrNotLive |
The sensor has no live readings |
If a newer Intiface offers something this library does not know about, the rest of the toy still works and the name is listed in its Unsupported field.
Licence
MIT