diff --git a/Cargo.toml b/Cargo.toml index ee6096a..55ad2a1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "joycon-rs" -version = "0.5.6" +version = "0.6.0" authors = ["Kaisei Yokoyama "] repository = "https://github.com/KaiseiYokoyama/joycon-rs" edition = "2018" @@ -17,6 +17,10 @@ exclude = ["/images/*"] hidapi-alt-for-hidapi-issue-127 = "1.2.1" crossbeam-channel = "0.4.2" lazy_static = "1.4.0" +serde = { version = "1.0", features = ["derive"] , optional = true } + +[features] +use_serde = ["serde"] [dev-dependencies] doc-comment = "0.3.3" diff --git a/src/joycon/driver/input_report_mode.rs b/src/joycon/driver/input_report_mode.rs index 27e77f8..950b0aa 100644 --- a/src/joycon/driver/input_report_mode.rs +++ b/src/joycon/driver/input_report_mode.rs @@ -34,6 +34,7 @@ mod common { use std::convert::TryFrom; /// Battery level + #[cfg_attr(feature = "use_serde", derive(Serialize, Deserialize))] #[derive(Debug, Clone, Copy, Hash, Eq, PartialEq, Ord, PartialOrd)] pub enum BatteryLevel { Empty, @@ -44,6 +45,7 @@ mod common { } /// Battery info + #[cfg_attr(feature = "use_serde", derive(Serialize, Deserialize))] #[derive(Debug, Clone, Copy, Hash, Eq, PartialEq)] pub struct Battery { pub level: BatteryLevel, @@ -72,6 +74,7 @@ mod common { } /// Device info + #[cfg_attr(feature = "use_serde", derive(Serialize, Deserialize))] #[derive(Debug, Clone, Copy, Hash, Eq, PartialEq)] pub enum Device { JoyCon, @@ -79,6 +82,7 @@ mod common { } /// Connection info + #[cfg_attr(feature = "use_serde", derive(Serialize, Deserialize))] #[derive(Debug, Clone, Copy, Hash, Eq, PartialEq)] pub struct ConnectionInfo { pub device: Device, @@ -104,6 +108,7 @@ mod common { } /// Button status + #[cfg_attr(feature = "use_serde", derive(Serialize, Deserialize))] #[derive(Debug, Clone, Hash, Eq, PartialEq)] pub struct PushedButtons { pub right: Vec, @@ -191,6 +196,7 @@ mod common { } /// Analog stick data + #[cfg_attr(feature = "use_serde", derive(Serialize, Deserialize))] #[derive(Debug, Clone, Hash, Eq, PartialEq)] pub struct AnalogStickData { pub horizontal: u16, @@ -210,6 +216,7 @@ mod common { } /// Common parts of the standard input report + #[cfg_attr(feature = "use_serde", derive(Serialize, Deserialize))] #[derive(Debug, Clone, Hash, Eq, PartialEq)] pub struct CommonReport { pub input_report_id: u8, @@ -303,6 +310,7 @@ pub trait InputReportMode: Sized { fn into_driver(self) -> D; } +#[cfg_attr(feature = "use_serde", derive(Serialize, Deserialize))] /// Standard input report with extra report. pub struct StandardInputReport> { pub common: CommonReport, @@ -537,6 +545,7 @@ pub mod standard_full_mode { use super::*; /// IMU(6-Axis sensor)'s value. + #[cfg_attr(feature = "use_serde", derive(Serialize, Deserialize))] #[derive(Debug, Clone, Copy, PartialEq)] pub struct AxisData { /// Acceleration to X measured @@ -575,6 +584,7 @@ pub mod standard_full_mode { } /// 6-Axis data. 3 frames of 2 groups of 3 Int16LE each. Group is Acc followed by Gyro. + #[cfg_attr(feature = "use_serde", derive(Serialize, Deserialize))] #[derive(Debug, Clone)] pub struct IMUData { pub data: [AxisData; 3] @@ -712,6 +722,7 @@ pub mod simple_hid_mode { use super::*; #[allow(non_camel_case_types)] + #[cfg_attr(feature = "use_serde", derive(Serialize, Deserialize))] #[derive(Copy, Clone, Hash, PartialEq, Eq, Debug)] pub enum SimpleHIDButton { Down, @@ -740,6 +751,7 @@ pub mod simple_hid_mode { }; /// Hold your controller sideways so that SL, SYNC, and SR line up with the screen. Pushing the stick towards a direction in this table will cause that value to be sent. + #[cfg_attr(feature = "use_serde", derive(Serialize, Deserialize))] #[derive(Copy, Clone, Hash, PartialEq, Eq, Debug)] pub enum StickDirection { Up, @@ -777,12 +789,13 @@ pub mod simple_hid_mode { } /// Pushed buttons and stick direction. + #[cfg_attr(feature = "use_serde", derive(Serialize, Deserialize))] #[derive(Debug, Clone)] pub struct SimpleHIDReport { pub input_report_id: u8, pub pushed_buttons: Vec, pub stick_direction: StickDirection, - pub filter_data: [u8; 8], + pub filler_data: [u8; 8], } impl TryFrom<[u8; 12]> for SimpleHIDReport { @@ -826,7 +839,7 @@ pub mod simple_hid_mode { input_report_id, pushed_buttons, stick_direction, - filter_data, + filler_data: filter_data, }) } } diff --git a/src/joycon/mod.rs b/src/joycon/mod.rs index fb0fb6a..83c6a81 100644 --- a/src/joycon/mod.rs +++ b/src/joycon/mod.rs @@ -20,6 +20,7 @@ pub use manager::{JoyConManager, JOYCON_RECEIVER}; use std::sync::Arc; use std::fmt::{Debug, Formatter}; +#[cfg_attr(feature = "use_serde", derive(Serialize, Deserialize))] #[derive(Copy, Clone, Hash, PartialEq, Eq, Debug)] pub enum Buttons { A, diff --git a/src/lib.rs b/src/lib.rs index 3e8392e..47cd79e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -158,6 +158,8 @@ extern crate doc_comment; 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::*;