joycon-rs

Joycon-rs

Test on mac Test on windows Test on ubuntu

A framework for dealing with Nintendo Switch Joy-Con on Rust easily and efficiently.

Joycon-rs provides utility to find communicate with, and operate Joy-Con. Please see the documentation comments for detailed instructions on how to use it.

Joycon-rs is in development and is still incomplete. Please be aware that the update will include breaking changes for the time being. Pardon out dust!

Setup

On macOS or Windows, there are no preparation.

On linux, sudo apt-get install libudev-dev libusb-1.0-0-dev libfox-1.6-dev

# Usage First, add dependency to Cargo.toml

toml [dependencies] joycon_rs = "*"

Then, use prelude on .rs file. use joycon_rs::prelude::*;

Perfect! Now you have Joycon-rs available in code.

### Receive reports For starters, let's take a simple signal from JoyCon. If you have more than one JoyCon, [mspc] can be very helpful.

```norun use joyconrs::prelude::*;

let (tx, rx) = std::sync::mpsc::channel();

JoyConManager::new() .unwrap() .connectedjoycondevices .intoiter() .flatmap(|dev| SimpleJoyConDriver::new(dev)) .tryforeach::<_, JoyConResult<()>>(|driver| { // Change JoyCon to Simple hid mode. let simplehidmode = SimpleHIDMode::new(driver)?;

     let tx = tx.clone();

     // Spawn thread
     std::thread::spawn( move || {
         loop {
             // Forward the report to the main thread
             tx.send(simple_hid_mode.read_input_report()).unwrap();
         }
     });

     Ok(())
 })
 .unwrap();

// Receive reports from threads while let Ok(report) = rx.recv() { // Output report dbg!(report); } ```

### Ser player lights Then, lets deal with player lights.

```norun use joyconrs::prelude::{, lights::};

let (tx, rx) = std::sync::mpsc::channel();

JoyConManager::new() .unwrap() .connectedjoycondevices .intoiter() .flatmap(|dev| SimpleJoyConDriver::new(dev)) .tryforeach::<_, JoyConResult<()>>(|mut driver| { // Set player lights // [SL BUTTON] πŸ“ΈπŸ’‘πŸ“ΈπŸ’‘ [SR BUTTON] driver.setplayerlights(&vec![LightUp::LED1, LightUp::LED3], &vec![Flash::LED0, Flash::LED2]).unwrap(); tx.send(driver.getplayerlights()).unwrap(); Ok(()) }) .unwrap();

// Receive status of player lights while let Ok(Ok(lightstatus)) = rx.recv() { asserteq!( lightstatus.extra.reply, LightsStatus { lightup: vec![LightUp::LED1, LightUp::LED3], flash: vec![Flash::LED0, Flash::LED2], } ) } ```

# Features You can use Joycon-rs for... - Send / Receive raw packets (u8 array) to / from Joy-Con - Receive input to Joy-Con - Receive pushed buttons, and stick directions (one of 8 directions) on every button pressed. - Receive pushed buttons, stick directions (analog value), and 6-Axis sensor at 60Hz. - Get status of Joy-Con - Deal with LED (Player lights)

## Planning - Vibration (Rumble) - Receive NFC/IR data - Deal with HOME light - Deal with Pro Controller - Disconnect / Reconnect