Crates.io | wiimote-rs |
lib.rs | wiimote-rs |
version | 0.1.2 |
source | src |
created_at | 2024-04-26 14:15:03.20718 |
updated_at | 2024-06-30 17:07:28.023508 |
description | Rust library to communicate with Wii remotes |
homepage | |
repository | https://github.com/cesmec/wiimote-rs |
max_upload_size | |
id | 1221480 |
size | 104,909 |
A Rust library to communicate with Wii remotes over Bluetooth.
wiimote-rs
is in development and currently supports:
1
+2
buttonsWindows: no additional setup required
Linux: install the following packages on debian-based systems, or their equivalent on other distributions:
sudo apt install libudev-dev libbluetooth-dev clang
macOS: not supported at the moment
Check the examples
directory for full examples.
use wiimote_rs::prelude::*;
fn main() -> WiimoteResult<()> {
let manager = WiimoteManager::get_instance();
let new_devices = {
let manager = manager.lock().unwrap();
manager.new_devices_receiver()
};
new_devices.iter().try_for_each(|device| -> WiimoteResult<()> {
// Do something with the connected Wii remote
Ok(())
})
}
use std::sync::{Arc, Mutex};
use wiimote_rs::prelude::*;
use wiimote_rs::output::{OutputReport, PlayerLedFlags};
fn change_leds(device: Arc<Mutex<WiimoteDevice>>) -> WiimoteResult<()> {
let led_report = OutputReport::PlayerLed(PlayerLedFlags::LED_2 | PlayerLedFlags::LED_3);
device.lock().unwrap().write(&led_report)
}
use std::sync::{Arc, Mutex};
use wiimote_rs::prelude::*;
use wiimote_rs::input::InputReport;
fn read_buttons(device: Arc<Mutex<WiimoteDevice>>) -> WiimoteResult<()> {
let input_report = device.lock().unwrap().read()?;
match input_report {
InputReport::DataReport(_, data) => {
// All data reports except 0x3d contain button data
let buttons = data.buttons();
}
_ => {}
}
Ok(())
}
use std::sync::{Arc, Mutex};
use wiimote_rs::prelude::*;
use wiimote_rs::input::InputReport;
fn read_accelerometer(device: Arc<Mutex<WiimoteDevice>>) -> WiimoteResult<()> {
// The accelerometer calibration can be stored and reused per WiimoteDevice
let accelerometer_calibration = device.lock().unwrap().accelerometer_calibration().clone();
let input_report = device.lock().unwrap().read()?;
match input_report {
// Note that the data report mode needs to be set to a mode that includes accelerometer data such as 0x31
InputReport::DataReport(0x31, wiimote_data) => {
let accelerometer_data = AccelerometerData::from_normal_reporting(&wiimote_data.data);
let (x, y, z) = accelerometer_calibration.get_acceleration(&accelerometer_data);
}
_ => {}
}
Ok(())
}