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
use crate::create_buttplug_protocol;
create_buttplug_protocol!(
LovehoneyDesire,
true,
(),
((VibrateCmd, {
// Store off result before the match, so we drop the lock ASAP.
let result = self.manager.lock().await.update_vibration(msg, false);
match result {
Ok(cmds_option) => {
if let Some(cmds) = cmds_option {
// The Lovehoney Desire has 2 types of commands
//
// - Set both motors with one command
// - Set each motor separately
//
// We'll need to check what we got back and write our
// commands accordingly.
//
// Neat way of checking if everything is the same via
// https://sts10.github.io/2019/06/06/is-all-equal-function.html.
//
// Just make sure we're not matching on None, 'cause if
// that's the case we ain't got shit to do.
if !cmds[0].is_none() && cmds.windows(2).all(|w| w[0] == w[1]) {
device
.write_value(DeviceWriteCmd::new(
Endpoint::Tx,
vec![0xF3, 0, cmds[0].unwrap() as u8],
false,
))
.await?;
return Ok(messages::Ok::default().into());
}
// We have differening values. Set each motor separately.
let mut i = 1;
for cmd in cmds {
if let Some(speed) = cmd {
device
.write_value(DeviceWriteCmd::new(
Endpoint::Tx,
vec![0xF3, i, speed as u8],
false,
))
.await?;
}
i += 1;
}
}
Ok(messages::Ok::default().into())
}
Err(e) => Err(e),
}
}))
);
#[cfg(test)]
mod test {
use crate::{
core::messages::{StopDeviceCmd, VibrateCmd, VibrateSubcommand},
device::{
device::{DeviceImplCommand, DeviceWriteCmd},
Endpoint,
},
test::{check_recv_value, TestDevice},
};
use async_std::task;
#[test]
pub fn test_lovehoney_desire_protocol() {
task::block_on(async move {
let (mut device, test_device) =
TestDevice::new_bluetoothle_test_device("PROSTATE VIBE")
.await
.unwrap();
let (_, command_receiver) = test_device.get_endpoint_channel_clone(&Endpoint::Tx).await;
// If we send one speed to one motor, we should only see one output.
device
.parse_message(&VibrateCmd::new(0, vec![VibrateSubcommand::new(0, 0.5)]).into())
.await
.unwrap();
check_recv_value(
&command_receiver,
DeviceImplCommand::Write(DeviceWriteCmd::new(
Endpoint::Tx,
vec![0xF3, 0x1, 0x3f],
false,
)),
)
.await;
assert!(command_receiver.is_empty());
// If we send the same speed to each motor, we should only get one command.
device
.parse_message(
&VibrateCmd::new(
0,
vec![
VibrateSubcommand::new(0, 0.1),
VibrateSubcommand::new(1, 0.1),
],
)
.into(),
)
.await
.unwrap();
check_recv_value(
&command_receiver,
DeviceImplCommand::Write(DeviceWriteCmd::new(
Endpoint::Tx,
vec![0xF3, 0x0, 0x0c],
false,
)),
)
.await;
assert!(command_receiver.is_empty());
// If we send different commands to both motors, we should get 2 different commands, each with an index.
device
.parse_message(
&VibrateCmd::new(
0,
vec![
VibrateSubcommand::new(0, 0.0),
VibrateSubcommand::new(1, 0.5),
],
)
.into(),
)
.await
.unwrap();
check_recv_value(
&command_receiver,
DeviceImplCommand::Write(DeviceWriteCmd::new(
Endpoint::Tx,
vec![0xF3, 0x01, 0x00],
false,
)),
)
.await;
check_recv_value(
&command_receiver,
DeviceImplCommand::Write(DeviceWriteCmd::new(
Endpoint::Tx,
vec![0xF3, 0x02, 0x3f],
false,
)),
)
.await;
assert!(command_receiver.is_empty());
device
.parse_message(&StopDeviceCmd::new(0).into())
.await
.unwrap();
check_recv_value(
&command_receiver,
DeviceImplCommand::Write(DeviceWriteCmd::new(
Endpoint::Tx,
vec![0xF3, 0x02, 0x0],
false,
)),
)
.await;
assert!(command_receiver.is_empty());
});
}
}