Skip to content
This repository has been archived by the owner on Feb 3, 2023. It is now read-only.

Commit

Permalink
examples: Update Rust examples for buttplug v5
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonweeks authored and qdot committed Nov 22, 2022
1 parent 5e0b2d1 commit 4ca2854
Show file tree
Hide file tree
Showing 8 changed files with 1,179 additions and 1,037 deletions.
2,129 changes: 1,125 additions & 1,004 deletions examples/rust/Cargo.lock

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions examples/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
name = "buttplug-developer-guide-examples"
version = "0.1.0"
authors = ["Melody Horn <[email protected]>", "Kyle Machulis <[email protected]>"]
edition = "2018"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
buttplug = "3.0.0"
buttplug = "5.1.10"
# buttplug = { path = "../../../buttplug-rs/buttplug", features = ["tokio-runtime", "client", "server", "serialize-json", "btleplug-manager", "websockets", "xinput-manager", "serial-manager", "lovense-dongle-manager"] }
anyhow = "1.0.34"
tracing-subscriber = "0.2.15"
futures = "0.3.13"
tokio = { version = "1.4.0", features = ["io-std", "io-util", "rt-multi-thread", "macros"] }
anyhow = "1.0.66"
tracing-subscriber = "0.3.16"
futures = "0.3.25"
tokio = { version = "1.21.2", features = ["io-std", "io-util", "rt-multi-thread", "macros"] }
5 changes: 3 additions & 2 deletions examples/rust/src/bin/async.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use futures::StreamExt;
use buttplug::{
client::{ButtplugClient, ButtplugClientEvent},
connector::{ButtplugRemoteClientConnector, ButtplugWebsocketClientTransport},
core::messages::serializer::ButtplugClientJSONSerializer,
};
use futures::StreamExt;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
Expand All @@ -26,7 +26,8 @@ async fn main() -> anyhow::Result<()> {
// so we'll await that while those (possibly somewhat slow, depending on if
// network is being used and other factors) transfers happen.
let client = ButtplugClient::new("Example Client");
client.connect(connector)
client
.connect(connector)
.await
.expect("Can't connect to Buttplug Server, exiting!");

Expand Down
10 changes: 7 additions & 3 deletions examples/rust/src/bin/connection.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
use tokio::io::{self, BufReader, AsyncBufReadExt};
use buttplug::{
client::{ButtplugClient, ButtplugClientError},
connector::ButtplugInProcessClientConnector,
core::errors::ButtplugError,
};
use tokio::io::{self, AsyncBufReadExt, BufReader};

async fn wait_for_input() {
BufReader::new(io::stdin()).lines().next_line().await.unwrap();
BufReader::new(io::stdin())
.lines()
.next_line()
.await
.unwrap();
}

#[tokio::main]
Expand Down Expand Up @@ -54,7 +58,7 @@ async fn main() -> anyhow::Result<()> {
wait_for_input().await;
return Ok(());
}
}
},
}
};

Expand Down
30 changes: 24 additions & 6 deletions examples/rust/src/bin/device_control.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,33 @@
use tokio::io::{self, BufReader, AsyncBufReadExt};
use buttplug::{
client::{device::VibrateCommand, ButtplugClient, ButtplugClientError, ButtplugClientDeviceMessageType},
client::{
device::VibrateCommand,
ButtplugClient,
ButtplugClientDeviceMessageType,
ButtplugClientError,
},
connector::ButtplugInProcessClientConnector,
test::new_bluetoothle_test_device,
server::comm_managers::test::{
new_bluetoothle_test_device,
TestDeviceCommunicationManagerBuilder,
},
};
use tokio::io::{self, AsyncBufReadExt, BufReader};

async fn wait_for_input() {
BufReader::new(io::stdin()).lines().next_line().await.unwrap();
BufReader::new(io::stdin())
.lines()
.next_line()
.await
.unwrap();
}

