From ab54ec7371e2d079667709a3babe48df23bf604a Mon Sep 17 00:00:00 2001 From: blackspherefollower Date: Wed, 28 Feb 2024 14:40:36 +0000 Subject: [PATCH] fix: Correct motorbunny rotation feature Porting the following changes from C#: https://github.com/buttplugio/buttplug-csharp/commit/1421284a78b36128d1b915da2768a1a6db52e129 https://github.com/buttplugio/buttplug-csharp/commit/f7db350c963fec23fd711dcad8c91e4cb0e7cbc5 --- .../buttplug-device-config.json | 6 +- .../buttplug-device-config.yml | 6 +- .../src/server/device/protocol/motorbunny.rs | 25 +++++++ buttplug/tests/test_device_protocols.rs | 4 + .../test_motorbunny_protocol.yaml | 74 +++++++++++++++++++ 5 files changed, 109 insertions(+), 6 deletions(-) create mode 100644 buttplug/tests/util/device_test/device_test_case/test_motorbunny_protocol.yaml diff --git a/buttplug/buttplug-device-config/buttplug-device-config.json b/buttplug/buttplug-device-config/buttplug-device-config.json index f8725727..a3d3ebcf 100644 --- a/buttplug/buttplug-device-config/buttplug-device-config.json +++ b/buttplug/buttplug-device-config/buttplug-device-config.json @@ -7272,7 +7272,7 @@ } }, "defaults": { - "name": "Meeese Device", + "name": "Meese Device", "messages": { "ScalarCmd": [ { @@ -7297,13 +7297,13 @@ "identifier": [ "Meese-V389" ], - "name": "Meeese Tera" + "name": "Meese Tera" }, { "identifier": [ "Meese-cd" ], - "name": "Meeese Modo", + "name": "Meese Modo", "messages": { "ScalarCmd": [ { diff --git a/buttplug/buttplug-device-config/buttplug-device-config.yml b/buttplug/buttplug-device-config/buttplug-device-config.yml index e958099c..3fa68606 100644 --- a/buttplug/buttplug-device-config/buttplug-device-config.yml +++ b/buttplug/buttplug-device-config/buttplug-device-config.yml @@ -3572,7 +3572,7 @@ protocols: 0000ffe0-0000-1000-8000-00805f9b34fb: tx: 0000ffe1-0000-1000-8000-00805f9b34fb defaults: - name: Meeese Device + name: Meese Device messages: ScalarCmd: - StepRange: [0, 10] @@ -3582,10 +3582,10 @@ protocols: configurations: - identifier: - Meese-V389 - name: Meeese Tera + name: Meese Tera - identifier: - Meese-cd - name: Meeese Modo + name: Meese Modo messages: ScalarCmd: - StepRange: [0, 10] diff --git a/buttplug/src/server/device/protocol/motorbunny.rs b/buttplug/src/server/device/protocol/motorbunny.rs index 37b9d9c8..05e5c799 100644 --- a/buttplug/src/server/device/protocol/motorbunny.rs +++ b/buttplug/src/server/device/protocol/motorbunny.rs @@ -54,4 +54,29 @@ impl ProtocolHandler for Motorbunny { ) .into()]) } + + fn handle_rotate_cmd( + &self, + commands: &[Option<(u32, bool)>], + ) -> Result, ButtplugDeviceError> { + let rotate = commands[0].unwrap_or((0, false)); + let mut command_vec: Vec; + if rotate.0 == 0 { + command_vec = vec![0xa0, 0x00, 0x00, 0x00, 0x00, 0xec]; + } else { + command_vec = vec![0xfa]; + let mut rotate_command = vec![if rotate.1 { 0x2a } else { 0x29 }, rotate.0 as u8].repeat(7); + let crc = rotate_command + .iter() + .fold(0u8, |a, b| a.overflowing_add(*b).0); + command_vec.append(&mut rotate_command); + command_vec.append(&mut vec![crc, 0xec]); + } + Ok(vec![HardwareWriteCmd::new( + Endpoint::Tx, + command_vec, + false, + ) + .into()]) + } } diff --git a/buttplug/tests/test_device_protocols.rs b/buttplug/tests/test_device_protocols.rs index db7363e4..ea53f445 100644 --- a/buttplug/tests/test_device_protocols.rs +++ b/buttplug/tests/test_device_protocols.rs @@ -98,6 +98,7 @@ async fn load_test_case(test_file: &str) -> DeviceTestCase { #[test_case("test_joyhub_protocol.yaml" ; "JoyHub Protocol")] #[test_case("test_itoys_protocol.yaml" ; "iToys Protocol")] #[test_case("test_leten_protocol.yaml" ; "Leten Protocol")] +#[test_case("test_motorbunny_protocol.yaml" ; "Motorbunny Protocol")] #[tokio::test] async fn test_device_protocols_embedded_v3(test_file: &str) { //tracing_subscriber::fmt::init(); @@ -185,6 +186,7 @@ async fn test_device_protocols_embedded_v3(test_file: &str) { #[test_case("test_joyhub_protocol.yaml" ; "JoyHub Protocol")] #[test_case("test_itoys_protocol.yaml" ; "iToys Protocol")] #[test_case("test_leten_protocol.yaml" ; "Leten Protocol")] +#[test_case("test_motorbunny_protocol.yaml" ; "Motorbunny Protocol")] #[tokio::test] async fn test_device_protocols_json_v3(test_file: &str) { //tracing_subscriber::fmt::init(); @@ -250,6 +252,7 @@ async fn test_device_protocols_json_v3(test_file: &str) { #[test_case("test_foreo_protocol.yaml" ; "Foreo Protocol")] #[test_case("test_itoys_protocol.yaml" ; "iToys Protocol")] #[test_case("test_leten_protocol.yaml" ; "Leten Protocol")] +#[test_case("test_motorbunny_protocol.yaml" ; "Motorbunny Protocol")] #[tokio::test] async fn test_device_protocols_embedded_v2(test_file: &str) { util::device_test::client::client_v2::run_embedded_test_case(&load_test_case(test_file).await) @@ -314,6 +317,7 @@ async fn test_device_protocols_embedded_v2(test_file: &str) { #[test_case("test_foreo_protocol.yaml" ; "Foreo Protocol")] #[test_case("test_itoys_protocol.yaml" ; "iToys Protocol")] #[test_case("test_leten_protocol.yaml" ; "Leten Protocol")] +#[test_case("test_motorbunny_protocol.yaml" ; "Motorbunny Protocol")] #[tokio::test] async fn test_device_protocols_json_v2(test_file: &str) { util::device_test::client::client_v2::run_json_test_case(&load_test_case(test_file).await).await; diff --git a/buttplug/tests/util/device_test/device_test_case/test_motorbunny_protocol.yaml b/buttplug/tests/util/device_test/device_test_case/test_motorbunny_protocol.yaml new file mode 100644 index 00000000..6ad36293 --- /dev/null +++ b/buttplug/tests/util/device_test/device_test_case/test_motorbunny_protocol.yaml @@ -0,0 +1,74 @@ +devices: + - identifier: + name: "MB Controller" + expected_name: "Motorbunny Classic" +device_commands: + - !Messages + device_index: 0 + messages: + - !Vibrate + - Index: 0 + Speed: 0.5 + - !Commands + device_index: 0 + commands: + - !Write + endpoint: tx + data: [0xff, 0x80, 0x14, 0x80, 0x14, 0x80, 0x14, 0x80, 0x14, 0x80, 0x14, 0x80, 0x14, 0x80, 0x14, 0x0c, 0xec] + write_with_response: false + - !Messages + device_index: 0 + messages: + - !Vibrate + - Index: 0 + Speed: 0.75 + - !Commands + device_index: 0 + commands: + - !Write + endpoint: tx + data: [0xff, 0xC0, 0x14, 0xC0, 0x14, 0xC0, 0x14, 0xC0, 0x14, 0xC0, 0x14, 0xC0, 0x14, 0xC0, 0x14, 0xcc, 0xec] + write_with_response: false + - !Messages + device_index: 0 + messages: + - !Rotate + - Index: 0 + Speed: 0.5 + Clockwise: true + - !Commands + device_index: 0 + commands: + - !Write + endpoint: tx + data: [0xfa, 0x2a, 0x80, 0x2a, 0x80, 0x2a, 0x80, 0x2a, 0x80, 0x2a, 0x80, 0x2a, 0x80, 0x2a, 0x80, 0xa6, 0xec] + write_with_response: false + - !Messages + device_index: 0 + messages: + - !Rotate + - Index: 0 + Speed: 0.75 + Clockwise: false + - !Commands + device_index: 0 + commands: + - !Write + endpoint: tx + data: [0xfa, 0x29, 0xC0, 0x29, 0xC0, 0x29, 0xC0, 0x29, 0xC0, 0x29, 0xC0, 0x29, 0xC0, 0x29, 0xC0, 0x5F, 0xec] + write_with_response: false + - !Messages + device_index: 0 + messages: + - !Stop + - !Commands + device_index: 0 + commands: + - !Write + endpoint: tx + data: [ 0xf0, 0x00, 0x00, 0x00, 0x00, 0xec ] + write_with_response: false + - !Write + endpoint: tx + data: [ 0xa0, 0x00, 0x00, 0x00, 0x00, 0xec ] + write_with_response: false