input_common: joycon: Remove Magic numbers from generic functions

This commit is contained in:
Narr the Reg 2023-01-30 10:08:16 -06:00
parent e74660673b
commit f629f2722c
3 changed files with 36 additions and 26 deletions

View File

@ -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<u8, 4> buffer{static_cast<u8>(gsen), static_cast<u8>(asen),
static_cast<u8>(gfrec), static_cast<u8>(afrec)};
std::array<u8, sizeof(MotionSensitivity)> 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<u8, 12> 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<u32>((buffer[0] << 16) | (buffer[1] << 8) | buffer[2]);
color.buttons = static_cast<u32>((buffer[3] << 16) | (buffer[4] << 8) | buffer[5]);
color.left_grip = static_cast<u32>((buffer[6] << 16) | (buffer[7] << 8) | buffer[8]);
color.right_grip = static_cast<u32>((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<u8, 16> 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<u8>(leds << 4));
}
u32 GenericProtocol::SpiColorToRGB(std::span<const u8> color) const {
return static_cast<u32>((color[0] << 16) | (color[1] << 8) | color[2]);
}
} // namespace InputCommon::Joycon

View File

@ -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<const u8> color) const;
};
} // namespace InputCommon::Joycon

View File

@ -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<MotionSensorCalibration, 3> 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<u8, 3> body;
std::array<u8, 3> buttons;
std::array<u8, 3> left_grip;
std::array<u8, 3> right_grip;
};
static_assert(sizeof(SpiColor) == 0xC, "SpiColor is an invalid size");
struct Color {
u32 body;
u32 buttons;