#pragma once #include #include #include #include "controller_state.h" #ifdef __cplusplus extern "C" { #endif typedef enum { CONTROLLER_TYPE_UNKNOWN = 0, CONTROLLER_TYPE_XBOX_BLE, CONTROLLER_TYPE_GENERIC_HOGP, CONTROLLER_TYPE_8BITDO, CONTROLLER_TYPE_STADIA, } controller_type_t; /** * @brief Scale an 8-bit analog stick axis (0..255, 128 centered) to signed 16-bit (-32768..32767) * with overflow protection. */ static inline int16_t scale_axis_8bit(uint8_t raw, bool invert) { int32_t val = invert ? ((int32_t)128 - raw) * 256 : ((int32_t)raw - 128) * 256; if (val > 32767) val = 32767; if (val < -32768) val = -32768; return (int16_t)val; } /** * @brief Parse a standard 0-indexed 8-way Hat Switch (D-Pad). * 0=Up, 1=Up-Right, 2=Right, 3=Down-Right, 4=Down, 5=Down-Left, 6=Left, 7=Up-Left, 8=Centered */ static inline void parse_hat_switch_8way(uint8_t hat, controller_state_t *out_state) { hat &= 0x0F; if (hat < 8) { out_state->dpad_up = (hat == 0 || hat == 1 || hat == 7) ? 1 : 0; out_state->dpad_right = (hat == 1 || hat == 2 || hat == 3) ? 1 : 0; out_state->dpad_down = (hat == 3 || hat == 4 || hat == 5) ? 1 : 0; out_state->dpad_left = (hat == 5 || hat == 6 || hat == 7) ? 1 : 0; } } /** * @brief Base parser function prototype */ typedef bool (*controller_parse_fn_t)(const uint8_t *report, size_t len, controller_state_t *out_state); typedef struct { const char *name; controller_type_t type; controller_parse_fn_t parse; } controller_driver_t; #ifdef __cplusplus } #endif