Skip to content

Commit

Permalink
DRV8833-Based Rumble (for XInput) (OpenStickCommunity#1090)
Browse files Browse the repository at this point in the history
* Captured rumble data -> featureData

* Add endpoint_out_checked and initial write to xinput_out_buffer

* Copy to featureData during endpoint_out claim

* Revert to simpler logic (endpoint_out always checked in process)

* Add GamepadRumbleState, update rumble state in XInput driver

* Create hard-coded DRV8833RumbleAddon, test 0 and 255 values

* Fix rumble state comparison

* Add rumble duty rescaling

* Properly handle case where one motor is zero and other is non-zero

* Working in WebConfig

* Remove leftover debug code
  • Loading branch information
nicolasrmerz authored Jul 20, 2024
1 parent 9ef24e5 commit c309d35
Show file tree
Hide file tree
Showing 15 changed files with 536 additions and 99 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ src/addons/playernum.cpp
src/addons/playerleds.cpp
src/addons/rotaryencoder.cpp
src/addons/reverse.cpp
src/addons/drv8833_rumble.cpp
src/addons/turbo.cpp
src/addons/slider_socd.cpp
src/addons/wiiext.cpp
Expand Down
74 changes: 74 additions & 0 deletions headers/addons/drv8833_rumble.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#ifndef RUMBLE_H_
#define RUMBLE_H_

#include <vector>
#include <string>
#include "gpaddon.h"
#include "gamepad/GamepadState.h"

#ifndef DRV8833_RUMBLE_ENABLED
#define DRV8833_RUMBLE_ENABLED 0
#endif

#ifndef DRV8833_RUMBLE_LEFT_MOTOR_PIN
#define DRV8833_RUMBLE_LEFT_MOTOR_PIN -1 //26
#endif

#ifndef DRV8833_RUMBLE_RIGHT_MOTOR_PIN
#define DRV8833_RUMBLE_RIGHT_MOTOR_PIN -1 //27
#endif

#ifndef DRV8833_RUMBLE_MOTOR_SLEEP_PIN
#define DRV8833_RUMBLE_MOTOR_SLEEP_PIN -1 //22
#endif

#ifndef DRV8833_RUMBLE_PWM_FREQUENCY
#define DRV8833_RUMBLE_PWM_FREQUENCY 10000 // 10 kHz
#endif

#ifndef DRV8833_RUMBLE_DUTY_MIN
#define DRV8833_RUMBLE_DUTY_MIN 0.0f
#endif

#ifndef DRV8833_RUMBLE_DUTY_MAX
#define DRV8833_RUMBLE_DUTY_MAX 100.0f
#endif

// DRV8833 Rumble Module
#define DRV8833RumbleName "DRV8833Rumble"

// Scale uint8 to 0 -> 100 range
#define motorToDuty(m) (100.0f * (m/255.0f))
// Rescale from 0 -> 100 range to min -> max range
#define scaleDuty(in, min, max) ((in/100.0f) * (max-min) + min)

// Buzzer Speaker
class DRV8833RumbleAddon : public GPAddon
{
public:
virtual bool available();
virtual void setup();
virtual void preprocess() {}
virtual void process();
virtual std::string name() { return DRV8833RumbleName; }
private:
uint32_t pwmSetFreqDuty(uint slice, uint channel, uint32_t frequency, float duty);
bool compareRumbleState(Gamepad * gamepad);
void setRumbleState(Gamepad * gamepad);
void disableMotors();
void enableMotors(Gamepad * gamepad);
uint8_t leftMotorPin;
uint8_t rightMotorPin;
uint8_t motorSleepPin;
uint8_t leftMotorPinSlice;
uint8_t leftMotorPinChannel;
uint8_t rightMotorPinSlice;
uint8_t rightMotorPinChannel;
uint32_t pwmFrequency;
float dutyMin;
float dutyMax;
uint32_t sysClock;
GamepadRumbleState currentRumbleState;
};

#endif
2 changes: 2 additions & 0 deletions headers/gamepad.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class Gamepad {

void hotkey();
void clearState();
void clearRumbleState();

/**
* @brief Flag to indicate analog trigger support.
Expand Down Expand Up @@ -135,6 +136,7 @@ class Gamepad {
GamepadState rawState;
GamepadState state;
GamepadState turboState;
GamepadRumbleState rumbleState;
GamepadButtonMapping *mapDpadUp;
GamepadButtonMapping *mapDpadDown;
GamepadButtonMapping *mapDpadLeft;
Expand Down
10 changes: 10 additions & 0 deletions headers/gamepad/GamepadState.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,16 @@ const uint32_t buttonMasks[] =
GAMEPAD_MASK_E12,
};

struct GamepadRumbleState
{
// XInput General Motors
uint8_t leftMotor {0};
uint8_t rightMotor {0};
// GameInput Trigger Motors (XBOne)
uint8_t leftTrigger {0};
uint8_t rightTrigger {0};
};

struct GamepadState
{
uint8_t dpad {0};
Expand Down
67 changes: 40 additions & 27 deletions proto/config.proto
Original file line number Diff line number Diff line change
Expand Up @@ -205,26 +205,26 @@ message ProfileOptions
message DisplayOptions
{
optional bool enabled = 1;

optional int32 i2cBlock = 2;
optional int32 deprecatedI2cSDAPin = 3 [deprecated = true];
optional int32 deprecatedI2cSCLPin = 4 [deprecated = true];
optional int32 i2cAddress = 5;
optional int32 deprecatedI2cSpeed = 6 [deprecated = true];

optional ButtonLayout buttonLayout = 7;
optional ButtonLayoutRight buttonLayoutRight = 8;
optional ButtonLayoutCustomOptions buttonLayoutCustomOptions = 9;

optional SplashMode splashMode = 10;
optional SplashChoice splashChoice = 11;
optional int32 splashDuration = 12;
optional bytes splashImage = 13 [(nanopb).max_size = 1024];

optional int32 size = 14;
optional int32 flip = 15;
optional bool invert = 16;

optional int32 displaySaverTimeout = 17;
optional bool turnOffWhenSuspended = 18;
}
Expand All @@ -237,7 +237,7 @@ message LEDOptions
optional uint32 ledsPerButton = 4;
optional uint32 brightnessMaximum = 5;
optional uint32 brightnessSteps = 6;

optional int32 indexUp = 7;
optional int32 indexDown = 8;
optional int32 indexLeft = 9;
Expand All @@ -256,7 +256,7 @@ message LEDOptions
optional int32 indexR3 = 22;
optional int32 indexA1 = 23;
optional int32 indexA2 = 24;

optional PLEDType pledType = 25;
optional int32 pledPin1 = 26;
optional int32 pledPin2 = 27;
Expand All @@ -282,7 +282,7 @@ message AnimationOptions_Proto
optional int32 chaseCycleTime = 5;
optional int32 rainbowCycleTime = 6;
optional uint32 themeIndex = 7;

optional bool hasCustomTheme = 8;
optional uint32 customThemeUp = 9;
optional uint32 customThemeDown = 10;
Expand Down Expand Up @@ -320,7 +320,7 @@ message AnimationOptions_Proto
optional uint32 customThemeR3Pressed = 42;
optional uint32 customThemeA1Pressed = 43;
optional uint32 customThemeA2Pressed = 44;
optional uint32 buttonPressColorCooldownTimeInMs = 45;
optional uint32 buttonPressColorCooldownTimeInMs = 45;
}

message BootselButtonOptions
Expand All @@ -338,7 +338,7 @@ message OnBoardLedOptions
message AnalogOptions
{
optional bool enabled = 1;

optional int32 analogAdc1PinX = 2;
optional int32 analogAdc1PinY = 3;
optional bool forced_circularity = 4;
Expand All @@ -356,12 +356,12 @@ message AnalogOptions
message TurboOptions
{
optional bool enabled = 1;

optional int32 deprecatedButtonPin = 2 [deprecated = true];
optional int32 ledPin = 3;
optional uint32 shotCount = 4;
optional int32 shmupDialPin = 5;

optional bool shmupModeEnabled = 6;
optional uint32 shmupAlwaysOn1 = 7;
optional uint32 shmupAlwaysOn2 = 8;
Expand All @@ -381,7 +381,7 @@ message TurboOptions
message SliderOptions
{
optional bool enabled = 1;

optional int32 deprecatedPinSliderOne = 2 [deprecated = true];
optional int32 deprecatedPinSliderTwo = 3 [deprecated = true];
optional DpadMode deprecatedModeOne = 4 [deprecated = true];
Expand All @@ -392,10 +392,10 @@ message SliderOptions
message SOCDSliderOptions
{
optional bool enabled = 1;

optional int32 deprecatedPinOne = 2 [deprecated = true];
optional int32 deprecatedPinTwo = 3 [deprecated = true];

optional SOCDMode modeDefault = 4;
optional SOCDMode deprecatedModeOne = 5 [deprecated = true];
optional SOCDMode deprecatedModeTwo = 6 [deprecated = true];
Expand All @@ -404,10 +404,10 @@ message SOCDSliderOptions
message ReverseOptions
{
optional bool enabled = 1;

optional int32 buttonPin = 2;
optional int32 ledPin = 3;

optional uint32 actionUp = 4;
optional uint32 actionDown = 5;
optional uint32 actionLeft = 6;
Expand All @@ -417,7 +417,7 @@ message ReverseOptions
message AnalogADS1219Options
{
optional bool enabled = 1;

optional int32 i2cBlock = 2;
optional int32 deprecatedI2cSDAPin = 3 [deprecated = true];
optional int32 deprecatedI2cSCLPin = 4 [deprecated = true];
Expand All @@ -439,12 +439,12 @@ message AnalogADS1256Options
message DualDirectionalOptions
{
optional bool enabled = 1;

optional int32 deprecatedUpPin = 2 [deprecated = true];
optional int32 deprecatedDownPin = 3 [deprecated = true];
optional int32 deprecatedLeftPin = 4 [deprecated = true];
optional int32 deprecatedRightPin = 5 [deprecated = true];

optional DpadMode dpadMode = 6;
optional DualDirectionalCombinationMode combineMode = 7;
optional bool fourWayMode = 8;
Expand All @@ -467,7 +467,7 @@ message TiltOptions
optional int32 tiltRightAnalogRightPin = 12;

optional SOCDMode tiltSOCDMode = 13;

optional int32 factorTilt1LeftX = 14;
optional int32 factorTilt1LeftY = 15;
optional int32 factorTilt1RightX = 16;
Expand All @@ -481,7 +481,7 @@ message TiltOptions
message BuzzerOptions
{
optional bool enabled = 1;

optional int32 pin = 2;
optional uint32 volume = 3;
optional int32 enablePin = 4;
Expand All @@ -490,7 +490,7 @@ message BuzzerOptions
message ExtraButtonOptions
{
optional bool enabled = 1;

optional int32 pin = 2;
optional uint32 buttonMap = 3;
}
Expand Down Expand Up @@ -543,7 +543,7 @@ message WiiOptions
optional AnalogAxis x = 1;
optional AnalogAxis y = 2;
}

message NunchukOptions
{
optional int32 buttonC = 1;
Expand Down Expand Up @@ -574,7 +574,7 @@ message WiiOptions
optional AnalogAxis rightTrigger = 19;
}

message TaikoOptions
message TaikoOptions
{
optional int32 buttonKatLeft = 1;
optional int32 buttonKatRight = 2;
Expand Down Expand Up @@ -738,6 +738,18 @@ message PCF8575Options
repeated GpioMappingInfo pins = 3 [(nanopb).max_count = 16];
}

message DRV8833RumbleOptions
{
optional bool enabled = 1;

optional int32 leftMotorPin = 2;
optional int32 rightMotorPin = 3;
optional int32 motorSleepPin = 4;
optional uint32 pwmFrequency = 5;
optional float dutyMin = 6;
optional float dutyMax = 7;
}

message AddonOptions
{
optional BootselButtonOptions bootselButtonOptions = 1;
Expand Down Expand Up @@ -765,6 +777,7 @@ message AddonOptions
optional AnalogADS1256Options analogADS1256Options = 23;
optional RotaryOptions rotaryOptions = 24;
optional PCF8575Options pcf8575Options = 25;
optional DRV8833RumbleOptions drv8833RumbleOptions = 26;
}

message MigrationHistory
Expand All @@ -777,7 +790,7 @@ message MigrationHistory
message Config
{
optional string boardVersion = 1 [(nanopb).max_length = 31];

optional GamepadOptions gamepadOptions = 2;
optional HotkeyOptions hotkeyOptions = 3;
optional PinMappings deprecatedPinMappings = 4 [deprecated = true];
Expand All @@ -788,7 +801,7 @@ message Config
optional AddonOptions addonOptions = 9;
optional ForcedSetupOptions forcedSetupOptions = 10;
optional ProfileOptions profileOptions = 11;

optional string boardConfig = 12 [(nanopb).max_length = 63];
optional GpioMappings gpioMappings = 13;
optional MigrationHistory migrations = 14;
Expand Down
Loading

0 comments on commit c309d35

Please sign in to comment.