Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom analog error rate #1136

Merged
merged 8 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion headers/addons/analog.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@
#define SMOOTHING_FACTOR 5
#endif

#ifndef ANALOG_ERROR
#define ANALOG_ERROR 1000
#endif

// Analog Module Name
#define AnalogName "Analog"

Expand All @@ -88,7 +92,7 @@ class AnalogInput : public GPAddon {
static float readPin(int pin, uint16_t center, bool autoCalibrate);
static float emaCalculation(float ema_value, float smoothing_factor, float ema_previous);
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 float magnitudeCalculation(float x, float y, float& x_magnitude, float& y_magnitude, float error);
static void radialDeadzone(float& x, float& y, float in_deadzone, float out_deadzone, float x_magnitude, float y_magnitude, float magnitude, bool circularity);
};

Expand Down
1 change: 1 addition & 0 deletions proto/config.proto
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ message AnalogOptions
optional uint32 outer_deadzone = 13;
optional bool analog_smoothing = 14;
optional float smoothing_factor = 15;
optional uint32 analog_error = 16;
}

message TurboOptions
Expand Down
7 changes: 4 additions & 3 deletions src/addons/analog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ void AnalogInput::process()

bool ema_option = analogOptions.analog_smoothing;
float ema_smoothing = analogOptions.smoothing_factor / 1000.0f;
float error_rate = analogOptions.analog_error / 1000.0f;

struct adc_pair
{
Expand Down Expand Up @@ -137,7 +138,7 @@ void AnalogInput::process()

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);
adc_pairs[i].x_magnitude, adc_pairs[i].y_magnitude, error_rate);

