Skip to content

Commit

Permalink
[ADAPTER] Fix N64 accessories toggle rumble feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
darthcloud committed Oct 6, 2024
1 parent b785ca5 commit 2648266
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 27 deletions.
32 changes: 31 additions & 1 deletion main/adapter/adapter.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "wired/wired.h"
#include "wireless/wireless.h"
#include "macro.h"
#include "bluetooth/host.h"

const uint32_t hat_to_ld_btns[16] = {
BIT(PAD_LD_UP), BIT(PAD_LD_UP) | BIT(PAD_LD_RIGHT), BIT(PAD_LD_RIGHT), BIT(PAD_LD_DOWN) | BIT(PAD_LD_RIGHT),
Expand All @@ -44,6 +45,7 @@ struct generic_fb fb_input;
struct bt_adapter bt_adapter = {0};
struct wired_adapter wired_adapter = {0};
static uint32_t adapter_out_mask[WIRED_MAX_DEV] = {0};
static bool rumble_mute = false;

static uint32_t btn_id_to_btn_idx(uint8_t btn_id) {
if (btn_id < 32) {
Expand Down Expand Up @@ -221,6 +223,9 @@ static void adapter_fb_stop_cb(void* arg) {

adapter_fb_stop_timer_stop((uint8_t)(uintptr_t)arg);

/* Unmute system rumble */
rumble_mute = false;

/* Send 0 byte data, system that require callback stop shall look for that */
adapter_q_fb(&fb_data);
}
Expand Down Expand Up @@ -495,7 +500,32 @@ uint32_t adapter_bridge_fb(struct raw_fb *fb_data, struct bt_data *bt_data) {

void IRAM_ATTR adapter_q_fb(struct raw_fb *fb_data) {
/* Best efford only on fb */
queue_bss_enqueue(wired_adapter.input_q_hdl, (uint8_t *)fb_data, sizeof(*fb_data));
if (!rumble_mute) {
queue_bss_enqueue(wired_adapter.input_q_hdl, (uint8_t *)fb_data, sizeof(*fb_data));
}
}

void adapter_toggle_fb(uint32_t wired_id, uint32_t duration_us) {
struct bt_dev *device = NULL;
struct bt_data *bt_data = NULL;

bt_host_get_active_dev_from_out_idx(wired_id, &device);
if (device) {
bt_data = &bt_adapter.data[device->ids.id];
if (bt_data) {
struct generic_fb fb_data = {0};

fb_data.wired_id = wired_id;
fb_data.type = FB_TYPE_RUMBLE;
fb_data.state = 1;
fb_data.hf_pwr = 0xFF;
fb_data.lf_pwr = 0xFF;
rumble_mute = true;
adapter_fb_stop_timer_start(wired_id, duration_us);
wireless_fb_from_generic(&fb_data, bt_data);
bt_hid_feedback(device, bt_data->base.output);
}
}
}

void adapter_init(void) {
Expand Down
1 change: 1 addition & 0 deletions main/adapter/adapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,7 @@ void adapter_fb_stop_timer_start(uint8_t dev_id, uint64_t dur_us);
void adapter_fb_stop_timer_stop(uint8_t dev_id);
uint32_t adapter_bridge_fb(struct raw_fb *fb_data, struct bt_data *bt_data);
void adapter_q_fb(struct raw_fb *fb_data);
void adapter_toggle_fb(uint32_t wired_id, uint32_t duration_us);
void adapter_init(void);
void adapter_meta_init(void);

Expand Down
40 changes: 14 additions & 26 deletions main/adapter/wired/n64.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
#include <string.h>
#include "zephyr/types.h"
#include "tools/util.h"
#include "adapter/adapter.h"
#include "adapter/config.h"
#include "bluetooth/host.h"
#include "adapter/wired/wired.h"
#include "adapter/wireless/wireless.h"
#include "n64.h"
Expand Down Expand Up @@ -186,26 +186,6 @@ void n64_meta_init(struct wired_ctrl *ctrl_data) {
}
}

static void n64_acc_toggle_fb(uint32_t wired_id, uint32_t duration_us) {
struct bt_dev *device = NULL;
struct bt_data *bt_data = NULL;

bt_host_get_active_dev_from_out_idx(wired_id, &device);
if (device) {
bt_data = &bt_adapter.data[device->ids.id];
if (bt_data) {
struct generic_fb fb_data = {0};

fb_data.wired_id = wired_id;
fb_data.type = FB_TYPE_RUMBLE;
fb_data.state = 1;
adapter_fb_stop_timer_start(wired_id, duration_us);
wireless_fb_from_generic(&fb_data, bt_data);
bt_hid_feedback(device, bt_data->base.output);
}
}
}

static void n64_ctrl_special_action(struct wired_ctrl *ctrl_data, struct wired_data *wired_data) {
/* Memory / Rumble toggle */
if (ctrl_data->map_mask[0] & generic_btns_mask[PAD_MT]) {
Expand All @@ -221,12 +201,12 @@ static void n64_ctrl_special_action(struct wired_ctrl *ctrl_data, struct wired_d
/* Change config directly but do not update file */
if (config.out_cfg[ctrl_data->index].acc_mode == ACC_MEM) {
config.out_cfg[ctrl_data->index].acc_mode = ACC_RUMBLE;
n64_acc_toggle_fb(ctrl_data->index, 250000);
adapter_toggle_fb(ctrl_data->index, 300000);
printf("# %s: Set rumble pak\n", __FUNCTION__);
}
else {
config.out_cfg[ctrl_data->index].acc_mode = ACC_MEM;
n64_acc_toggle_fb(ctrl_data->index, 75000);
adapter_toggle_fb(ctrl_data->index, 150000);
printf("# %s: Set ctrl pak\n", __FUNCTION__);
}
}
Expand Down Expand Up @@ -374,9 +354,17 @@ void n64_from_generic(int32_t dev_mode, struct wired_ctrl *ctrl_data, struct wir
void n64_fb_to_generic(int32_t dev_mode, struct raw_fb *raw_fb_data, struct generic_fb *fb_data) {
fb_data->wired_id = raw_fb_data->header.wired_id;
fb_data->type = raw_fb_data->header.type;
fb_data->state = raw_fb_data->data[0];
fb_data->lf_pwr = (fb_data->state) ? 0xFF : 0x00;
fb_data->hf_pwr = (fb_data->state) ? 0xFF : 0x00;

/* This stop rumble when BR timeout trigger */
if (raw_fb_data->header.data_len == 0) {
fb_data->state = 0;
fb_data->lf_pwr = fb_data->hf_pwr = 0;
}
else {
fb_data->state = raw_fb_data->data[0];
fb_data->lf_pwr = (fb_data->state) ? 0xFF : 0x00;
fb_data->hf_pwr = (fb_data->state) ? 0xFF : 0x00;
}
}

void IRAM_ATTR n64_gen_turbo_mask(struct wired_data *wired_data) {
Expand Down

0 comments on commit 2648266

Please sign in to comment.