Skip to content

Commit

Permalink
fix analog range (#1006)
Browse files Browse the repository at this point in the history
* fix analog range

Fix for analog range to be complete at the loss of circularity, and some other minor fixes.

* introduce outer deadzones

EternalDahaka showed me that I unknowingly stumbled upon outer deadzone with the magic number I kept messing with. After learning what that was, everything made a lot more sense. Now it's an adjustable value and everything is working correctly, including circularity.

* fix pb numbering
  • Loading branch information
NickGuyver authored Jun 3, 2024
1 parent dde8644 commit cd757db
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 44 deletions.
11 changes: 7 additions & 4 deletions headers/addons/analog.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,12 @@
#define FORCED_CIRCULARITY_ENABLED 0
#endif

#ifndef DEFAULT_ANALOG_DEADZONE
#define DEFAULT_ANALOG_DEADZONE 5
#ifndef DEFAULT_INNER_DEADZONE
#define DEFAULT_INNER_DEADZONE 5
#endif

#ifndef DEFAULT_OUTER_DEADZONE
#define DEFAULT_OUTER_DEADZONE 95
#endif

#ifndef AUTO_CALIBRATE_ENABLED
Expand All @@ -76,8 +80,7 @@ class AnalogInput : public GPAddon {
static float readPin(int pin, uint16_t center, bool autoCalibrate);
static uint16_t map(uint16_t x, uint16_t in_min, uint16_t in_max, uint16_t out_min, uint16_t out_max);
static float magnitudeCalculation(float x, float y, float& x_magnitude, float& y_magnitude);
static void radialDeadzone(float& x, float& y, float deadzone, float x_magnitude, float y_magnitude, float magnitude);
static void adjustCircularity(float& x, float& y, float x_magnitude, float y_magnitude, float magnitude);
static void radialDeadzone(float& x, float& y, float in_deadzone, float out_deadzone, float x_magnitude, float y_magnitude, float magnitude, bool circularity);
};

#endif // _Analog_H_
3 changes: 2 additions & 1 deletion proto/config.proto
Original file line number Diff line number Diff line change
Expand Up @@ -340,14 +340,15 @@ message AnalogOptions
optional int32 analogAdc1PinX = 2;
optional int32 analogAdc1PinY = 3;
optional bool forced_circularity = 4;
optional uint32 analog_deadzone = 5;
optional uint32 inner_deadzone = 5;
optional int32 analogAdc2PinX = 6;
optional int32 analogAdc2PinY = 7;
optional DpadMode analogAdc1Mode = 8;
optional DpadMode analogAdc2Mode = 9;
optional InvertMode analogAdc1Invert = 10;
optional InvertMode analogAdc2Invert = 11;
optional bool auto_calibrate = 12;
optional uint32 outer_deadzone = 13;
}

message TurboOptions
Expand Down
42 changes: 19 additions & 23 deletions src/addons/analog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
#define ADC_MAX ((1 << 12) - 1) // 4095
#define ADC_PIN_OFFSET 26
#define ANALOG_MAX 1.0f
#define ANALOG_CENTER ANALOG_MAX / 2
#define ANALOG_CENTER 0.5f
#define ANALOG_MINIMUM 0.0f

bool AnalogInput::available() {
return Storage::getInstance().getAddonOptions().analogOptions.enabled;
Expand Down Expand Up @@ -54,7 +55,8 @@ void AnalogInput::process()
float adc_1_y = ANALOG_CENTER;
float adc_2_x = ANALOG_CENTER;
float adc_2_y = ANALOG_CENTER;
float adc_deadzone = analogOptions.analog_deadzone / 200.0f;
float in_deadzone = analogOptions.inner_deadzone / 100.0f;
float out_deadzone = analogOptions.outer_deadzone / 100.0f;
float x_magnitude_1 = 0.0f;
float y_magnitude_1 = 0.0f;
float x_magnitude_2 = 0.0f;
Expand Down Expand Up @@ -116,26 +118,21 @@ void AnalogInput::process()
}
}

