From 89c3ada28f8fd649758b0e73f6fa7781426b2461 Mon Sep 17 00:00:00 2001 From: Will Xyen Date: Wed, 9 Dec 2020 17:21:29 -0800 Subject: [PATCH] iidxhook9: Add turntable multiplier --- dist/iidx/iidxhook-27.conf | 3 +++ src/main/bio2emu-iidx/bi2a.c | 34 ++++++++++++++++++++++++++++++++-- src/main/bio2emu-iidx/bi2a.h | 1 + src/main/iidxhook9/config-io.c | 20 ++++++++++++++++++++ src/main/iidxhook9/config-io.h | 1 + src/main/util/Module.mk | 1 + src/main/util/math.c | 22 ++++++++++++++++++++++ src/main/util/math.h | 9 +++++++++ src/main/vigem-sdvxio/main.c | 23 +++-------------------- 9 files changed, 92 insertions(+), 22 deletions(-) create mode 100644 src/main/util/math.c create mode 100644 src/main/util/math.h diff --git a/dist/iidx/iidxhook-27.conf b/dist/iidx/iidxhook-27.conf index 7627a20..5b89420 100644 --- a/dist/iidx/iidxhook-27.conf +++ b/dist/iidx/iidxhook-27.conf @@ -16,6 +16,9 @@ io.disable_cams=false # Disables the built in file hooks, requiring manual file creation io.disable_file_hooks=false +# Turntable sensitivity multiplier (1.0 is default) +io.tt_multiplier=1.0 + # Disables the camera emulation cam.disable_emu=false diff --git a/src/main/bio2emu-iidx/bi2a.c b/src/main/bio2emu-iidx/bi2a.c index fc66222..4af5e54 100644 --- a/src/main/bio2emu-iidx/bi2a.c +++ b/src/main/bio2emu-iidx/bi2a.c @@ -4,11 +4,13 @@ #include /* for _BitScanForward */ +#include #include #include #include #include "acioemu/emu.h" +#include "util/math.h" #include "bemanitools/iidxio.h" @@ -27,6 +29,11 @@ static bool poll_delay; static bool coin_latch; static uint8_t coin_count; +static bool tt_multiplier_set; +static float tt_multiplier = 1.f; +static uint8_t tt_accum[2]; +static int16_t tt_last[2]; + int get_default_slider_valid(size_t idx) { if (default_sliders[idx] >= 0 && default_sliders[idx] <= 15) { @@ -36,6 +43,12 @@ int get_default_slider_valid(size_t idx) } } +void bio2_emu_bi2a_set_tt_multiplier(float multiplier) +{ + tt_multiplier_set = true; + tt_multiplier = multiplier; +} + void bio2_emu_bi2a_init( struct bio2emu_port *bio2_emu, bool disable_poll_limiter) { @@ -158,6 +171,15 @@ static void bio2_emu_bi2a_send_status( ac_io_emu_response_push(emu, &resp, 0); } +static int16_t tt_mult_delta(size_t tt_no) { + int16_t current_tt = iidx_io_ep2_get_turntable(tt_no); + int16_t delta = get_wrapped_delta_s16(current_tt, tt_last[tt_no], 256); + + tt_last[tt_no] = current_tt; + + return round(delta * tt_multiplier); +} + static void bio2_emu_bi2a_send_state(struct ac_io_emu *emu, const struct ac_io_message *req) { @@ -233,8 +255,16 @@ bio2_emu_bi2a_send_state(struct ac_io_emu *emu, const struct ac_io_message *req) return bio2_emu_bi2a_send_status(emu, req, 0); } - body->TURNTABLE1 = iidx_io_ep2_get_turntable(0); - body->TURNTABLE2 = iidx_io_ep2_get_turntable(1); + if (tt_multiplier_set) { + tt_accum[0] += tt_mult_delta(0); + tt_accum[1] += tt_mult_delta(1); + + body->TURNTABLE1 = tt_accum[0]; + body->TURNTABLE2 = tt_accum[1]; + } else { + body->TURNTABLE1 = iidx_io_ep2_get_turntable(0); + body->TURNTABLE2 = iidx_io_ep2_get_turntable(1); + } body->SLIDER1.s_val = get_default_slider_valid(0) ? default_sliders[0] : diff --git a/src/main/bio2emu-iidx/bi2a.h b/src/main/bio2emu-iidx/bi2a.h index c8e3d55..7648072 100644 --- a/src/main/bio2emu-iidx/bi2a.h +++ b/src/main/bio2emu-iidx/bi2a.h @@ -6,6 +6,7 @@ #include "bio2emu/emu.h" void bio2_emu_bi2a_init(struct bio2emu_port *in, bool disable_poll_limiter); +void bio2_emu_bi2a_set_tt_multiplier(float multiplier); void bio2_emu_bi2a_dispatch_request( struct bio2emu_port *bio2port, const struct ac_io_message *req); diff --git a/src/main/iidxhook9/config-io.c b/src/main/iidxhook9/config-io.c index 2d5eb0e..a6f02a9 100644 --- a/src/main/iidxhook9/config-io.c +++ b/src/main/iidxhook9/config-io.c @@ -11,6 +11,7 @@ #define IIDXHOOK9_CONFIG_IO_LIGHTNING_MODE_KEY "io.lightning_mode" #define IIDXHOOK9_CONFIG_IO_DISABLE_CAMS_KEY "io.disable_cams" #define IIDXHOOK9_CONFIG_IO_DISABLE_FILE_HOOKS_KEY "io.disable_file_hooks" +#define IIDXHOOK9_CONFIG_IO_TT_MULTIPLIER_KEY "io.tt_multiplier" #define IIDXHOOK9_CONFIG_IO_DEFAULT_DISABLE_CARD_READER_EMU_VALUE false #define IIDXHOOK9_CONFIG_IO_DEFAULT_DISABLE_BIO2_EMU_VALUE false @@ -18,6 +19,7 @@ #define IIDXHOOK9_CONFIG_IO_DEFAULT_LIGHTNING_MODE_VALUE false #define IIDXHOOK9_CONFIG_IO_DEFAULT_DISABLE_CAMS_VALUE false #define IIDXHOOK9_CONFIG_IO_DEFAULT_DISABLE_FILE_HOOKS_VALUE false +#define IIDXHOOK9_CONFIG_IO_DEFAULT_TT_MULTIPLIER_VALUE 1.0 void iidxhook9_config_io_init(struct cconfig *config) { @@ -57,6 +59,12 @@ void iidxhook9_config_io_init(struct cconfig *config) IIDXHOOK9_CONFIG_IO_DISABLE_FILE_HOOKS_KEY, IIDXHOOK9_CONFIG_IO_DEFAULT_DISABLE_FILE_HOOKS_VALUE, "Disables the built in file hooks, requiring manual file creation"); + + cconfig_util_set_float( + config, + IIDXHOOK9_CONFIG_IO_TT_MULTIPLIER_KEY, + IIDXHOOK9_CONFIG_IO_DEFAULT_TT_MULTIPLIER_VALUE, + "Turntable sensitivity multiplier (1.0 is default)"); } void iidxhook9_config_io_get( @@ -133,4 +141,16 @@ void iidxhook9_config_io_get( IIDXHOOK9_CONFIG_IO_DISABLE_FILE_HOOKS_KEY, IIDXHOOK9_CONFIG_IO_DEFAULT_DISABLE_FILE_HOOKS_VALUE); } + + if (!cconfig_util_get_float( + config, + IIDXHOOK9_CONFIG_IO_TT_MULTIPLIER_KEY, + &config_io->tt_multiplier, + IIDXHOOK9_CONFIG_IO_DEFAULT_TT_MULTIPLIER_VALUE)) { + log_warning( + "Invalid value for key '%s' specified, fallback " + "to default '%f'", + IIDXHOOK9_CONFIG_IO_TT_MULTIPLIER_KEY, + IIDXHOOK9_CONFIG_IO_DEFAULT_TT_MULTIPLIER_VALUE); + } } diff --git a/src/main/iidxhook9/config-io.h b/src/main/iidxhook9/config-io.h index 564b360..6bc3091 100644 --- a/src/main/iidxhook9/config-io.h +++ b/src/main/iidxhook9/config-io.h @@ -12,6 +12,7 @@ struct iidxhook9_config_io { bool lightning_mode; bool disable_cams; bool disable_file_hooks; + float tt_multiplier; }; void iidxhook9_config_io_init(struct cconfig *config); diff --git a/src/main/util/Module.mk b/src/main/util/Module.mk index 74c508a..478e841 100644 --- a/src/main/util/Module.mk +++ b/src/main/util/Module.mk @@ -10,6 +10,7 @@ src_util := \ iobuf.c \ list.c \ log.c \ + math.c \ mem.c \ msg-thread.c \ net.c \ diff --git a/src/main/util/math.c b/src/main/util/math.c new file mode 100644 index 0000000..e06f1e8 --- /dev/null +++ b/src/main/util/math.c @@ -0,0 +1,22 @@ +#include "util/math.h" + +int16_t get_wrapped_delta_s16(int16_t val, int16_t last, int16_t bound) { + int16_t half_point = bound / 2; + + int16_t delta = val - last; + + if (delta > half_point) { + delta -= bound; + } + + if (delta < -half_point) { + delta += bound; + } + + // delta is now between (-half_point - half_point) + return delta; +} + +int16_t sign_s16(int16_t x) { + return (x > 0) - (x < 0); +} diff --git a/src/main/util/math.h b/src/main/util/math.h new file mode 100644 index 0000000..e26d23e --- /dev/null +++ b/src/main/util/math.h @@ -0,0 +1,9 @@ +#ifndef UTIL_MATH_H +#define UTIL_MATH_H + +#include + +int16_t get_wrapped_delta_s16(int16_t val, int16_t last, int16_t bound); +int16_t sign_s16(int16_t x); + +#endif diff --git a/src/main/vigem-sdvxio/main.c b/src/main/vigem-sdvxio/main.c index 8194292..8505518 100644 --- a/src/main/vigem-sdvxio/main.c +++ b/src/main/vigem-sdvxio/main.c @@ -9,6 +9,7 @@ #include "bemanitools/sdvxio.h" #include "util/log.h" +#include "util/math.h" #include "util/thread.h" #include "vigemstub/helper.h" @@ -22,25 +23,6 @@ int16_t convert_analog_to_s16(uint16_t val) return (int64_t)(val * 64); } -int16_t get_relative_delta(int16_t val, int16_t last) -{ - // val is 10 bit - const int16_t half_point = 512; // 2^9 - - int16_t delta = val - last; - - if (delta > half_point) { - delta -= 1024; - } - - if (delta < -half_point) { - delta += 1024; - } - - // delta is now between (-512 - 512) - return delta; -} - int16_t filter_floor(int32_t value, int16_t floor) { if (abs(value) < floor) { return 0; @@ -58,7 +40,8 @@ int16_t filter_floor(int32_t value, int16_t floor) { int32_t convert_relative_analog( uint16_t val, uint16_t last, int32_t buffered_last, int16_t multiplier) { - int16_t delta = get_relative_delta(val, last); + // val is 10 bit + int16_t delta = get_wrapped_delta_s16(val, last, 1024); if (delta == 0) { // ease the stick back to 0 like a real stick would