@@ -75,7 +75,7 @@ struct {
7575
7676// Joybus state
7777static struct joybus_gecko joybus_gecko ;
78- static struct joybus_gc_controller joybus_gc_controller ;
78+ static struct joybus_gc_controller wavebird_controller ;
7979static struct joybus * joybus = JOYBUS (& joybus_gecko );
8080
8181// Buttons, switches, and LEDs
@@ -99,25 +99,6 @@ void SysTick_Handler(void)
9999 millis ++ ;
100100}
101101
102- // Initialize (or reinitialize) as a controller on the SI bus
103- static void initialize_controller (uint8_t controller_type )
104- {
105- switch (controller_type ) {
106- case WP_CONT_TYPE_GC_WIRED :
107- joybus_gc_controller_init (& joybus_gc_controller , JOYBUS_GAMECUBE_CONTROLLER );
108- break ;
109- case WP_CONT_TYPE_GC_WIRED_NOMOTOR :
110- joybus_gc_controller_init (& joybus_gc_controller , JOYBUS_GAMECUBE_CONTROLLER | JOYBUS_ID_GCN_NO_MOTOR );
111- break ;
112- default :
113- printf ("Unknown controller type '%d', defaulting to WaveBird" , controller_type );
114- /* fall through */
115- case WP_CONT_TYPE_GC_WAVEBIRD :
116- joybus_gc_controller_init (& joybus_gc_controller , JOYBUS_WAVEBIRD_RECEIVER );
117- break ;
118- }
119- }
120-
121102#if HAS_PAIR_BTN
122103// Begin or end pairing when the pair button is pressed
123104static void handle_pair_button_press ()
@@ -164,13 +145,13 @@ static void handle_wavebird_packet(const uint8_t *packet)
164145 // Check the controller id is as expected
165146 if (settings .cont_type == WP_CONT_TYPE_GC_WAVEBIRD ) {
166147 // Implement wireless ID pinning exactly as OEM WaveBird receivers do
167- if (joybus_gc_controller_wireless_id_fixed (& joybus_gc_controller )) {
148+ if (joybus_gc_controller_wireless_id_fixed (& wavebird_controller )) {
168149 // Drop packets from other controllers if the ID has been fixed
169- if (joybus_gc_controller_get_wireless_id (& joybus_gc_controller ) != wireless_id )
150+ if (joybus_gc_controller_get_wireless_id (& wavebird_controller ) != wireless_id )
170151 return ;
171152 } else {
172153 // Set the controller ID if it is not fixed
173- joybus_gc_controller_set_wireless_id (& joybus_gc_controller , wireless_id );
154+ joybus_gc_controller_set_wireless_id (& wavebird_controller , wireless_id );
174155 }
175156 } else {
176157 // Emulate wireless ID pinning for wired controllers
@@ -196,15 +177,15 @@ static void handle_wavebird_packet(const uint8_t *packet)
196177 //
197178
198179 // Copy the buttons from the WaveBird message
199- joybus_gc_controller .input .buttons &= ~JOYBUS_GCN_BUTTON_MASK ;
200- joybus_gc_controller .input .buttons |=
180+ wavebird_controller .input .buttons &= ~JOYBUS_GCN_BUTTON_MASK ;
181+ wavebird_controller .input .buttons |=
201182 ((message [3 ] & 0x80 ) >> 7 ) | ((message [2 ] & 0x0F ) << 1 ) | ((message [3 ] & 0x7F ) << 8 );
202183
203184 // Copy the stick, substick, and trigger values
204- memcpy (& joybus_gc_controller .input .stick_x , & message [4 ], 6 );
185+ memcpy (& wavebird_controller .input .stick_x , & message [4 ], 6 );
205186
206187 // Set the input state as valid, and (re)set the validity timer
207- joybus_gc_controller_input_valid (& joybus_gc_controller , true);
188+ joybus_gc_controller_input_valid (& wavebird_controller , true);
208189 input_valid_until = millis + INPUT_VALID_MS ;
209190 } else {
210191 //
@@ -216,7 +197,7 @@ static void handle_wavebird_packet(const uint8_t *packet)
216197 wavebird_origin_copy ((uint8_t * )& new_origin .stick_x , message );
217198
218199 // Update the origin state in the Joybus device
219- joybus_gc_controller_set_origin (& joybus_gc_controller , & new_origin );
200+ joybus_gc_controller_set_origin (& wavebird_controller , & new_origin );
220201 }
221202}
222203
@@ -260,9 +241,6 @@ static void handle_pairing_finished(uint8_t status, uint8_t channel)
260241 // Set the LED solid for 1 second to indicate pairing success
261242 if (status_led )
262243 led_effect_blink (status_led , 1000 , 1 );
263-
264- // Reset the controller
265- initialize_controller (settings .cont_type );
266244 } else if (status == WB_RADIO_PAIRING_TIMEOUT ) {
267245 printf ("Pairing timed out\n" );
268246
@@ -378,12 +356,25 @@ int main(void)
378356 wavebird_radio_set_channel (settings .chan );
379357 }
380358
359+ // Initialize the WaveBird controller target
360+ switch (settings .cont_type ) {
361+ case WP_CONT_TYPE_GC_WIRED :
362+ joybus_gc_controller_init (& wavebird_controller , JOYBUS_GAMECUBE_CONTROLLER );
363+ break ;
364+ case WP_CONT_TYPE_GC_WIRED_NOMOTOR :
365+ joybus_gc_controller_init (& wavebird_controller , JOYBUS_GAMECUBE_CONTROLLER | JOYBUS_ID_GCN_NO_MOTOR );
366+ break ;
367+ default :
368+ printf ("Unknown controller type '%d', defaulting to WaveBird" , settings .cont_type );
369+ /* fall through */
370+ case WP_CONT_TYPE_GC_WAVEBIRD :
371+ joybus_gc_controller_init (& wavebird_controller , JOYBUS_WAVEBIRD_RECEIVER );
372+ break ;
373+ }
374+
381375 // Initialize Joybus
382376 joybus_gecko_init (& joybus_gecko , JOYBUS_PORT , JOYBUS_PIN , JOYBUS_TIMER , JOYBUS_USART );
383- joybus_target_register (joybus , JOYBUS_TARGET (& joybus_gc_controller ));
384-
385- // Register to handle controller SI commands
386- initialize_controller (settings .cont_type );
377+ joybus_target_register (joybus , JOYBUS_TARGET (& wavebird_controller ));
387378
388379 // Enable Joybus
389380 joybus_enable (joybus );
@@ -405,7 +396,7 @@ int main(void)
405396 led_effect_update (status_led , millis );
406397
407398 // Invalidate stale inputs
408- if (joybus_gc_controller .input_valid && (int32_t )(millis - input_valid_until ) >= 0 )
409- joybus_gc_controller_input_valid (& joybus_gc_controller , false);
399+ if (wavebird_controller .input_valid && (int32_t )(millis - input_valid_until ) >= 0 )
400+ joybus_gc_controller_input_valid (& wavebird_controller , false);
410401 }
411402}
0 commit comments