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 crate::device::{
11 hardware::{HardwareCommand, HardwareWriteCmd},
12 protocol::{ProtocolHandler, generic_protocol_setup},
13};
14use buttplug_core::errors::ButtplugDeviceError;
15use buttplug_server_device_config::Endpoint;
16
17generic_protocol_setup!(Zalo, "zalo");
18
19#[derive(Default)]
20pub struct Zalo {
21 speeds: [AtomicU8; 2],
22}
23
24impl ProtocolHandler for Zalo {
25 fn handle_output_vibrate_cmd(
26 &self,
27 feature_index: u32,
28 feature_id: uuid::Uuid,
29 speed: u32,
30 ) -> Result<Vec<HardwareCommand>, ButtplugDeviceError> {
31 self.speeds[feature_index as usize].store(speed as u8, Ordering::Relaxed);
32 let speed0: u8 = self.speeds[0].load(Ordering::Relaxed);
33 let speed1: u8 = self.speeds[1].load(Ordering::Relaxed);
34 Ok(vec![
35 HardwareWriteCmd::new(
36 &[feature_id],
37 Endpoint::Tx,
38 vec![
39 if speed0 == 0 && speed1 == 0 {
40 0x02
41 } else {
42 0x01
43 },
44 if speed0 == 0 { 0x01 } else { speed0 },
45 if speed1 == 0 { 0x01 } else { speed1 },
46 ],
47 true,
48 )
49 .into(),
50 ])
51 }
52}