use haply::{ device_model::{ Linear3D, PositionInput }, HaplyDevice };
use std::io::Write;
use std::time::Duration;
use tokio::time::{ interval, sleep };
const HTTP_BASE: &str = "http://localhost:10001";
const WS_URL: &str = "ws://localhost:10001/";
const TARGET_POSITION: Linear3D = Linear3D { x: 0.03, y: -0.1, z: 0.2 };
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let mut device = HaplyDevice::new(HTTP_BASE, WS_URL).await?;
sleep(Duration::from_millis(300)).await;
let state = device.read_state().await?;
let inv = state.inverse3.first().ok_or("No Inverse3 device connected")?;
let id = inv.device_id.clone();
println!("Using Inverse3 device: {}", id);
println!(
"Holding target position: ({:.4}, {:.4}, {:.4}) (Ctrl+C to stop)",
TARGET_POSITION.x,
TARGET_POSITION.y,
TARGET_POSITION.z
);
let mut ticker = interval(Duration::from_millis(10));
let mut tick_count: u32 = 0;
loop {
ticker.tick().await;
tick_count += 1;
device.update_position(
vec![PositionInput {
device_id: id.clone(),
positions: TARGET_POSITION,
}],
Some(false)
).await?;
if tick_count % 10 == 0 {
device.send_force_full_render().await?;
} else {
device.send_command().await?;
}
let state = device.read_state().await?;
if let Some(inv) = state.inverse3.first() {
let pos = inv.state.cursor_position.unwrap_or_default();
let rendered_force = inv.state.current_cursor_force.unwrap_or_default();
print!(
"\rTarget: ({:.4},{:.4},{:.4}) Pos: ({:.4},{:.4},{:.4}) Rendered Force: ({:.4},{:.4},{:.4}) ",
TARGET_POSITION.x,
TARGET_POSITION.y,
TARGET_POSITION.z,
pos.x,
pos.y,
pos.z,
rendered_force.x,
rendered_force.y,
rendered_force.z
);
std::io::stdout().flush().unwrap();
}
}
}