Buttplug sex toy control library
1// Buttplug Rust Source Code File - See https://buttplug.io for more info.
2//
3// Copyright 2016-2024 Nonpolynomial Labs LLC. All rights reserved.
4//
5// Licensed under the BSD 3-Clause license. See LICENSE file in the project root
6// for full license information.
7
8use std::sync::atomic::{AtomicU8, Ordering};
9
10use uuid::{Uuid, uuid};
11
12use crate::device::{
13 hardware::{HardwareCommand, HardwareWriteCmd},
14 protocol::{ProtocolHandler, generic_protocol_setup},
15};
16use buttplug_core::errors::ButtplugDeviceError;
17use buttplug_server_device_config::Endpoint;
18
19const MAGIC_MOTION_2_PROTOCOL_UUID: Uuid = uuid!("4d6e9297-c57e-4ce7-a63c-24cc7d117a47");
20
21generic_protocol_setup!(MagicMotionV2, "magic-motion-2");
22
23pub struct MagicMotionV2 {
24 speeds: [AtomicU8; 2],
25}
26
27impl Default for MagicMotionV2 {
28 fn default() -> Self {
29 Self {
30 speeds: [AtomicU8::new(0), AtomicU8::new(0)],
31 }
32 }
33}
34
35impl ProtocolHandler for MagicMotionV2 {
36 fn handle_output_vibrate_cmd(
37 &self,
38 feature_index: u32,
39 _feature_id: Uuid,
40 speed: u32,
41 ) -> Result<Vec<HardwareCommand>, ButtplugDeviceError> {
42 self.speeds[feature_index as usize].store(speed as u8, Ordering::Relaxed);
43 let data = vec![
44 0x10,
45 0xff,
46 0x04,
47 0x0a,
48 0x32,
49 0x0a,
50 0x00,
51 0x04,
52 0x08,
53 self.speeds[0].load(Ordering::Relaxed),
54 0x64,
55 0x00,
56 0x04,
57 0x08,
58 self.speeds[1].load(Ordering::Relaxed),
59 0x64,
60 0x01,
61 ];
62 Ok(vec![
63 HardwareWriteCmd::new(&[MAGIC_MOTION_2_PROTOCOL_UUID], Endpoint::Tx, data, false).into(),
64 ])
65 }
66}