diff --git a/src/input_common/helpers/joycon_protocol/generic_functions.cpp b/src/input_common/helpers/joycon_protocol/generic_functions.cpp index 548a4b9e3..ab9eebc1b 100644 --- a/src/input_common/helpers/joycon_protocol/generic_functions.cpp +++ b/src/input_common/helpers/joycon_protocol/generic_functions.cpp @@ -54,12 +54,10 @@ DriverResult GenericProtocol::EnableImu(bool enable) { return SendSubCommand(SubCommand::ENABLE_IMU, buffer); } -DriverResult GenericProtocol::SetImuConfig(GyroSensitivity gsen, GyroPerformance gfrec, - AccelerometerSensitivity asen, - AccelerometerPerformance afrec) { +DriverResult GenericProtocol::SetImuConfig(const MotionSensitivity& sensitivity) { ScopedSetBlocking sb(this); - const std::array buffer{static_cast(gsen), static_cast(asen), - static_cast(gfrec), static_cast(afrec)}; + std::array buffer{}; + memcpy(buffer.data(),&sensitivity,sizeof(MotionSensitivity)); return SendSubCommand(SubCommand::SET_IMU_SENSITIVITY, buffer); } @@ -71,15 +69,15 @@ DriverResult GenericProtocol::GetBattery(u32& battery_level) { DriverResult GenericProtocol::GetColor(Color& color) { ScopedSetBlocking sb(this); - std::array buffer{}; - const auto result = ReadRawSPI(SpiAddress::COLOR_DATA, buffer); + SpiColor spi_colors{}; + const auto result = ReadSPI(SpiAddress::COLOR_DATA, spi_colors); color = {}; if (result == DriverResult::Success) { - color.body = static_cast((buffer[0] << 16) | (buffer[1] << 8) | buffer[2]); - color.buttons = static_cast((buffer[3] << 16) | (buffer[4] << 8) | buffer[5]); - color.left_grip = static_cast((buffer[6] << 16) | (buffer[7] << 8) | buffer[8]); - color.right_grip = static_cast((buffer[9] << 16) | (buffer[10] << 8) | buffer[11]); + color.body = SpiColorToRGB(spi_colors.body); + color.buttons = SpiColorToRGB(spi_colors.buttons); + color.left_grip = SpiColorToRGB(spi_colors.left_grip); + color.right_grip = SpiColorToRGB(spi_colors.right_grip); } return result; @@ -87,15 +85,7 @@ DriverResult GenericProtocol::GetColor(Color& color) { DriverResult GenericProtocol::GetSerialNumber(SerialNumber& serial_number) { ScopedSetBlocking sb(this); - std::array buffer{}; - const auto result = ReadRawSPI(SpiAddress::SERIAL_NUMBER, buffer); - - serial_number = {}; - if (result == DriverResult::Success) { - memcpy(serial_number.data(), buffer.data() + 1, sizeof(SerialNumber)); - } - - return result; + return ReadSPI(SpiAddress::SERIAL_NUMBER2, serial_number); } DriverResult GenericProtocol::GetTemperature(u32& temperature) { @@ -133,4 +123,8 @@ DriverResult GenericProtocol::SetLedBlinkPattern(u8 leds) { return SetLedPattern(static_cast(leds << 4)); } +u32 GenericProtocol::SpiColorToRGB(std::span color) const { + return static_cast((color[0] << 16) | (color[1] << 8) | color[2]); +} + } // namespace InputCommon::Joycon diff --git a/src/input_common/helpers/joycon_protocol/generic_functions.h b/src/input_common/helpers/joycon_protocol/generic_functions.h index 424831e81..47cb1ab98 100644 --- a/src/input_common/helpers/joycon_protocol/generic_functions.h +++ b/src/input_common/helpers/joycon_protocol/generic_functions.h @@ -51,13 +51,9 @@ public: /** * Configures the motion sensor with the specified parameters - * @param gsen gyroscope sensor sensitvity in degrees per second - * @param gfrec gyroscope sensor frequency in hertz - * @param asen accelerometer sensitivity in G force - * @param afrec accelerometer frequency in hertz + * @param sensitivity of the imu sensor for gyro and accelerometer */ - DriverResult SetImuConfig(GyroSensitivity gsen, GyroPerformance gfrec, - AccelerometerSensitivity asen, AccelerometerPerformance afrec); + DriverResult SetImuConfig(const MotionSensitivity& sensitivity); /** * Request battery level from the device @@ -110,5 +106,8 @@ public: * @returns bit flag containing the led state */ DriverResult SetLedBlinkPattern(u8 leds); + +private: + u32 SpiColorToRGB(std::span color) const; }; } // namespace InputCommon::Joycon diff --git a/src/input_common/helpers/joycon_protocol/joycon_types.h b/src/input_common/helpers/joycon_protocol/joycon_types.h index b91934990..304e9c801 100644 --- a/src/input_common/helpers/joycon_protocol/joycon_types.h +++ b/src/input_common/helpers/joycon_protocol/joycon_types.h @@ -167,6 +167,7 @@ enum class SpiAddress : u16 { PAIRING_INFO = 0x2000, SHIPMENT = 0x5000, SERIAL_NUMBER = 0x6000, + SERIAL_NUMBER2 = 0x6001, // Ignores first byte DEVICE_TYPE = 0x6012, FORMAT_VERSION = 0x601B, FACT_IMU_DATA = 0x6020, @@ -395,6 +396,14 @@ struct MotionCalibration { std::array gyro; }; +struct MotionSensitivity { + GyroSensitivity gyro_sensitivity{}; + AccelerometerSensitivity accel_sensitivity{}; + GyroPerformance gyro_performance{}; + AccelerometerPerformance accel_performance{}; +}; +static_assert(sizeof(MotionSensitivity) == 0x4, "MotionSensitivity is an invalid size"); + // Basic motion data containing data from the sensors and a timestamp in microseconds struct MotionData { float gyro_x{}; @@ -456,6 +465,14 @@ struct RingCalibration { s16 min_value; }; +struct SpiColor { + std::array body; + std::array buttons; + std::array left_grip; + std::array right_grip; +}; +static_assert(sizeof(SpiColor) == 0xC, "SpiColor is an invalid size"); + struct Color { u32 body; u32 buttons;