Files
MimiCon/main/input_calibration.c
T
dark98 21f54f1b92 Initial commit: MimiCon v1.0.0 - Universal Gamepad Mimic & Console Bridge
- Dual-Core Architecture: Dedicated 1000Hz USB gaming pipeline on Core 1 and BLE/Wi-Fi/Web telemetry on Core 0.
- Multi-Input BLE Host: Parsers for Xbox Wireless, Google Stadia, 8BitDo (Ultimate/Pro2/SN30), and Generic HOGP gamepads.
- Multi-Console USB Emulation: Nintendo Switch, PC DirectInput, PS3, PS4, Xbox 360 (libxsm3 cryptographic challenge-response), Original Xbox, and Couch Mouse/Keyboard.
- Onboard Web Portal & Captive Portal: Real-time SVG gamepad visualizer, stick deadzone calibration, button combo remapping, and Wi-Fi manager.
- Low-Latency Radio Management: Interactive SoftAP toggle to drop to 0-latency gaming mode.
- Dual OTA Firmware Flashing: Browser-based wireless firmware updater and automated Python OTA script.
- Strict Build Quality: Clean build under -Wall -Wextra with zero warnings or errors.
- Versioning & Licensing: Semantic Versioning v1.0.0 and GNU GPLv3 with Commercial Restrictions.
2026-10-17 18:26:50 +01:00

204 lines
6.7 KiB
C