if (adc_deadzone >= 0.0f || analogOptions.forced_circularity == true) {
if (in_deadzone >= 0.0f || analogOptions.forced_circularity == true) {
adc_pairs[i].xy_magnitude = magnitudeCalculation(adc_pairs[i].x_value, adc_pairs[i].y_value,
adc_pairs[i].x_magnitude, adc_pairs[i].y_magnitude);

if (adc_deadzone >= 0.0f) {
if (adc_pairs[i].xy_magnitude < adc_deadzone) {
if (in_deadzone >= 0.0f) {
if (adc_pairs[i].xy_magnitude < in_deadzone) {
adc_pairs[i].x_value = ANALOG_CENTER;
adc_pairs[i].y_value = ANALOG_CENTER;
}
else {
radialDeadzone(adc_pairs[i].x_value, adc_pairs[i].y_value, adc_deadzone,
adc_pairs[i].x_magnitude, adc_pairs[i].y_magnitude, adc_pairs[i].xy_magnitude);
radialDeadzone(adc_pairs[i].x_value, adc_pairs[i].y_value, in_deadzone, out_deadzone,
adc_pairs[i].x_magnitude, adc_pairs[i].y_magnitude, adc_pairs[i].xy_magnitude,
analogOptions.forced_circularity);
}
}
if (adc_pairs[i].x_value != ANALOG_CENTER && adc_pairs[i].y_value != ANALOG_CENTER &&
analogOptions.forced_circularity == true && adc_pairs[i].xy_magnitude > ANALOG_CENTER) {

adjustCircularity(adc_pairs[i].x_value, adc_pairs[i].y_value,
adc_pairs[i].x_magnitude, adc_pairs[i].y_magnitude, adc_pairs[i].xy_magnitude);
}
}

if (adc_pairs[i].analog_dpad == DpadMode::DPAD_MODE_LEFT_ANALOG) {
Expand Down Expand Up @@ -185,17 +182,16 @@ float AnalogInput::magnitudeCalculation(float x, float y, float& x_magnitude, fl
return std::sqrt((x_magnitude * x_magnitude) + (y_magnitude * y_magnitude));
}

void AnalogInput::radialDeadzone(float& x, float& y, float deadzone, float x_magnitude, float y_magnitude, float xy_magnitude) {
float scaling_factor = (xy_magnitude - deadzone) / (1.0f - 1.6f * deadzone);

void AnalogInput::radialDeadzone(float& x, float& y, float in_deadzone, float out_deadzone, float x_magnitude, float y_magnitude, float xy_magnitude, bool circularity) {
float scaling_factor = (xy_magnitude - in_deadzone) / (out_deadzone - in_deadzone);

if (circularity == true) {
scaling_factor = std::fmin(scaling_factor, ANALOG_CENTER);
}

x = ((x_magnitude / xy_magnitude) * scaling_factor) + ANALOG_CENTER;
y = ((y_magnitude / xy_magnitude) * scaling_factor) + ANALOG_CENTER;

x = std::fmin(x, 1.0f);
y = std::fmin(y, 1.0f);
}

void AnalogInput::adjustCircularity(float& x, float& y, float x_magnitude, float y_magnitude, float xy_magnitude) {
x = ((x_magnitude / xy_magnitude) * ANALOG_CENTER + ANALOG_CENTER);
y = ((y_magnitude / xy_magnitude) * ANALOG_CENTER + ANALOG_CENTER);
x = std::clamp(x, ANALOG_MINIMUM, ANALOG_MAX);
y = std::clamp(y, ANALOG_MINIMUM, ANALOG_MAX);
}
3 changes: 2 additions & 1 deletion src/config_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,8 @@ void ConfigUtils::initUnsetPropertiesWithDefaults(Config& config)
INIT_UNSET_PROPERTY(config.addonOptions.analogOptions, analogAdc2Mode, ANALOG_ADC_2_MODE);
INIT_UNSET_PROPERTY(config.addonOptions.analogOptions, analogAdc2Invert, ANALOG_ADC_2_INVERT);
INIT_UNSET_PROPERTY(config.addonOptions.analogOptions, forced_circularity, !!FORCED_CIRCULARITY_ENABLED);
INIT_UNSET_PROPERTY(config.addonOptions.analogOptions, analog_deadzone, DEFAULT_ANALOG_DEADZONE);
INIT_UNSET_PROPERTY(config.addonOptions.analogOptions, inner_deadzone, DEFAULT_INNER_DEADZONE);
INIT_UNSET_PROPERTY(config.addonOptions.analogOptions, outer_deadzone, DEFAULT_OUTER_DEADZONE);
INIT_UNSET_PROPERTY(config.addonOptions.analogOptions, auto_calibrate, !!AUTO_CALIBRATE_ENABLED);

// addonOptions.turboOptions
Expand Down
6 changes: 4 additions & 2 deletions src/configs/webconfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1325,7 +1325,8 @@ std::string setAddonOptions()
docToValue(analogOptions.analogAdc2Mode, doc, "analogAdc2Mode");
docToValue(analogOptions.analogAdc2Invert, doc, "analogAdc2Invert");
docToValue(analogOptions.forced_circularity, doc, "forced_circularity");
docToValue(analogOptions.analog_deadzone, doc, "analog_deadzone");
docToValue(analogOptions.inner_deadzone, doc, "inner_deadzone");
docToValue(analogOptions.outer_deadzone, doc, "outer_deadzone");
docToValue(analogOptions.auto_calibrate, doc, "auto_calibrate");
docToValue(analogOptions.enabled, doc, "AnalogInputEnabled");

Expand Down Expand Up @@ -1740,7 +1741,8 @@ std::string getAddonOptions()
writeDoc(doc, "analogAdc2Mode", analogOptions.analogAdc2Mode);
writeDoc(doc, "analogAdc2Invert", analogOptions.analogAdc2Invert);
writeDoc(doc, "forced_circularity", analogOptions.forced_circularity);
writeDoc(doc, "analog_deadzone", analogOptions.analog_deadzone);
writeDoc(doc, "inner_deadzone", analogOptions.inner_deadzone);
writeDoc(doc, "outer_deadzone", analogOptions.outer_deadzone);
writeDoc(doc, "auto_calibrate", analogOptions.auto_calibrate);
writeDoc(doc, "AnalogInputEnabled", analogOptions.enabled);

Expand Down
3 changes: 2 additions & 1 deletion www/server/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,8 @@ app.get('/api/getAddonsOptions', (req, res) => {
analogAdc2Mode: 2,
analogAdc2Invert: 0,
forced_circularity: 0,
analog_deadzone: 5,
inner_deadzone: 5,
outer_deadzone: 95,
auto_calibrate: 0,
bootselButtonMap: 0,
buzzerPin: -1,
Expand Down
34 changes: 26 additions & 8 deletions www/src/Addons/Analog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,13 @@ export const analogScheme = {
.number()
.label('Force Circularity')
.validateRangeWhenValue('AnalogInputEnabled', 0, 1),
analog_deadzone: yup
inner_deadzone: yup
.number()
.label('Deadzone Size (%)')
.label('Inner Deadzone Size (%)')
.validateRangeWhenValue('AnalogInputEnabled', 0, 100),
outer_deadzone: yup
.number()
.label('Outer Deadzone Size (%)')
.validateRangeWhenValue('AnalogInputEnabled', 0, 100),
auto_calibrate: yup
.number()
Expand All @@ -82,7 +86,8 @@ export const analogState = {
analogAdc2Mode: 2,
analogAdc2Invert: 0,
forced_circularity: 0,
analog_deadzone: 5,
inner_deadzone: 5,
outer_deadzone: 95,
auto_calibrate: 0,
};

Expand Down Expand Up @@ -226,13 +231,26 @@ const Analog = ({ values, errors, handleChange, handleCheckbox }) => {
<Row className="mb-3">
<FormControl
type="number"
label={t('AddonsConfig:analog-deadzone-size')}
name="analog_deadzone"
label={t('AddonsConfig:inner-deadzone-size')}
name="inner_deadzone"
className="form-control-sm"
groupClassName="col-sm-3 mb-3"
value={values.inner_deadzone}
error={errors.inner_deadzone}
isInvalid={errors.inner_deadzone}
onChange={handleChange}
min={0}
max={100}
/>
<FormControl
type="number"
label={t('AddonsConfig:outer-deadzone-size')}
name="outer_deadzone"
className="form-control-sm"
groupClassName="col-sm-3 mb-3"
value={values.analog_deadzone}
error={errors.analog_deadzone}
isInvalid={errors.analog_deadzone}
value={values.outer_deadzone}
error={errors.outer_deadzone}
isInvalid={errors.outer_deadzone}
onChange={handleChange}
min={0}
max={100}
Expand Down
1 change: 0 additions & 1 deletion www/src/Locales/de-DE/AddonsConfig.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ export default {
'analog-adc-2-mode-label': 'Analog Stick 2 Modus',
'analog-adc-2-invert-label': 'Analog Stick 2 Invertieren',
'analog-force-circularity': 'Zirkularität erzwingen',
'analog-deadzone-size': 'Deadzone Größe (%)',
'analog-auto-calibrate': 'Auto Kalibrierung',
'turbo-header-text': 'Turbo',
'turbo-button-pin-label': 'Turbo Pin',
Expand Down
3 changes: 2 additions & 1 deletion www/src/Locales/en/AddonsConfig.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ export default {
'analog-adc-2-mode-label': 'Analog Stick 2 Mode',
'analog-adc-2-invert-label': 'Analog Stick 2 Invert',
'analog-force-circularity': 'Force Circularity',
'analog-deadzone-size': 'Deadzone Size (%)',
'inner-deadzone-size': 'Inner Deadzone Size (%)',
'outer-deadzone-size': 'Outer Deadzone Size (%)',
'analog-auto-calibrate': 'Auto Calibration',
'turbo-header-text': 'Turbo',
'turbo-button-pin-label': 'Turbo Pin',
Expand Down
1 change: 0 additions & 1 deletion www/src/Locales/pt-BR/AddonsConfig.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ export default {
'analog-adc-2-mode-label': 'Modo Analógico 2',
'analog-adc-2-invert-label': 'Inverter Analógico 2',
'analog-force-circularity': 'Forçar Circularidade',
'analog-deadzone-size': 'Tamanho da Zona Morta (%)',
'analog-auto-calibrate': 'Calibração Automática',
'turbo-header-text': 'Turbo',
'turbo-button-pin-label': 'Pino do Turbo',
Expand Down
1 change: 0 additions & 1 deletion www/src/Locales/zh-CN/AddonsConfig.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ export default {
'analog-adc-2-mode-label': '模拟摇杆2 模式',
'analog-adc-2-invert-label': '模拟摇杆2 反转',
'analog-force-circularity': '强制循环',
'analog-deadzone-size': '死区范围(%)',
'analog-auto-calibrate': '自动校准',
'turbo-header-text': 'Turbo 连发',
'turbo-button-pin-label': 'Turbo 引脚',
Expand Down

0 comments on commit cd757db

Please sign in to comment.