1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
#![doc(html_favicon_url = "https://raw.githubusercontent.com/KaiseiYokoyama/joycon-rs/master/images/favicon.ico")]
#![doc(html_logo_url = "https://raw.githubusercontent.com/KaiseiYokoyama/joycon-rs/master/images/logo.png")]
//! # 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`
//!
//! ```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.
//!
//! ```no_run
//! 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.
//!
//! ```no_run
//! 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...
//! - Manage Joy-Cons
//! - Connection / Disconnection / Reconnection
//! - [Send] / [Receive] raw packets (u8 array) to / from Joy-Con
//! - [Receive input to Joy-Con][input_report_mode]
//! - [Receive pushed buttons, and stick directions (one of 8 directions) on every button pressed.][SimpleHIDMode<D>]
//! - [Receive pushed buttons, stick directions (analog value), and 6-Axis sensor at 60Hz.][StandardFullMode<D>]
//! - [Get status of Joy-Con][SubCommandMode<D, RD>]
//! - [Deal with LED (Player lights)]
//! - [Vibration (Rumble)]
//!
//! ## Planning
//! - Receive NFC/IR data
//! - Deal with HOME light
//!
//! [Github]: https://github.com/KaiseiYokoyama/joycon-rs
//! [`hidapi-rs`]: https://github.com/ruabmbua/hidapi-rs
//! [`mspc`]: https://doc.rust-lang.org/book/ch16-02-message-passing.html
//! [Send]: joycon/trait.JoyConDriver.html#tymethod.write
//! [Receive]: joycon/trait.JoyConDriver.html#tymethod.read
//! [input_report_mode]: joycon/input_report_mode/index.html
//! [SimpleHIDMode<D>]: joycon/input_report_mode/simple_hid_mode/struct.SimpleHIDMode.html
//! [StandardFullMode<D>]: joycon/input_report_mode/standard_full_mode/struct.StandardFullMode.html
//! [SubCommandMode<D, RD>]: joycon/input_report_mode/sub_command_mode/struct.SubCommandMode.html
//! [Deal with LED (Player lights)]: joycon/lights/index.html
//! [Vibration (Rumble)]:joycon/struct.Rumble.html
extern crate hidapi;
#[macro_use]
extern crate lazy_static;
pub mod joycon;
#[cfg(doctest)]
#[macro_use]
extern crate doc_comment;
#[cfg(doctest)]
doctest!("../README.md");
pub mod prelude {
#[cfg(feature = "use_serde")]
pub(crate) use serde::{Serialize, Deserialize};
pub use hidapi::*;
pub use crossbeam_channel;
pub use crate::result::*;
pub use crate::joycon::*;
}
pub mod result {
// use crate::prelude::SubCommand;
use hidapi::HidError;
#[derive(Debug)]
pub enum JoyConError {
HidApiError(hidapi::HidError),
// SubCommandError(SubCommand),
SubCommandError(u8, Vec<u8>),
JoyConDeviceError(JoyConDeviceError),
JoyConReportError(JoyConReportError),
Disconnected,
}
impl From<hidapi::HidError> for JoyConError {
fn from(e: HidError) -> Self {
JoyConError::HidApiError(e)
}
}
#[derive(Debug)]
pub enum JoyConDeviceError {
InvalidVendorID(u16),
InvalidProductID(u16),
FailedStickParameterLoading,
FailedStickCalibrationLoading,
FailedIMUOffsetsLoading,
FailedIMUCalibrationLoading,
FailedColorLoading,
}
impl From<JoyConDeviceError> for JoyConError {
fn from(e: JoyConDeviceError) -> Self {
JoyConError::JoyConDeviceError(e)
}
}
#[derive(Debug)]
pub enum JoyConReportError {
InvalidSimpleHidReport(InvalidSimpleHIDReport),
InvalidStandardInputReport(InvalidStandardInputReport),
EmptyReport,
}
impl From<JoyConReportError> for JoyConError {
fn from(e: JoyConReportError) -> Self {
JoyConError::JoyConReportError(e)
}
}
#[derive(Debug)]
pub enum InvalidSimpleHIDReport {
InvalidReport(Vec<u8>),
InvalidStickDirection(u8),
}
impl From<InvalidSimpleHIDReport> for JoyConReportError {
fn from(e: InvalidSimpleHIDReport) -> Self {
JoyConReportError::InvalidSimpleHidReport(e)
}
}
impl From<InvalidSimpleHIDReport> for JoyConError {
fn from(e: InvalidSimpleHIDReport) -> Self {
let report_error = JoyConReportError::from(e);
JoyConError::from(report_error)
}
}
#[derive(Debug)]
pub enum InvalidStandardInputReport {
InvalidReport(Vec<u8>),
InvalidExtraReport(Vec<u8>),
Battery(u8),
ConnectionInfo(u8),
InvalidInputReportId(u8),
}
impl From<InvalidStandardInputReport> for JoyConReportError {
fn from(e: InvalidStandardInputReport) -> Self {
JoyConReportError::InvalidStandardInputReport(e)
}
}
impl From<InvalidStandardInputReport> for JoyConError {
fn from(e: InvalidStandardInputReport) -> Self {
let report_error = JoyConReportError::from(e);
JoyConError::from(report_error)
}
}
pub type JoyConResult<T> = Result<T, JoyConError>;
}
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
assert_eq!(2 + 2, 4);
}
}