-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdevice.go
More file actions
205 lines (188 loc) · 5.48 KB
/
device.go
File metadata and controls
205 lines (188 loc) · 5.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package golibbuttplug
import (
"errors"
"fmt"
"github.com/funjack/golibbuttplug/message"
)
const (
// CommandStopDevice ...
CommandStopDevice = "StopDeviceCmd"
// CommandRaw ...
CommandRaw = "RawCmd"
// CommandSingleMotorVibrate ...
CommandSingleMotorVibrate = "SingleMotorVibrateCmd"
// CommandKiiroo ...
CommandKiiroo = "KiirooCmd"
// CommandFleshlightLaunchFW12 ...
CommandFleshlightLaunchFW12 = "FleshlightLaunchFW12Cmd"
// CommandLovense ...
CommandLovense = "LovenseCmd"
// CommandVorzeA10Cyclone ...
CommandVorzeA10Cyclone = "VorzeA10CycloneCmd"
)
var (
// ErrUnsupported is the error returned when the command executed is
// not supported by the device.
ErrUnsupported = errors.New("unsupported command")
// ErrInvalidSpeed is the error retured when the speed is not supported
// by the device.
ErrInvalidSpeed = errors.New("invalid speed")
// ErrInvalidPosition is the error retured when the position is not
// supported by the device.
ErrInvalidPosition = errors.New("invalid position")
// ErrInvalidCmd is the error retured when the command is not
// supported by the device.
ErrInvalidCmd = errors.New("invalid command")
)
// Device structs represents a connected device and can be used to execute
// commands.
type Device struct {
client *Client
device message.Device
done chan struct{}
}
func (d *Device) String() string {
return fmt.Sprintf("%s(%d)", d.device.DeviceName, d.device.DeviceIndex)
}
// Name returns the device name.
func (d *Device) Name() string {
return d.device.DeviceName
}
// IsSupported returns true if the message type is supported.
func (d *Device) IsSupported(msgtype string) bool {
for _, dm := range d.device.DeviceMessages {
if dm == msgtype {
return true
}
}
return false
}
// Supported returns a list of all supported message types for this device.
func (d *Device) Supported() []string {
return d.device.DeviceMessages
}
// StopDeviceCmd stops a device from whatever actions it may be taking.
func (d *Device) StopDeviceCmd() error {
if !d.IsSupported(CommandStopDevice) {
return ErrUnsupported
}
id := d.client.counter.Generate()
return d.client.sendMessage(id, message.OutgoingMessage{
StopDeviceCmd: &message.Device{
ID: id,
DeviceIndex: d.device.DeviceIndex,
},
})
}
// RawCmd sends a raw byte string to a device.
func (d *Device) RawCmd(cmd []byte) error {
if !d.IsSupported(CommandRaw) {
return ErrUnsupported
}
id := d.client.counter.Generate()
return d.client.sendMessage(id, message.OutgoingMessage{
RawCmd: &message.RawCmd{
ID: id,
DeviceIndex: d.device.DeviceIndex,
Command: cmd,
},
})
}
// SingleMotorVibrateCmd causes a toy that supports vibration to run at a
// certain speed. In order to abstract the dynamic range of different toys, the
// value sent is a float with a range of [0.0-1.0].
func (d *Device) SingleMotorVibrateCmd(spd float64) error {
if !d.IsSupported(CommandSingleMotorVibrate) {
return ErrUnsupported
}
if spd < 0 || spd > 1 {
return ErrInvalidSpeed
}
id := d.client.counter.Generate()
return d.client.sendMessage(id, message.OutgoingMessage{
SingleMotorVibrateCmd: &message.SingleMotorVibrateCmd{
ID: id,
DeviceIndex: d.device.DeviceIndex,
Speed: spd,
},
})
}
// KiirooCmd causes a toy that supports Kiiroo style commands to run whatever
// event may be related.
func (d *Device) KiirooCmd(cmd int) error {
if !d.IsSupported(CommandKiiroo) {
return ErrUnsupported
}
if cmd < 0 || cmd > 4 {
return ErrInvalidCmd
}
id := d.client.counter.Generate()
return d.client.sendMessage(id, message.OutgoingMessage{
KiirooCmd: &message.KiirooCmd{
ID: id,
DeviceIndex: d.device.DeviceIndex,
Command: cmd,
},
})
}
// FleshlightLaunchFW12Cmd causes a toy that supports Fleshlight Launch
// (Firmware Version 1.2) style commands to run whatever event may be related.
func (d *Device) FleshlightLaunchFW12Cmd(pos, spd int) error {
if !d.IsSupported(CommandFleshlightLaunchFW12) {
return ErrUnsupported
}
if pos < 0 || pos > 99 {
return ErrInvalidPosition
}
if spd < 0 || spd > 99 {
return ErrInvalidSpeed
}
id := d.client.counter.Generate()
return d.client.sendMessage(id, message.OutgoingMessage{
FleshlightLaunchFW12Cmd: &message.FleshlightLaunchFW12Cmd{
ID: id,
DeviceIndex: d.device.DeviceIndex,
Position: pos,
Speed: spd,
},
})
}
// LovenseCmd causes a toy that supports Lovense style commands to run whatever
// event may be related.
func (d *Device) LovenseCmd(cmd string) error {
if !d.IsSupported(CommandLovense) {
return ErrUnsupported
}
id := d.client.counter.Generate()
return d.client.sendMessage(id, message.OutgoingMessage{
LovenseCmd: &message.LovenseCmd{
ID: id,
DeviceIndex: d.device.DeviceIndex,
Command: cmd,
},
})
}
// VorzeA10CycloneCmd causes a toy that supports VorzeA10Cyclone style commands
// to run whatever event may be related.
func (d *Device) VorzeA10CycloneCmd(spd int, clockwise bool) error {
if !d.IsSupported(CommandVorzeA10Cyclone) {
return ErrUnsupported
}
if spd < 0 || spd > 100 {
return ErrInvalidSpeed
}
id := d.client.counter.Generate()
return d.client.sendMessage(id, message.OutgoingMessage{
VorzeA10CycloneCmd: &message.VorzeA10CycloneCmd{
ID: id,
DeviceIndex: d.device.DeviceIndex,
Speed: spd,
Clockwise: clockwise,
},
})
}
// Disconnected returns a receiving channel, that is closed when the device is
// removed.
func (d *Device) Disconnected() <-chan struct{} {
return d.done
}