Skip to content

Commit 1af879b

Browse files
committed
libsi: Add helper function to test if device is wireless
1 parent 5e0f2ed commit 1af879b

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

firmware/libsi/include/si/device/gc_controller.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#include <stdbool.h>
88
#include <stdint.h>
99

10+
#include "si/si.h"
11+
1012
// GameCube controller commands
1113
#define SI_CMD_GC_SHORT_POLL 0x40
1214
#define SI_CMD_GC_SHORT_POLL_LEN 3
@@ -116,6 +118,18 @@ struct si_device_gc_controller {
116118
*/
117119
void si_device_gc_init(struct si_device_gc_controller *device, uint8_t type);
118120

121+
/**
122+
* Check if the device is a WaveBird controller.
123+
*
124+
* @param device the device to check
125+
*
126+
* @return true if the device is a WaveBird controller
127+
*/
128+
static inline bool si_device_gc_is_wireless(struct si_device_gc_controller *device)
129+
{
130+
return device->info[0] & SI_GC_WIRELESS;
131+
}
132+
119133
/**
120134
* Set the wireless ID of the controller.
121135
*

firmware/libsi/src/device/gc_controller.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ static int handle_short_poll(const uint8_t *command, si_complete_cb_t callback,
116116
uint8_t analog_mode = command[1] & 0x07;
117117
uint8_t motor_state = command[2] & 0x03;
118118

119-
if (!(device->info[0] & SI_GC_WIRELESS)) {
119+
if (!si_device_gc_is_wireless(device)) {
120120
// Update the origin flags
121121
device->input.buttons.need_origin = (device->info[2] & SI_NEED_ORIGIN) != 0;
122122
device->input.buttons.use_origin = true;
@@ -155,7 +155,7 @@ static int handle_read_origin(const uint8_t *command, si_complete_cb_t callback,
155155
struct si_device_gc_controller *device = (struct si_device_gc_controller *)context;
156156

157157
// Tell the host it no longer needs to fetch the origin
158-
if (!(device->info[0] & SI_GC_WIRELESS)) {
158+
if (!si_device_gc_is_wireless(device)) {
159159
device->info[2] &= ~SI_NEED_ORIGIN;
160160
}
161161

@@ -187,7 +187,7 @@ static int handle_calibrate(const uint8_t *command, si_complete_cb_t callback, v
187187
device->origin.trigger_right = device->input.trigger_right;
188188

189189
// Tell the host it no longer needs to fetch the origin
190-
if (!(device->info[0] & SI_GC_WIRELESS)) {
190+
if (!si_device_gc_is_wireless(device)) {
191191
device->info[2] &= ~SI_NEED_ORIGIN;
192192
}
193193

@@ -218,7 +218,7 @@ static int handle_long_poll(const uint8_t *command, si_complete_cb_t callback, v
218218
device->input.buttons.use_origin = true;
219219

220220
// Save the analog mode and motor state
221-
if (!(device->info[0] & SI_GC_WIRELESS)) {
221+
if (!si_device_gc_is_wireless(device)) {
222222
device->info[2] &= ~(SI_MOTOR_STATE_MASK | SI_ANALOG_MODE_MASK);
223223
device->info[2] |= motor_state << 3 | analog_mode;
224224
}
@@ -297,7 +297,7 @@ void si_device_gc_init(struct si_device_gc_controller *device, uint8_t type)
297297
device->input_valid = true;
298298

299299
// Request the origin on non-wireless controllers
300-
if (!(type & SI_GC_WIRELESS))
300+
if (!si_device_gc_is_wireless(device))
301301
device->info[2] = SI_NEED_ORIGIN;
302302

303303
// Register the SI commands handled by GameCube controllers
@@ -309,7 +309,7 @@ void si_device_gc_init(struct si_device_gc_controller *device, uint8_t type)
309309
si_command_register(SI_CMD_GC_LONG_POLL, SI_CMD_GC_LONG_POLL_LEN, handle_long_poll, device);
310310

311311
// Register additional commands handled by WaveBird receivers
312-
if (type & SI_GC_WIRELESS) {
312+
if (si_device_gc_is_wireless(device)) {
313313
si_command_register(SI_CMD_GC_PROBE_DEVICE, SI_CMD_GC_PROBE_DEVICE_LEN, handle_probe_device, device);
314314
si_command_register(SI_CMD_GC_FIX_DEVICE, SI_CMD_GC_FIX_DEVICE_LEN, handle_fix_device, device);
315315
}
@@ -340,5 +340,6 @@ void si_device_gc_set_wireless_origin(struct si_device_gc_controller *device, ui
340340
}
341341

342342
// Set the "has wireless origin" flag in the device info
343-
device->info[1] |= SI_WIRELESS_ORIGIN;
343+
if (si_device_gc_is_wireless(device))
344+
device->info[1] |= SI_WIRELESS_ORIGIN;
344345
}

0 commit comments

Comments
 (0)