Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
qdot committed Sep 30, 2024
2 parents 827ab02 + 5a164fa commit 9f6d70d
Show file tree
Hide file tree
Showing 19 changed files with 2,192 additions and 165 deletions.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

69 changes: 69 additions & 0 deletions buttplug/src/server/device/protocol/amorelie_joy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Buttplug Rust Source Code File - See https://buttplug.io for more info.
//
// Copyright 2016-2024 Nonpolynomial Labs LLC. All rights reserved.
//
// Licensed under the BSD 3-Clause license. See LICENSE file in the project root
// for full license information.

use crate::{
core::{errors::ButtplugDeviceError, message::Endpoint},
server::device::{
configuration::{UserDeviceDefinition, UserDeviceIdentifier},
hardware::{Hardware, HardwareCommand, HardwareWriteCmd},
protocol::{
generic_protocol_initializer_setup,
ProtocolCommunicationSpecifier,
ProtocolHandler,
ProtocolIdentifier,
ProtocolInitializer,
},
},
};
use async_trait::async_trait;
use std::sync::Arc;

generic_protocol_initializer_setup!(AmorelieJoy, "amorelie-joy");

#[derive(Default)]
pub struct AmorelieJoyInitializer {}

#[async_trait]
impl ProtocolInitializer for AmorelieJoyInitializer {
async fn initialize(
&mut self,
hardware: Arc<Hardware>,
_: &UserDeviceDefinition,
) -> Result<Arc<dyn ProtocolHandler>, ButtplugDeviceError> {
hardware
.write_value(&HardwareWriteCmd::new(Endpoint::Tx, vec![0x03], false))
.await?;
Ok(Arc::new(AmorelieJoy::default()))
}
}

#[derive(Default)]
pub struct AmorelieJoy {}

impl ProtocolHandler for AmorelieJoy {
fn keepalive_strategy(&self) -> super::ProtocolKeepaliveStrategy {
super::ProtocolKeepaliveStrategy::RepeatLastPacketStrategy
}

fn handle_scalar_vibrate_cmd(
&self,
_index: u32,
scalar: u32,
) -> Result<Vec<HardwareCommand>, ButtplugDeviceError> {
Ok(vec![HardwareWriteCmd::new(
Endpoint::Tx,
[
0x01, // static header
0x01, // pattern (1 = steady),
scalar as u8, // speed 0-100
]
.to_vec(),
false,
)
.into()])
}
}
69 changes: 69 additions & 0 deletions buttplug/src/server/device/protocol/cowgirl_cone.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Buttplug Rust Source Code File - See https://buttplug.io for more info.
//
// Copyright 2016-2024 Nonpolynomial Labs LLC. All rights reserved.
//
// Licensed under the BSD 3-Clause license. See LICENSE file in the project root
// for full license information.

use crate::{
core::{errors::ButtplugDeviceError, message::Endpoint},
server::device::{
configuration::{ProtocolCommunicationSpecifier, UserDeviceDefinition, UserDeviceIdentifier},
hardware::{Hardware, HardwareCommand, HardwareWriteCmd},
protocol::{
generic_protocol_initializer_setup,
ProtocolHandler,
ProtocolIdentifier,
ProtocolInitializer,
},
},
util::sleep,
};
use async_trait::async_trait;
use std::{sync::Arc, time::Duration};

generic_protocol_initializer_setup!(CowgirlCone, "cowgirl-cone");

#[derive(Default)]
pub struct CowgirlConeInitializer {}

#[async_trait]
impl ProtocolInitializer for CowgirlConeInitializer {
async fn initialize(
&mut self,
hardware: Arc<Hardware>,
_: &UserDeviceDefinition,
) -> Result<Arc<dyn ProtocolHandler>, ButtplugDeviceError> {
hardware
.write_value(&HardwareWriteCmd::new(
Endpoint::Tx,
vec![0xaa, 0x56, 0x00, 0x00],
false,
))
.await?;
sleep(Duration::from_millis(3000)).await;
Ok(Arc::new(CowgirlCone::default()))
}
}

#[derive(Default)]
pub struct CowgirlCone {}