#[tokio::main]
async fn main() -> anyhow::Result<()> {
let connector = ButtplugInProcessClientConnector::default();
let server = connector.server_ref();
server.add_test_comm_manager()?;
server
.device_manager()
.add_comm_manager(TestDeviceCommunicationManagerBuilder::default())?;
let client = ButtplugClient::new("Example Client");
client.connect(connector).await?;

Expand Down Expand Up @@ -75,7 +89,11 @@ async fn main() -> anyhow::Result<()> {
.get(&ButtplugClientDeviceMessageType::VibrateCmd)
.and_then(|attributes| attributes.feature_count);

println!("{} has {} vibrators.", test_client_device.name, vibrator_count.unwrap_or(0));
println!(
"{} has {} vibrators.",
test_client_device.name,
vibrator_count.unwrap_or(0)
);

test_client_device
.vibrate(VibrateCommand::SpeedVec(vec![1.0, 0.0]))
Expand Down
15 changes: 8 additions & 7 deletions examples/rust/src/bin/device_enumeration.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use tokio::io::{self, BufReader, AsyncBufReadExt};
use buttplug::client::{ButtplugClient, ButtplugClientEvent};
use futures::StreamExt;
use buttplug::{
client::{ButtplugClient, ButtplugClientEvent},
server::ButtplugServerOptions,
};
use tokio::io::{self, AsyncBufReadExt, BufReader};

async fn wait_for_input() {
BufReader::new(io::stdin()).lines().next_line().await.unwrap();
BufReader::new(io::stdin())
.lines()
.next_line()
.await
.unwrap();
}

#[tokio::main]
Expand All @@ -15,7 +16,7 @@ async fn main() -> anyhow::Result<()> {
// of the subtype managers for us (the default features include all of them).
let client = ButtplugClient::new("Example Client");
let mut events = client.event_stream();
client.connect_in_process(&ButtplugServerOptions::default()).await?;
client.connect_in_process(None).await?;

// Set up our DeviceAdded/DeviceRemoved/ScanningFinished event handlers before connecting.
tokio::spawn(async move {
Expand Down
7 changes: 4 additions & 3 deletions examples/rust/src/bin/embedded_connector.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use buttplug::{
client::ButtplugClient,
connector::ButtplugInProcessClientConnector,
server::{comm_managers::btleplug::BtlePlugCommunicationManager, ButtplugServerOptions},
server::comm_managers::btleplug::BtlePlugCommunicationManagerBuilder,
};

#[allow(dead_code)]
Expand All @@ -11,7 +11,8 @@ async fn main_the_hard_way() -> anyhow::Result<()> {
// This is how we add Bluetooth manually. (We could also do this with any other communication manager.)
connector
.server_ref()
.add_comm_manager::<BtlePlugCommunicationManager>()?;
.device_manager()
.add_comm_manager(BtlePlugCommunicationManagerBuilder::default())?;

let client = ButtplugClient::new("Example Client");
client.connect(connector).await?;
Expand All @@ -23,7 +24,7 @@ async fn main_the_hard_way() -> anyhow::Result<()> {
async fn main() -> anyhow::Result<()> {
// This is the easy way, it sets up an embedded server with everything set up automatically
let client = ButtplugClient::new("Example Client");
client.connect_in_process(&ButtplugServerOptions::default()).await?;
client.connect_in_process(None).await?;

Ok(())
}
8 changes: 2 additions & 6 deletions examples/rust/src/bin/logging.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
use buttplug::{
client::ButtplugClient,
server::ButtplugServerOptions,
};
use tracing_subscriber;
use buttplug::client::ButtplugClient;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
Expand All @@ -13,7 +9,7 @@ async fn main() -> anyhow::Result<()> {
// (set it to "Info" or "Debug"), you should see messages about connection
// setup.
let client = ButtplugClient::new("Example Client");
client.connect_in_process(&ButtplugServerOptions::default()).await?;
client.connect_in_process(None).await?;

Ok(())
}

0 comments on commit 4ca2854

Please sign in to comment.