Skip to content

Commit

Permalink
[MACRO] Rework macro to be customizable via virtual buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
darthcloud committed Aug 23, 2023
1 parent 1510a52 commit ff56aa8
Show file tree
Hide file tree
Showing 23 changed files with 129 additions and 90 deletions.
5 changes: 2 additions & 3 deletions main/adapter/adapter.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019-2022, Jacques Gagnon
* Copyright (c) 2019-2023, Jacques Gagnon
* SPDX-License-Identifier: Apache-2.0
*/

Expand Down Expand Up @@ -341,8 +341,6 @@ void adapter_bridge(struct bt_data *bt_data) {
return;
}

sys_macro_hdl(ctrl_input, &bt_data->base.flags[PAD]);

#ifdef CONFIG_BLUERETRO_ADAPTER_INPUT_DBG
#ifdef CONFIG_BLUERETRO_JSON_DBG
printf("{\"log_type\": \"generic_input\"");
Expand Down Expand Up @@ -372,6 +370,7 @@ void adapter_bridge(struct bt_data *bt_data) {
for (uint32_t i = 0; out_mask; i++, out_mask >>= 1) {
if (out_mask & 0x1) {
ctrl_output[i].index = i;
sys_macro_hdl(&ctrl_output[i], &bt_data->base.flags[PAD]);
wired_from_generic(config.out_cfg[i].dev_mode, &ctrl_output[i], &wired_adapter.data[i]);
}
}
Expand Down
15 changes: 15 additions & 0 deletions main/adapter/adapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,21 @@ enum {
KBM_MAX,
};

enum {
BR_COMBO_BASE_1 = 119,
BR_COMBO_BASE_2,
BR_COMBO_BASE_3,
BR_COMBO_BASE_4,
BR_COMBO_SYS_RESET,
BR_COMBO_BT_INQUIRY,
BR_COMBO_SYS_POWER_OFF,
BR_COMBO_FACTORY_RESET,
BR_COMBO_DEEP_SLEEP,
BR_COMBO_MAX,
};
#define BR_COMBO_CNT (BR_COMBO_MAX - BR_COMBO_BASE_1)
#define BR_COMBO_MASK 0xFF800000

enum {
AXIS_NONE = -1,
AXIS_LX,
Expand Down
44 changes: 41 additions & 3 deletions main/adapter/config.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019-2022, Jacques Gagnon
* Copyright (c) 2019-2023, Jacques Gagnon
* SPDX-License-Identifier: Apache-2.0
*/

Expand All @@ -19,17 +19,23 @@ static uint32_t config_version_magic[] = {
CONFIG_MAGIC_V0,
CONFIG_MAGIC_V1,
CONFIG_MAGIC_V2,
CONFIG_MAGIC_V3,
};
static uint8_t config_default_combo[BR_COMBO_CNT] = {
PAD_LM, PAD_RM, PAD_MM, PAD_RB_UP, PAD_RB_LEFT, PAD_RB_RIGHT, PAD_RB_DOWN, PAD_LD_UP, PAD_LD_DOWN
};

static void config_init_struct(struct config *data);
static int32_t config_load_from_file(struct config *data, char *filename);
static int32_t config_store_on_file(struct config *data, char *filename);
static int32_t config_v0_update(struct config *data, char *filename);
static int32_t config_v1_update(struct config *data, char *filename);
static int32_t config_v2_update(struct config *data, char *filename);

static int32_t (*config_ver_update[])(struct config *data, char *filename) = {
config_v0_update,
config_v1_update,
config_v2_update,
NULL,
};

Expand Down Expand Up @@ -85,6 +91,27 @@ static int32_t config_get_version(uint32_t magic) {
return -1;
}

static int32_t config_v2_update(struct config *data, char *filename) {
data->magic = CONFIG_MAGIC;

for (uint32_t i = 0; i < WIRED_MAX_DEV; i++) {
uint32_t j = data->in_cfg[i].map_size;
data->in_cfg[i].map_size += BR_COMBO_CNT;
for (uint32_t k = 0; k < BR_COMBO_CNT; j++, k++) {
data->in_cfg[i].map_cfg[j].src_btn = config_default_combo[k];
data->in_cfg[i].map_cfg[j].dst_btn = k + BR_COMBO_BASE_1;
data->in_cfg[i].map_cfg[j].dst_id = i;
data->in_cfg[i].map_cfg[j].perc_max = 100;
data->in_cfg[i].map_cfg[j].perc_threshold = 50;
data->in_cfg[i].map_cfg[j].perc_deadzone = 135;
data->in_cfg[i].map_cfg[j].turbo = 0;
data->in_cfg[i].map_cfg[j].algo = 0;
}
}

return config_store_on_file(data, filename);
}

static void config_init_struct(struct config *data) {
data->magic = CONFIG_MAGIC;
data->global_cfg.system_cfg = WIRED_AUTO;
Expand All @@ -97,8 +124,9 @@ static void config_init_struct(struct config *data) {
data->out_cfg[i].acc_mode = ACC_NONE;
data->in_cfg[i].bt_dev_id = 0x00; /* Not used placeholder */
data->in_cfg[i].bt_subdev_id = 0x00; /* Not used placeholder */
data->in_cfg[i].map_size = KBM_MAX;
for (uint32_t j = 0; j < KBM_MAX; j++) {
data->in_cfg[i].map_size = KBM_MAX + BR_COMBO_CNT;
uint32_t j = 0;
for (; j < KBM_MAX; j++) {
data->in_cfg[i].map_cfg[j].src_btn = j;
data->in_cfg[i].map_cfg[j].dst_btn = j;
data->in_cfg[i].map_cfg[j].dst_id = i;
Expand All @@ -108,6 +136,16 @@ static void config_init_struct(struct config *data) {
data->in_cfg[i].map_cfg[j].turbo = 0;
data->in_cfg[i].map_cfg[j].algo = 0;
}
for (uint32_t k = 0; k < BR_COMBO_CNT; j++, k++) {
data->in_cfg[i].map_cfg[j].src_btn = config_default_combo[k];
data->in_cfg[i].map_cfg[j].dst_btn = k + BR_COMBO_BASE_1;
data->in_cfg[i].map_cfg[j].dst_id = i;
data->in_cfg[i].map_cfg[j].perc_max = 100;
data->in_cfg[i].map_cfg[j].perc_threshold = 50;
data->in_cfg[i].map_cfg[j].perc_deadzone = 135;
data->in_cfg[i].map_cfg[j].turbo = 0;
data->in_cfg[i].map_cfg[j].algo = 0;
}
}
}

Expand Down
7 changes: 4 additions & 3 deletions main/adapter/config.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019-2022, Jacques Gagnon
* Copyright (c) 2019-2023, Jacques Gagnon
* SPDX-License-Identifier: Apache-2.0
*/

Expand All @@ -11,8 +11,9 @@
#define CONFIG_MAGIC_V0 0xA5A5A5A5
#define CONFIG_MAGIC_V1 0xF380D824
#define CONFIG_MAGIC_V2 0x1782DB8A
#define CONFIG_MAGIC CONFIG_MAGIC_V2
#define CONFIG_VERSION 2
#define CONFIG_MAGIC_V3 0xAB89C69A
#define CONFIG_MAGIC CONFIG_MAGIC_V3
#define CONFIG_VERSION 3
#define ADAPTER_MAPPING_MAX 128

enum {
Expand Down
36 changes: 11 additions & 25 deletions main/adapter/macro.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019-2022, Jacques Gagnon
* Copyright (c) 2019-2023, Jacques Gagnon
* SPDX-License-Identifier: Apache-2.0
*/

Expand All @@ -8,12 +8,14 @@
#include "system/manager.h"
#include "macro.h"

#define MACRO_BASE (BIT(PAD_MM) | BIT(PAD_LM) | BIT(PAD_RM))
#define SYS_RESET (MACRO_BASE | BIT(PAD_RB_LEFT))
#define BT_INQUIRY (MACRO_BASE | BIT(PAD_RB_RIGHT))
#define SYS_POWER_OFF (MACRO_BASE | BIT(PAD_RB_DOWN))
#define FACTORY_RESET (MACRO_BASE | BIT(PAD_LD_UP) | BIT(PAD_RB_UP))
#define DEEP_SLEEP (MACRO_BASE | BIT(PAD_LD_DOWN) | BIT(PAD_RB_UP))
#define BIT32(n) (1UL << ((n) & 0x1F))

#define MACRO_BASE (BIT32(BR_COMBO_BASE_1) | BIT32(BR_COMBO_BASE_2) | BIT32(BR_COMBO_BASE_3))
#define SYS_RESET (MACRO_BASE | BIT32(BR_COMBO_SYS_RESET))
#define BT_INQUIRY (MACRO_BASE | BIT32(BR_COMBO_BT_INQUIRY))
#define SYS_POWER_OFF (MACRO_BASE | BIT32(BR_COMBO_SYS_POWER_OFF))
#define FACTORY_RESET (MACRO_BASE | BIT32(BR_COMBO_BASE_4) | BIT32(BR_COMBO_FACTORY_RESET))
#define DEEP_SLEEP (MACRO_BASE | BIT32(BR_COMBO_BASE_4) | BIT32(BR_COMBO_DEEP_SLEEP))

struct macro {
uint32_t macro;
Expand Down Expand Up @@ -43,24 +45,8 @@ static void check_macro(int32_t value, struct macro *macro, atomic_t *flags) {
}
}

void sys_macro_hdl(struct wireless_ctrl *ctrl_data, atomic_t *flags) {
int32_t value = ctrl_data->btns[0].value;

if (ctrl_data->axes[TRIG_L].meta && *ctrl_data->desc & BIT(PAD_LM)) {
int32_t threshold = (int32_t)(((float)50.0/100) * ctrl_data->axes[TRIG_L].meta->abs_max);

if (ctrl_data->axes[TRIG_L].value > threshold) {
value |= BIT(PAD_LM);
}
}

if (ctrl_data->axes[TRIG_R].meta && *ctrl_data->desc & BIT(PAD_RM)) {
int32_t threshold = (int32_t)(((float)50.0/100) * ctrl_data->axes[TRIG_R].meta->abs_max);

if (ctrl_data->axes[TRIG_R].value > threshold) {
value |= BIT(PAD_RM);
}
}
void sys_macro_hdl(struct wired_ctrl *ctrl_data, atomic_t *flags) {
int32_t value = ctrl_data->btns[3].value;

for (uint32_t i = 0; i < sizeof(macros)/sizeof(macros[0]); i++) {
struct macro *macro = &macros[i];
Expand Down
2 changes: 1 addition & 1 deletion main/adapter/macro.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@

#include "adapter/adapter.h"

void sys_macro_hdl(struct wireless_ctrl *ctrl_data, atomic_t *flags);
void sys_macro_hdl(struct wired_ctrl *ctrl_data, atomic_t *flags);

#endif /* _SYS_MACRO_H_ */
8 changes: 4 additions & 4 deletions main/adapter/wired/cdi.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019-2022, Jacques Gagnon
* Copyright (c) 2019-2023, Jacques Gagnon
* SPDX-License-Identifier: Apache-2.0
*/

Expand Down Expand Up @@ -43,7 +43,7 @@ struct cdi_map {
int32_t raw_axes[2];
} __packed;

static const uint32_t cdi_mask[4] = {0x0005000F, 0x00000000, 0x00000000, 0x00000000};
static const uint32_t cdi_mask[4] = {0x0005000F, 0x00000000, 0x00000000, BR_COMBO_MASK};
static const uint32_t cdi_desc[4] = {0x0000000F, 0x00000000, 0x00000000, 0x00000000};
static DRAM_ATTR const uint32_t cdi_btns_mask[32] = {
0, 0, 0, 0,
Expand All @@ -56,7 +56,7 @@ static DRAM_ATTR const uint32_t cdi_btns_mask[32] = {
0, 0, 0, 0,
};

static const uint32_t cdi_mouse_mask[4] = {0x190000F0, 0x00000000, 0x00000000, 0x00000000};
static const uint32_t cdi_mouse_mask[4] = {0x190000F0, 0x00000000, 0x00000000, BR_COMBO_MASK};
static const uint32_t cdi_mouse_desc[4] = {0x000000F0, 0x00000000, 0x00000000, 0x00000000};
static const uint32_t cdi_mouse_btns_mask[32] = {
0, 0, 0, 0,
Expand All @@ -69,7 +69,7 @@ static const uint32_t cdi_mouse_btns_mask[32] = {
BIT(CDI_1), 0, 0, 0,
};

static const uint32_t cdi_kb_mask[4] = {0xE6FF0F0F, 0xFFFFFFFF, 0xFFFFFFFF, 0x0007FFFF};
static const uint32_t cdi_kb_mask[4] = {0xE6FF0F0F, 0xFFFFFFFF, 0xFFFFFFFF, 0x0007FFFF | BR_COMBO_MASK};
static const uint32_t cdi_kb_desc[4] = {0x00000000, 0x00000000, 0x00000000, 0x00000000};

/* Flash bite the bullet here, their is no dominant pattern between the special */
Expand Down
8 changes: 4 additions & 4 deletions main/adapter/wired/dc.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019-2022, Jacques Gagnon
* Copyright (c) 2019-2023, Jacques Gagnon
* SPDX-License-Identifier: Apache-2.0
*/

Expand Down Expand Up @@ -99,7 +99,7 @@ struct dc_kb_map {
};
} __packed;

static const uint32_t dc_mask[4] = {0x333FFFFF, 0x00000000, 0x00000000, 0x00000000};
static const uint32_t dc_mask[4] = {0x333FFFFF, 0x00000000, 0x00000000, BR_COMBO_MASK};
static const uint32_t dc_desc[4] = {0x110000FF, 0x00000000, 0x00000000, 0x00000000};
static DRAM_ATTR const uint32_t dc_btns_mask[32] = {
0, 0, 0, 0,
Expand All @@ -112,7 +112,7 @@ static DRAM_ATTR const uint32_t dc_btns_mask[32] = {
0, BIT(DC_C), 0, 0,
};

static const uint32_t dc_mouse_mask[4] = {0x1901C0F0, 0x00000000, 0x00000000, 0x00000000};
static const uint32_t dc_mouse_mask[4] = {0x1901C0F0, 0x00000000, 0x00000000, BR_COMBO_MASK};
static const uint32_t dc_mouse_desc[4] = {0x0000C0F0, 0x00000000, 0x00000000, 0x00000000};
static const uint32_t dc_mouse_btns_mask[32] = {
0, 0, 0, 0,
Expand All @@ -125,7 +125,7 @@ static const uint32_t dc_mouse_btns_mask[32] = {
BIT(DC_X), 0, 0, 0,
};

static const uint32_t dc_kb_mask[4] = {0xE6FF0F0F, 0xFFFFFFFF, 0xFFFFFFFF, 0x0007FFFF};
static const uint32_t dc_kb_mask[4] = {0xE6FF0F0F, 0xFFFFFFFF, 0xFFFFFFFF, 0x0007FFFF | BR_COMBO_MASK};
static const uint32_t dc_kb_desc[4] = {0x00000000, 0x00000000, 0x00000000, 0x00000000};
static const uint8_t dc_kb_scancode[KBM_MAX] = {
/* KB_A, KB_D, KB_S, KB_W, MOUSE_X_LEFT, MOUSE_X_RIGHT, MOUSE_Y_DOWN MOUSE_Y_UP */
Expand Down
6 changes: 3 additions & 3 deletions main/adapter/wired/gc.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019-2022, Jacques Gagnon
* Copyright (c) 2019-2023, Jacques Gagnon
* SPDX-License-Identifier: Apache-2.0
*/

Expand Down Expand Up @@ -53,7 +53,7 @@ struct gc_kb_map {
uint8_t xor;
} __packed;

static const uint32_t gc_mask[4] = {0x771F0FFF, 0x00000000, 0x00000000, 0x00000000};
static const uint32_t gc_mask[4] = {0x771F0FFF, 0x00000000, 0x00000000, BR_COMBO_MASK};
static const uint32_t gc_desc[4] = {0x110000FF, 0x00000000, 0x00000000, 0x00000000};
static DRAM_ATTR const uint32_t gc_btns_mask[32] = {
0, 0, 0, 0,
Expand All @@ -66,7 +66,7 @@ static DRAM_ATTR const uint32_t gc_btns_mask[32] = {
0, BIT(GC_Z), BIT(GC_R), 0,
};

static const uint32_t gc_kb_mask[4] = {0xE6FF0F0F, 0xFFFFFFFF, 0x1FBFFFFF, 0x0003C000};
static const uint32_t gc_kb_mask[4] = {0xE6FF0F0F, 0xFFFFFFFF, 0x1FBFFFFF, 0x0003C000 | BR_COMBO_MASK};
static const uint32_t gc_kb_desc[4] = {0x00000000, 0x00000000, 0x00000000, 0x00000000};
static const uint8_t gc_kb_scancode[KBM_MAX] = {
/* KB_A, KB_D, KB_S, KB_W, MOUSE_X_LEFT, MOUSE_X_RIGHT, MOUSE_Y_DOWN MOUSE_Y_UP */
Expand Down
6 changes: 3 additions & 3 deletions main/adapter/wired/genesis.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020-2022, Jacques Gagnon
* Copyright (c) 2020-2023, Jacques Gagnon
* SPDX-License-Identifier: Apache-2.0
*/

Expand Down Expand Up @@ -103,7 +103,7 @@ struct sega_mouse_map {
int32_t raw_axes[2];
} __packed;

static const uint32_t sega_mouse_mask[4] = {0x190100F0, 0x00000000, 0x00000000, 0x00000000};
static const uint32_t sega_mouse_mask[4] = {0x190100F0, 0x00000000, 0x00000000, BR_COMBO_MASK};
static const uint32_t sega_mouse_desc[4] = {0x000000F0, 0x00000000, 0x00000000, 0x00000000};
static const uint32_t sega_mouse_btns_mask[32] = {
0, 0, 0, 0,
Expand All @@ -122,7 +122,7 @@ struct genesis_map {
uint16_t twh_buttons;
} __packed;

static const uint32_t genesis_mask[4] = {0x333F0F00, 0x00000000, 0x00000000, 0x00000000};
static const uint32_t genesis_mask[4] = {0x333F0F00, 0x00000000, 0x00000000, BR_COMBO_MASK};
static const uint32_t genesis_desc[4] = {0x00000000, 0x00000000, 0x00000000, 0x00000000};
static DRAM_ATTR const uint32_t genesis_btns_mask[2][3][32] = {
{
Expand Down
6 changes: 3 additions & 3 deletions main/adapter/wired/jag.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021-2022, Jacques Gagnon
* Copyright (c) 2021-2023, Jacques Gagnon
* SPDX-License-Identifier: Apache-2.0
*/

Expand Down Expand Up @@ -68,7 +68,7 @@ struct jag_map {
uint32_t buttons_s1[3][4];
} __packed;

static const uint32_t jag_mask[4] = {0xBB7F0FFF, 0x00000000, 0x00000000, 0x00000000};
static const uint32_t jag_mask[4] = {0xBB7F0FFF, 0x00000000, 0x00000000, BR_COMBO_MASK};
static const uint32_t jag_desc[4] = {0x00000000, 0x00000000, 0x00000000, 0x00000000};
static DRAM_ATTR const uint32_t jag_btns_mask[][32] = {
/* Pause, A, Up, Down, Left, Right */
Expand Down Expand Up @@ -117,7 +117,7 @@ static DRAM_ATTR const uint32_t jag_btns_mask[][32] = {
},
};

static const uint32_t jag_6d_mask[4] = {0xFFFF7FFF, 0x00000000, 0x00000000, 0x00000000};
static const uint32_t jag_6d_mask[4] = {0xFFFF7FFF, 0x00000000, 0x00000000, BR_COMBO_MASK};
static const uint32_t jag_6d_desc[4] = {0x110000FF, 0x00000000, 0x00000000, 0x00000000};
static DRAM_ATTR const uint32_t jag_6d_btns_mask[][32] = {
/* Pause, A, Up, Down, Left, Right */
Expand Down
4 changes: 2 additions & 2 deletions main/adapter/wired/jvs.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019-2022, Jacques Gagnon
* Copyright (c) 2019-2023, Jacques Gagnon
* SPDX-License-Identifier: Apache-2.0
*/

Expand Down Expand Up @@ -50,7 +50,7 @@ struct jvs_map {
uint8_t test;
} __packed;

static const uint32_t jvs_mask[4] = {0xBBFF0F0F, 0x00000000, 0x00000000, 0x00000000};
static const uint32_t jvs_mask[4] = {0xBBFF0F0F, 0x00000000, 0x00000000, BR_COMBO_MASK};
static const uint32_t jvs_desc[4] = {0x0000000F, 0x00000000, 0x00000000, 0x00000000};
static DRAM_ATTR const uint32_t jvs_btns_mask[32] = {
0, 0, 0, 0,
Expand Down
Loading

0 comments on commit ff56aa8

Please sign in to comment.