From 37c26ecadb2ce4d2cb3a61ce96c2c931ba2fc346 Mon Sep 17 00:00:00 2001 From: d4nin3u Date: Tue, 6 Aug 2024 21:35:51 +0000 Subject: [PATCH] chuniio: Add OpeNITHM LED protocol support (#26) This commit basically copy-pastes the last commits from https://dev.s-ul.net/VeroxZik/segatools/-/commits/master to add OpenITHM LED support. Doesn't need to edit segatools.ini because the relevant config lines are already there for some reason. Tested with my OpenITHM controller and behaves exactly like the other fork. Reviewed-on: https://gitea.tendokyu.moe/Dniel97/segatools/pulls/26 Co-authored-by: d4nin3u Co-committed-by: d4nin3u --- chuniio/chuniio.h | 7 ++++--- chuniio/config.c | 2 ++ chuniio/config.h | 2 ++ chuniio/ledoutput.c | 6 +++++- chuniio/meson.build | 1 + chuniio/serialimpl.c | 24 ++++++++++++++++++++++++ chuniio/serialimpl.h | 1 + dist/chuni/segatools.ini | 4 +++- dist/chusan/segatools.ini | 8 +++----- 9 files changed, 45 insertions(+), 10 deletions(-) diff --git a/chuniio/chuniio.h b/chuniio/chuniio.h index 8c575c9..c13bff4 100644 --- a/chuniio/chuniio.h +++ b/chuniio/chuniio.h @@ -139,9 +139,10 @@ void chuni_io_slider_start(chuni_io_slider_callback_t callback); void chuni_io_slider_stop(void); /* Update the RGB lighting on the slider. A pointer to an array of 32 * 3 = 96 - bytes is supplied. The illuminated areas on the touch slider are some - combination of rectangular regions and dividing lines between these regions - but the exact mapping of this lighting control buffer is still TBD. + bytes is supplied, organized in BRG format. + The first set of bytes is the right-most slider key, and from there the bytes + alternate between the dividers and the keys until the left-most key. + There are 31 illuminated sections in total. Minimum API version: 0x0100 */ diff --git a/chuniio/config.c b/chuniio/config.c index aaa048e..0f8b31b 100644 --- a/chuniio/config.c +++ b/chuniio/config.c @@ -63,6 +63,8 @@ void chuni_io_config_load( cfg->controller_led_output_pipe = GetPrivateProfileIntW(L"led", L"controllerLedOutputPipe", 1, filename); cfg->controller_led_output_serial = GetPrivateProfileIntW(L"led", L"controllerLedOutputSerial", 0, filename); + cfg->controller_led_output_openithm = GetPrivateProfileIntW(L"led", L"controllerLedOutputOpeNITHM", 0, filename); + cfg->led_serial_baud = GetPrivateProfileIntW(L"led", L"serialBaud", 921600, filename); GetPrivateProfileStringW( diff --git a/chuniio/config.h b/chuniio/config.h index 3eb8223..8c5bf2c 100644 --- a/chuniio/config.h +++ b/chuniio/config.h @@ -18,6 +18,8 @@ struct chuni_io_config { bool controller_led_output_pipe; bool controller_led_output_serial; + bool controller_led_output_openithm; + // The name of a COM port to output LED data on, in serial mode wchar_t led_serial_port[12]; int32_t led_serial_baud; diff --git a/chuniio/ledoutput.c b/chuniio/ledoutput.c index 8cac5a3..6052e42 100644 --- a/chuniio/ledoutput.c +++ b/chuniio/ledoutput.c @@ -127,7 +127,11 @@ void led_output_update(uint8_t board, const byte* rgb) if (config->controller_led_output_serial) { - led_serial_update(escaped_data); + if (config->controller_led_output_openithm){ + led_serial_update_openithm(rgb); + } else { + led_serial_update(escaped_data); + } } } } diff --git a/chuniio/meson.build b/chuniio/meson.build index 864aa87..8b03dff 100644 --- a/chuniio/meson.build +++ b/chuniio/meson.build @@ -4,6 +4,7 @@ chuniio_lib = static_library( include_directories : inc, implicit_include_directories : false, c_pch : '../precompiled.h', + sources : [ 'chu2to3.c', 'chu2to3.h', diff --git a/chuniio/serialimpl.c b/chuniio/serialimpl.c index 57ebb7f..7a68435 100644 --- a/chuniio/serialimpl.c +++ b/chuniio/serialimpl.c @@ -97,3 +97,27 @@ void led_serial_update(struct _chuni_led_data_buf_t* data) ReleaseMutex(serial_write_mutex); } + +void led_serial_update_openithm(const byte* rgb) +{ + if (serial_port != INVALID_HANDLE_VALUE) + { + char led_buffer[100]; + DWORD bytes_to_write; // No of bytes to write into the port + DWORD bytes_written = 0; // No of bytes written to the port + bytes_to_write = sizeof(led_buffer); + BOOL status; + + led_buffer[0] = 0xAA; + led_buffer[1] = 0xAA; + memcpy(led_buffer+2, rgb, sizeof(uint8_t) * 96); + led_buffer[98] = 0xDD; + led_buffer[99] = 0xDD; + + status = WriteFile(serial_port, // Handle to the Serial port + led_buffer, // Data to be written to the port + bytes_to_write, // No of bytes to write + &bytes_written, // Bytes written + NULL); + } +} diff --git a/chuniio/serialimpl.h b/chuniio/serialimpl.h index f8f276b..3f37803 100644 --- a/chuniio/serialimpl.h +++ b/chuniio/serialimpl.h @@ -13,3 +13,4 @@ HRESULT led_serial_init(wchar_t led_com[12], DWORD baud); void led_serial_update(struct _chuni_led_data_buf_t* data); +void led_serial_update_openithm(const byte* rgb); \ No newline at end of file diff --git a/dist/chuni/segatools.ini b/dist/chuni/segatools.ini index 87d923d..5b8af0c 100644 --- a/dist/chuni/segatools.ini +++ b/dist/chuni/segatools.ini @@ -82,10 +82,12 @@ cabLedOutputSerial=0 controllerLedOutputPipe=1 ; Output slider LED data to the serial port controllerLedOutputSerial=0 +; Use the OpeNITHM protocol for serial LED output +controllerLedOutputOpeNITHM=0 ; Serial port to send data to if using serial output. Default is COM5. ;serialPort=COM5 -; Baud rate for serial data +; Baud rate for serial data (set to 115200 if using OpeNITHM) ;serialBaud=921600 ; Data output a sequence of bytes, with JVS-like framing. diff --git a/dist/chusan/segatools.ini b/dist/chusan/segatools.ini index c167f26..c0b19de 100644 --- a/dist/chusan/segatools.ini +++ b/dist/chusan/segatools.ini @@ -108,10 +108,12 @@ cabLedOutputSerial=0 controllerLedOutputPipe=1 ; Output slider LED data to the serial port controllerLedOutputSerial=0 +; Use the OpeNITHM protocol for serial LED output +controllerLedOutputOpeNITHM=0 ; Serial port to send data to if using serial output. Default is COM5. ;serialPort=COM5 -; Baud rate for serial data +; Baud rate for serial data (set to 115200 if using OpeNITHM) ;serialBaud=921600 ; Data output a sequence of bytes, with JVS-like framing. @@ -202,7 +204,3 @@ ir=0x20 ; ... etc ... ;cell31=0x53 ;cell32=0x53 - -; Enable slider LED serial output. This follows OpeNITHM Serial LED Protocol. -; eg. COM5 -;ledport=