From 4e29afefc4afbce77f7ea0d110d7e844ce411eac Mon Sep 17 00:00:00 2001 From: Narr the Reg Date: Fri, 27 Jan 2023 17:47:47 -0600 Subject: [PATCH] input_common: joycon: Replace ReadSPI vector with span --- .../joycon_protocol/common_protocol.cpp | 18 +++++++++++------ .../helpers/joycon_protocol/common_protocol.h | 20 +++++++++---------- .../joycon_protocol/generic_functions.cpp | 8 ++++---- 3 files changed, 26 insertions(+), 20 deletions(-) diff --git a/src/input_common/helpers/joycon_protocol/common_protocol.cpp b/src/input_common/helpers/joycon_protocol/common_protocol.cpp index 2f0ab6b2a..0ef240344 100644 --- a/src/input_common/helpers/joycon_protocol/common_protocol.cpp +++ b/src/input_common/helpers/joycon_protocol/common_protocol.cpp @@ -22,8 +22,8 @@ void JoyconCommonProtocol::SetNonBlocking() { } DriverResult JoyconCommonProtocol::GetDeviceType(ControllerType& controller_type) { - std::vector buffer; - const auto result = ReadSPI(SpiAddress::DEVICE_TYPE, 1, buffer); + std::array buffer{}; + const auto result = ReadRawSPI(SpiAddress::DEVICE_TYPE, buffer); controller_type = ControllerType::None; if (result == DriverResult::Success) { @@ -148,11 +148,13 @@ DriverResult JoyconCommonProtocol::SendVibrationReport(std::span buffe return SendData(local_buffer); } -DriverResult JoyconCommonProtocol::ReadSPI(SpiAddress addr, u8 size, std::vector& output) { +DriverResult JoyconCommonProtocol::ReadRawSPI(SpiAddress addr, std::span output) { + constexpr std::size_t HeaderSize = 20; constexpr std::size_t MaxTries = 10; + const auto size = output.size(); std::size_t tries = 0; - std::array buffer = {0x00, 0x00, 0x00, 0x00, size}; - std::vector local_buffer(size + 20); + std::array buffer = {0x00, 0x00, 0x00, 0x00, static_cast(size)}; + std::vector local_buffer{}; buffer[0] = static_cast(static_cast(addr) & 0x00FF); buffer[1] = static_cast((static_cast(addr) & 0xFF00) >> 8); @@ -167,8 +169,12 @@ DriverResult JoyconCommonProtocol::ReadSPI(SpiAddress addr, u8 size, std::vector } } while (local_buffer[15] != buffer[0] || local_buffer[16] != buffer[1]); + if (local_buffer.size() < size + HeaderSize) { + return DriverResult::WrongReply; + } + // Remove header from output - output = std::vector(local_buffer.begin() + 20, local_buffer.begin() + 20 + size); + memcpy(output.data(), local_buffer.data() + HeaderSize, size); return DriverResult::Success; } diff --git a/src/input_common/helpers/joycon_protocol/common_protocol.h b/src/input_common/helpers/joycon_protocol/common_protocol.h index 675eacf68..75d3f20a4 100644 --- a/src/input_common/helpers/joycon_protocol/common_protocol.h +++ b/src/input_common/helpers/joycon_protocol/common_protocol.h @@ -97,26 +97,26 @@ public: /** * Reads the SPI memory stored on the joycon * @param Initial address location - * @param size in bytes to be read * @returns output buffer containing the responce */ - DriverResult ReadSPI(SpiAddress addr, u8 size, std::vector& output); + DriverResult ReadRawSPI(SpiAddress addr, std::span output); + /** + * Reads the SPI memory stored on the joycon + * @param Initial address location + * @returns output object containing the responce + */ template - requires(std::is_trivially_copyable_v) - DriverResult ReadSPI(SpiAddress addr, Output& output) { - std::vector buffer; + requires std::is_trivially_copyable_v DriverResult ReadSPI(SpiAddress addr, + Output& output) { + std::array buffer; output = {}; - const auto result = ReadSPI(addr, sizeof(Output), buffer); + const auto result = ReadRawSPI(addr, buffer); if (result != DriverResult::Success) { return result; } - if (buffer.size() != sizeof(Output)) { - return DriverResult::WrongReply; - } - std::memcpy(&output, buffer.data(), sizeof(Output)); return DriverResult::Success; } diff --git a/src/input_common/helpers/joycon_protocol/generic_functions.cpp b/src/input_common/helpers/joycon_protocol/generic_functions.cpp index 4763ba3e6..484c208e6 100644 --- a/src/input_common/helpers/joycon_protocol/generic_functions.cpp +++ b/src/input_common/helpers/joycon_protocol/generic_functions.cpp @@ -71,8 +71,8 @@ DriverResult GenericProtocol::GetBattery(u32& battery_level) { DriverResult GenericProtocol::GetColor(Color& color) { ScopedSetBlocking sb(this); - std::vector buffer; - const auto result = ReadSPI(SpiAddress::COLOR_DATA, 12, buffer); + std::array buffer{}; + const auto result = ReadRawSPI(SpiAddress::COLOR_DATA, buffer); color = {}; if (result == DriverResult::Success) { @@ -87,8 +87,8 @@ DriverResult GenericProtocol::GetColor(Color& color) { DriverResult GenericProtocol::GetSerialNumber(SerialNumber& serial_number) { ScopedSetBlocking sb(this); - std::vector buffer; - const auto result = ReadSPI(SpiAddress::SERIAL_NUMBER, 16, buffer); + std::array buffer{}; + const auto result = ReadRawSPI(SpiAddress::SERIAL_NUMBER, buffer); serial_number = {}; if (result == DriverResult::Success) {