input_common: joycon: Remove Magic numbers from generic functions
This commit is contained in:
@@ -54,12 +54,10 @@ DriverResult GenericProtocol::EnableImu(bool enable) {
|
|||||||
return SendSubCommand(SubCommand::ENABLE_IMU, buffer);
|
return SendSubCommand(SubCommand::ENABLE_IMU, buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
DriverResult GenericProtocol::SetImuConfig(GyroSensitivity gsen, GyroPerformance gfrec,
|
DriverResult GenericProtocol::SetImuConfig(const MotionSensitivity& sensitivity) {
|
||||||
AccelerometerSensitivity asen,
|
|
||||||
AccelerometerPerformance afrec) {
|
|
||||||
ScopedSetBlocking sb(this);
|
ScopedSetBlocking sb(this);
|
||||||
const std::array<u8, 4> buffer{static_cast<u8>(gsen), static_cast<u8>(asen),
|
std::array<u8, sizeof(MotionSensitivity)> buffer{};
|
||||||
static_cast<u8>(gfrec), static_cast<u8>(afrec)};
|
memcpy(buffer.data(),&sensitivity,sizeof(MotionSensitivity));
|
||||||
return SendSubCommand(SubCommand::SET_IMU_SENSITIVITY, buffer);
|
return SendSubCommand(SubCommand::SET_IMU_SENSITIVITY, buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -71,15 +69,15 @@ DriverResult GenericProtocol::GetBattery(u32& battery_level) {
|
|||||||
|
|
||||||
DriverResult GenericProtocol::GetColor(Color& color) {
|
DriverResult GenericProtocol::GetColor(Color& color) {
|
||||||
ScopedSetBlocking sb(this);
|
ScopedSetBlocking sb(this);
|
||||||
std::array<u8, 12> buffer{};
|
SpiColor spi_colors{};
|
||||||
const auto result = ReadRawSPI(SpiAddress::COLOR_DATA, buffer);
|
const auto result = ReadSPI(SpiAddress::COLOR_DATA, spi_colors);
|
||||||
|
|
||||||
color = {};
|
color = {};
|
||||||
if (result == DriverResult::Success) {
|
if (result == DriverResult::Success) {
|
||||||
color.body = static_cast<u32>((buffer[0] << 16) | (buffer[1] << 8) | buffer[2]);
|
color.body = SpiColorToRGB(spi_colors.body);
|
||||||
color.buttons = static_cast<u32>((buffer[3] << 16) | (buffer[4] << 8) | buffer[5]);
|
color.buttons = SpiColorToRGB(spi_colors.buttons);
|
||||||
color.left_grip = static_cast<u32>((buffer[6] << 16) | (buffer[7] << 8) | buffer[8]);
|
color.left_grip = SpiColorToRGB(spi_colors.left_grip);
|
||||||
color.right_grip = static_cast<u32>((buffer[9] << 16) | (buffer[10] << 8) | buffer[11]);
|
color.right_grip = SpiColorToRGB(spi_colors.right_grip);
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
@@ -87,15 +85,7 @@ DriverResult GenericProtocol::GetColor(Color& color) {
|
|||||||
|
|
||||||
DriverResult GenericProtocol::GetSerialNumber(SerialNumber& serial_number) {
|
DriverResult GenericProtocol::GetSerialNumber(SerialNumber& serial_number) {
|
||||||
ScopedSetBlocking sb(this);
|
ScopedSetBlocking sb(this);
|
||||||
std::array<u8, 16> buffer{};
|
return ReadSPI(SpiAddress::SERIAL_NUMBER2, serial_number);
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
DriverResult GenericProtocol::GetTemperature(u32& temperature) {
|
DriverResult GenericProtocol::GetTemperature(u32& temperature) {
|
||||||
@@ -133,4 +123,8 @@ DriverResult GenericProtocol::SetLedBlinkPattern(u8 leds) {
|
|||||||
return SetLedPattern(static_cast<u8>(leds << 4));
|
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
|
} // namespace InputCommon::Joycon
|
||||||
|
@@ -51,13 +51,9 @@ public:
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Configures the motion sensor with the specified parameters
|
* Configures the motion sensor with the specified parameters
|
||||||
* @param gsen gyroscope sensor sensitvity in degrees per second
|
* @param sensitivity of the imu sensor for gyro and accelerometer
|
||||||
* @param gfrec gyroscope sensor frequency in hertz
|
|
||||||
* @param asen accelerometer sensitivity in G force
|
|
||||||
* @param afrec accelerometer frequency in hertz
|
|
||||||
*/
|
*/
|
||||||
DriverResult SetImuConfig(GyroSensitivity gsen, GyroPerformance gfrec,
|
DriverResult SetImuConfig(const MotionSensitivity& sensitivity);
|
||||||
AccelerometerSensitivity asen, AccelerometerPerformance afrec);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Request battery level from the device
|
* Request battery level from the device
|
||||||
@@ -110,5 +106,8 @@ public:
|
|||||||
* @returns bit flag containing the led state
|
* @returns bit flag containing the led state
|
||||||
*/
|
*/
|
||||||
DriverResult SetLedBlinkPattern(u8 leds);
|
DriverResult SetLedBlinkPattern(u8 leds);
|
||||||
|
|
||||||
|
private:
|
||||||
|
u32 SpiColorToRGB(std::span<const u8> color) const;
|
||||||
};
|
};
|
||||||
} // namespace InputCommon::Joycon
|
} // namespace InputCommon::Joycon
|
||||||
|
@@ -167,6 +167,7 @@ enum class SpiAddress : u16 {
|
|||||||
PAIRING_INFO = 0x2000,
|
PAIRING_INFO = 0x2000,
|
||||||
SHIPMENT = 0x5000,
|
SHIPMENT = 0x5000,
|
||||||
SERIAL_NUMBER = 0x6000,
|
SERIAL_NUMBER = 0x6000,
|
||||||
|
SERIAL_NUMBER2 = 0x6001, // Ignores first byte
|
||||||
DEVICE_TYPE = 0x6012,
|
DEVICE_TYPE = 0x6012,
|
||||||
FORMAT_VERSION = 0x601B,
|
FORMAT_VERSION = 0x601B,
|
||||||
FACT_IMU_DATA = 0x6020,
|
FACT_IMU_DATA = 0x6020,
|
||||||
@@ -395,6 +396,14 @@ struct MotionCalibration {
|
|||||||
std::array<MotionSensorCalibration, 3> gyro;
|
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
|
// Basic motion data containing data from the sensors and a timestamp in microseconds
|
||||||
struct MotionData {
|
struct MotionData {
|
||||||
float gyro_x{};
|
float gyro_x{};
|
||||||
@@ -456,6 +465,14 @@ struct RingCalibration {
|
|||||||
s16 min_value;
|
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 {
|
struct Color {
|
||||||
u32 body;
|
u32 body;
|
||||||
u32 buttons;
|
u32 buttons;
|
||||||
|
Reference in New Issue
Block a user