diff --git a/switch_input.h b/switch_input.h new file mode 100644 index 0000000..93b65e0 --- /dev/null +++ b/switch_input.h @@ -0,0 +1,72 @@ +#pragma once + +#include +#include + +#define SWITCH_PRO_HAT_UP 0x00 +#define SWITCH_PRO_HAT_UPRIGHT 0x01 +#define SWITCH_PRO_HAT_RIGHT 0x02 +#define SWITCH_PRO_HAT_DOWNRIGHT 0x03 +#define SWITCH_PRO_HAT_DOWN 0x04 +#define SWITCH_PRO_HAT_DOWNLEFT 0x05 +#define SWITCH_PRO_HAT_LEFT 0x06 +#define SWITCH_PRO_HAT_UPLEFT 0x07 +#define SWITCH_PRO_HAT_NOTHING 0x08 + +#define SWITCH_PRO_MASK_Y (1U << 0) +#define SWITCH_PRO_MASK_B (1U << 1) +#define SWITCH_PRO_MASK_A (1U << 2) +#define SWITCH_PRO_MASK_X (1U << 3) +#define SWITCH_PRO_MASK_L (1U << 4) +#define SWITCH_PRO_MASK_R (1U << 5) +#define SWITCH_PRO_MASK_ZL (1U << 6) +#define SWITCH_PRO_MASK_ZR (1U << 7) +#define SWITCH_PRO_MASK_MINUS (1U << 8) +#define SWITCH_PRO_MASK_PLUS (1U << 9) +#define SWITCH_PRO_MASK_L3 (1U << 10) +#define SWITCH_PRO_MASK_R3 (1U << 11) +#define SWITCH_PRO_MASK_HOME (1U << 12) +#define SWITCH_PRO_MASK_CAPTURE (1U << 13) + +#define SWITCH_PRO_JOYSTICK_MIN 0x0000 +#define SWITCH_PRO_JOYSTICK_MID 0x7FFF +#define SWITCH_PRO_JOYSTICK_MAX 0xFFFF + +typedef struct { + int16_t accel_x; + int16_t accel_y; + int16_t accel_z; + int16_t gyro_x; + int16_t gyro_y; + int16_t gyro_z; +} SwitchImuSample; + +typedef struct { + bool dpad_up; + bool dpad_down; + bool dpad_left; + bool dpad_right; + + bool button_a; + bool button_b; + bool button_x; + bool button_y; + bool button_l; + bool button_r; + bool button_zl; + bool button_zr; + bool button_plus; + bool button_minus; + bool button_home; + bool button_capture; + bool button_l3; + bool button_r3; + + uint16_t lx; + uint16_t ly; + uint16_t rx; + uint16_t ry; + + uint8_t imu_sample_count; + SwitchImuSample imu_samples[3]; +} SwitchInputState; diff --git a/tests/firmware/test_switch_input.cpp b/tests/firmware/test_switch_input.cpp new file mode 100644 index 0000000..11781e0 --- /dev/null +++ b/tests/firmware/test_switch_input.cpp @@ -0,0 +1,52 @@ +#include "test_support.h" + +#include + +#include "../../switch_input.h" + +namespace { + +bool switch_input_constants_match_legacy_values() { + // Given/When: the shared input constants are compiled after extraction. + // Then: every UART-visible legacy value remains unchanged. + CHECK(SWITCH_PRO_HAT_UP == 0x00 && SWITCH_PRO_HAT_UPRIGHT == 0x01); + CHECK(SWITCH_PRO_HAT_RIGHT == 0x02 && SWITCH_PRO_HAT_DOWNRIGHT == 0x03); + CHECK(SWITCH_PRO_HAT_DOWN == 0x04 && SWITCH_PRO_HAT_DOWNLEFT == 0x05); + CHECK(SWITCH_PRO_HAT_LEFT == 0x06 && SWITCH_PRO_HAT_UPLEFT == 0x07); + CHECK(SWITCH_PRO_HAT_NOTHING == 0x08); + CHECK(SWITCH_PRO_MASK_Y == (1U << 0) && SWITCH_PRO_MASK_B == (1U << 1)); + CHECK(SWITCH_PRO_MASK_A == (1U << 2) && SWITCH_PRO_MASK_X == (1U << 3)); + CHECK(SWITCH_PRO_MASK_L == (1U << 4) && SWITCH_PRO_MASK_R == (1U << 5)); + CHECK(SWITCH_PRO_MASK_ZL == (1U << 6) && SWITCH_PRO_MASK_ZR == (1U << 7)); + CHECK(SWITCH_PRO_MASK_MINUS == (1U << 8) && SWITCH_PRO_MASK_PLUS == (1U << 9)); + CHECK(SWITCH_PRO_MASK_L3 == (1U << 10) && SWITCH_PRO_MASK_R3 == (1U << 11)); + CHECK(SWITCH_PRO_MASK_HOME == (1U << 12) && SWITCH_PRO_MASK_CAPTURE == (1U << 13)); + CHECK(SWITCH_PRO_JOYSTICK_MIN == 0x0000); + CHECK(SWITCH_PRO_JOYSTICK_MID == 0x7FFF); + CHECK(SWITCH_PRO_JOYSTICK_MAX == 0xFFFF); + return true; +} + +bool switch_input_layout_matches_legacy_layout() { + // Given/When: the shared input types are compiled after extraction. + // Then: field offsets and aggregate sizes match the former driver-owned layout. + CHECK(sizeof(SwitchImuSample) == 12); + CHECK(offsetof(SwitchImuSample, accel_x) == 0); + CHECK(offsetof(SwitchImuSample, gyro_z) == 10); + CHECK(offsetof(SwitchInputState, dpad_up) == 0); + CHECK(offsetof(SwitchInputState, button_a) == 4); + CHECK(offsetof(SwitchInputState, button_r3) == 17); + CHECK(offsetof(SwitchInputState, lx) == 18); + CHECK(offsetof(SwitchInputState, ry) == 24); + CHECK(offsetof(SwitchInputState, imu_sample_count) == 26); + CHECK(offsetof(SwitchInputState, imu_samples) == 28); + CHECK(sizeof(SwitchInputState) == 64); + return true; +} + +} // namespace + +void run_switch_input_tests(TestRunner& runner) { + runner.run("shared input constants preserve legacy values", switch_input_constants_match_legacy_values); + runner.run("shared input types preserve legacy layout", switch_input_layout_matches_legacy_layout); +}