Compare commits

...

3 Commits

Author SHA1 Message Date
Narr the Reg ab4a961768 add support for grip 2023-02-01 10:34:16 -06:00
Narr the Reg 0d25195f22 alskjd 2023-01-30 18:22:23 -06:00
Narr the Reg f629f2722c input_common: joycon: Remove Magic numbers from generic functions 2023-01-30 10:08:16 -06:00
8 changed files with 184 additions and 45 deletions

View File

@ -46,6 +46,12 @@ void Joycons::Reset() {
}
device->Stop();
}
for (const auto& device : grip_joycons) {
if (!device) {
continue;
}
device->Stop();
}
SDL_hid_exit();
}
@ -61,6 +67,11 @@ void Joycons::Setup() {
PreSetController(GetIdentifier(port, Joycon::ControllerType::Right));
device = std::make_shared<Joycon::JoyconDriver>(port++);
}
port = 0;
for (auto& device : grip_joycons) {
PreSetController(GetIdentifier(port, Joycon::ControllerType::Grip));
device = std::make_shared<Joycon::JoyconDriver>(port++);
}
scan_thread = std::jthread([this](std::stop_token stop_token) { ScanThread(stop_token); });
}
@ -129,6 +140,13 @@ bool Joycons::IsDeviceNew(SDL_hid_device_info* device_info) const {
}
}
break;
case Joycon::ControllerType::Grip:
for (const auto& device : grip_joycons) {
if (is_handle_identical(device)) {
return false;
}
}
break;
default:
return false;
}
@ -199,6 +217,14 @@ std::shared_ptr<Joycon::JoyconDriver> Joycons::GetNextFreeHandle(
return *unconnected_device;
}
}
if (type == Joycon::ControllerType::Grip) {
const auto unconnected_device = std::ranges::find_if(
grip_joycons, [](auto& device) { return !device->IsConnected(); });
if (unconnected_device != grip_joycons.end()) {
return *unconnected_device;
}
}
return nullptr;
}
@ -408,6 +434,14 @@ std::shared_ptr<Joycon::JoyconDriver> Joycons::GetHandle(PadIdentifier identifie
return *matching_device;
}
}
if (type == Joycon::ControllerType::Grip) {
const auto matching_device = std::ranges::find_if(
grip_joycons, [is_handle_active](auto& device) { return is_handle_active(device); });
if (matching_device != grip_joycons.end()) {
return *matching_device;
}
}
return nullptr;
}
@ -455,6 +489,9 @@ std::vector<Common::ParamPackage> Joycons::GetInputDevices() const {
for (const auto& controller : right_joycons) {
add_entry(controller);
}
for (const auto& controller : grip_joycons) {
add_entry(controller);
}
// List dual joycon pairs
for (std::size_t i = 0; i < MaxSupportedControllers; i++) {
@ -666,8 +703,8 @@ std::string Joycons::JoyconName(Joycon::ControllerType type) const {
return "Left Joycon";
case Joycon::ControllerType::Right:
return "Right Joycon";
case Joycon::ControllerType::Pro:
return "Pro Controller";
case Joycon::ControllerType::Grip:
return "Joycon grip <- custom driver";
case Joycon::ControllerType::Dual:
return "Dual Joycon";
default:

View File

@ -106,6 +106,7 @@ private:
// Joycon types are split by type to ease supporting dualjoycon configurations
std::array<std::shared_ptr<Joycon::JoyconDriver>, MaxSupportedControllers> left_joycons{};
std::array<std::shared_ptr<Joycon::JoyconDriver>, MaxSupportedControllers> right_joycons{};
std::array<std::shared_ptr<Joycon::JoyconDriver>, MaxSupportedControllers> grip_joycons{};
};
} // namespace InputCommon

View File

@ -336,7 +336,7 @@ void SDLDriver::InitJoystick(int joystick_index) {
if (Settings::values.enable_joycon_driver) {
if (guid.uuid[5] == 0x05 && guid.uuid[4] == 0x7e &&
(guid.uuid[8] == 0x06 || guid.uuid[8] == 0x07)) {
(guid.uuid[8] == 0x06 || guid.uuid[8] == 0x07 || guid.uuid[8] == 0x0e)) {
LOG_WARNING(Input, "Preferring joycon driver for device index {}", joystick_index);
SDL_JoystickClose(sdl_joystick);
return;

View File

@ -164,6 +164,15 @@ void JoyconDriver::InputThread(std::stop_token stop_token) {
void JoyconDriver::OnNewData(std::span<u8> buffer) {
const auto report_mode = static_cast<ReportMode>(buffer[0]);
std::string out = "";
for (const u8& bb : buffer) {
out += fmt::format("{} ", bb);
}
LOG_ERROR(Input, "{}", out);
// Packages can be a litte bit inconsistent. Average the delta time to provide a smoother motion
// experience
switch (report_mode) {
@ -253,8 +262,8 @@ DriverResult JoyconDriver::SetPollingMode() {
if (motion_enabled && supported_features.motion) {
generic_protocol->EnableImu(true);
generic_protocol->SetImuConfig(gyro_sensitivity, gyro_performance,
accelerometer_sensitivity, accelerometer_performance);
generic_protocol->SetImuConfig({gyro_sensitivity, accelerometer_sensitivity,
gyro_performance, accelerometer_performance});
} else {
generic_protocol->EnableImu(false);
}
@ -543,9 +552,10 @@ void JoyconDriver::SetCallbacks(const JoyconCallbacks& callbacks) {
DriverResult JoyconDriver::GetDeviceType(SDL_hid_device_info* device_info,
ControllerType& controller_type) {
static constexpr std::array<std::pair<u32, ControllerType>, 2> supported_devices{
static constexpr std::array<std::pair<u32, ControllerType>, 3> supported_devices{
std::pair<u32, ControllerType>{0x2006, ControllerType::Left},
{0x2007, ControllerType::Right},
{0x200e, ControllerType::Grip},
};
constexpr u16 nintendo_vendor_id = 0x057e;

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,21 +85,29 @@ 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) {
// Not all devices have temperature sensor
temperature = 25;
return DriverResult::NotSupported;
ScopedSetBlocking sb(this);
SubCommandResponse output{};
std::array<u8, sizeof(ImuRegister)> buffer{};
constexpr ImuRegister temperature_reg{
.address = ImuRegistersAddress::OUT_TEMP,
.size = sizeof(SubCommandResponse::temperature),
};
// TODO: Ensure the IMU sensor is enabled
temperature = 0;
memcpy(buffer.data(), &temperature_reg, sizeof(ImuRegister));
const auto result = SendSubCommand(SubCommand::READ_IMU_REG, buffer, output);
if (result == DriverResult::Success) {
temperature = output.temperature;
}
return result;
}
DriverResult GenericProtocol::GetVersionNumber(FirmwareVersion& version) {
@ -113,12 +119,30 @@ DriverResult GenericProtocol::GetVersionNumber(FirmwareVersion& version) {
return result;
}
DriverResult GenericProtocol::SetHomeLight() {
DriverResult GenericProtocol::SetHomeLight(bool state) {
ScopedSetBlocking sb(this);
static constexpr std::array<u8, 3> buffer{0x0f, 0xf0, 0x00};
static constexpr std::array<u8, 3> buffer{0x00, 0x0a, 0x00};
return SendSubCommand(SubCommand::SET_HOME_LIGHT, buffer);
}
DriverResult GenericProtocol::SetHomeLightBlink(u8 duration) {
ScopedSetBlocking sb(this);
duration = std::clamp<u8>(duration, 0x8, 0xaf);
const std::array<u8, 4> buffer{0x05, duration, 0xf0, 0x01};
return SendSubCommand(SubCommand::SET_HOME_LIGHT, buffer);
}
DriverResult GenericProtocol::SetHomeLightPattern(u8 short_cycle_count, u8 short_cycle_duration, u8 brightness, u8 cycle_count) {
ScopedSetBlocking sb(this);
short_cycle_count = std::min<u8>(short_cycle_count, 0x0f);
short_cycle_duration = std::min<u8>(short_cycle_duration, 0xaf);
brightness = std::min<u8>(brightness, 0x64);
cycle_count = std::min<u8>(cycle_count, 0x0f);
const std::array<u8, 4> buffer{0x05, cycle_count, 0xf0, 0x01};
return SendSubCommand(SubCommand::SET_HOME_LIGHT, buffer);
}
DriverResult GenericProtocol::SetLedBusy() {
return DriverResult::NotSupported;
}
@ -133,4 +157,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
@ -78,22 +74,27 @@ public:
DriverResult GetSerialNumber(SerialNumber& serial_number);
/**
* Request joycon serial number from the device
* @returns 16 byte serial number
* Reads the temperature directly from the IMU sensor registers. IMU sensor needs to be enabled
* for this funtion to work properly
* @returns temperature in degrees
*/
DriverResult GetTemperature(u32& temperature);
/**
* Request joycon serial number from the device
* @returns 16 byte serial number
* @returns 15 byte firmware version
*/
DriverResult GetVersionNumber(FirmwareVersion& version);
/**
* Sets home led behaviour
*/
DriverResult SetHomeLight();
DriverResult SetHomeLight(bool state);
DriverResult SetHomeLightBlink(u8 duration);
DriverResult SetHomeLightPattern(u8 short_cycle_count, u8 short_cycle_duration, u8 brightness,
u8 cycle_count);
/**
* Sets home led into a slow breathing state
*/
@ -110,5 +111,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

@ -29,6 +29,7 @@ enum class ControllerType : u8 {
Left = 0x01,
Right = 0x02,
Pro = 0x03,
Grip = 0x04,
Dual = 0x05, // TODO: Verify this id
LarkHvc1 = 0x07,
LarkHvc2 = 0x08,
@ -167,6 +168,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,
@ -364,6 +366,43 @@ enum class IrRegistersAddress : u16 {
DenoiseColor = 0x6901,
};
// See LSM6DS3 register table
enum class ImuRegistersAddress : u8 {
FUNC_CFG_ACCESS = 0x01,
SENSOR_SYNC_TIME_FRAME = 0x04,
FIFO_CTRL = 0x06,
ORIENT_CFG_G = 0x0b,
INT1_CTRL = 0x0d,
INT2_CTRL = 0x0e,
WHO_AM_I = 0x0f,
ACCEL_GYRO_CONTROL_REG = 0x10,
MASTER_CONFIG = 0x1a,
WAKE_UP_SRC = 0x1b,
TAP_SRC = 0x1c,
D6D_SRC = 0x1d,
STATUS_REG = 0x1e,
OUT_TEMP = 0x20,
OUTX_GYRO = 0x22,
OUTX_ACCEL = 0x28,
SENSORHUB_REG = 0x2e,
FIFO_STATUS = 0x39,
FIFO_DATA_OUT = 0x3e,
TIMESTAMP_REG = 0x40,
STEP_TIMESTAMP = 0x49,
STEP_COUNTER = 0x4b,
SENSORHUB13_REG = 0x4d,
FUNC_SRC = 0x53,
TAP_CFG = 0x58,
TAP_THS_6D = 0x59,
INT_DUR2 = 0x5a,
WAKE_UP_THS = 0x5b,
WAKE_UP_DUR = 0x5c,
FREE_FALL = 0x5d,
MD1_CFG = 0x5e,
MD2_CFG = 0x5f,
OUT_MAG_RAW = 0x66,
};
enum class ExternalDeviceId : u16 {
RingController = 0x2000,
Starlink = 0x2800,
@ -395,6 +434,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 +503,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;
@ -608,6 +663,11 @@ struct IrsWriteRegisters {
static_assert(sizeof(IrsWriteRegisters) == 0x26, "IrsWriteRegisters is an invalid size");
#pragma pack(pop)
struct ImuRegister {
ImuRegistersAddress address;
u8 size;
};
struct FirmwareVersion {
u8 major;
u8 minor;
@ -668,6 +728,7 @@ struct SubCommandResponse {
SpiAddress spi_address; // Reply from SPI_FLASH_READ subcommand
ExternalDeviceId external_device_id; // Reply from GET_EXTERNAL_DEVICE_INFO subcommand
DeviceInfo device_info; // Reply from REQ_DEV_INFO subcommand
u16 temperature; // Reply from READ_IMU_REG->OUT_TEMP
};
u8 crc; // This is never used
};

View File

@ -29,9 +29,8 @@ void JoyconPoller::ReadActiveMode(std::span<u8> buffer, const MotionStatus& moti
UpdateActiveRightPadInput(data, motion_status);
break;
case Joycon::ControllerType::Pro:
UpdateActiveProPadInput(data, motion_status);
break;
default:
UpdateActiveProPadInput(data, motion_status);
break;
}
@ -54,9 +53,8 @@ void JoyconPoller::ReadPassiveMode(std::span<u8> buffer) {
UpdatePasiveRightPadInput(data);
break;
case Joycon::ControllerType::Pro:
UpdatePasiveProPadInput(data);
break;
default:
UpdatePasiveProPadInput(data);
break;
}
}