2024-03-27 19:34:14 +01:00
/**
* Storage for controller configuration, including helper functions & types, as well as sane defaults.
2024-03-29 16:07:44 +01:00
* Also includes necessary logic for configuring the controller & calibrating the sticks.
2024-03-27 19:34:14 +01:00
*/
2024-04-03 17:17:21 +02:00
use core ::{ cmp ::min , f32 ::consts ::PI };
2024-03-27 19:34:14 +01:00
2024-03-29 23:42:05 +01:00
use defmt ::{ debug , error , info , warn , Format };
2024-04-01 23:54:08 +02:00
use embassy_futures ::yield_now ;
2024-03-27 19:34:14 +01:00
use embassy_rp ::{
2024-04-03 17:17:21 +02:00
flash ::{ Async , Flash , ERASE_SIZE },
2024-03-27 19:34:14 +01:00
peripherals ::FLASH ,
};
2024-04-08 20:53:23 +00:00
use packed_struct ::{
derive ::{ PackedStruct , PrimitiveEnum_u8 },
PackedStruct ,
};
2024-03-27 19:34:14 +01:00
use crate ::{
2024-04-01 23:54:08 +02:00
helpers ::{ PackedFloat , ToPackedFloatArray , ToRegularArray , XyValuePair },
2024-10-14 19:55:54 +00:00
hid ::gcc ::{ GcButtons1 , GcButtons2 , GcState },
2024-03-29 23:42:05 +01:00
input ::{
2024-12-28 03:50:33 +00:00
read_ext_adc , ControllerState , Stick , StickAxis , FLOAT_ORIGIN , SPI_ACS_SHARED ,
SPI_CCS_SHARED , SPI_SHARED ,
2024-03-29 23:42:05 +01:00
},
2024-03-29 21:31:55 +01:00
stick ::{
2024-04-01 23:07:06 +02:00
calc_stick_values , legalize_notches , AppliedCalibration , CleanedCalibrationPoints ,
LinearizedCalibration , NotchCalibration , NotchStatus , CALIBRATION_ORDER ,
NOTCH_ADJUSTMENT_ORDER , NO_OF_ADJ_NOTCHES , NO_OF_CALIBRATION_POINTS , NO_OF_NOTCHES ,
2024-03-29 21:31:55 +01:00
},
2024-10-14 19:55:54 +00:00
usb_comms ::{ MUTEX_INPUT_CONSISTENCY_MODE , SIGNAL_CHANGE_RUMBLE_STRENGTH },
2024-03-27 19:34:14 +01:00
ADDR_OFFSET , FLASH_SIZE ,
};
2024-03-29 19:54:52 +01:00
use embassy_sync ::{
2024-03-30 13:20:35 +01:00
blocking_mutex ::raw ::{ CriticalSectionRawMutex , RawMutex , ThreadModeRawMutex },
2024-04-03 17:17:21 +02:00
pubsub ::Subscriber ,
2024-03-29 19:54:52 +01:00
signal ::Signal ,
};
2024-04-20 11:39:58 +02:00
use embassy_time ::Timer ;
2024-03-29 16:07:44 +01:00
2024-12-28 03:50:33 +00:00
use crate ::input ::CHANNEL_CONTROLLER_STATE ;
2024-03-29 16:07:44 +01:00
2024-03-29 19:54:52 +01:00
/// Whether we are currently calibrating the sticks. Updates are dispatched when the status changes.
/// Initial status is assumed to be false.
2024-03-30 13:20:35 +01:00
pub static SIGNAL_IS_CALIBRATING : Signal < ThreadModeRawMutex , bool > = Signal ::new ();
2024-03-29 19:54:52 +01:00
2024-04-01 23:54:08 +02:00
/// Config change signalled to the stick task.
pub static SIGNAL_CONFIG_CHANGE : Signal < ThreadModeRawMutex , ControllerConfig > = Signal ::new ();
2024-03-29 23:42:05 +01:00
/// Signal used to override the stick state in order to display desired stick positions during calibration.
pub static SIGNAL_OVERRIDE_STICK_STATE : Signal <
CriticalSectionRawMutex ,
Option < OverrideStickState > ,
> = Signal ::new ();
2024-03-29 19:54:52 +01:00
/// Dispatched when we want to override the GCC state for a short amount of time.
2024-12-28 03:50:33 +00:00
pub static SIGNAL_OVERRIDE_CONTROLLER_STATE : Signal <
CriticalSectionRawMutex ,
OverrideGcReportInstruction ,
> = Signal ::new ();
2024-03-29 19:54:52 +01:00
2024-04-02 22:05:57 +02:00
/// Dispatched when we want to enter config mode, sent from core1 so config mode
/// doesn't need to watch for the entire button combo every time.
static SIGNAL_CONFIG_MODE_STATUS_CHANGE : Signal < CriticalSectionRawMutex , bool > = Signal ::new ();
const ABS_MAX_SNAPBACK : i8 = 10 ;
const MAX_WAVESHAPING : i8 = 15 ;
const MAX_SMOOTHING : i8 = 9 ;
const MAX_RUMBLE_STRENGTH : i8 = 11 ;
const MAX_CARDINAL_SNAP : i8 = 6 ;
const MIN_ANALOG_SCALER : u8 = 82 ;
const MAX_ANALOG_SCALER : u8 = 125 ;
2024-03-29 19:54:52 +01:00
/// Struct used for overriding the GCC state for a given amount of
/// time, useful for providing feedback to the user e.g. if we just entered
/// a certain mode.
#[derive(Default, Debug, Clone, Format)]
pub struct OverrideGcReportInstruction {
2024-12-28 03:50:33 +00:00
pub report : ControllerState ,
2024-03-29 19:54:52 +01:00
pub duration_ms : u64 ,
}
2024-03-29 16:07:44 +01:00
const CONFIG_MODE_ENTRY_COMBO : [ AwaitableButtons ; 4 ] = [
AwaitableButtons ::Start ,
AwaitableButtons ::A ,
AwaitableButtons ::X ,
AwaitableButtons ::Y ,
];
2024-04-02 22:05:57 +02:00
const LSTICK_CALIBRATION_COMBO : [ AwaitableButtons ; 4 ] = [
AwaitableButtons ::A ,
AwaitableButtons ::X ,
AwaitableButtons ::Y ,
AwaitableButtons ::L ,
];
2024-03-29 19:54:52 +01:00
2024-04-02 22:05:57 +02:00
const RSTICK_CALIBRATION_COMBO : [ AwaitableButtons ; 4 ] = [
AwaitableButtons ::A ,
AwaitableButtons ::Y ,
AwaitableButtons ::X ,
AwaitableButtons ::R ,
];
const LSTICK_SNAPBACK_X_INCREASE_COMBO : [ AwaitableButtons ; 4 ] = [
AwaitableButtons ::A ,
AwaitableButtons ::X ,
AwaitableButtons ::Up ,
2024-04-03 16:53:52 +00:00
AwaitableButtons ::NotZ ,
2024-04-02 22:05:57 +02:00
];
const LSTICK_SNAPBACK_Y_INCREASE_COMBO : [ AwaitableButtons ; 4 ] = [
AwaitableButtons ::A ,
2024-04-03 16:53:52 +00:00
AwaitableButtons ::NotZ ,
2024-04-02 22:05:57 +02:00
AwaitableButtons ::Up ,
AwaitableButtons ::Y ,
];
const LSTICK_SNAPBACK_X_DECREASE_COMBO : [ AwaitableButtons ; 4 ] = [
AwaitableButtons ::A ,
AwaitableButtons ::X ,
AwaitableButtons ::Down ,
2024-04-03 16:53:52 +00:00
AwaitableButtons ::NotZ ,
2024-04-02 22:05:57 +02:00
];
const LSTICK_SNAPBACK_Y_DECREASE_COMBO : [ AwaitableButtons ; 4 ] = [
AwaitableButtons ::A ,
AwaitableButtons ::Y ,
AwaitableButtons ::Down ,
2024-04-03 16:53:52 +00:00
AwaitableButtons ::NotZ ,
2024-04-02 22:05:57 +02:00
];
const RSTICK_SNAPBACK_X_INCREASE_COMBO : [ AwaitableButtons ; 4 ] = [
AwaitableButtons ::A ,
AwaitableButtons ::X ,
AwaitableButtons ::Up ,
AwaitableButtons ::Z ,
];
const RSTICK_SNAPBACK_Y_INCREASE_COMBO : [ AwaitableButtons ; 4 ] = [
AwaitableButtons ::A ,
AwaitableButtons ::Y ,
AwaitableButtons ::Up ,
AwaitableButtons ::Z ,
];
const RSTICK_SNAPBACK_X_DECREASE_COMBO : [ AwaitableButtons ; 4 ] = [
AwaitableButtons ::A ,
AwaitableButtons ::X ,
AwaitableButtons ::Down ,
AwaitableButtons ::Z ,
];
const RSTICK_SNAPBACK_Y_DECREASE_COMBO : [ AwaitableButtons ; 4 ] = [
AwaitableButtons ::A ,
AwaitableButtons ::Y ,
AwaitableButtons ::Down ,
AwaitableButtons ::Z ,
];
const LSTICK_WAVESHAPING_X_INCREASE_COMBO : [ AwaitableButtons ; 4 ] = [
AwaitableButtons ::X ,
AwaitableButtons ::Up ,
2024-04-03 16:53:52 +00:00
AwaitableButtons ::NotZ ,
2024-04-02 22:05:57 +02:00
AwaitableButtons ::L ,
];
const LSTICK_WAVESHAPING_Y_INCREASE_COMBO : [ AwaitableButtons ; 4 ] = [
AwaitableButtons ::Y ,
AwaitableButtons ::Up ,
2024-04-03 16:53:52 +00:00
AwaitableButtons ::NotZ ,
2024-04-02 22:05:57 +02:00
AwaitableButtons ::L ,
];
const LSTICK_WAVESHAPING_X_DECREASE_COMBO : [ AwaitableButtons ; 4 ] = [
AwaitableButtons ::X ,
AwaitableButtons ::Down ,
2024-04-03 16:53:52 +00:00
AwaitableButtons ::NotZ ,
2024-04-02 22:05:57 +02:00
AwaitableButtons ::L ,
];
const LSTICK_WAVESHAPING_Y_DECREASE_COMBO : [ AwaitableButtons ; 4 ] = [
AwaitableButtons ::Y ,
AwaitableButtons ::Down ,
2024-04-03 16:53:52 +00:00
AwaitableButtons ::NotZ ,
2024-04-02 22:05:57 +02:00
AwaitableButtons ::L ,
];
const RSTICK_WAVESHAPING_X_INCREASE_COMBO : [ AwaitableButtons ; 4 ] = [
AwaitableButtons ::X ,
AwaitableButtons ::Up ,
AwaitableButtons ::Z ,
AwaitableButtons ::L ,
];
const RSTICK_WAVESHAPING_Y_INCREASE_COMBO : [ AwaitableButtons ; 4 ] = [
AwaitableButtons ::Y ,
AwaitableButtons ::Up ,
AwaitableButtons ::Z ,
AwaitableButtons ::L ,
];
const RSTICK_WAVESHAPING_X_DECREASE_COMBO : [ AwaitableButtons ; 4 ] = [
AwaitableButtons ::X ,
AwaitableButtons ::Down ,
AwaitableButtons ::Z ,
AwaitableButtons ::L ,
];
const RSTICK_WAVESHAPING_Y_DECREASE_COMBO : [ AwaitableButtons ; 4 ] = [
AwaitableButtons ::Y ,
AwaitableButtons ::Down ,
AwaitableButtons ::Z ,
AwaitableButtons ::L ,
];
const LSTICK_SMOOTH_X_INCREASE_COMBO : [ AwaitableButtons ; 4 ] = [
AwaitableButtons ::R ,
AwaitableButtons ::X ,
AwaitableButtons ::Up ,
2024-04-03 16:53:52 +00:00
AwaitableButtons ::NotZ ,
2024-04-02 22:05:57 +02:00
];
const LSTICK_SMOOTH_Y_INCREASE_COMBO : [ AwaitableButtons ; 4 ] = [
AwaitableButtons ::R ,
AwaitableButtons ::Y ,
AwaitableButtons ::Up ,
2024-04-03 16:53:52 +00:00
AwaitableButtons ::NotZ ,
2024-04-02 22:05:57 +02:00
];
const LSTICK_SMOOTH_X_DECREASE_COMBO : [ AwaitableButtons ; 4 ] = [
AwaitableButtons ::R ,
AwaitableButtons ::X ,
AwaitableButtons ::Down ,
2024-04-03 16:53:52 +00:00
AwaitableButtons ::NotZ ,
2024-04-02 22:05:57 +02:00
];
const LSTICK_SMOOTH_Y_DECREASE_COMBO : [ AwaitableButtons ; 4 ] = [
AwaitableButtons ::R ,
AwaitableButtons ::Y ,
AwaitableButtons ::Down ,
2024-04-03 16:53:52 +00:00
AwaitableButtons ::NotZ ,
2024-04-02 22:05:57 +02:00
];
const RSTICK_SMOOTH_X_INCREASE_COMBO : [ AwaitableButtons ; 4 ] = [
AwaitableButtons ::Z ,
AwaitableButtons ::Y ,
AwaitableButtons ::Up ,
AwaitableButtons ::R ,
];
const RSTICK_SMOOTH_Y_INCREASE_COMBO : [ AwaitableButtons ; 4 ] = [
AwaitableButtons ::Z ,
AwaitableButtons ::X ,
AwaitableButtons ::Up ,
AwaitableButtons ::R ,
];
const RSTICK_SMOOTH_X_DECREASE_COMBO : [ AwaitableButtons ; 4 ] = [
AwaitableButtons ::Z ,
AwaitableButtons ::Y ,
AwaitableButtons ::Down ,
AwaitableButtons ::R ,
];
const RSTICK_SMOOTH_Y_DECREASE_COMBO : [ AwaitableButtons ; 4 ] = [
AwaitableButtons ::Z ,
AwaitableButtons ::X ,
AwaitableButtons ::Down ,
AwaitableButtons ::R ,
];
const LSTICK_CARDINALSNAP_INCREASE_COMBO : [ AwaitableButtons ; 4 ] = [
AwaitableButtons ::R ,
AwaitableButtons ::A ,
AwaitableButtons ::Up ,
2024-04-03 16:53:52 +00:00
AwaitableButtons ::NotZ ,
2024-04-02 22:05:57 +02:00
];
const LSTICK_CARDINALSNAP_DECREASE_COMBO : [ AwaitableButtons ; 4 ] = [
AwaitableButtons ::R ,
AwaitableButtons ::A ,
AwaitableButtons ::Down ,
2024-04-03 16:53:52 +00:00
AwaitableButtons ::NotZ ,
2024-04-02 22:05:57 +02:00
];
const RSTICK_CARDINALSNAP_INCREASE_COMBO : [ AwaitableButtons ; 4 ] = [
AwaitableButtons ::Z ,
AwaitableButtons ::A ,
AwaitableButtons ::Up ,
AwaitableButtons ::R ,
];
const RSTICK_CARDINALSNAP_DECREASE_COMBO : [ AwaitableButtons ; 4 ] = [
AwaitableButtons ::Z ,
AwaitableButtons ::A ,
AwaitableButtons ::Down ,
AwaitableButtons ::R ,
];
const LSTICK_SCALING_INCREASE_COMBO : [ AwaitableButtons ; 4 ] = [
AwaitableButtons ::L ,
AwaitableButtons ::A ,
AwaitableButtons ::Up ,
2024-04-03 16:53:52 +00:00
AwaitableButtons ::NotZ ,
2024-04-02 22:05:57 +02:00
];
const LSTICK_SCALING_DECREASE_COMBO : [ AwaitableButtons ; 4 ] = [
AwaitableButtons ::L ,
AwaitableButtons ::A ,
AwaitableButtons ::Down ,
2024-04-03 16:53:52 +00:00
AwaitableButtons ::NotZ ,
2024-04-02 22:05:57 +02:00
];
const RSTICK_SCALING_INCREASE_COMBO : [ AwaitableButtons ; 4 ] = [
AwaitableButtons ::L ,
AwaitableButtons ::A ,
AwaitableButtons ::Up ,
AwaitableButtons ::Z ,
];
const RSTICK_SCALING_DECREASE_COMBO : [ AwaitableButtons ; 4 ] = [
AwaitableButtons ::L ,
AwaitableButtons ::A ,
AwaitableButtons ::Down ,
AwaitableButtons ::Z ,
];
const RUMBLE_STRENGTH_INCREASE_COMBO : [ AwaitableButtons ; 4 ] = [
AwaitableButtons ::B ,
AwaitableButtons ::A ,
AwaitableButtons ::Up ,
AwaitableButtons ::Wildcard ,
];
const RUMBLE_STRENGTH_DECREASE_COMBO : [ AwaitableButtons ; 4 ] = [
AwaitableButtons ::B ,
AwaitableButtons ::A ,
AwaitableButtons ::Down ,
AwaitableButtons ::Wildcard ,
];
const INPUT_CONSISTENCY_TOGGLE_COMBO : [ AwaitableButtons ; 4 ] = [
AwaitableButtons ::A ,
AwaitableButtons ::Z ,
AwaitableButtons ::Start ,
AwaitableButtons ::Wildcard ,
];
2024-04-06 21:05:22 +02:00
const DISPLAY_WAVESHAPING_COMBO : [ AwaitableButtons ; 4 ] = [
AwaitableButtons ::L ,
AwaitableButtons ::Start ,
AwaitableButtons ::Wildcard ,
AwaitableButtons ::Wildcard ,
];
const DISPLAY_SMOOTHING_COMBO : [ AwaitableButtons ; 4 ] = [
AwaitableButtons ::R ,
AwaitableButtons ::Start ,
AwaitableButtons ::Wildcard ,
AwaitableButtons ::Wildcard ,
];
const DISPLAY_SNAPBACK_COMBO : [ AwaitableButtons ; 4 ] = [
AwaitableButtons ::Up ,
AwaitableButtons ::Start ,
AwaitableButtons ::Wildcard ,
AwaitableButtons ::Wildcard ,
];
2024-04-02 22:05:57 +02:00
const EXIT_CONFIG_MODE_COMBO : [ AwaitableButtons ; 4 ] = [
AwaitableButtons ::A ,
AwaitableButtons ::X ,
AwaitableButtons ::Y ,
AwaitableButtons ::Start ,
];
2024-03-29 19:54:52 +01:00
2024-03-29 16:07:44 +01:00
/// This doesn't need to be super fast, since it's only used
/// in config mode.
const BUTTON_POLL_INTERVAL_MILLIS : u64 = 20 ;
2024-03-27 19:34:14 +01:00
pub const DEFAULT_NOTCH_STATUS : [ NotchStatus ; NO_OF_NOTCHES ] = [
NotchStatus ::Cardinal ,
NotchStatus ::TertActive ,
NotchStatus ::Secondary ,
NotchStatus ::TertActive ,
NotchStatus ::Cardinal ,
NotchStatus ::TertActive ,
NotchStatus ::Secondary ,
NotchStatus ::TertActive ,
NotchStatus ::Cardinal ,
NotchStatus ::TertActive ,
NotchStatus ::Secondary ,
NotchStatus ::TertActive ,
NotchStatus ::Cardinal ,
NotchStatus ::TertActive ,
NotchStatus ::Secondary ,
NotchStatus ::TertActive ,
];
#[rustfmt::skip]
const DEFAULT_CAL_POINTS_X : [ f32 ; NO_OF_CALIBRATION_POINTS ] = [
2024-04-02 22:05:57 +02:00
0.5279579 , 0.37779236 , // right
0.5280895 , 0.52808 ,
0.5280571 , 0.41205978 , // up right
0.5280628 , 0.5280857 ,
0.528059 , 0.5217171 , // up
0.5281048 , 0.52808 ,
0.52776146 , 0.6376648 , // up left
0.5280838 , 0.5280762 ,
0.5277729 , 0.6680012 , // left
0.5280876 , 0.5280762 ,
0.5289936 , 0.64232254 , // down left
0.52809143 , 0.52807236 ,
0.528574 , 0.52160454 , // down
0.52809143 , 0.5281086 ,
0.5288601 , 0.4079399 , // down right
0.5281067 , 0.52807426
2024-03-27 19:34:14 +01:00
];
2024-04-02 22:05:57 +02:00
// cal_points_y: [PackedFloat(0.50214195), PackedFloat(0.51600456), PackedFloat(0.5022125), PackedFloat(0.5021801), PackedFloat(0.5019417), PackedFloat(0.39642334), PackedFloat(0.5022106), PackedFloat(0.5021858), PackedFloat(0.5018635), PackedFloat(0.3603382), PackedFloat(0.5022011), PackedFloat(0.5022621), PackedFloat(0.5016308), PackedFloat(0.4004345), PackedFloat(0.5022621), PackedFloat(0.5022583), PackedFloat(0.501112), PackedFloat(0.51377296), PackedFloat(0.5022392), PackedFloat(0.5022774), PackedFloat(0.5010319), PackedFloat(0.62000275), PackedFloat(0.50230026), PackedFloat(0.5023346), PackedFloat(0.50206566), PackedFloat(0.6559849), PackedFloat(0.50224113), PackedFloat(0.50229454), PackedFloat(0.50209045), PackedFloat(0.6217842), PackedFloat(0.50227165), PackedFloat(0.50227356)]
2024-03-27 19:34:14 +01:00
#[rustfmt::skip]
const DEFAULT_CAL_POINTS_Y : [ f32 ; NO_OF_CALIBRATION_POINTS ] = [
2024-04-02 22:05:57 +02:00
0.50214195 , 0.51600456 , // right
0.5022125 , 0.5021801 ,
0.5019417 , 0.39642334 , // up right
0.5022106 , 0.5021858 ,
0.5018635 , 0.3603382 , // up
0.5022011 , 0.5022621 ,
0.5016308 , 0.4004345 , // up left
0.5022621 , 0.5022583 ,
0.501112 , 0.51377296 , // left
0.5022392 , 0.5022774 ,
0.5010319 , 0.62000275 , // down left
0.50230026 , 0.5023346 ,
0.50206566 , 0.6559849 , // down
0.50224113 , 0.50229454 ,
0.50209045 , 0.6217842 , // down right
0.50227165 , 0.50227356
2024-03-27 19:34:14 +01:00
];
2024-03-29 19:54:52 +01:00
pub const DEFAULT_ANGLES : [ f32 ; NO_OF_NOTCHES ] = [
2024-03-27 19:34:14 +01:00
0. ,
PI / 8.0 ,
PI * 2. / 8. ,
PI * 3. / 8. ,
PI * 4. / 8. ,
PI * 5. / 8. ,
PI * 6. / 8. ,
PI * 7. / 8. ,
PI * 8. / 8. ,
PI * 9. / 8. ,
PI * 10. / 8. ,
PI * 11. / 8. ,
PI * 12. / 8. ,
PI * 13. / 8. ,
PI * 14. / 8. ,
PI * 15. / 8. ,
];
2024-03-29 23:42:05 +01:00
#[derive(Clone, Format)]
pub struct OverrideStickState {
pub x : u8 ,
pub y : u8 ,
pub which_stick : Stick ,
}
2024-04-15 23:50:03 +02:00
/// Enum button representation mainly used in the calibration process,
/// in conjunction with `is_awaitable_button_pressed`
2024-04-03 17:17:21 +02:00
#[allow(dead_code)]
2024-04-09 17:14:43 +00:00
#[derive(Clone, Copy, Debug, Format, PartialEq, Eq)]
2024-04-03 19:44:02 +00:00
pub enum AwaitableButtons {
2024-03-29 16:07:44 +01:00
A ,
B ,
X ,
Y ,
Up ,
Down ,
Left ,
Right ,
Start ,
L ,
R ,
2024-04-02 22:05:57 +02:00
Z ,
2024-04-15 23:50:03 +02:00
/// Special, because Z is used for cstick calibration.
2024-04-03 16:53:52 +00:00
NotZ ,
2024-04-15 23:50:03 +02:00
/// Can be used for padding arrays to a fixed length.
2024-04-02 22:05:57 +02:00
Wildcard ,
2024-04-15 23:50:03 +02:00
/// Can be used for disabling certain button combinations.
2024-04-02 22:05:57 +02:00
Impossible ,
2024-03-29 16:07:44 +01:00
}
2024-04-01 23:07:06 +02:00
#[derive(Clone, Copy, Debug, Format)]
enum NotchAdjustmentType {
Clockwise ,
CounterClockwise ,
Reset ,
None ,
}
2024-04-02 22:05:57 +02:00
/// This needs to be incremented for ANY change to ControllerConfig
/// else we risk loading uninitialized memory.
2024-04-03 13:52:19 +02:00
pub const CONTROLLER_CONFIG_REVISION : u8 = 1 ;
2024-04-02 22:05:57 +02:00
2024-03-27 20:36:41 +01:00
#[derive(Debug, Clone, Format, PackedStruct)]
#[packed_struct(endian = "msb" )]
pub struct StickConfig {
#[packed_field(size_bits = "8" )]
pub x_waveshaping : u8 ,
#[packed_field(size_bits = "8" )]
pub y_waveshaping : u8 ,
#[packed_field(size_bits = "8" )]
pub analog_scaler : u8 ,
#[packed_field(size_bits = "8" )]
pub x_snapback : i8 , // not used for CStick
#[packed_field(size_bits = "8" )]
pub y_snapback : i8 , // not used for CStick
#[packed_field(size_bits = "8" )]
2024-04-03 13:52:19 +02:00
pub cardinal_snapping : i8 ,
2024-03-27 22:12:28 +01:00
#[packed_field(size_bits = "8" )]
2024-03-27 20:36:41 +01:00
pub x_smoothing : u8 ,
#[packed_field(size_bits = "8" )]
pub y_smoothing : u8 ,
#[packed_field(element_size_bytes = "4" )]
2024-03-29 19:54:52 +01:00
pub cal_points_x : [ PackedFloat ; 32 ],
2024-03-27 20:36:41 +01:00
#[packed_field(element_size_bytes = "4" )]
2024-03-29 19:54:52 +01:00
pub cal_points_y : [ PackedFloat ; 32 ],
2024-03-27 20:36:41 +01:00
#[packed_field(element_size_bytes = "4" )]
pub angles : [ PackedFloat ; 16 ],
}
impl Default for StickConfig {
fn default () -> Self {
Self {
x_waveshaping : 0 ,
y_waveshaping : 0 ,
2024-03-29 23:42:05 +01:00
x_snapback : 4 ,
y_snapback : 4 ,
2024-03-27 20:36:41 +01:00
x_smoothing : 0 ,
y_smoothing : 0 ,
2024-03-29 23:42:05 +01:00
cardinal_snapping : 6 ,
2024-03-27 22:12:28 +01:00
analog_scaler : 100 ,
2024-03-29 19:54:52 +01:00
cal_points_x : * DEFAULT_CAL_POINTS_X . to_packed_float_array (),
cal_points_y : * DEFAULT_CAL_POINTS_Y . to_packed_float_array (),
2024-03-27 20:36:41 +01:00
angles : * DEFAULT_ANGLES . to_packed_float_array (),
}
}
}
2024-04-08 20:53:23 +00:00
#[derive(Debug, Clone, Copy, Format, PrimitiveEnum_u8, PartialEq, Eq)]
pub enum InputConsistencyMode {
/// Transmit inputs every 8ms, same as the original GCC adapter (and any other).
Original = 0 ,
/// Forcibly delay transmissions to be 8.33ms apart, to better align with the game's frame rate.
ConsistencyHack = 1 ,
/// Transmit inputs _at most_ every 8.33ms, but don't transmit anything at all if the controller state doesn't change.
/// This has the potential to drastically improve latency in certain situations, such as when you are waiting to react
/// to something your opponent does.
/// The name is not meant to imply that this is a hack that is super, but rather that this is super hacky.
SuperHack = 2 ,
2024-04-19 17:14:20 +02:00
/// Transmit inputs every 1 ms, for use on PC or other devices that are not garbage.
/// This is not recommended for use on the Switch!
PC = 3 ,
2024-04-08 20:53:23 +00:00
}
2024-10-14 19:55:54 +00:00
/// Not saved, but set upon plugging in the controller.
#[derive(Debug, Clone, Copy, Format, PrimitiveEnum_u8, PartialEq, Eq)]
pub enum ControllerMode {
/// Advertise itself as a GCC adapter with 1 controller (itself) connected.
GcAdapter = 0 ,
/// Pretend to be a Nintendo Switch Pro Controller connected via USB.
Procon = 1 ,
2024-10-29 19:53:18 +00:00
/// Act as an XInput device, and also advertise itself with 1000Hz polling capability.
XInput = 2 ,
2024-10-14 19:55:54 +00:00
}
2024-03-27 19:34:14 +01:00
#[derive(Debug, Clone, Format, PackedStruct)]
#[packed_struct(endian = "msb" )]
pub struct ControllerConfig {
#[packed_field(size_bits = "8" )]
pub config_revision : u8 ,
2024-04-01 21:06:41 +02:00
/// Toggle for input consistency mode. If true, the controller
/// will trick the Switch into updating the state every 8.33ms
/// instead of every 8ms. The tradeoff is a slight increase in
/// input lag.
2024-04-08 20:53:23 +00:00
#[packed_field(size_bits = "8" , ty = "enum" )]
pub input_consistency_mode : InputConsistencyMode ,
2024-04-02 22:05:57 +02:00
#[packed_field(size_bits = "8" )]
pub rumble_strength : u8 ,
2024-03-27 22:12:28 +01:00
#[packed_field(size_bytes = "328" )]
2024-03-27 20:36:41 +01:00
pub astick_config : StickConfig ,
2024-03-27 22:12:28 +01:00
#[packed_field(size_bytes = "328" )]
2024-03-27 20:36:41 +01:00
pub cstick_config : StickConfig ,
2024-03-27 19:34:14 +01:00
}
impl Default for ControllerConfig {
fn default () -> Self {
Self {
config_revision : CONTROLLER_CONFIG_REVISION ,
2024-04-08 20:53:23 +00:00
input_consistency_mode : InputConsistencyMode ::ConsistencyHack ,
2024-03-27 20:36:41 +01:00
astick_config : StickConfig ::default (),
2024-04-02 22:05:57 +02:00
rumble_strength : 9 ,
2024-04-03 13:52:19 +02:00
cstick_config : StickConfig ::default (),
2024-03-27 19:34:14 +01:00
}
}
}
impl ControllerConfig {
pub fn from_flash_memory (
2024-04-03 16:53:52 +00:00
flash : & mut Flash < 'static , FLASH , Async , FLASH_SIZE > ,
2024-03-27 19:34:14 +01:00
) -> Result < Self , embassy_rp ::flash ::Error > {
2024-04-03 17:21:10 +02:00
let mut controller_config_packed : < ControllerConfig as packed_struct ::PackedStruct > ::ByteArray = ControllerConfig ::default (). pack (). unwrap ();
2024-04-03 01:46:14 +02:00
2024-04-15 23:50:03 +02:00
flash . blocking_read ( ADDR_OFFSET , & mut controller_config_packed ) ? ;
2024-03-27 19:34:14 +01:00
2024-04-03 14:33:23 +02:00
match ControllerConfig ::unpack ( & controller_config_packed ) {
Ok ( cfg ) => match cfg {
a if a . config_revision == CONTROLLER_CONFIG_REVISION => {
info! ( "Controller config loaded from flash: {}" , a );
2024-04-03 17:21:10 +02:00
return Ok ( a );
2024-04-03 14:33:23 +02:00
}
a => {
warn! ( "Outdated controller config detected ({:02X}), or controller config was never present, using default." , a . config_revision );
}
},
Err ( _ ) => {
2024-04-03 17:21:10 +02:00
warn! ( "Controller config corrupted or flash uninitialized, using default." );
2024-03-27 19:34:14 +01:00
}
2024-04-03 17:21:10 +02:00
};
let cfg = ControllerConfig ::default ();
info! ( "Going to save default controller config." );
2024-04-03 16:53:52 +00:00
cfg . write_to_flash ( flash ) ? ;
2024-04-03 17:21:10 +02:00
Ok ( cfg )
2024-03-27 19:34:14 +01:00
}
pub fn write_to_flash (
& self ,
flash : & mut Flash < 'static , FLASH , Async , FLASH_SIZE > ,
) -> Result < (), embassy_rp ::flash ::Error > {
info! ( "Writing controller config to flash." );
flash . blocking_erase ( ADDR_OFFSET , ADDR_OFFSET + ERASE_SIZE as u32 ) ? ;
flash . blocking_write ( ADDR_OFFSET , & self . pack (). unwrap ()) ? ;
Ok (())
}
}
2024-03-29 16:07:44 +01:00
2024-04-15 23:50:03 +02:00
/// Trait for providing button presses, used in the calibration process.
trait ButtonPressProvider {
2024-03-29 16:07:44 +01:00
/// Wait for a single button press.
2024-10-14 19:55:54 +00:00
// TODO: remove allow once this is used somewhere
#[allow(dead_code)]
2024-03-29 16:07:44 +01:00
async fn wait_for_button_press ( & mut self , button_to_wait_for : & AwaitableButtons );
2024-03-29 23:42:05 +01:00
/// Wait for a single button release.
async fn wait_for_button_release ( & mut self , button_to_wait_for : & AwaitableButtons );
2024-04-15 23:50:03 +02:00
/// Wait for multiple buttons to be pressed simultaneously. Non-exclusive.
2024-03-29 16:07:44 +01:00
async fn wait_for_simultaneous_button_presses < const N : usize > (
& mut self ,
buttons_to_wait_for : & [ AwaitableButtons ; N ],
);
2024-04-15 23:50:03 +02:00
/// Wait for a single button press of specified buttons, and return the button that was pressed. Non-exclusive.
2024-03-29 16:07:44 +01:00
async fn wait_and_filter_button_press < const N : usize > (
& mut self ,
buttons_to_wait_for : & [ AwaitableButtons ; N ],
) -> AwaitableButtons ;
2024-04-15 23:50:03 +02:00
/// See if one of the buttons in buttons_to_look_out_for is pressed, and return the pressed button, otherwise None. Non-exclusive.
2024-04-01 23:07:06 +02:00
fn filter_button_press_if_present < const N : usize > (
& mut self ,
buttons_to_look_out_for : & [ AwaitableButtons ; N ],
) -> Option < AwaitableButtons > ;
2024-03-29 16:07:44 +01:00
/// Wait for multiple possible button combinations to be pressed simultaneously, and return the index of the combination that was pressed.
2024-04-15 23:50:03 +02:00
///
/// Note that this is done non-exclusively, so if the pressed buttons match multiple combinations, the first matching index in the array will be returned.
2024-03-29 16:07:44 +01:00
async fn wait_and_filter_simultaneous_button_presses < const N : usize , const M : usize > (
& mut self ,
buttons_to_wait_for : & [[ AwaitableButtons ; N ]; M ],
) -> usize ;
}
2024-04-15 23:50:03 +02:00
impl < 'a , T : RawMutex , const I : usize , const J : usize , const K : usize > ButtonPressProvider
2024-12-28 03:50:33 +00:00
for Subscriber < 'a , T , ControllerState , I , J , K >
2024-03-29 16:07:44 +01:00
{
async fn wait_for_button_press ( & mut self , button_to_wait_for : & AwaitableButtons ) {
loop {
2024-03-29 19:54:52 +01:00
let report = self . next_message_pure (). await ;
if is_awaitable_button_pressed ( & report , button_to_wait_for ) {
2024-03-29 16:07:44 +01:00
break ;
}
2024-03-29 19:54:52 +01:00
2024-03-29 16:07:44 +01:00
Timer ::after_millis ( BUTTON_POLL_INTERVAL_MILLIS ). await ;
}
}
2024-03-29 23:42:05 +01:00
async fn wait_for_button_release ( & mut self , button_to_wait_for : & AwaitableButtons ) {
loop {
let report = self . next_message_pure (). await ;
if ! is_awaitable_button_pressed ( & report , button_to_wait_for ) {
break ;
}
Timer ::after_millis ( BUTTON_POLL_INTERVAL_MILLIS ). await ;
}
}
2024-03-29 16:07:44 +01:00
async fn wait_for_simultaneous_button_presses < const N : usize > (
& mut self ,
buttons_to_wait_for : & [ AwaitableButtons ; N ],
) {
loop {
2024-03-29 19:54:52 +01:00
let report = self . next_message_pure (). await ;
if buttons_to_wait_for
. iter ()
. all ( | button | is_awaitable_button_pressed ( & report , button ))
{
2024-03-29 16:07:44 +01:00
break ;
}
2024-03-29 19:54:52 +01:00
2024-03-29 16:07:44 +01:00
Timer ::after_millis ( BUTTON_POLL_INTERVAL_MILLIS ). await ;
}
}
async fn wait_and_filter_button_press < const N : usize > (
& mut self ,
buttons_to_wait_for : & [ AwaitableButtons ; N ],
) -> AwaitableButtons {
loop {
2024-03-29 19:54:52 +01:00
let report = self . next_message_pure (). await ;
for button in buttons_to_wait_for {
if is_awaitable_button_pressed ( & report , button ) {
return * button ;
2024-03-29 16:07:44 +01:00
}
}
2024-03-29 19:54:52 +01:00
2024-03-29 16:07:44 +01:00
Timer ::after_millis ( BUTTON_POLL_INTERVAL_MILLIS ). await ;
}
}
async fn wait_and_filter_simultaneous_button_presses < const N : usize , const M : usize > (
& mut self ,
buttons_to_wait_for : & [[ AwaitableButtons ; N ]; M ],
) -> usize {
loop {
2024-03-29 19:54:52 +01:00
let report = self . next_message_pure (). await ;
for ( i , buttons ) in buttons_to_wait_for . iter (). enumerate () {
if buttons
. iter ()
. all ( | button | is_awaitable_button_pressed ( & report , button ))
{
return i ;
2024-03-29 16:07:44 +01:00
}
}
2024-03-29 19:54:52 +01:00
2024-03-29 16:07:44 +01:00
Timer ::after_millis ( BUTTON_POLL_INTERVAL_MILLIS ). await ;
}
}
2024-04-01 23:07:06 +02:00
fn filter_button_press_if_present < const N : usize > (
& mut self ,
buttons_to_look_out_for : & [ AwaitableButtons ; N ],
) -> Option < AwaitableButtons > {
let report = self . try_next_message_pure ();
if let Some ( report ) = report {
for button in buttons_to_look_out_for {
if is_awaitable_button_pressed ( & report , button ) {
return Some ( * button );
}
}
}
2024-04-03 16:53:52 +00:00
None
2024-04-01 23:07:06 +02:00
}
2024-03-29 16:07:44 +01:00
}
2024-04-03 19:44:02 +00:00
pub fn is_awaitable_button_pressed (
2024-12-28 03:50:33 +00:00
report : & ControllerState ,
2024-04-03 19:44:02 +00:00
button_to_wait_for : & AwaitableButtons ,
) -> bool {
2024-03-29 16:07:44 +01:00
match button_to_wait_for {
2024-12-28 03:50:33 +00:00
AwaitableButtons ::A => report . button_a ,
AwaitableButtons ::B => report . button_b ,
AwaitableButtons ::X => report . button_x ,
AwaitableButtons ::Y => report . button_y ,
AwaitableButtons ::Up => report . dpad_up ,
AwaitableButtons ::Down => report . dpad_down ,
AwaitableButtons ::Left => report . dpad_left ,
AwaitableButtons ::Right => report . dpad_right ,
AwaitableButtons ::Start => report . button_start ,
AwaitableButtons ::L => report . trigger_l ,
AwaitableButtons ::R => report . trigger_r ,
AwaitableButtons ::Z => report . trigger_zr ,
2024-04-02 22:05:57 +02:00
AwaitableButtons ::Wildcard => true ,
AwaitableButtons ::Impossible => false ,
2024-12-28 03:50:33 +00:00
AwaitableButtons ::NotZ => ! report . trigger_zr ,
2024-03-29 19:54:52 +01:00
}
}
#[derive(Debug, Format)]
struct StickCalibrationProcess < 'a > {
which_stick : Stick ,
calibration_step : u8 ,
gcc_config : & 'a mut ControllerConfig ,
cal_points : [ XyValuePair < f32 > ; NO_OF_CALIBRATION_POINTS ],
2024-03-29 21:31:55 +01:00
applied_calibration : AppliedCalibration ,
2024-03-29 19:54:52 +01:00
}
impl < 'a > StickCalibrationProcess < 'a > {
pub fn new ( gcc_config : & 'a mut ControllerConfig , which_stick : Stick ) -> Self {
Self {
which_stick ,
calibration_step : 0 ,
gcc_config ,
cal_points : [ XyValuePair ::default (); NO_OF_CALIBRATION_POINTS ],
2024-03-29 21:31:55 +01:00
applied_calibration : AppliedCalibration ::default (),
2024-03-29 19:54:52 +01:00
}
}
2024-04-01 23:54:08 +02:00
fn adjust_notch ( & mut self , notch_adjustment_type : NotchAdjustmentType ) {
let stick_config = match self . which_stick {
Stick ::ControlStick => & mut self . gcc_config . astick_config ,
Stick ::CStick => & mut self . gcc_config . cstick_config ,
};
2024-04-01 23:07:06 +02:00
let notch_idx =
NOTCH_ADJUSTMENT_ORDER [ self . calibration_step as usize - NO_OF_CALIBRATION_POINTS ];
if self . applied_calibration . cleaned_calibration . notch_status [ notch_idx ]
== NotchStatus ::TertInactive
2024-04-03 16:53:52 +00:00
{
return ;
}
2024-04-01 23:07:06 +02:00
// assumes a tick rate of 1ms
match notch_adjustment_type {
NotchAdjustmentType ::Clockwise => {
2024-04-01 23:54:08 +02:00
stick_config . angles [ notch_idx ] -= 0.0075 ;
2024-04-01 23:07:06 +02:00
}
NotchAdjustmentType ::CounterClockwise => {
2024-04-01 23:54:08 +02:00
stick_config . angles [ notch_idx ] += 0.0075 ;
2024-04-01 23:07:06 +02:00
}
NotchAdjustmentType ::Reset => {
2024-04-01 23:54:08 +02:00
stick_config . angles [ notch_idx ] =
PackedFloat ( self . applied_calibration . measured_notch_angles [ notch_idx ]);
2024-04-01 23:07:06 +02:00
}
2024-04-01 23:54:08 +02:00
NotchAdjustmentType ::None => {}
2024-04-01 23:07:06 +02:00
}
match notch_adjustment_type {
NotchAdjustmentType ::Clockwise
| NotchAdjustmentType ::CounterClockwise
| NotchAdjustmentType ::Reset => {
let cleaned_calibration_points =
CleanedCalibrationPoints ::from_temp_calibration_points (
& self . cal_points . map ( | e | e . x ),
& self . cal_points . map ( | e | e . y ),
2024-04-01 23:54:08 +02:00
& self . applied_calibration . measured_notch_angles ,
2024-04-01 23:07:06 +02:00
);
let linearized_calibration =
LinearizedCalibration ::from_calibration_points ( & cleaned_calibration_points );
self . applied_calibration . stick_params . fit_coeffs = XyValuePair {
x : linearized_calibration . fit_coeffs . x . map ( | e | e as f32 ),
y : linearized_calibration . fit_coeffs . y . map ( | e | e as f32 ),
};
let notch_calibration = NotchCalibration ::from_cleaned_and_linearized_calibration (
& cleaned_calibration_points ,
& linearized_calibration ,
);
self . applied_calibration . stick_params . affine_coeffs =
notch_calibration . affine_coeffs ;
self . applied_calibration . stick_params . boundary_angles =
notch_calibration . boundary_angles ;
2024-04-01 23:54:08 +02:00
stick_config . angles = * legalize_notches (
self . calibration_step as usize ,
& self . applied_calibration . measured_notch_angles ,
2024-04-03 16:53:52 +00:00
stick_config . angles . to_regular_array (),
2024-04-01 23:54:08 +02:00
)
. to_packed_float_array ();
SIGNAL_CONFIG_CHANGE . signal ( self . gcc_config . clone ());
}
NotchAdjustmentType ::None => {}
}
2024-04-01 23:07:06 +02:00
}
2024-04-09 17:14:43 +00:00
fn calibration_skip_measurement ( & mut self ) {
self . calibration_step = NO_OF_CALIBRATION_POINTS as u8 ;
let stick_config = match self . which_stick {
Stick ::ControlStick => & mut self . gcc_config . astick_config ,
Stick ::CStick => & mut self . gcc_config . cstick_config ,
};
for i in 0 .. NO_OF_CALIBRATION_POINTS {
self . cal_points [ i ] = XyValuePair {
x : * stick_config . cal_points_x [ i ],
y : * stick_config . cal_points_y [ i ],
};
}
self . applied_calibration = AppliedCalibration ::from_points (
stick_config . cal_points_x . to_regular_array (),
stick_config . cal_points_y . to_regular_array (),
stick_config ,
);
}
2024-03-29 23:42:05 +01:00
async fn calibration_advance ( & mut self ) -> bool {
info! (
"Running calibration advance on stick {} at step {}" ,
self . which_stick , self . calibration_step
);
2024-03-29 21:31:55 +01:00
let stick_config = match self . which_stick {
Stick ::ControlStick => & mut self . gcc_config . astick_config ,
Stick ::CStick => & mut self . gcc_config . cstick_config ,
};
2024-03-29 19:54:52 +01:00
let mut spi_unlocked = SPI_SHARED . lock (). await ;
let mut spi_acs_unlocked = SPI_ACS_SHARED . lock (). await ;
let mut spi_ccs_unlocked = SPI_CCS_SHARED . lock (). await ;
let spi = spi_unlocked . as_mut (). unwrap ();
let spi_acs = spi_acs_unlocked . as_mut (). unwrap ();
let spi_ccs = spi_ccs_unlocked . as_mut (). unwrap ();
if self . calibration_step < NO_OF_CALIBRATION_POINTS as u8 {
let mut x : f32 = 0. ;
let mut y : f32 = 0. ;
for _ in 0 .. 128 {
x += read_ext_adc ( self . which_stick , StickAxis ::XAxis , spi , spi_acs , spi_ccs ) as f32
/ 4096.0 ;
y += read_ext_adc ( self . which_stick , StickAxis ::YAxis , spi , spi_acs , spi_ccs ) as f32
/ 4096.0 ;
}
x /= 128. ;
y /= 128. ;
2024-03-29 23:42:05 +01:00
let idx = CALIBRATION_ORDER [ self . calibration_step as usize ];
self . cal_points [ idx ] = XyValuePair { x , y };
2024-03-29 19:54:52 +01:00
}
self . calibration_step += 1 ;
// TODO: phob does something related to undo here
if self . calibration_step == NO_OF_CALIBRATION_POINTS as u8 {
2024-03-29 21:31:55 +01:00
self . applied_calibration = AppliedCalibration ::from_points (
& self . cal_points . map ( | e | e . x ),
& self . cal_points . map ( | e | e . y ),
2024-04-03 16:53:52 +00:00
stick_config ,
2024-03-29 21:31:55 +01:00
);
2024-04-02 19:18:45 +02:00
stick_config . angles = * legalize_notches (
self . calibration_step as usize ,
& self . applied_calibration . measured_notch_angles ,
& DEFAULT_ANGLES ,
)
. to_packed_float_array ();
2024-03-29 21:31:55 +01:00
}
2024-03-29 23:42:05 +01:00
if self . calibration_step >= NO_OF_CALIBRATION_POINTS as u8 {
2024-04-01 23:07:06 +02:00
let mut notch_idx = NOTCH_ADJUSTMENT_ORDER [ min (
self . calibration_step - NO_OF_CALIBRATION_POINTS as u8 ,
NO_OF_ADJ_NOTCHES as u8 - 1 ,
) as usize ];
while self . applied_calibration . cleaned_calibration . notch_status [ notch_idx ]
== NotchStatus ::TertInactive
&& self . calibration_step < NO_OF_CALIBRATION_POINTS as u8 + NO_OF_ADJ_NOTCHES as u8
{
stick_config . angles = * legalize_notches (
self . calibration_step as usize ,
& self . applied_calibration . measured_notch_angles ,
2024-04-03 16:53:52 +00:00
stick_config . angles . to_regular_array (),
2024-04-01 23:07:06 +02:00
)
. to_packed_float_array ();
self . calibration_step += 1 ;
notch_idx = NOTCH_ADJUSTMENT_ORDER [ min (
self . calibration_step - NO_OF_CALIBRATION_POINTS as u8 ,
NO_OF_ADJ_NOTCHES as u8 - 1 ,
) as usize ];
}
}
2024-04-03 13:52:19 +02:00
stick_config . cal_points_x = self . cal_points . map ( | p | p . x . into ());
stick_config . cal_points_y = self . cal_points . map ( | p | p . y . into ());
2024-03-29 23:42:05 +01:00
2024-04-03 13:52:19 +02:00
if self . calibration_step >= NO_OF_CALIBRATION_POINTS as u8 + NO_OF_ADJ_NOTCHES as u8 {
2024-04-01 23:54:08 +02:00
SIGNAL_CONFIG_CHANGE . signal ( self . gcc_config . clone ());
2024-03-29 23:42:05 +01:00
info! ( "Finished calibrating stick {}" , self . which_stick );
return true ;
2024-03-29 19:54:52 +01:00
}
2024-03-29 21:31:55 +01:00
2024-04-02 19:18:45 +02:00
if self . calibration_step == NO_OF_CALIBRATION_POINTS as u8 {
SIGNAL_CONFIG_CHANGE . signal ( self . gcc_config . clone ());
}
2024-04-03 16:53:52 +00:00
false
2024-03-29 19:54:52 +01:00
}
pub async fn calibrate_stick ( & mut self ) {
2024-03-29 23:42:05 +01:00
info! ( "Beginning stick calibration for {}" , self . which_stick );
2024-12-28 03:50:33 +00:00
let mut gcc_subscriber = CHANNEL_CONTROLLER_STATE . subscriber (). unwrap ();
2024-03-29 23:42:05 +01:00
SIGNAL_IS_CALIBRATING . signal ( true );
2024-04-09 17:14:43 +00:00
let mut done = false ;
while ! done {
2024-04-01 23:07:06 +02:00
if self . calibration_step < NO_OF_CALIBRATION_POINTS as u8 {
// Calibration phase
2024-03-29 23:42:05 +01:00
2024-04-01 23:07:06 +02:00
let ( x , y ) = get_stick_display_coords ( self . calibration_step as usize );
debug! (
"Raw display coords for step {}: {}, {}" ,
self . calibration_step , x , y
);
2024-03-29 23:42:05 +01:00
2024-04-01 23:07:06 +02:00
SIGNAL_OVERRIDE_STICK_STATE . signal ( Some ( OverrideStickState {
x : x as u8 ,
y : y as u8 ,
which_stick : match self . which_stick {
Stick ::ControlStick => Stick ::CStick ,
Stick ::CStick => Stick ::ControlStick ,
},
}));
2024-03-29 23:42:05 +01:00
2024-04-01 23:07:06 +02:00
gcc_subscriber
. wait_for_button_release ( & AwaitableButtons ::A )
. await ;
// Prevent accidental double presses
Timer ::after_millis ( 100 ). await ;
2024-04-09 17:14:43 +00:00
let btn_result = gcc_subscriber
. wait_and_filter_button_press ( & [ AwaitableButtons ::A , AwaitableButtons ::Start ])
2024-04-01 23:07:06 +02:00
. await ;
2024-04-09 17:14:43 +00:00
if btn_result == AwaitableButtons ::Start {
self . calibration_skip_measurement ();
continue ;
}
2024-04-01 23:07:06 +02:00
} else {
// Notch adjustment phase
gcc_subscriber
. wait_for_button_release ( & AwaitableButtons ::A )
. await ;
Timer ::after_millis ( 100 ). await ;
let notch_idx = NOTCH_ADJUSTMENT_ORDER
[ self . calibration_step as usize - NO_OF_CALIBRATION_POINTS ];
let ( init_x , init_y ) =
calc_stick_values ( self . applied_calibration . measured_notch_angles [ notch_idx ]);
SIGNAL_OVERRIDE_STICK_STATE . signal ( Some ( OverrideStickState {
x : ( init_x + FLOAT_ORIGIN ) as u8 ,
y : ( init_y + FLOAT_ORIGIN ) as u8 ,
which_stick : match self . which_stick {
Stick ::ControlStick => Stick ::CStick ,
Stick ::CStick => Stick ::ControlStick ,
},
}));
'adjust : loop {
let btn_result = gcc_subscriber . filter_button_press_if_present ( & [
AwaitableButtons ::A ,
AwaitableButtons ::B ,
AwaitableButtons ::X ,
AwaitableButtons ::Y ,
]);
2024-04-01 23:54:08 +02:00
match btn_result {
2024-04-01 23:07:06 +02:00
Some ( btn ) => match btn {
AwaitableButtons ::A => {
debug! ( "Btn A release pressed" );
break 'adjust ;
}
AwaitableButtons ::B => self . adjust_notch ( NotchAdjustmentType ::Reset ),
AwaitableButtons ::X => {
self . adjust_notch ( NotchAdjustmentType ::Clockwise )
}
AwaitableButtons ::Y => {
self . adjust_notch ( NotchAdjustmentType ::CounterClockwise )
}
_ => self . adjust_notch ( NotchAdjustmentType ::None ),
},
None => self . adjust_notch ( NotchAdjustmentType ::None ),
};
2024-04-20 11:39:58 +02:00
Timer ::after_millis ( 1 ). await ;
2024-04-01 23:54:08 +02:00
yield_now (). await ;
2024-04-01 23:07:06 +02:00
}
};
2024-03-29 23:42:05 +01:00
2024-04-09 17:14:43 +00:00
done = self . calibration_advance (). await ;
}
2024-03-29 23:42:05 +01:00
SIGNAL_IS_CALIBRATING . signal ( false );
SIGNAL_OVERRIDE_STICK_STATE . signal ( None );
}
}
fn get_stick_display_coords ( current_step : usize ) -> ( f32 , f32 ) {
let idx = CALIBRATION_ORDER [ current_step ];
if idx % 2 != 0 {
let notch_idx = idx / 2 ;
2024-04-03 16:53:52 +00:00
let ( x , y ) = calc_stick_values ( DEFAULT_ANGLES [ notch_idx ]);
( x + FLOAT_ORIGIN , y + FLOAT_ORIGIN )
2024-03-29 23:42:05 +01:00
} else {
( 127.5 , 127.5 )
2024-03-29 19:54:52 +01:00
}
}
2024-04-02 22:05:57 +02:00
pub async fn override_gcc_state_and_wait ( state : & OverrideGcReportInstruction ) {
2024-12-28 03:50:33 +00:00
SIGNAL_OVERRIDE_CONTROLLER_STATE . signal ( state . clone ());
2024-04-02 22:05:57 +02:00
Timer ::after_millis ( state . duration_ms ). await ;
}
2024-03-29 19:54:52 +01:00
async fn configuration_main_loop <
'a ,
M : RawMutex ,
const C : usize ,
const S : usize ,
const P : usize ,
> (
current_config : & ControllerConfig ,
2024-04-03 16:53:52 +00:00
flash : & mut Flash < 'static , FLASH , Async , FLASH_SIZE > ,
2024-12-28 03:50:33 +00:00
gcc_subscriber : & mut Subscriber < 'a , M , ControllerState , C , S , P > ,
2024-04-03 14:33:23 +02:00
) -> ControllerConfig {
2024-03-29 19:54:52 +01:00
let mut final_config = current_config . clone ();
2024-04-02 22:05:57 +02:00
let config_options = [
EXIT_CONFIG_MODE_COMBO ,
LSTICK_CALIBRATION_COMBO ,
RSTICK_CALIBRATION_COMBO ,
LSTICK_SNAPBACK_X_INCREASE_COMBO ,
LSTICK_SNAPBACK_Y_INCREASE_COMBO ,
LSTICK_SNAPBACK_X_DECREASE_COMBO ,
LSTICK_SNAPBACK_Y_DECREASE_COMBO ,
// NOTE: cstick snapback is currently
// unused, but we have the combos here
// anyway.
RSTICK_SNAPBACK_X_INCREASE_COMBO ,
RSTICK_SNAPBACK_Y_INCREASE_COMBO ,
RSTICK_SNAPBACK_X_DECREASE_COMBO ,
RSTICK_SNAPBACK_Y_DECREASE_COMBO ,
LSTICK_WAVESHAPING_X_INCREASE_COMBO ,
LSTICK_WAVESHAPING_Y_INCREASE_COMBO ,
LSTICK_WAVESHAPING_X_DECREASE_COMBO ,
LSTICK_WAVESHAPING_Y_DECREASE_COMBO ,
RSTICK_WAVESHAPING_X_INCREASE_COMBO ,
RSTICK_WAVESHAPING_Y_INCREASE_COMBO ,
RSTICK_WAVESHAPING_X_DECREASE_COMBO ,
RSTICK_WAVESHAPING_Y_DECREASE_COMBO ,
LSTICK_SMOOTH_X_INCREASE_COMBO ,
LSTICK_SMOOTH_Y_INCREASE_COMBO ,
LSTICK_SMOOTH_X_DECREASE_COMBO ,
LSTICK_SMOOTH_Y_DECREASE_COMBO ,
RSTICK_SMOOTH_X_INCREASE_COMBO ,
RSTICK_SMOOTH_Y_INCREASE_COMBO ,
RSTICK_SMOOTH_X_DECREASE_COMBO ,
RSTICK_SMOOTH_Y_DECREASE_COMBO ,
LSTICK_CARDINALSNAP_INCREASE_COMBO ,
LSTICK_CARDINALSNAP_DECREASE_COMBO ,
RSTICK_CARDINALSNAP_INCREASE_COMBO ,
RSTICK_CARDINALSNAP_DECREASE_COMBO ,
LSTICK_SCALING_INCREASE_COMBO ,
LSTICK_SCALING_DECREASE_COMBO ,
RSTICK_SCALING_INCREASE_COMBO ,
RSTICK_SCALING_DECREASE_COMBO ,
RUMBLE_STRENGTH_INCREASE_COMBO ,
RUMBLE_STRENGTH_DECREASE_COMBO ,
INPUT_CONSISTENCY_TOGGLE_COMBO ,
2024-04-06 21:05:22 +02:00
DISPLAY_WAVESHAPING_COMBO ,
DISPLAY_SMOOTHING_COMBO ,
DISPLAY_SNAPBACK_COMBO ,
2024-04-02 22:05:57 +02:00
];
2024-03-29 19:54:52 +01:00
'main : loop {
2024-04-03 16:53:52 +00:00
let selection = gcc_subscriber
2024-03-29 19:54:52 +01:00
. wait_and_filter_simultaneous_button_presses ( & config_options )
2024-04-03 16:53:52 +00:00
. await ;
match selection {
// exit
0 => {
override_gcc_state_and_wait ( & OverrideGcReportInstruction {
2024-10-14 19:55:54 +00:00
report : GcState {
2024-04-03 16:53:52 +00:00
trigger_r : 255 ,
trigger_l : 255 ,
2024-10-14 19:55:54 +00:00
buttons_2 : GcButtons2 {
2024-04-03 16:53:52 +00:00
button_r : true ,
button_l : true ,
.. Default ::default ()
2024-04-02 22:05:57 +02:00
},
2024-10-14 19:55:54 +00:00
buttons_1 : GcButtons1 {
2024-04-03 16:53:52 +00:00
button_x : true ,
button_y : true ,
button_a : true ,
.. Default ::default ()
2024-04-02 22:05:57 +02:00
},
2024-04-03 16:53:52 +00:00
stick_x : 127 ,
stick_y : 127 ,
cstick_x : 127 ,
cstick_y : 127 ,
2024-12-28 03:50:33 +00:00
}
. into (),
2024-04-03 16:53:52 +00:00
duration_ms : 1000 ,
})
. await ;
break 'main ;
}
// calibrate lstick
1 => {
override_gcc_state_and_wait ( & OverrideGcReportInstruction {
2024-10-14 19:55:54 +00:00
report : GcState {
2024-04-03 16:53:52 +00:00
trigger_r : 255 ,
trigger_l : 255 ,
2024-10-14 19:55:54 +00:00
buttons_2 : GcButtons2 {
2024-04-03 16:53:52 +00:00
button_r : true ,
button_l : true ,
.. Default ::default ()
2024-04-02 22:05:57 +02:00
},
2024-10-14 19:55:54 +00:00
buttons_1 : GcButtons1 {
2024-04-03 16:53:52 +00:00
button_x : true ,
button_y : true ,
button_a : true ,
.. Default ::default ()
2024-04-02 22:05:57 +02:00
},
2024-04-03 16:53:52 +00:00
stick_x : 255 ,
stick_y : 255 ,
cstick_x : 127 ,
cstick_y : 127 ,
2024-12-28 03:50:33 +00:00
}
. into (),
2024-04-03 16:53:52 +00:00
duration_ms : 1000 ,
})
. await ;
StickCalibrationProcess ::new ( & mut final_config , Stick ::ControlStick )
. calibrate_stick ()
2024-04-02 22:05:57 +02:00
. await ;
2024-04-03 16:53:52 +00:00
}
// calibrate rstick
2 => {
override_gcc_state_and_wait ( & OverrideGcReportInstruction {
2024-10-14 19:55:54 +00:00
report : GcState {
2024-04-03 16:53:52 +00:00
trigger_r : 255 ,
trigger_l : 255 ,
2024-10-14 19:55:54 +00:00
buttons_2 : GcButtons2 {
2024-04-03 16:53:52 +00:00
button_r : true ,
button_l : true ,
.. Default ::default ()
2024-04-02 22:05:57 +02:00
},
2024-10-14 19:55:54 +00:00
buttons_1 : GcButtons1 {
2024-04-03 16:53:52 +00:00
button_x : true ,
button_y : true ,
button_a : true ,
.. Default ::default ()
2024-04-02 22:05:57 +02:00
},
2024-04-03 16:53:52 +00:00
stick_x : 127 ,
stick_y : 127 ,
cstick_x : 255 ,
cstick_y : 255 ,
2024-12-28 03:50:33 +00:00
}
. into (),
2024-04-03 16:53:52 +00:00
duration_ms : 1000 ,
})
. await ;
StickCalibrationProcess ::new ( & mut final_config , Stick ::CStick )
. calibrate_stick ()
2024-04-02 22:05:57 +02:00
. await ;
2024-04-03 16:53:52 +00:00
}
// snapback changes
i if ( 3 ..= 10 ). contains ( & i ) => {
let stick = match i {
3 ..= 6 => Stick ::ControlStick ,
7 ..= 10 => Stick ::CStick ,
_ => unreachable! (),
};
2024-04-02 22:05:57 +02:00
2024-04-03 16:53:52 +00:00
let axis = match i {
3 | 7 | 5 | 9 => StickAxis ::XAxis ,
4 | 8 | 6 | 10 => StickAxis ::YAxis ,
_ => unreachable! (),
};
let stick_config = match stick {
Stick ::ControlStick => & mut final_config . astick_config ,
Stick ::CStick => & mut final_config . cstick_config ,
};
let to_adjust = match axis {
StickAxis ::XAxis => & mut stick_config . x_snapback ,
StickAxis ::YAxis => & mut stick_config . y_snapback ,
};
* to_adjust = ( * to_adjust
+ match i {
3 | 7 | 4 | 8 => 1 ,
5 | 9 | 6 | 10 => - 1 ,
2024-04-02 22:05:57 +02:00
_ => unreachable! (),
2024-04-03 16:53:52 +00:00
})
. clamp ( - ABS_MAX_SNAPBACK , ABS_MAX_SNAPBACK );
2024-04-02 22:05:57 +02:00
2024-04-03 16:53:52 +00:00
override_gcc_state_and_wait ( & OverrideGcReportInstruction {
2024-10-14 19:55:54 +00:00
report : GcState {
2024-04-03 16:53:52 +00:00
trigger_r : 255 ,
trigger_l : 255 ,
2024-10-14 19:55:54 +00:00
buttons_2 : GcButtons2 {
2024-04-03 16:53:52 +00:00
button_r : true ,
button_l : true ,
.. Default ::default ()
},
2024-10-14 19:55:54 +00:00
buttons_1 : GcButtons1 {
2024-04-03 16:53:52 +00:00
button_x : true ,
button_y : true ,
button_a : true ,
.. Default ::default ()
},
stick_x : ( 127
+ match stick {
Stick ::ControlStick => match axis {
StickAxis ::XAxis => * to_adjust ,
StickAxis ::YAxis => 0 ,
},
Stick ::CStick => 0 ,
}) as u8 ,
stick_y : ( 127
+ match stick {
Stick ::ControlStick => match axis {
StickAxis ::XAxis => 0 ,
StickAxis ::YAxis => * to_adjust ,
},
Stick ::CStick => 0 ,
}) as u8 ,
cstick_x : ( 127
+ match stick {
Stick ::ControlStick => 0 ,
Stick ::CStick => match axis {
StickAxis ::XAxis => * to_adjust ,
StickAxis ::YAxis => 0 ,
},
}) as u8 ,
cstick_y : ( 127
+ match stick {
Stick ::ControlStick => 0 ,
Stick ::CStick => match axis {
StickAxis ::XAxis => 0 ,
StickAxis ::YAxis => * to_adjust ,
},
}) as u8 ,
2024-12-28 03:50:33 +00:00
}
. into (),
2024-04-03 16:53:52 +00:00
duration_ms : 750 ,
})
. await ;
SIGNAL_CONFIG_CHANGE . signal ( final_config . clone ());
}
// waveshaping changes
i if ( 11 ..= 18 ). contains ( & i ) => {
let stick = match i {
11 ..= 14 => Stick ::ControlStick ,
15 ..= 18 => Stick ::CStick ,
_ => unreachable! (),
};
let axis = match i {
11 | 15 | 13 | 17 => StickAxis ::XAxis ,
12 | 16 | 14 | 18 => StickAxis ::YAxis ,
_ => unreachable! (),
};
let stick_config = match stick {
Stick ::ControlStick => & mut final_config . astick_config ,
Stick ::CStick => & mut final_config . cstick_config ,
};
let to_adjust = match axis {
StickAxis ::XAxis => & mut stick_config . x_waveshaping ,
StickAxis ::YAxis => & mut stick_config . y_waveshaping ,
};
* to_adjust = ( * to_adjust as i8
+ match i {
11 | 15 | 12 | 16 => 1 ,
13 | 17 | 14 | 18 => - 1 ,
2024-04-02 22:05:57 +02:00
_ => unreachable! (),
})
2024-04-03 16:53:52 +00:00
. clamp ( 0 , MAX_WAVESHAPING ) as u8 ;
2024-04-02 22:05:57 +02:00
2024-04-03 16:53:52 +00:00
override_gcc_state_and_wait ( & OverrideGcReportInstruction {
2024-10-14 19:55:54 +00:00
report : GcState {
2024-04-03 16:53:52 +00:00
trigger_r : 255 ,
trigger_l : 255 ,
2024-10-14 19:55:54 +00:00
buttons_2 : GcButtons2 {
2024-04-03 16:53:52 +00:00
button_r : true ,
button_l : true ,
.. Default ::default ()
},
2024-10-14 19:55:54 +00:00
buttons_1 : GcButtons1 {
2024-04-03 16:53:52 +00:00
button_x : true ,
button_y : true ,
button_a : true ,
.. Default ::default ()
},
stick_x : ( 127
+ match stick {
Stick ::ControlStick => match axis {
StickAxis ::XAxis => * to_adjust ,
StickAxis ::YAxis => 0 ,
},
Stick ::CStick => 0 ,
}) as u8 ,
stick_y : ( 127
+ match stick {
Stick ::ControlStick => match axis {
StickAxis ::XAxis => 0 ,
StickAxis ::YAxis => * to_adjust ,
},
Stick ::CStick => 0 ,
}) as u8 ,
cstick_x : ( 127
+ match stick {
Stick ::ControlStick => 0 ,
Stick ::CStick => match axis {
StickAxis ::XAxis => * to_adjust ,
StickAxis ::YAxis => 0 ,
},
}) as u8 ,
cstick_y : ( 127
+ match stick {
Stick ::ControlStick => 0 ,
Stick ::CStick => match axis {
StickAxis ::XAxis => 0 ,
StickAxis ::YAxis => * to_adjust ,
},
}) as u8 ,
2024-12-28 03:50:33 +00:00
}
. into (),
2024-04-03 16:53:52 +00:00
duration_ms : 750 ,
})
. await ;
SIGNAL_CONFIG_CHANGE . signal ( final_config . clone ());
}
// smoothing changes
i if ( 19 ..= 26 ). contains ( & i ) => {
let stick = match i {
19 ..= 22 => Stick ::ControlStick ,
23 ..= 26 => Stick ::CStick ,
_ => unreachable! (),
};
let axis = match i {
19 | 23 | 21 | 25 => StickAxis ::XAxis ,
20 | 24 | 22 | 26 => StickAxis ::YAxis ,
_ => unreachable! (),
};
let stick_config = match stick {
Stick ::ControlStick => & mut final_config . astick_config ,
Stick ::CStick => & mut final_config . cstick_config ,
};
let to_adjust = match axis {
StickAxis ::XAxis => & mut stick_config . x_smoothing ,
StickAxis ::YAxis => & mut stick_config . y_smoothing ,
};
* to_adjust = ( * to_adjust as i8
+ match i {
19 | 23 | 20 | 24 => 1 ,
21 | 25 | 22 | 26 => - 1 ,
2024-04-02 22:05:57 +02:00
_ => unreachable! (),
2024-04-03 16:53:52 +00:00
})
. clamp ( 0 , MAX_SMOOTHING ) as u8 ;
2024-04-02 22:05:57 +02:00
2024-04-03 16:53:52 +00:00
override_gcc_state_and_wait ( & OverrideGcReportInstruction {
2024-10-14 19:55:54 +00:00
report : GcState {
2024-04-03 16:53:52 +00:00
trigger_r : 255 ,
trigger_l : 255 ,
2024-10-14 19:55:54 +00:00
buttons_2 : GcButtons2 {
2024-04-03 16:53:52 +00:00
button_r : true ,
button_l : true ,
.. Default ::default ()
},
2024-10-14 19:55:54 +00:00
buttons_1 : GcButtons1 {
2024-04-03 16:53:52 +00:00
button_x : true ,
button_y : true ,
button_a : true ,
.. Default ::default ()
},
stick_x : ( 127
+ match stick {
Stick ::ControlStick => match axis {
StickAxis ::XAxis => * to_adjust ,
StickAxis ::YAxis => 0 ,
},
Stick ::CStick => 0 ,
}) as u8 ,
stick_y : ( 127
+ match stick {
Stick ::ControlStick => match axis {
StickAxis ::XAxis => 0 ,
StickAxis ::YAxis => * to_adjust ,
},
Stick ::CStick => 0 ,
}) as u8 ,
cstick_x : ( 127
+ match stick {
Stick ::ControlStick => 0 ,
Stick ::CStick => match axis {
StickAxis ::XAxis => * to_adjust ,
StickAxis ::YAxis => 0 ,
},
}) as u8 ,
cstick_y : ( 127
+ match stick {
Stick ::ControlStick => 0 ,
Stick ::CStick => match axis {
StickAxis ::XAxis => 0 ,
StickAxis ::YAxis => * to_adjust ,
},
}) as u8 ,
2024-12-28 03:50:33 +00:00
}
. into (),
2024-04-03 16:53:52 +00:00
duration_ms : 750 ,
})
. await ;
SIGNAL_CONFIG_CHANGE . signal ( final_config . clone ());
}
// cardinalsnap increase/decrease
i if ( 27 ..= 30 ). contains ( & i ) => {
let stick = match i {
27 | 28 => Stick ::ControlStick ,
29 | 30 => Stick ::CStick ,
_ => unreachable! (),
};
let to_adjust = match stick {
Stick ::ControlStick => & mut final_config . astick_config . cardinal_snapping ,
Stick ::CStick => & mut final_config . cstick_config . cardinal_snapping ,
};
* to_adjust = ( * to_adjust
+ match i {
27 | 29 => 1 ,
28 | 30 => - 1 ,
2024-04-02 22:05:57 +02:00
_ => unreachable! (),
})
2024-04-03 16:53:52 +00:00
. clamp ( - 1 , MAX_CARDINAL_SNAP );
2024-04-02 22:05:57 +02:00
2024-04-03 16:53:52 +00:00
override_gcc_state_and_wait ( & OverrideGcReportInstruction {
2024-10-14 19:55:54 +00:00
report : GcState {
2024-04-03 16:53:52 +00:00
trigger_r : 255 ,
trigger_l : 255 ,
2024-10-14 19:55:54 +00:00
buttons_2 : GcButtons2 {
2024-04-03 16:53:52 +00:00
button_r : true ,
button_l : true ,
.. Default ::default ()
2024-04-02 22:05:57 +02:00
},
2024-10-14 19:55:54 +00:00
buttons_1 : GcButtons1 {
2024-04-03 16:53:52 +00:00
button_x : true ,
button_y : true ,
button_a : true ,
.. Default ::default ()
2024-04-02 22:05:57 +02:00
},
2024-04-03 16:53:52 +00:00
stick_x : 127 ,
stick_y : ( 127
+ match stick {
Stick ::ControlStick => * to_adjust ,
Stick ::CStick => 0 ,
}) as u8 ,
cstick_x : 127 ,
cstick_y : ( 127
+ match stick {
Stick ::ControlStick => 0 ,
Stick ::CStick => * to_adjust ,
}) as u8 ,
2024-12-28 03:50:33 +00:00
}
. into (),
2024-04-03 16:53:52 +00:00
duration_ms : 750 ,
})
. await ;
2024-04-02 22:05:57 +02:00
2024-04-03 16:53:52 +00:00
SIGNAL_CONFIG_CHANGE . signal ( final_config . clone ());
}
// scaling changes
i if ( 31 ..= 34 ). contains ( & i ) => {
let stick = match i {
31 | 32 => Stick ::ControlStick ,
33 | 34 => Stick ::CStick ,
_ => unreachable! (),
};
let to_adjust = match stick {
Stick ::ControlStick => & mut final_config . astick_config . analog_scaler ,
Stick ::CStick => & mut final_config . cstick_config . analog_scaler ,
};
* to_adjust = ( * to_adjust as i8
+ match i {
31 | 33 => 1 ,
32 | 34 => - 1 ,
_ => unreachable! (),
})
. clamp ( MIN_ANALOG_SCALER as i8 , MAX_ANALOG_SCALER as i8 )
as u8 ;
override_gcc_state_and_wait ( & OverrideGcReportInstruction {
2024-10-14 19:55:54 +00:00
report : GcState {
2024-04-03 16:53:52 +00:00
trigger_r : 255 ,
trigger_l : 255 ,
2024-10-14 19:55:54 +00:00
buttons_2 : GcButtons2 {
2024-04-03 16:53:52 +00:00
button_r : true ,
button_l : true ,
.. Default ::default ()
},
2024-10-14 19:55:54 +00:00
buttons_1 : GcButtons1 {
2024-04-03 16:53:52 +00:00
button_x : true ,
button_y : true ,
button_a : true ,
.. Default ::default ()
},
stick_x : 127 ,
stick_y : ( 127
+ match stick {
Stick ::ControlStick => * to_adjust ,
Stick ::CStick => 0 ,
}) as u8 ,
cstick_x : 127 ,
cstick_y : ( 127
+ match stick {
Stick ::ControlStick => 0 ,
Stick ::CStick => * to_adjust ,
}) as u8 ,
2024-12-28 03:50:33 +00:00
}
. into (),
2024-04-03 16:53:52 +00:00
duration_ms : 750 ,
})
. await ;
SIGNAL_CONFIG_CHANGE . signal ( final_config . clone ());
}
// rumble strength changes
i if ( 35 ..= 36 ). contains ( & i ) => {
let to_adjust = & mut final_config . rumble_strength ;
* to_adjust = ( * to_adjust as i8
+ match i {
35 => 1 ,
36 => - 1 ,
_ => unreachable! (),
})
. clamp ( 0 , MAX_RUMBLE_STRENGTH ) as u8 ;
SIGNAL_CHANGE_RUMBLE_STRENGTH . signal ( * to_adjust );
override_gcc_state_and_wait ( & OverrideGcReportInstruction {
2024-10-14 19:55:54 +00:00
report : GcState {
2024-04-03 16:53:52 +00:00
trigger_r : 255 ,
trigger_l : 255 ,
2024-10-14 19:55:54 +00:00
buttons_2 : GcButtons2 {
2024-04-03 16:53:52 +00:00
button_r : true ,
button_l : true ,
button_z : true ,
.. Default ::default ()
},
2024-10-14 19:55:54 +00:00
buttons_1 : GcButtons1 {
2024-04-03 16:53:52 +00:00
button_x : true ,
button_y : true ,
button_a : true ,
.. Default ::default ()
},
stick_x : 127 ,
2024-04-09 19:20:45 +02:00
stick_y : 127 + * to_adjust ,
2024-04-03 16:53:52 +00:00
cstick_x : 127 ,
cstick_y : 127 ,
2024-12-28 03:50:33 +00:00
}
. into (),
2024-04-03 16:53:52 +00:00
duration_ms : 750 ,
})
. await ;
SIGNAL_CONFIG_CHANGE . signal ( final_config . clone ());
}
// input consistency toggle
37 => {
2024-04-08 20:53:23 +00:00
final_config . input_consistency_mode = match final_config . input_consistency_mode {
2024-04-19 17:14:20 +02:00
InputConsistencyMode ::Original => InputConsistencyMode ::PC ,
InputConsistencyMode ::PC => InputConsistencyMode ::ConsistencyHack ,
2024-04-08 20:53:23 +00:00
InputConsistencyMode ::ConsistencyHack => InputConsistencyMode ::SuperHack ,
InputConsistencyMode ::SuperHack => InputConsistencyMode ::Original ,
};
2024-04-03 16:53:52 +00:00
override_gcc_state_and_wait ( & OverrideGcReportInstruction {
2024-10-14 19:55:54 +00:00
report : GcState {
2024-04-03 16:53:52 +00:00
trigger_r : 255 ,
trigger_l : 255 ,
2024-10-14 19:55:54 +00:00
buttons_2 : GcButtons2 {
2024-04-03 16:53:52 +00:00
button_r : true ,
button_l : true ,
.. Default ::default ()
},
2024-10-14 19:55:54 +00:00
buttons_1 : GcButtons1 {
2024-04-03 16:53:52 +00:00
button_x : true ,
button_y : true ,
button_a : true ,
.. Default ::default ()
},
stick_x : 127 ,
stick_y : ( 127_ i8
+ match final_config . input_consistency_mode {
2024-04-08 20:53:23 +00:00
InputConsistencyMode ::Original => - 69 ,
2024-04-19 17:14:20 +02:00
InputConsistencyMode ::PC => - 42 ,
2024-04-08 20:53:23 +00:00
InputConsistencyMode ::ConsistencyHack => 42 ,
InputConsistencyMode ::SuperHack => 69 ,
2024-04-03 16:53:52 +00:00
}) as u8 ,
cstick_x : 127 ,
cstick_y : 127 ,
2024-12-28 03:50:33 +00:00
}
. into (),
2024-04-03 16:53:52 +00:00
duration_ms : 750 ,
})
. await ;
SIGNAL_CONFIG_CHANGE . signal ( final_config . clone ());
}
2024-04-06 21:05:22 +02:00
// display waveshaping values on both sticks
38 => {
override_gcc_state_and_wait ( & OverrideGcReportInstruction {
2024-10-14 19:55:54 +00:00
report : GcState {
2024-04-06 21:05:22 +02:00
trigger_r : 255 ,
trigger_l : 255 ,
2024-10-14 19:55:54 +00:00
buttons_2 : GcButtons2 {
2024-04-06 21:05:22 +02:00
button_r : true ,
button_l : true ,
.. Default ::default ()
},
2024-10-14 19:55:54 +00:00
buttons_1 : GcButtons1 {
2024-04-06 21:05:22 +02:00
button_x : true ,
button_y : true ,
button_a : true ,
.. Default ::default ()
},
stick_x : 127 + final_config . astick_config . x_waveshaping ,
stick_y : 127 + final_config . astick_config . y_waveshaping ,
cstick_x : 127 + final_config . cstick_config . x_waveshaping ,
cstick_y : 127 + final_config . cstick_config . y_waveshaping ,
2024-12-28 03:50:33 +00:00
}
. into (),
2024-04-06 21:05:22 +02:00
duration_ms : 1000 ,
})
. await ;
}
// display stick smoothing values on both sticks
39 => {
override_gcc_state_and_wait ( & OverrideGcReportInstruction {
2024-10-14 19:55:54 +00:00
report : GcState {
2024-04-06 21:05:22 +02:00
trigger_r : 255 ,
trigger_l : 255 ,
2024-10-14 19:55:54 +00:00
buttons_2 : GcButtons2 {
2024-04-06 21:05:22 +02:00
button_r : true ,
button_l : true ,
.. Default ::default ()
},
2024-10-14 19:55:54 +00:00
buttons_1 : GcButtons1 {
2024-04-06 21:05:22 +02:00
button_x : true ,
button_y : true ,
button_a : true ,
.. Default ::default ()
},
stick_x : 127 + final_config . astick_config . x_smoothing ,
stick_y : 127 + final_config . astick_config . y_smoothing ,
cstick_x : 127 + final_config . cstick_config . x_smoothing ,
cstick_y : 127 + final_config . cstick_config . y_smoothing ,
2024-12-28 03:50:33 +00:00
}
. into (),
2024-04-06 21:05:22 +02:00
duration_ms : 1000 ,
})
. await ;
}
// display snapback values on both sticks
40 => {
override_gcc_state_and_wait ( & OverrideGcReportInstruction {
2024-10-14 19:55:54 +00:00
report : GcState {
2024-04-06 21:05:22 +02:00
trigger_r : 255 ,
trigger_l : 255 ,
2024-10-14 19:55:54 +00:00
buttons_2 : GcButtons2 {
2024-04-06 21:05:22 +02:00
button_r : true ,
button_l : true ,
.. Default ::default ()
},
2024-10-14 19:55:54 +00:00
buttons_1 : GcButtons1 {
2024-04-06 21:05:22 +02:00
button_x : true ,
button_y : true ,
button_a : true ,
.. Default ::default ()
},
stick_x : ( 127 + final_config . astick_config . x_snapback ) as u8 ,
stick_y : ( 127 + final_config . astick_config . y_snapback ) as u8 ,
cstick_x : ( 127 + final_config . cstick_config . x_snapback ) as u8 ,
cstick_y : ( 127 + final_config . cstick_config . y_snapback ) as u8 ,
2024-12-28 03:50:33 +00:00
}
. into (),
2024-04-06 21:05:22 +02:00
duration_ms : 1000 ,
})
. await ;
}
2024-04-03 16:53:52 +00:00
s => {
error! ( "Invalid selection in config loop: {}" , s );
continue ;
}
2024-03-29 19:54:52 +01:00
};
2024-04-03 16:53:52 +00:00
final_config . write_to_flash ( flash ). unwrap ();
2024-03-29 16:07:44 +01:00
}
2024-03-29 23:42:05 +01:00
info! ( "Exiting config main loop." );
2024-04-03 14:33:23 +02:00
final_config
2024-03-29 16:07:44 +01:00
}
#[embassy_executor::task]
2024-04-03 14:33:23 +02:00
pub async fn config_task ( mut flash : Flash < 'static , FLASH , Async , FLASH_SIZE > ) {
2024-12-28 03:50:33 +00:00
let mut gcc_subscriber = CHANNEL_CONTROLLER_STATE . subscriber (). unwrap ();
2024-03-29 16:07:44 +01:00
2024-03-30 13:20:35 +01:00
info! ( "Config task is running." );
2024-04-03 14:33:23 +02:00
// We are loading the config from flash slightly deferred mainly because
// if a debug probe is connected, it could potentially interfere.
// This means we need to dispatch "updated" config status at least once to the
// other tasks.
2024-04-03 01:46:14 +02:00
2024-04-03 14:33:23 +02:00
Timer ::after_millis ( 10 ). await ;
2024-04-03 01:46:14 +02:00
2024-04-03 14:33:23 +02:00
let mut current_config = ControllerConfig ::from_flash_memory ( & mut flash ). unwrap ();
2024-04-08 20:53:23 +00:00
{
let mut m_input_consistency = MUTEX_INPUT_CONSISTENCY_MODE . lock (). await ;
* m_input_consistency = Some ( current_config . input_consistency_mode );
}
2024-04-03 14:33:23 +02:00
SIGNAL_CHANGE_RUMBLE_STRENGTH . signal ( current_config . rumble_strength );
SIGNAL_CONFIG_CHANGE . signal ( current_config . clone ());
2024-04-03 01:46:14 +02:00
2024-03-29 16:07:44 +01:00
loop {
2024-04-02 22:05:57 +02:00
let desired_config_state = SIGNAL_CONFIG_MODE_STATUS_CHANGE . wait (). await ;
if ! desired_config_state {
info! ( "Desired config state is false, skipping config mode." );
continue ;
}
2024-03-29 19:54:52 +01:00
info! ( "Entering config mode." );
2024-04-02 22:05:57 +02:00
override_gcc_state_and_wait ( & OverrideGcReportInstruction {
2024-10-14 19:55:54 +00:00
report : GcState {
2024-04-03 16:53:52 +00:00
trigger_r : 255 ,
trigger_l : 255 ,
2024-10-14 19:55:54 +00:00
buttons_2 : GcButtons2 {
2024-04-03 16:53:52 +00:00
button_l : true ,
button_r : true ,
.. Default ::default ()
},
2024-10-14 19:55:54 +00:00
buttons_1 : GcButtons1 {
2024-04-03 16:53:52 +00:00
button_x : true ,
button_y : true ,
button_a : true ,
.. Default ::default ()
},
stick_x : 127 ,
stick_y : 127 ,
cstick_x : 127 ,
cstick_y : 127 ,
2024-12-28 03:50:33 +00:00
}
. into (),
2024-03-29 19:54:52 +01:00
duration_ms : 1000 ,
2024-04-02 22:05:57 +02:00
})
. await ;
2024-03-29 23:42:05 +01:00
2024-04-03 14:33:23 +02:00
current_config =
configuration_main_loop ( & current_config , & mut flash , & mut gcc_subscriber ). await ;
2024-03-29 19:54:52 +01:00
info! ( "Exiting config mode." );
2024-04-02 22:05:57 +02:00
SIGNAL_CONFIG_MODE_STATUS_CHANGE . signal ( false );
}
}
#[embassy_executor::task]
pub async fn enter_config_mode_task () {
2024-12-28 03:50:33 +00:00
let mut gcc_subscriber = CHANNEL_CONTROLLER_STATE . subscriber (). unwrap ();
2024-04-02 22:05:57 +02:00
info! ( "Enter config mode task is running." );
loop {
gcc_subscriber
. wait_for_simultaneous_button_presses ( & CONFIG_MODE_ENTRY_COMBO )
. await ;
info! ( "Signalling config mode status change." );
SIGNAL_CONFIG_MODE_STATUS_CHANGE . signal ( true );
Timer ::after_millis ( 1000 ). await ;
while SIGNAL_CONFIG_MODE_STATUS_CHANGE . wait (). await {}
2024-03-29 16:07:44 +01:00
}
}