#include "input_calibration.h"
#include "nvs_flash.h"
#include "nvs.h"
#include "esp_log.h"
#include "freertos/FreeRTOS.h"
#include "freertos/portmacro.h"
#include <string.h>
#include <math.h>
static const char *TAG = "INPUT_CALIB";
static const char *NVS_NAMESPACE = "calib_cfg";
static input_calibration_cfg_t s_cfg = {
.left_deadzone = 5,
.right_deadzone = 5,
.trigger_deadzone = 0,
.swap_ab = false,
.swap_xy = false,
.swap_bumpers_triggers = false,
.invert_lx = false,
.invert_ly = false,
.invert_rx = false,
.invert_ry = false,
};
static portMUX_TYPE s_calib_mutex = portMUX_INITIALIZER_UNLOCKED;
static void load_from_nvs(void)
{
nvs_handle_t nvs_h;
if (nvs_open(NVS_NAMESPACE, NVS_READONLY, &nvs_h) != ESP_OK) {
return;
}
uint8_t u8_val = 0;
if (nvs_get_u8(nvs_h, "ldz", &u8_val) == ESP_OK && u8_val <= 40) s_cfg.left_deadzone = u8_val;
if (nvs_get_u8(nvs_h, "rdz", &u8_val) == ESP_OK && u8_val <= 40) s_cfg.right_deadzone = u8_val;
if (nvs_get_u8(nvs_h, "tdz", &u8_val) == ESP_OK && u8_val <= 50) s_cfg.trigger_deadzone = u8_val;
if (nvs_get_u8(nvs_h, "sab", &u8_val) == ESP_OK) s_cfg.swap_ab = (u8_val != 0);
if (nvs_get_u8(nvs_h, "sxy", &u8_val) == ESP_OK) s_cfg.swap_xy = (u8_val != 0);
if (nvs_get_u8(nvs_h, "sbt", &u8_val) == ESP_OK) s_cfg.swap_bumpers_triggers = (u8_val != 0);
if (nvs_get_u8(nvs_h, "ilx", &u8_val) == ESP_OK) s_cfg.invert_lx = (u8_val != 0);
if (nvs_get_u8(nvs_h, "ily", &u8_val) == ESP_OK) s_cfg.invert_ly = (u8_val != 0);
if (nvs_get_u8(nvs_h, "irx", &u8_val) == ESP_OK) s_cfg.invert_rx = (u8_val != 0);
if (nvs_get_u8(nvs_h, "iry", &u8_val) == ESP_OK) s_cfg.invert_ry = (u8_val != 0);
nvs_close(nvs_h);
}
static esp_err_t save_to_nvs(const input_calibration_cfg_t *cfg)
{
nvs_handle_t nvs_h;
esp_err_t err = nvs_open(NVS_NAMESPACE, NVS_READWRITE, &nvs_h);
if (err != ESP_OK) return err;
nvs_set_u8(nvs_h, "ldz", cfg->left_deadzone);
nvs_set_u8(nvs_h, "rdz", cfg->right_deadzone);
nvs_set_u8(nvs_h, "tdz", cfg->trigger_deadzone);
nvs_set_u8(nvs_h, "sab", cfg->swap_ab ? 1 : 0);
nvs_set_u8(nvs_h, "sxy", cfg->swap_xy ? 1 : 0);
nvs_set_u8(nvs_h, "sbt", cfg->swap_bumpers_triggers ? 1 : 0);
nvs_set_u8(nvs_h, "ilx", cfg->invert_lx ? 1 : 0);
nvs_set_u8(nvs_h, "ily", cfg->invert_ly ? 1 : 0);
nvs_set_u8(nvs_h, "irx", cfg->invert_rx ? 1 : 0);
nvs_set_u8(nvs_h, "iry", cfg->invert_ry ? 1 : 0);
err = nvs_commit(nvs_h);
nvs_close(nvs_h);
return err;
}
esp_err_t input_calibration_init(void)
{
load_from_nvs();
ESP_LOGI(TAG, "Input calibration loaded: L-DZ=%d%%, R-DZ=%d%%, SwapAB=%d, SwapXY=%d",
s_cfg.left_deadzone, s_cfg.right_deadzone, s_cfg.swap_ab, s_cfg.swap_xy);
return ESP_OK;
}
void input_calibration_get_config(input_calibration_cfg_t *cfg)
{
if (!cfg) return;
portENTER_CRITICAL(&s_calib_mutex);
*cfg = s_cfg;
portEXIT_CRITICAL(&s_calib_mutex);
}
esp_err_t input_calibration_set_config(const input_calibration_cfg_t *cfg)
{
if (!cfg) return ESP_ERR_INVALID_ARG;
input_calibration_cfg_t validated = *cfg;
if (validated.left_deadzone > 40) validated.left_deadzone = 40;
if (validated.right_deadzone > 40) validated.right_deadzone = 40;
if (validated.trigger_deadzone > 50) validated.trigger_deadzone = 50;
portENTER_CRITICAL(&s_calib_mutex);
s_cfg = validated;
portEXIT_CRITICAL(&s_calib_mutex);
save_to_nvs(&validated);
ESP_LOGI(TAG, "Updated calibration: L-DZ=%d%%, R-DZ=%d%%, TrgDZ=%d",
validated.left_deadzone, validated.right_deadzone, validated.trigger_deadzone);
return ESP_OK;
}
esp_err_t input_calibration_reset_defaults(void)
{
input_calibration_cfg_t def = {
.left_deadzone = 5,
.right_deadzone = 5,
.trigger_deadzone = 0,
.swap_ab = false,
.swap_xy = false,
.swap_bumpers_triggers = false,
.invert_lx = false,
.invert_ly = false,
.invert_rx = false,
.invert_ry = false,
};
return input_calibration_set_config(&def);
}
static void apply_stick_deadzone(int16_t *x, int16_t *y, uint8_t deadzone_pct)
{
if (deadzone_pct == 0) return;
float fx = (float)*x;
float fy = (float)*y;
float mag = sqrtf(fx * fx + fy * fy);
float dz = (float)deadzone_pct * (32767.0f / 100.0f);
if (mag <= dz) {
*x = 0;
*y = 0;
} else {
float norm = (mag - dz) / (32767.0f - dz);
if (norm > 1.0f) norm = 1.0f;
*x = (int16_t)((fx / mag) * norm * 32767.0f);
*y = (int16_t)((fy / mag) * norm * 32767.0f);
}
}
void input_calibration_apply(const controller_state_t *in_raw, controller_state_t *out_calibrated)
{
if (!in_raw || !out_calibrated) return;
input_calibration_cfg_t cfg;
portENTER_CRITICAL(&s_calib_mutex);
cfg = s_cfg;
portEXIT_CRITICAL(&s_calib_mutex);
/* Copy raw state base */
*out_calibrated = *in_raw;
/* 1. Apply Radial Analog Deadzones */
apply_stick_deadzone(&out_calibrated->lx, &out_calibrated->ly, cfg.left_deadzone);
apply_stick_deadzone(&out_calibrated->rx, &out_calibrated->ry, cfg.right_deadzone);
/* 2. Apply Stick Inversions */
if (cfg.invert_lx) out_calibrated->lx = -out_calibrated->lx;
if (cfg.invert_ly) out_calibrated->ly = -out_calibrated->ly;
if (cfg.invert_rx) out_calibrated->rx = -out_calibrated->rx;
if (cfg.invert_ry) out_calibrated->ry = -out_calibrated->ry;
/* 3. Apply Trigger Deadzone */
if (cfg.trigger_deadzone > 0) {
if (out_calibrated->lt < cfg.trigger_deadzone) {
out_calibrated->lt = 0;
out_calibrated->btn_lt = 0;
}
if (out_calibrated->rt < cfg.trigger_deadzone) {
out_calibrated->rt = 0;
out_calibrated->btn_rt = 0;
}
}
/* 4. Apply Button Remapping / Swaps */
if (cfg.swap_ab) {
uint8_t tmp = out_calibrated->btn_a;
out_calibrated->btn_a = out_calibrated->btn_b;
out_calibrated->btn_b = tmp;
}
if (cfg.swap_xy) {
uint8_t tmp = out_calibrated->btn_x;
out_calibrated->btn_x = out_calibrated->btn_y;
out_calibrated->btn_y = tmp;
}
if (cfg.swap_bumpers_triggers) {
uint8_t tmp_lb = out_calibrated->btn_lb;
uint8_t tmp_rb = out_calibrated->btn_rb;
uint8_t tmp_lt = out_calibrated->lt;
uint8_t tmp_rt = out_calibrated->rt;
out_calibrated->btn_lb = (tmp_lt > 100) ? 1 : 0;
out_calibrated->btn_rb = (tmp_rt > 100) ? 1 : 0;
out_calibrated->lt = tmp_lb ? 255 : 0;
out_calibrated->rt = tmp_rb ? 255 : 0;
out_calibrated->btn_lt = (out_calibrated->lt > 0);
out_calibrated->btn_rt = (out_calibrated->rt > 0);
}
}