MCPcopy Create free account
hub / github.com/buttplugio/buttplug / main

Function main

examples/src/bin/device_control.rs:23–124  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

21
22#[tokio::main]
23async fn main() -> anyhow::Result<()> {
24 let connector = ButtplugRemoteClientConnector::<
25 ButtplugWebsocketClientTransport,
26 ButtplugClientJSONSerializer,
27 >::new(ButtplugWebsocketClientTransport::new_insecure_connector(
28 "ws://127.0.0.1:12345",
29 ));
30
31 let client = ButtplugClient::new("Example Client");
32 client.connect(connector).await?;
33
34 println!("Connected!");
35
36 // You usually shouldn't run Start/Stop scanning back-to-back like
37 // this, but with TestDevice we know our device will be found when we
38 // call StartScanning, so we can get away with it.
39 client.start_scanning().await?;
40 client.stop_scanning().await?;
41 println!("Client currently knows about these devices:");
42 let mut device_index: i32 = -1;
43 for (i, device) in client.devices() {
44 device_index = i as i32;
45 println!("- {}", device.name());
46 }
47 wait_for_input().await;
48
49 for (_, device) in client.devices() {
50 println!("{} supports these outputs:", device.name());
51 for output_type in OutputType::iter() {
52 for feature in device.device_features().values() {
53 if feature.feature().contains_output(output_type) {
54 println!("- {}", output_type);
55 }
56 }
57 }
58 }
59
60 println!("Sending commands");
61
62 // Now that we know the message types for our connected device, we
63 // can send a message over! Seeing as we want to stick with the
64 // modern generic messages, we'll go with VibrateCmd.
65 //
66 // There's a couple of ways to send this message.
67 let devices = client.devices();
68 let test_client_device = devices.get(&(device_index as u32)).unwrap();
69
70 // We can use the convenience functions on ButtplugClientDevice to
71 // send the message. This version sets all of the motors on a
72 // vibrating device to the same speed.
73 test_client_device
74 .run_output(&ClientDeviceOutputCommand::Vibrate(
75 ClientDeviceCommandValue::Percent(0.5f64),
76 ))
77 .await?;
78
79 // If we wanted to just set one motor on and the other off, we could
80 // try this version that uses an array. It'll throw an exception if

Callers

nothing calls this directly

Calls 10

OkClass · 0.85
outputsMethod · 0.80
wait_for_inputFunction · 0.70
connectMethod · 0.45
start_scanningMethod · 0.45
stop_scanningMethod · 0.45
devicesMethod · 0.45
contains_outputMethod · 0.45
run_outputMethod · 0.45
disconnectMethod · 0.45

Tested by

no test coverage detected