[][src]Crate joycon_rs

Joycon-rs Library Documentation

Hello, and welcome to joycon-rs documentation.

Joycon-rs is a framework for dealing with Nintendo Switch Joy-Con on Rust easily and efficiently. In a way, this library is a wrapper of hidapi-rs. This is a free and open source library, the source code is available for download on Github.

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!

Usage

First, add dependency to Cargo.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.

use joycon_rs::prelude::*;

let (tx, rx) = std::sync::mpsc::channel();
let _output = std::thread::spawn( move || {
    while let Ok(update) = rx.recv() {
        dbg!(update);
    }
});

let manager = JoyConManager::get_instance();

let devices = {
    let lock = manager.lock();
    match lock {
        Ok(manager) => manager.new_devices(),
        Err(_) => return,
    }
};

devices.iter()
    .flat_map(|dev| SimpleJoyConDriver::new(&dev))
    .try_for_each::<_, JoyConResult<()>>(|driver| {
        // Change JoyCon to Simple hid mode.
        let simple_hid_mode = 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();

Set player lights

Then, lets deal with player lights.

use joycon_rs::prelude::{*, lights::*};
use joycon_rs::joycon::input_report_mode::StandardInputReport;
use joycon_rs::joycon::input_report_mode::sub_command_mode::SubCommandReport;

let (tx, rx) =
    std::sync::mpsc::channel::<JoyConResult<SubCommandReply<StandardInputReport<SubCommandReport<LightsStatus>>>>>();

// Receive status of player lights
std::thread::spawn(move ||{
    while let Ok(Ok(SubCommandReply::Checked(light_status))) = rx.recv() {
        assert_eq!(
            light_status.extra.reply,
            LightsStatus {
                light_up: vec![LightUp::LED1, LightUp::LED3],
                flash: vec![Flash::LED0, Flash::LED2],
            }
        )
    }
});

let manager = JoyConManager::get_instance();

let devices = {
    let lock = manager.lock();
    match lock {
        Ok(manager) => manager.new_devices(),
        Err(_) => return,
    }
};

devices.iter()
    .flat_map(|dev| SimpleJoyConDriver::new(&dev))
    .try_for_each::<_, JoyConResult<()>>(|mut driver| {
        // Set player lights
        // [SL BUTTON] 📸💡📸💡 [SR BUTTON]
        driver.set_player_lights(&vec![LightUp::LED1, LightUp::LED3], &vec![Flash::LED0, Flash::LED2]).unwrap();
        tx.send(driver.get_player_lights()).unwrap();
        Ok(())
    })
    .unwrap();

Features

You can use Joycon-rs for...

Planning

  • Receive NFC/IR data
  • Deal with HOME light

Modules

joycon
prelude
result