impl ProtocolHandler for CowgirlCone {
fn keepalive_strategy(&self) -> super::ProtocolKeepaliveStrategy {
super::ProtocolKeepaliveStrategy::RepeatLastPacketStrategy
}

fn handle_scalar_vibrate_cmd(
&self,
_index: u32,
scalar: u32,
) -> Result<Vec<HardwareCommand>, ButtplugDeviceError> {
Ok(vec![HardwareWriteCmd::new(
Endpoint::Tx,
vec![0xf1, 0x01, scalar as u8, 0x00],
false,
)
.into()])
}
}
29 changes: 27 additions & 2 deletions buttplug/src/server/device/protocol/joyhub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ async fn delayed_constrict_handler(device: Arc<Hardware>, scalar: u8) {
error!("Delayed JoyHub Constrict command error: {:?}", res.err());
}
}

fn vibes_changed(
old_commands_lock: &RwLock<Vec<Option<(ActuatorType, u32)>>>,
new_commands: &[Option<(ActuatorType, u32)>],
Expand All @@ -70,6 +71,28 @@ fn vibes_changed(
false
}

fn scalar_changed(
old_commands_lock: &RwLock<Vec<Option<(ActuatorType, u32)>>>,
new_commands: &[Option<(ActuatorType, u32)>],
index: usize,
) -> bool {
let old_commands = old_commands_lock.read().expect("locks should work");
if old_commands.len() != new_commands.len() {
return true;
}

if index < old_commands.len() {
if let Some(ocmd) = old_commands[index] {
if let Some(ncmd) = new_commands[index] {
if ocmd.1 != ncmd.1 {
return true;
}
}
}
}
false
}

#[derive(Default)]
pub struct JoyHubInitializer {}

Expand Down Expand Up @@ -123,10 +146,12 @@ impl ProtocolHandler for JoyHub {

if let Some(cmd) = cmd2 {
if cmd.0 == ActuatorType::Constrict {
if vibes_changed(&self.last_cmds, commands, vec![1usize]) {
cmd2 = None;
if !scalar_changed(&self.last_cmds, commands, 1usize) {
// no-op
} else if vibes_changed(&self.last_cmds, commands, vec![1usize]) {
let dev = self.device.clone();
async_manager::spawn(async move { delayed_constrict_handler(dev, cmd.1 as u8).await });
cmd2 = None;
} else {
let mut command_writer = self.last_cmds.write().expect("Locks should work");
*command_writer = commands.to_vec();
Expand Down
35 changes: 31 additions & 4 deletions buttplug/src/server/device/protocol/joyhub_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ async fn delayed_constrict_handler(device: Arc<Hardware>, scalar: u8) {
error!("Delayed JoyHub Constrict command error: {:?}", res.err());
}
}

fn vibes_changed(
old_commands_lock: &RwLock<Vec<Option<(ActuatorType, u32)>>>,
new_commands: &[Option<(ActuatorType, u32)>],
Expand All @@ -63,6 +64,28 @@ fn vibes_changed(
false
}

fn scalar_changed(
old_commands_lock: &RwLock<Vec<Option<(ActuatorType, u32)>>>,
new_commands: &[Option<(ActuatorType, u32)>],
index: usize,
) -> bool {
let old_commands = old_commands_lock.read().expect("locks should work");
if old_commands.len() != new_commands.len() {
return true;
}

if index < old_commands.len() {
if let Some(ocmd) = old_commands[index] {
if let Some(ncmd) = new_commands[index] {
if ocmd.1 != ncmd.1 {
return true;
}
}
}
}
false
}

#[derive(Default)]
pub struct JoyHubV2Initializer {}

Expand Down Expand Up @@ -116,10 +139,12 @@ impl ProtocolHandler for JoyHubV2 {

if let Some(cmd) = cmd2 {
if cmd.0 == ActuatorType::Constrict {
if vibes_changed(&self.last_cmds, commands, vec![1usize]) {
cmd2 = None;
if !scalar_changed(&self.last_cmds, commands, 1usize) {
// no-op
} else if vibes_changed(&self.last_cmds, commands, vec![1usize]) {
let dev = self.device.clone();
async_manager::spawn(async move { delayed_constrict_handler(dev, cmd.1 as u8).await });
cmd2 = None;
} else {
let mut command_writer = self.last_cmds.write().expect("Locks should work");
*command_writer = commands.to_vec();
Expand All @@ -136,10 +161,12 @@ impl ProtocolHandler for JoyHubV2 {

if let Some(cmd) = cmd3 {
if cmd.0 == ActuatorType::Constrict {
if vibes_changed(&self.last_cmds, commands, vec![2usize]) {
cmd3 = None;
if !scalar_changed(&self.last_cmds, commands, 2usize) {
// no-op
} else if vibes_changed(&self.last_cmds, commands, vec![2usize]) {
let dev = self.device.clone();
async_manager::spawn(async move { delayed_constrict_handler(dev, cmd.1 as u8).await });
cmd3 = None;
} else {
let mut command_writer = self.last_cmds.write().expect("Locks should work");
*command_writer = commands.to_vec();
Expand Down
31 changes: 28 additions & 3 deletions buttplug/src/server/device/protocol/joyhub_v4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,28 @@ fn vibes_changed(
false
}

fn scalar_changed(
old_commands_lock: &RwLock<Vec<Option<(ActuatorType, u32)>>>,
new_commands: &[Option<(ActuatorType, u32)>],
index: usize,
) -> bool {
let old_commands = old_commands_lock.read().expect("locks should work");
if old_commands.len() != new_commands.len() {
return true;
}

if index < old_commands.len() {
if let Some(ocmd) = old_commands[index] {
if let Some(ncmd) = new_commands[index] {
if ocmd.1 != ncmd.1 {
return true;
}
}
}
}
false
}

#[derive(Default)]
pub struct JoyHubV4Initializer {}

Expand Down Expand Up @@ -115,15 +137,18 @@ impl ProtocolHandler for JoyHubV4 {
} else {
None
};
let cmd3 = if commands.len() > 2 {
let mut cmd3 = if commands.len() > 2 {
commands[2]
} else {
None
};

if let Some(cmd) = cmd3 {
if cmd.0 == ActuatorType::Constrict {
if vibes_changed(&self.last_cmds, commands, vec![2usize]) {
cmd3 = None;
if !scalar_changed(&self.last_cmds, commands, 2usize) {
// no-op
} else if vibes_changed(&self.last_cmds, commands, vec![2usize]) {
let dev = self.device.clone();
async_manager::spawn(async move { delayed_constrict_handler(dev, cmd.1 as u8).await });
} else {
Expand Down Expand Up @@ -157,7 +182,7 @@ impl ProtocolHandler for JoyHubV4 {
0x03,
cmd1.unwrap_or((ActuatorType::Vibrate, 0)).1 as u8,
0x00,
0x00,
cmd3.unwrap_or((ActuatorType::Vibrate, 0)).1 as u8,
cmd2.unwrap_or((ActuatorType::Rotate, 0)).1 as u8,
0xaa,
],
Expand Down
28 changes: 27 additions & 1 deletion buttplug/src/server/device/protocol/joyhub_v5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ async fn delayed_constrict_handler(device: Arc<Hardware>, scalar: u8) {
error!("Delayed JoyHub Constrict command error: {:?}", res.err());
}
}

fn vibes_changed(
old_commands_lock: &RwLock<Vec<Option<(ActuatorType, u32)>>>,
new_commands: &[Option<(ActuatorType, u32)>],
Expand All @@ -70,6 +71,28 @@ fn vibes_changed(
false
}

fn scalar_changed(
old_commands_lock: &RwLock<Vec<Option<(ActuatorType, u32)>>>,
new_commands: &[Option<(ActuatorType, u32)>],
index: usize,
) -> bool {
let old_commands = old_commands_lock.read().expect("locks should work");
if old_commands.len() != new_commands.len() {
return true;
}

if index < old_commands.len() {
if let Some(ocmd) = old_commands[index] {
if let Some(ncmd) = new_commands[index] {
if ocmd.1 != ncmd.1 {
return true;
}
}
}
}
false
}

#[derive(Default)]
pub struct JoyHubV5Initializer {}

Expand Down Expand Up @@ -118,7 +141,10 @@ impl ProtocolHandler for JoyHubV5 {

if let Some(cmd) = cmd2 {
if cmd.0 == ActuatorType::Constrict {
if vibes_changed(&self.last_cmds, commands, vec![2usize]) {
// cmd2 = None; // Not used again in this protocol variant
if !scalar_changed(&self.last_cmds, commands, 1usize) {
// no-op
} else if vibes_changed(&self.last_cmds, commands, vec![1usize]) {
let dev = self.device.clone();
async_manager::spawn(async move { delayed_constrict_handler(dev, cmd.1 as u8).await });
} else {
Expand Down
Loading

0 comments on commit 9f6d70d

Please sign in to comment.