if (in_deadzone >= 0.0f) {
if (adc_pairs[i].xy_magnitude < in_deadzone) {
Expand Down Expand Up @@ -196,11 +197,11 @@ uint16_t AnalogInput::map(uint16_t x, uint16_t in_min, uint16_t in_max, uint16_t
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

float AnalogInput::magnitudeCalculation(float x, float y, float& x_magnitude, float& y_magnitude) {
float AnalogInput::magnitudeCalculation(float x, float y, float& x_magnitude, float& y_magnitude, float error) {
x_magnitude = x - ANALOG_CENTER;
y_magnitude = y - ANALOG_CENTER;

return std::sqrt((x_magnitude * x_magnitude) + (y_magnitude * y_magnitude));
return error * std::sqrt((x_magnitude * x_magnitude) + (y_magnitude * y_magnitude));
}

void AnalogInput::radialDeadzone(float& x, float& y, float in_deadzone, float out_deadzone, float x_magnitude, float y_magnitude, float xy_magnitude, bool circularity) {
Expand Down
1 change: 1 addition & 0 deletions src/config_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,7 @@ void ConfigUtils::initUnsetPropertiesWithDefaults(Config& config)
INIT_UNSET_PROPERTY(config.addonOptions.analogOptions, auto_calibrate, !!AUTO_CALIBRATE_ENABLED);
INIT_UNSET_PROPERTY(config.addonOptions.analogOptions, analog_smoothing, !!ANALOG_SMOOTHING_ENABLED);
INIT_UNSET_PROPERTY(config.addonOptions.analogOptions, smoothing_factor, !!SMOOTHING_FACTOR);
INIT_UNSET_PROPERTY(config.addonOptions.analogOptions, analog_error, ANALOG_ERROR);

// addonOptions.turboOptions
INIT_UNSET_PROPERTY(config.addonOptions.turboOptions, enabled, !!TURBO_ENABLED);
Expand Down
2 changes: 2 additions & 0 deletions src/configs/webconfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1398,6 +1398,7 @@ std::string setAddonOptions()
docToValue(analogOptions.auto_calibrate, doc, "auto_calibrate");
docToValue(analogOptions.analog_smoothing, doc, "analog_smoothing");
docToValue(analogOptions.smoothing_factor, doc, "smoothing_factor");
docToValue(analogOptions.analog_error, doc, "analog_error");
docToValue(analogOptions.enabled, doc, "AnalogInputEnabled");

BootselButtonOptions& bootselButtonOptions = Storage::getInstance().getAddonOptions().bootselButtonOptions;
Expand Down Expand Up @@ -1824,6 +1825,7 @@ std::string getAddonOptions()
writeDoc(doc, "auto_calibrate", analogOptions.auto_calibrate);
writeDoc(doc, "analog_smoothing", analogOptions.analog_smoothing);
writeDoc(doc, "smoothing_factor", analogOptions.smoothing_factor);
writeDoc(doc, "analog_error", analogOptions.analog_error);
writeDoc(doc, "AnalogInputEnabled", analogOptions.enabled);

const BootselButtonOptions& bootselButtonOptions = Storage::getInstance().getAddonOptions().bootselButtonOptions;
Expand Down
1 change: 1 addition & 0 deletions www/server/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,7 @@ app.get('/api/getAddonsOptions', (req, res) => {
auto_calibrate: 0,
analog_smoothing: 0,
smoothing_factor: 5,
analog_error: 1000,
bootselButtonMap: 0,
buzzerPin: -1,
buzzerEnablePin: -1,
Expand Down
69 changes: 55 additions & 14 deletions www/src/Addons/Analog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,25 @@ const INVERT_MODES = [
{ label: 'X/Y Axis', value: 3 },
];

const ANALOG_ERROR_RATES = [
{ label: '0%', value: 1000 },
{ label: '1%', value: 990 },
{ label: '2%', value: 979 },
{ label: '3%', value: 969 },
{ label: '4%', value: 958 },
{ label: '5%', value: 946 },
{ label: '6%', value: 934 },
{ label: '7%', value: 922 },
{ label: '8%', value: 911 },
{ label: '9%', value: 900 },
{ label: '10%', value: 890 },
{ label: '11%', value: 876 },
{ label: '12%', value: 863 },
{ label: '13%', value: 848 },
{ label: '14%', value: 834 },
{ label: '15%', value: 821 },
];

export const analogScheme = {
AnalogInputEnabled: yup.number().required().label('Analog Input Enabled'),
analogAdc1PinX: yup
Expand Down Expand Up @@ -81,6 +100,10 @@ export const analogScheme = {
.number()
.label('Smoothing Factor')
.validateRangeWhenValue('AnalogInputEnabled', 0, 100),
analog_error: yup
.number()
.label('Error Rate')
.validateSelectionWhenValue('AnalogInputEnabled', ANALOG_ERROR_RATES),
};

export const analogState = {
Expand All @@ -99,6 +122,7 @@ export const analogState = {
auto_calibrate: 0,
analog_smoothing: 0,
smoothing_factor: 5,
analog_error: 1,
};

const Analog = ({ values, errors, handleChange, handleCheckbox }) => {
Expand All @@ -107,6 +131,7 @@ const Analog = ({ values, errors, handleChange, handleCheckbox }) => {
const availableAnalogPins = ANALOG_PINS.filter(
(pin) => !usedPins?.includes(pin),
);

return (
<Section title={t('AddonsConfig:analog-header-text')}>
<div id="AnalogInputOptions" hidden={!values.AnalogInputEnabled}>
Expand Down Expand Up @@ -265,6 +290,36 @@ const Analog = ({ values, errors, handleChange, handleCheckbox }) => {
min={0}
max={100}
/>

<FormSelect
hidden={!values.forced_circularity}
label={t('AddonsConfig:analog-error-label')}
name="analog_error"
className="form-control-sm"
groupClassName="col-sm-3 mb-3"
value={values.analog_error}
onChange={handleChange}
>
{ANALOG_ERROR_RATES.map((o, i) => (
<option key={`analog_error-option-${i}`} value={o.value}>
{o.label}
</option>
))}
</FormSelect>
<FormControl
hidden={!values.analog_smoothing}
type="number"
label={t('AddonsConfig:smoothing-factor')}
name="smoothing_factor"
className="form-control-sm"
groupClassName="col-sm-3 mb-3"
value={values.smoothing_factor}
error={errors.smoothing_factor}
isInvalid={errors.smoothing_factor}
onChange={handleChange}
min={0}
max={100}
/>
<FormCheck
label={t('AddonsConfig:analog-force-circularity')}
type="switch"
Expand Down Expand Up @@ -301,20 +356,6 @@ const Analog = ({ values, errors, handleChange, handleCheckbox }) => {
handleChange(e);
}}
/>
<FormControl
hidden={!values.analog_smoothing}
type="number"
label={t('AddonsConfig:smoothing-factor')}
name="smoothing_factor"
className="form-control-sm"
groupClassName="col-sm-3 mb-3"
value={values.smoothing_factor}
error={errors.smoothing_factor}
isInvalid={errors.smoothing_factor}
onChange={handleChange}
min={0}
max={100}
/>
</Row>
</div>
<FormCheck
Expand Down
59 changes: 29 additions & 30 deletions www/src/Addons/FocusMode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,36 +93,35 @@ const FocusMode = ({
/>
</div>
<Row>
{BUTTON_MASKS_OPTIONS.map((mask) =>
values.focusModeButtonLockMask & mask.value ? (
<FormSelect
key={`focusModeButtonLockMask-${mask.label}`}
name="focusModeButtonLockMask"
className="form-select-sm"
groupClassName="col-sm-3 mb-3"
value={values.focusModeButtonLockMask & mask.value}
error={errors.focusModeButtonLockMask}
isInvalid={errors.focusModeButtonLockMask}
onChange={(e) => {
setFieldValue(
'focusModeButtonLockMask',
(values.focusModeButtonLockMask ^ mask.value) |
e.target.value,
);
}}
>
{BUTTON_MASKS_OPTIONS.map((o, i) => (
<option
key={`focusModeButtonLockMask-option-${i}`}
value={o.value}
>
{o.label}
</option>
))}
</FormSelect>
) : (
<></>
),
{BUTTON_MASKS_OPTIONS.map(
(mask) =>
Boolean(values.focusModeButtonLockMask & mask.value) && (
<FormSelect
key={`focusModeButtonLockMask-${mask.label}`}
name="focusModeButtonLockMask"
className="form-select-sm"
groupClassName="col-sm-3 mb-3"
value={values.focusModeButtonLockMask & mask.value}
error={errors.focusModeButtonLockMask}
isInvalid={errors.focusModeButtonLockMask}
onChange={(e) => {
setFieldValue(
'focusModeButtonLockMask',
(values.focusModeButtonLockMask ^ mask.value) |
e.target.value,
);
}}
>
{BUTTON_MASKS_OPTIONS.map((o, i) => (
<option
key={`focusModeButtonLockMask-option-${i}`}
value={o.value}
>
{o.label}
</option>
))}
</FormSelect>
),
)}
<FormSelect
name="focusModeButtonLockMask"
Expand Down
19 changes: 0 additions & 19 deletions www/src/Components/FormSelect.jsx

This file was deleted.

27 changes: 27 additions & 0 deletions www/src/Components/FormSelect.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import React from 'react';
import { Form, FormSelectProps } from 'react-bootstrap';

type FormSelectTypes = {
label?: string;
error?: string;
groupClassName?: string;
hidden?: boolean;
} & FormSelectProps;

const FormSelect = ({
label,
error,
groupClassName = '',
hidden = false,
...props
}: FormSelectTypes) => {
return (
<Form.Group className={groupClassName} hidden={hidden}>
{label && <Form.Label>{label}</Form.Label>}
<Form.Select {...props} />
<Form.Control.Feedback type="invalid">{error}</Form.Control.Feedback>
</Form.Group>
);
};

export default FormSelect;
1 change: 1 addition & 0 deletions www/src/Locales/en/AddonsConfig.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export default {
'analog-auto-calibrate': 'Auto Calibration',
'analog-smoothing': 'Analog Smoothing',
'smoothing-factor': 'Smoothing Factor',
'analog-error-label': 'Error Rate',
'turbo-header-text': 'Turbo',
'turbo-button-pin-label': 'Turbo Pin',
'turbo-led-pin-label': 'Turbo LED Pin',
Expand Down