Skip to content

Commit

Permalink
wii u pro: rumble stops when duration_ms = 0
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardoquesada committed Apr 16, 2024
1 parent fc8e6ae commit 19cb8d3
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 26 deletions.
10 changes: 5 additions & 5 deletions src/components/bluepad32/parser/uni_hid_parser_ds3.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ typedef struct __attribute((packed)) {
static ds3_instance_t* get_ds3_instance(uni_hid_device_t* d);
static void ds3_update_led(uni_hid_device_t* d, uint8_t player_leds);
static void ds3_send_output_report(uni_hid_device_t* d, ds3_output_report_t* out);
static void ds3_set_rumble_on(btstack_timer_source_t* ts);
static void ds3_play_dual_rumble_now(struct uni_hid_device_s* d,
static void on_ds3_set_rumble_on(btstack_timer_source_t* ts);
static void ds3_play_dual_rumble_now(uni_hid_device_t* d,
uint16_t duration_ms,
uint8_t weak_magnitude,
uint8_t strong_magnitude);
Expand Down Expand Up @@ -283,7 +283,7 @@ void uni_hid_parser_ds3_play_dual_rumble(struct uni_hid_device_s* d,
ds3_play_dual_rumble_now(d, duration_ms, weak_magnitude, strong_magnitude);
} else {
// Set timer to have a delayed start
ins->rumble_timer_delayed_start.process = &ds3_set_rumble_on;
ins->rumble_timer_delayed_start.process = &on_ds3_set_rumble_on;
ins->rumble_timer_delayed_start.context = d;
ins->rumble_state = DS3_STATE_RUMBLE_DELAYED;
ins->rumble_duration_ms = duration_ms;
Expand Down Expand Up @@ -353,7 +353,7 @@ static void ds3_update_led(uni_hid_device_t* d, uint8_t player_leds) {
ds3_send_output_report(d, &out);
}

static void ds3_play_dual_rumble_now(struct uni_hid_device_s* d,
static void ds3_play_dual_rumble_now(uni_hid_device_t* d,
uint16_t duration_ms,
uint8_t weak_magnitude,
uint8_t strong_magnitude) {
Expand All @@ -378,7 +378,7 @@ static void ds3_play_dual_rumble_now(struct uni_hid_device_s* d,
ins->rumble_state = DS3_STATE_RUMBLE_DISABLED;
}

static void ds3_set_rumble_on(btstack_timer_source_t* ts) {
static void on_ds3_set_rumble_on(btstack_timer_source_t* ts) {
uni_hid_device_t* d = ts->context;
ds3_instance_t* ins = get_ds3_instance(d);

Expand Down
52 changes: 31 additions & 21 deletions src/components/bluepad32/parser/uni_hid_parser_wii.c
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,8 @@ static void wii_fsm_dump_eeprom(uni_hid_device_t* d);
static void wii_read_mem(uni_hid_device_t* d, wii_read_type_t t, uint32_t offset, uint16_t size);
static wii_instance_t* get_wii_instance(uni_hid_device_t* d);
static void wii_set_led(uni_hid_device_t* d, uni_gamepad_seat_t seat);
static void wii_set_rumble_on(btstack_timer_source_t* ts);
static void wii_set_rumble_off(btstack_timer_source_t* ts);
static void on_wii_set_rumble_on(btstack_timer_source_t* ts);
static void on_wii_set_rumble_off(btstack_timer_source_t* ts);
static void wii_play_dual_rumble_now(struct uni_hid_device_s* d, uint16_t duration_ms);

// Constants
Expand Down Expand Up @@ -1327,14 +1327,14 @@ void uni_hid_parser_wii_play_dual_rumble(struct uni_hid_device_s* d,
uint16_t duration_ms,
uint8_t weak_magnitude,
uint8_t strong_magnitude) {
ARG_UNUSED(weak_magnitude);
ARG_UNUSED(strong_magnitude);

if (d == NULL) {
loge("Wii: Invalid device\n");
return;
}

if ((weak_magnitude == 0 && strong_magnitude == 0) || duration_ms == 0)
return;

wii_instance_t* ins = get_wii_instance(d);
if (ins->state < WII_FSM_LED_UPDATED) {
return;
Expand All @@ -1356,7 +1356,7 @@ void uni_hid_parser_wii_play_dual_rumble(struct uni_hid_device_s* d,
wii_play_dual_rumble_now(d, duration_ms);
} else {
// Set timer to have a delayed start
ins->rumble_timer_delayed_start.process = &wii_set_rumble_on;
ins->rumble_timer_delayed_start.process = &on_wii_set_rumble_on;
ins->rumble_timer_delayed_start.context = d;
ins->rumble_state = WII_STATE_RUMBLE_DELAYED;
ins->rumble_duration_ms = duration_ms;
Expand Down Expand Up @@ -1399,42 +1399,52 @@ static void wii_set_led(uni_hid_device_t* d, uni_gamepad_seat_t seat) {
uni_hid_device_send_intr_report(d, report, sizeof(report));
}

static void wii_play_dual_rumble_now(struct uni_hid_device_s* d, uint16_t duration_ms) {
static void wii_stop_rumble_now(uni_hid_device_t* d) {
wii_instance_t* ins = get_wii_instance(d);

// No need to protect it with a mutex since it runs in the same main thread
assert(ins->rumble_state == WII_STATE_RUMBLE_IN_PROGRESS);
ins->rumble_state = WII_STATE_RUMBLE_DISABLED;

// Disable rumble
uint8_t report[] = {
0xa2, WIIPROTO_REQ_RUMBLE, 0x00 /* Rumble off*/
};
uni_hid_device_send_intr_report(d, report, sizeof(report));
}

static void wii_play_dual_rumble_now(uni_hid_device_t* d, uint16_t duration_ms) {
wii_instance_t* ins = get_wii_instance(d);

if (duration_ms == 0) {
if (ins->rumble_state == WII_STATE_RUMBLE_IN_PROGRESS)
wii_stop_rumble_now(d);
return;
}

uint8_t report[] = {
0xa2, WIIPROTO_REQ_RUMBLE, 0x01 /* Rumble on*/
};
uni_hid_device_send_intr_report(d, report, sizeof(report));

// Set timer to turn off rumble
ins->rumble_timer_duration.process = &wii_set_rumble_off;
ins->rumble_timer_duration.process = &on_wii_set_rumble_off;
ins->rumble_timer_duration.context = d;
ins->rumble_state = WII_STATE_RUMBLE_IN_PROGRESS;
btstack_run_loop_set_timer(&ins->rumble_timer_duration, duration_ms);
btstack_run_loop_add_timer(&ins->rumble_timer_duration);
}

static void wii_set_rumble_on(btstack_timer_source_t* ts) {
static void on_wii_set_rumble_on(btstack_timer_source_t* ts) {
uni_hid_device_t* d = ts->context;
wii_instance_t* ins = get_wii_instance(d);

wii_play_dual_rumble_now(d, ins->rumble_duration_ms);
}

static void wii_set_rumble_off(btstack_timer_source_t* ts) {
static void on_wii_set_rumble_off(btstack_timer_source_t* ts) {
uni_hid_device_t* d = btstack_run_loop_get_timer_context(ts);
wii_instance_t* ins = get_wii_instance(d);

// No need to protect it with a mutex since it runs in the same main thread
assert(ins->rumble_state == WII_STATE_RUMBLE_IN_PROGRESS);
ins->rumble_state = WII_STATE_RUMBLE_DISABLED;

// Disable rumble
uint8_t report[] = {
0xa2, WIIPROTO_REQ_RUMBLE, 0x00 /* Rumble off*/
};
uni_hid_device_send_intr_report(d, report, sizeof(report));
wii_stop_rumble_now(d);
}

static void wii_read_mem(uni_hid_device_t* d, wii_read_type_t t, uint32_t offset, uint16_t size) {
Expand Down

0 comments on commit 19cb8d3

Please sign in to comment.