From 960bef65dd924a908033686af6933f12d61f75b1 Mon Sep 17 00:00:00 2001 From: d4nin3u Date: Mon, 5 Aug 2024 08:13:33 +0100 Subject: [PATCH] Reuse existing LED output serial config and code for OpenITHM LED protocol --- chuniio/chuniio.c | 56 --------------------------------------- chuniio/config.c | 6 ++--- chuniio/config.h | 3 ++- chuniio/ledoutput.c | 6 ++++- chuniio/meson.build | 3 --- chuniio/serialimpl.c | 26 ++++++++++++++++++ chuniio/serialimpl.h | 1 + dist/chuni/segatools.ini | 4 ++- dist/chusan/segatools.ini | 8 +++--- 9 files changed, 42 insertions(+), 71 deletions(-) diff --git a/chuniio/chuniio.c b/chuniio/chuniio.c index 251f62f..27fe7b0 100644 --- a/chuniio/chuniio.c +++ b/chuniio/chuniio.c @@ -19,7 +19,6 @@ static uint8_t chuni_io_hand_pos; static HANDLE chuni_io_slider_thread; static bool chuni_io_slider_stop_flag; static struct chuni_io_config chuni_io_cfg; -static HANDLE chuni_io_slider_led_port; uint16_t chuni_io_get_api_version(void) { @@ -122,38 +121,6 @@ void chuni_io_slider_start(chuni_io_slider_callback_t callback) callback, 0, NULL); - - chuni_io_slider_led_port = CreateFileW(chuni_io_cfg.led_com, - GENERIC_READ | GENERIC_WRITE, - 0, - NULL, - OPEN_EXISTING, - 0, - NULL); - - if (chuni_io_slider_led_port == INVALID_HANDLE_VALUE) - dprintf("OpenITHM LEDs: Failed to open COM port (Attempted on %S)\n", chuni_io_cfg.led_com); - else - dprintf("OpenITHM LEDs: COM Port Success!\n"); - - DCB dcb_serial_params = { 0 }; - dcb_serial_params.DCBlength = sizeof(dcb_serial_params); - status = GetCommState(chuni_io_slider_led_port, &dcb_serial_params); - - dcb_serial_params.BaudRate = CBR_115200; // Setting BaudRate = 115200 - dcb_serial_params.ByteSize = 8; // Setting ByteSize = 8 - dcb_serial_params.StopBits = ONESTOPBIT;// Setting StopBits = 1 - dcb_serial_params.Parity = NOPARITY; // Setting Parity = None - SetCommState(chuni_io_slider_led_port, &dcb_serial_params); - - COMMTIMEOUTS timeouts = { 0 }; - timeouts.ReadIntervalTimeout = 50; // in milliseconds - timeouts.ReadTotalTimeoutConstant = 50; // in milliseconds - timeouts.ReadTotalTimeoutMultiplier = 10; // in milliseconds - timeouts.WriteTotalTimeoutConstant = 50; // in milliseconds - timeouts.WriteTotalTimeoutMultiplier = 10; // in milliseconds - - SetCommTimeouts(chuni_io_slider_led_port, &timeouts); } void chuni_io_slider_stop(void) @@ -168,34 +135,11 @@ void chuni_io_slider_stop(void) CloseHandle(chuni_io_slider_thread); chuni_io_slider_thread = NULL; chuni_io_slider_stop_flag = false; - - dprintf("OpenITHM LEDs: Closing COM port\n"); - CloseHandle(chuni_io_slider_led_port); } void chuni_io_slider_set_leds(const uint8_t *rgb) { led_output_update(2, rgb); - if (chuni_io_slider_led_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(chuni_io_slider_led_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); - } } static unsigned int __stdcall chuni_io_slider_thread_proc(void *ctx) diff --git a/chuniio/config.c b/chuniio/config.c index 47ae181..5c7bca1 100644 --- a/chuniio/config.c +++ b/chuniio/config.c @@ -57,16 +57,14 @@ void chuni_io_config_load( filename); } - GetPrivateProfileStringW(L"slider", L"ledport", L"COM5", port_input, 6, filename); - wcsncpy(cfg->led_com, L"\\\\.\\", 4); - wcsncat_s(cfg->led_com, 11, port_input, 6); - cfg->cab_led_output_pipe = GetPrivateProfileIntW(L"led", L"cabLedOutputPipe", 1, filename); cfg->cab_led_output_serial = GetPrivateProfileIntW(L"led", L"cabLedOutputSerial", 0, filename); 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 0efe394..8c5bf2c 100644 --- a/chuniio/config.h +++ b/chuniio/config.h @@ -10,7 +10,6 @@ struct chuni_io_config { uint8_t vk_ir_emu; uint8_t vk_ir[6]; uint8_t vk_cell[32]; - wchar_t led_com[12]; // Which ways to output LED information are enabled bool cab_led_output_pipe; @@ -19,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 71bedc1..8b03dff 100644 --- a/chuniio/meson.build +++ b/chuniio/meson.build @@ -4,9 +4,6 @@ chuniio_lib = static_library( include_directories : inc, implicit_include_directories : false, c_pch : '../precompiled.h', - link_with : [ - util_lib - ], sources : [ 'chu2to3.c', diff --git a/chuniio/serialimpl.c b/chuniio/serialimpl.c index 57ebb7f..04b1c39 100644 --- a/chuniio/serialimpl.c +++ b/chuniio/serialimpl.c @@ -97,3 +97,29 @@ 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..7ef3996 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..a97c06c 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=