Skip to content

Commit

Permalink
feat: Enable Tilt Value Configuration via WebConfig (#500)
Browse files Browse the repository at this point in the history
* Enable Tilt values configuration via WebConfig with rounding issue.

* feat: Enable Tilt Value onfiguration via WebConfig

* revert: Remove newly added settings due to missing translations in Chinese and Portuguese

* fix: Preserve protobuf field tag numbers to maintain backwards compatibility

* fix: Preserve protobuf field tag numbers to maintain backwards compatibility

* fix: Preserve protobuf field tag numbers to maintain backwards compatibility

* fix: Preserve protobuf field tag numbers to maintain backwards compatibility

* Reflected the changes from the merged pull request.

---------

Co-authored-by: Luke Arntson <[email protected]>
  • Loading branch information
smashBrosKanu and arntsonl authored Sep 15, 2023
1 parent 7a5b5b3 commit 69a20ee
Show file tree
Hide file tree
Showing 9 changed files with 279 additions and 30 deletions.
8 changes: 8 additions & 0 deletions configs/Pico/BoardConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,15 @@

// TILTAdd-on Options
#define PIN_TILT_1 -1
#define TILT1_FACTOR_LEFT_X 35 //Default value for the TILT button to function.
#define TILT1_FACTOR_LEFT_Y 45 //Default value for the TILT button to function.
#define TILT1_FACTOR_RIGHT_X 35 //Default value for the TILT button to function.
#define TILT1_FACTOR_RIGHT_Y 170 //Default value for the TILT button to function.
#define PIN_TILT_2 -1
#define TILT2_FACTOR_LEFT_X 65 //Default value for the TILT button to function.
#define TILT2_FACTOR_LEFT_Y 35 //Default value for the TILT button to function.
#define TILT2_FACTOR_RIGHT_X 30 //Default value for the TILT button to function.
#define TILT2_FACTOR_RIGHT_Y 30 //Default value for the TILT button to function.
#define PIN_TILT_FUNCTION -1
#define PIN_TILT_LEFT_ANALOG_UP -1
#define PIN_TILT_LEFT_ANALOG_DOWN -1
Expand Down
41 changes: 41 additions & 0 deletions headers/addons/tilt.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,43 @@
#define PIN_TILT_1 -1
#endif

#ifndef TILT1_FACTOR_LEFT_X
#define TILT1_FACTOR_LEFT_X -1
#endif

#ifndef TILT1_FACTOR_LEFT_Y
#define TILT1_FACTOR_LEFT_Y -1
#endif

#ifndef TILT1_FACTOR_RIGHT_X
#define TILT1_FACTOR_RIGHT_X -1
#endif

#ifndef TILT1_FACTOR_RIGHT_Y
#define TILT1_FACTOR_RIGHT_Y -1
#endif

#ifndef PIN_TILT_2
#define PIN_TILT_2 -1
#endif

#ifndef TILT2_FACTOR_LEFT_X
#define TILT2_FACTOR_LEFT_X -1
#endif

#ifndef TILT2_FACTOR_LEFT_Y
#define TILT2_FACTOR_LEFT_Y -1
#endif

#ifndef TILT2_FACTOR_RIGHT_X
#define TILT2_FACTOR_RIGHT_X -1
#endif

#ifndef TILT2_FACTOR_RIGHT_Y
#define TILT2_FACTOR_RIGHT_Y -1
#endif


#ifndef PIN_TILT_LEFT_ANALOG_UP
#define PIN_TILT_LEFT_ANALOG_UP -1
#endif
Expand Down Expand Up @@ -79,7 +112,15 @@ class TiltInput : public GPAddon {
DpadDirection lastTiltLR; // Gamepad Last Left-Right
uint32_t dpadTime[4];
uint8_t pinTilt1;
uint8_t tilt1FactorLeftX;
uint8_t tilt1FactorLeftY;
uint8_t tilt1FactorRightX;
uint8_t tilt1FactorRightY;
uint8_t pinTilt2;
uint8_t tilt2FactorLeftX;
uint8_t tilt2FactorLeftY;
uint8_t tilt2FactorRightX;
uint8_t tilt2FactorRightY;
uint8_t pinTiltLeftAnalogDown;
uint8_t pinTiltLeftAnalogUp;
uint8_t pinTiltLeftAnalogLeft;
Expand Down
9 changes: 9 additions & 0 deletions proto/config.proto
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,15 @@ message TiltOptions
optional int32 tiltRightAnalogRightPin = 12;

optional SOCDMode tiltSOCDMode = 13;

optional int32 factorTilt1LeftX = 14;
optional int32 factorTilt1LeftY = 15;
optional int32 factorTilt1RightX = 16;
optional int32 factorTilt1RightY = 17;
optional int32 factorTilt2LeftX = 18;
optional int32 factorTilt2LeftY = 19;
optional int32 factorTilt2RightX = 20;
optional int32 factorTilt2RightY = 21;
}

message BuzzerOptions
Expand Down
59 changes: 30 additions & 29 deletions src/addons/tilt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,6 @@
#include "helper.h"
#include "config.pb.h"

// These constants define the adjustment factors for gamepad analog inputs under different tilt states.
// Defines the behavior of the left analog stick when the Tilt1 and Tilt2 buttons are pressed.
// The main purpose of the left analog stick is to move the character.
// Pressing it simultaneously with Tilt 1 will make the character walk.
// Pressing it simultaneously with Tilt 2 will make the character walk more slowly.
const double TILT1_FACTOR_LEFT_X = 0.35; // Adjustment factor for left analog stick X direction
const double TILT1_FACTOR_LEFT_Y = 0.45; // Adjustment factor for left analog stick Y direction
const double TILT2_FACTOR_LEFT_X = 0.65; // Adjustment factor for left analog stick X direction
const double TILT2_FACTOR_LEFT_Y = 0.35; // Adjustment factor for left analog stick Y direction

// The Right analog stick has 8 directions, which can be handled by pressing up, down, left, right, and simultaneously.
// This function adds to that the ability to tilt it at an angle closer to horizontal than diagonal.
const double TILT1_FACTOR_RIGHT_X = 0.3; // Adjustment factor for right analog stick X direction
const double TILT1_FACTOR_RIGHT_Y = 1.7; // Adjustment factor for right analog stick Y direction
const double TILT2_FACTOR_RIGHT_X = 0.3; // Adjustment factor for right analog stick X direction
const double TILT2_FACTOR_RIGHT_Y = 0.3; // Adjustment factor for right analog stick Y direction

bool TiltInput::available() {
return Storage::getInstance().getAddonOptions().tiltOptions.enabled;
}
Expand All @@ -29,7 +12,15 @@ void TiltInput::setup() {
tiltSOCDMode = options.tiltSOCDMode;

pinTilt1 = options.tilt1Pin;
tilt1FactorLeftX = options.factorTilt1LeftX;
tilt1FactorLeftY = options.factorTilt1LeftY;
tilt1FactorRightX = options.factorTilt1RightX;
tilt1FactorRightY = options.factorTilt1RightY;
pinTilt2 = options.tilt2Pin;
tilt2FactorLeftX = options.factorTilt2LeftX;
tilt2FactorLeftY = options.factorTilt2LeftY;
tilt2FactorRightX = options.factorTilt2RightX;
tilt2FactorRightY = options.factorTilt2RightY;
pinTiltLeftAnalogDown = options.tiltLeftAnalogDownPin;
pinTiltLeftAnalogUp = options.tiltLeftAnalogUpPin;
pinTiltLeftAnalogLeft = options.tiltLeftAnalogLeftPin;
Expand Down Expand Up @@ -168,14 +159,24 @@ void TiltInput::OverrideGamepad(Gamepad* gamepad, uint8_t dpad1, uint8_t dpad2)
bool pinTilt1Pressed = (pinTilt1 != (uint8_t)-1) ? !gpio_get(pinTilt1) : false;
bool pinTilt2Pressed = (pinTilt2 != (uint8_t)-1) ? !gpio_get(pinTilt2) : false;

if (pinTilt1Pressed) {
gamepad->state.lx = dpadToAnalogX(dpad1) + (GAMEPAD_JOYSTICK_MID - dpadToAnalogX(dpad1)) * TILT1_FACTOR_LEFT_X;
gamepad->state.ly = dpadToAnalogY(dpad1) + (GAMEPAD_JOYSTICK_MID - dpadToAnalogY(dpad1)) * TILT1_FACTOR_LEFT_Y;
}
else if (pinTilt2Pressed) {
gamepad->state.lx = dpadToAnalogX(dpad1) + (GAMEPAD_JOYSTICK_MID - dpadToAnalogX(dpad1)) * TILT2_FACTOR_LEFT_X;
gamepad->state.ly = dpadToAnalogY(dpad1) + (GAMEPAD_JOYSTICK_MID - dpadToAnalogY(dpad1)) * TILT2_FACTOR_LEFT_Y;
}
double scaledTilt1FactorLeftX = tilt1FactorLeftX / 100.0;
double scaledTilt1FactorLeftY = tilt1FactorLeftY / 100.0;
double scaledTilt1FactorRightX = tilt1FactorRightX / 100.0;
double scaledTilt1FactorRightY = tilt1FactorRightY / 100.0;
double scaledTilt2FactorLeftX = tilt2FactorLeftX / 100.0;
double scaledTilt2FactorLeftY = tilt2FactorLeftY / 100.0;
double scaledTilt2FactorRightX = tilt2FactorRightX / 100.0;
double scaledTilt2FactorRightY = tilt2FactorRightY / 100.0;


if (pinTilt1Pressed) {
gamepad->state.lx = dpadToAnalogX(dpad1) + (GAMEPAD_JOYSTICK_MID - dpadToAnalogX(dpad1)) * scaledTilt1FactorLeftX;
gamepad->state.ly = dpadToAnalogY(dpad1) + (GAMEPAD_JOYSTICK_MID - dpadToAnalogY(dpad1)) * scaledTilt1FactorLeftY;
}
else if (pinTilt2Pressed) {
gamepad->state.lx = dpadToAnalogX(dpad1) + (GAMEPAD_JOYSTICK_MID - dpadToAnalogX(dpad1)) * scaledTilt2FactorLeftX;
gamepad->state.ly = dpadToAnalogY(dpad1) + (GAMEPAD_JOYSTICK_MID - dpadToAnalogY(dpad1)) * scaledTilt2FactorLeftY;
}
else {
gamepad->state.lx = dpadToAnalogX(dpad1);
gamepad->state.ly = dpadToAnalogY(dpad1);
Expand All @@ -186,8 +187,8 @@ void TiltInput::OverrideGamepad(Gamepad* gamepad, uint8_t dpad1, uint8_t dpad2)
}
else if (pinTilt1Pressed) {
if (dpad2 & (GAMEPAD_MASK_LEFT | GAMEPAD_MASK_RIGHT)) {
gamepad->state.rx = dpadToAnalogX(dpad2) + (GAMEPAD_JOYSTICK_MID - dpadToAnalogX(dpad2)) * TILT1_FACTOR_RIGHT_X;
gamepad->state.ry = GAMEPAD_JOYSTICK_MID * TILT1_FACTOR_RIGHT_Y;
gamepad->state.rx = dpadToAnalogX(dpad2) + (GAMEPAD_JOYSTICK_MID - dpadToAnalogX(dpad2)) * scaledTilt1FactorRightX;
gamepad->state.ry = GAMEPAD_JOYSTICK_MID * scaledTilt1FactorRightY;
}
else {
gamepad->state.rx = dpadToAnalogX(dpad2);
Expand All @@ -196,8 +197,8 @@ void TiltInput::OverrideGamepad(Gamepad* gamepad, uint8_t dpad1, uint8_t dpad2)
}
else if (pinTilt2Pressed) {
if (dpad2 & (GAMEPAD_MASK_LEFT | GAMEPAD_MASK_RIGHT)) {
gamepad->state.rx = dpadToAnalogX(dpad2) + (GAMEPAD_JOYSTICK_MID - dpadToAnalogX(dpad2)) * TILT2_FACTOR_RIGHT_X;
gamepad->state.ry = GAMEPAD_JOYSTICK_MID * TILT2_FACTOR_RIGHT_Y;
gamepad->state.rx = dpadToAnalogX(dpad2) + (GAMEPAD_JOYSTICK_MID - dpadToAnalogX(dpad2)) * scaledTilt2FactorRightX;
gamepad->state.ry = GAMEPAD_JOYSTICK_MID * scaledTilt2FactorRightY;
}
else {
gamepad->state.rx = dpadToAnalogX(dpad2);
Expand Down
8 changes: 8 additions & 0 deletions src/config_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,15 @@ void ConfigUtils::initUnsetPropertiesWithDefaults(Config& config)
// addonOptions.tiltOptions
INIT_UNSET_PROPERTY(config.addonOptions.tiltOptions, enabled, !!TILT_ENABLED);
INIT_UNSET_PROPERTY(config.addonOptions.tiltOptions, tilt1Pin, PIN_TILT_1);
INIT_UNSET_PROPERTY(config.addonOptions.tiltOptions, factorTilt1LeftX, TILT1_FACTOR_LEFT_X);
INIT_UNSET_PROPERTY(config.addonOptions.tiltOptions, factorTilt1LeftY, TILT1_FACTOR_LEFT_Y);
INIT_UNSET_PROPERTY(config.addonOptions.tiltOptions, factorTilt1RightX, TILT1_FACTOR_RIGHT_X);
INIT_UNSET_PROPERTY(config.addonOptions.tiltOptions, factorTilt1RightY, TILT1_FACTOR_RIGHT_Y);
INIT_UNSET_PROPERTY(config.addonOptions.tiltOptions, tilt2Pin, PIN_TILT_2);
INIT_UNSET_PROPERTY(config.addonOptions.tiltOptions, factorTilt2LeftX, TILT2_FACTOR_LEFT_X);
INIT_UNSET_PROPERTY(config.addonOptions.tiltOptions, factorTilt2LeftY, TILT2_FACTOR_LEFT_Y);
INIT_UNSET_PROPERTY(config.addonOptions.tiltOptions, factorTilt2RightX, TILT2_FACTOR_RIGHT_X);
INIT_UNSET_PROPERTY(config.addonOptions.tiltOptions, factorTilt2RightY, TILT2_FACTOR_RIGHT_Y);
INIT_UNSET_PROPERTY(config.addonOptions.tiltOptions, tiltLeftAnalogDownPin, PIN_TILT_LEFT_ANALOG_DOWN);
INIT_UNSET_PROPERTY(config.addonOptions.tiltOptions, tiltLeftAnalogUpPin, PIN_TILT_LEFT_ANALOG_UP);
INIT_UNSET_PROPERTY(config.addonOptions.tiltOptions, tiltLeftAnalogLeftPin, PIN_TILT_LEFT_ANALOG_LEFT);
Expand Down
16 changes: 16 additions & 0 deletions src/configs/webconfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1002,7 +1002,15 @@ std::string setAddonOptions()

TiltOptions& tiltOptions = Storage::getInstance().getAddonOptions().tiltOptions;
docToPin(tiltOptions.tilt1Pin, doc, "tilt1Pin");
docToValue(tiltOptions.factorTilt1LeftX, doc, "factorTilt1LeftX");
docToValue(tiltOptions.factorTilt1LeftY, doc, "factorTilt1LeftY");
docToValue(tiltOptions.factorTilt1RightX, doc, "factorTilt1RightX");
docToValue(tiltOptions.factorTilt1RightY, doc, "factorTilt1RightY");
docToPin(tiltOptions.tilt2Pin, doc, "tilt2Pin");
docToValue(tiltOptions.factorTilt2LeftX, doc, "factorTilt2LeftX");
docToValue(tiltOptions.factorTilt2LeftY, doc, "factorTilt2LeftY");
docToValue(tiltOptions.factorTilt2RightX, doc, "factorTilt2RightX");
docToValue(tiltOptions.factorTilt2RightY, doc, "factorTilt2RightY");
docToPin(tiltOptions.tiltLeftAnalogUpPin, doc, "tiltLeftAnalogUpPin");
docToPin(tiltOptions.tiltLeftAnalogDownPin, doc, "tiltLeftAnalogDownPin");
docToPin(tiltOptions.tiltLeftAnalogLeftPin, doc, "tiltLeftAnalogLeftPin");
Expand Down Expand Up @@ -1414,7 +1422,15 @@ std::string getAddonOptions()

const TiltOptions& tiltOptions = Storage::getInstance().getAddonOptions().tiltOptions;
writeDoc(doc, "tilt1Pin", cleanPin(tiltOptions.tilt1Pin));
writeDoc(doc, "factorTilt1LeftX", tiltOptions.factorTilt1LeftX);
writeDoc(doc, "factorTilt1LeftY", tiltOptions.factorTilt1LeftY);
writeDoc(doc, "factorTilt1RightX", tiltOptions.factorTilt1RightX);
writeDoc(doc, "factorTilt1RightY", tiltOptions.factorTilt1RightY);
writeDoc(doc, "tilt2Pin", cleanPin(tiltOptions.tilt2Pin));
writeDoc(doc, "factorTilt2LeftX", tiltOptions.factorTilt2LeftX);
writeDoc(doc, "factorTilt2LeftY", tiltOptions.factorTilt2LeftY);
writeDoc(doc, "factorTilt2RightX", tiltOptions.factorTilt2RightX);
writeDoc(doc, "factorTilt2RightY", tiltOptions.factorTilt2RightY);
writeDoc(doc, "tiltLeftAnalogUpPin", cleanPin(tiltOptions.tiltLeftAnalogUpPin));
writeDoc(doc, "tiltLeftAnalogDownPin", cleanPin(tiltOptions.tiltLeftAnalogDownPin));
writeDoc(doc, "tiltLeftAnalogLeftPin", cleanPin(tiltOptions.tiltLeftAnalogLeftPin));
Expand Down
8 changes: 8 additions & 0 deletions www/server/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,15 @@ app.get('/api/getAddonsOptions', (req, res) => {
dualDirCombineMode: 0,
dualDirFourWayMode: 0,
tilt1Pin: -1,
factorTilt1LeftX: -1,
factorTilt1LeftY: -1,
factorTilt1RightX: -1,
factorTilt1RightY: -1,
tilt2Pin: -1,
factorTilt2LeftX: -1,
factorTilt2LeftY: -1,
factorTilt2RightX: -1,
factorTilt2RightY: -1,
tiltLeftAnalogUpPin: -1,
tiltLeftAnalogDownPin: -1,
tiltLeftAnalogLeftPin: -1,
Expand Down
Loading

0 comments on commit 69a20ee

Please sign in to comment.