2022-01-04 09:46:30 +01:00
|
|
|
#include <windows.h>
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <process.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "board/slider-cmd.h"
|
|
|
|
#include "board/slider-frame.h"
|
|
|
|
|
|
|
|
#include "mercuryhook/mercury-dll.h"
|
|
|
|
#include "mercuryhook/touch.h"
|
|
|
|
|
|
|
|
#include "hook/iobuf.h"
|
|
|
|
#include "hook/iohook.h"
|
|
|
|
|
|
|
|
#include "hooklib/uart.h"
|
2022-01-05 08:03:55 +01:00
|
|
|
#include "hooklib/fdshark.h"
|
2022-01-04 09:46:30 +01:00
|
|
|
|
|
|
|
#include "util/dprintf.h"
|
|
|
|
#include "util/dump.h"
|
|
|
|
|
2022-05-18 07:33:09 +02:00
|
|
|
const char SYNC_BOARD_VER[6] = "190523";
|
|
|
|
const char UNIT_BOARD_VER[6] = "190514";
|
|
|
|
|
2022-01-05 11:07:29 +01:00
|
|
|
static HRESULT touch_handle_irp(struct irp *irp);
|
2022-01-04 09:46:30 +01:00
|
|
|
static HRESULT touch0_handle_irp_locked(struct irp *irp);
|
2022-01-05 08:03:55 +01:00
|
|
|
static HRESULT touch1_handle_irp_locked(struct irp *irp);
|
2022-01-04 09:46:30 +01:00
|
|
|
|
2022-01-05 08:03:55 +01:00
|
|
|
static HRESULT touch_req_dispatch(const struct touch_req *req);
|
2022-01-04 09:46:30 +01:00
|
|
|
|
2022-01-05 11:07:29 +01:00
|
|
|
static HRESULT touch_frame_decode(struct touch_req *dest, struct iobuf *iobuf, int side);
|
2022-01-08 03:16:33 +01:00
|
|
|
static uint8_t calc_checksum(const void *ptr, size_t nbytes);
|
2022-01-05 08:03:55 +01:00
|
|
|
|
2022-01-08 08:25:42 +01:00
|
|
|
static HRESULT touch_handle_get_sync_board_ver(const struct touch_req *req);
|
2022-02-10 18:17:10 +01:00
|
|
|
static HRESULT touch_handle_next_read(const struct touch_req *req);
|
2022-01-08 08:25:42 +01:00
|
|
|
static HRESULT touch_handle_get_unit_board_ver(const struct touch_req *req);
|
2022-01-08 03:16:33 +01:00
|
|
|
static HRESULT touch_handle_mystery1(const struct touch_req *req);
|
|
|
|
static HRESULT touch_handle_mystery2(const struct touch_req *req);
|
|
|
|
static HRESULT touch_handle_start_auto_scan(const struct touch_req *req);
|
2022-01-09 18:30:31 +01:00
|
|
|
static void touch_res_auto_scan(const bool *state);
|
2022-01-08 03:16:33 +01:00
|
|
|
|
2022-01-08 23:19:10 +01:00
|
|
|
uint8_t input_frame_count_0 = 0x7b;
|
|
|
|
uint8_t input_frame_count_1 = 0x7b;
|
|
|
|
bool touch0_auto = false;
|
|
|
|
bool touch1_auto = false;
|
2022-01-04 09:46:30 +01:00
|
|
|
|
|
|
|
static CRITICAL_SECTION touch0_lock;
|
|
|
|
static struct uart touch0_uart;
|
|
|
|
static uint8_t touch0_written_bytes[520];
|
|
|
|
static uint8_t touch0_readable_bytes[520];
|
|
|
|
|
|
|
|
static CRITICAL_SECTION touch1_lock;
|
|
|
|
static struct uart touch1_uart;
|
|
|
|
static uint8_t touch1_written_bytes[520];
|
|
|
|
static uint8_t touch1_readable_bytes[520];
|
|
|
|
|
|
|
|
HRESULT touch_hook_init(const struct touch_config *cfg)
|
2022-01-08 03:16:33 +01:00
|
|
|
{
|
2022-01-04 09:46:30 +01:00
|
|
|
assert(cfg != NULL);
|
2022-01-08 03:16:33 +01:00
|
|
|
assert(mercury_dll.touch_init != NULL);
|
2022-01-04 09:46:30 +01:00
|
|
|
|
2022-01-08 08:25:42 +01:00
|
|
|
if (!cfg->enable) {
|
2022-01-04 09:46:30 +01:00
|
|
|
return S_FALSE;
|
2022-01-08 08:25:42 +01:00
|
|
|
}
|
2022-01-04 09:46:30 +01:00
|
|
|
|
|
|
|
InitializeCriticalSection(&touch0_lock);
|
|
|
|
InitializeCriticalSection(&touch1_lock);
|
2022-05-29 06:23:24 +02:00
|
|
|
dprintf("Wacca touch: Init\n");
|
2022-01-04 09:46:30 +01:00
|
|
|
|
|
|
|
uart_init(&touch0_uart, 3);
|
|
|
|
touch0_uart.written.bytes = touch0_written_bytes;
|
|
|
|
touch0_uart.written.nbytes = sizeof(touch0_written_bytes);
|
|
|
|
touch0_uart.readable.bytes = touch0_readable_bytes;
|
|
|
|
touch0_uart.readable.nbytes = sizeof(touch0_readable_bytes);
|
|
|
|
|
|
|
|
uart_init(&touch1_uart, 4);
|
|
|
|
touch1_uart.written.bytes = touch1_written_bytes;
|
|
|
|
touch1_uart.written.nbytes = sizeof(touch1_written_bytes);
|
|
|
|
touch1_uart.readable.bytes = touch1_readable_bytes;
|
|
|
|
touch1_uart.readable.nbytes = sizeof(touch1_readable_bytes);
|
|
|
|
|
2022-01-05 11:07:29 +01:00
|
|
|
return iohook_push_handler(touch_handle_irp);
|
2022-01-04 09:46:30 +01:00
|
|
|
}
|
|
|
|
|
2022-01-05 11:07:29 +01:00
|
|
|
static HRESULT touch_handle_irp(struct irp *irp)
|
2022-01-04 09:46:30 +01:00
|
|
|
{
|
|
|
|
HRESULT hr;
|
|
|
|
|
|
|
|
assert(irp != NULL);
|
|
|
|
|
2022-01-05 11:07:29 +01:00
|
|
|
if (uart_match_irp(&touch0_uart, irp)) {
|
|
|
|
EnterCriticalSection(&touch0_lock);
|
|
|
|
hr = touch0_handle_irp_locked(irp);
|
|
|
|
LeaveCriticalSection(&touch0_lock);
|
|
|
|
}
|
2022-01-08 03:16:33 +01:00
|
|
|
else if (uart_match_irp(&touch1_uart, irp)) {
|
2022-01-05 11:07:29 +01:00
|
|
|
EnterCriticalSection(&touch1_lock);
|
|
|
|
hr = touch1_handle_irp_locked(irp);
|
|
|
|
LeaveCriticalSection(&touch1_lock);
|
|
|
|
}
|
|
|
|
else {
|
2022-01-04 09:46:30 +01:00
|
|
|
return iohook_invoke_next(irp);
|
|
|
|
}
|
|
|
|
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT touch0_handle_irp_locked(struct irp *irp)
|
|
|
|
{
|
2022-01-05 08:03:55 +01:00
|
|
|
struct touch_req req;
|
2022-01-04 09:46:30 +01:00
|
|
|
HRESULT hr;
|
|
|
|
|
|
|
|
if (irp->op == IRP_OP_OPEN) {
|
2022-01-05 08:03:55 +01:00
|
|
|
dprintf("Wacca touch0: Starting backend\n");
|
2022-01-04 09:46:30 +01:00
|
|
|
hr = mercury_dll.touch_init();
|
|
|
|
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
dprintf("Wacca touch: Backend error: %x\n", (int) hr);
|
|
|
|
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
hr = uart_handle_irp(&touch0_uart, irp);
|
|
|
|
|
|
|
|
if (FAILED(hr) || irp->op != IRP_OP_WRITE) {
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (;;) {
|
2022-01-08 03:16:33 +01:00
|
|
|
#if 0
|
2022-01-05 08:03:55 +01:00
|
|
|
dprintf("TX0 Buffer:\n");
|
2022-01-04 09:46:30 +01:00
|
|
|
dump_iobuf(&touch0_uart.written);
|
|
|
|
#endif
|
2022-01-05 08:03:55 +01:00
|
|
|
hr = touch_frame_decode(&req, &touch0_uart.written, 0);
|
2022-01-04 09:46:30 +01:00
|
|
|
|
|
|
|
if (hr != S_OK) {
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
dprintf("Wacca touch: Deframe error: %x\n", (int) hr);
|
|
|
|
}
|
|
|
|
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
2022-01-05 08:03:55 +01:00
|
|
|
hr = touch_req_dispatch(&req);
|
|
|
|
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
dprintf("Wacca touch: Processing error: %x\n", (int) hr);
|
|
|
|
}
|
|
|
|
|
2022-01-05 11:07:29 +01:00
|
|
|
return hr;
|
2022-01-05 08:03:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT touch1_handle_irp_locked(struct irp *irp)
|
|
|
|
{
|
|
|
|
struct touch_req req;
|
|
|
|
HRESULT hr;
|
|
|
|
|
|
|
|
if (irp->op == IRP_OP_OPEN) {
|
|
|
|
dprintf("Wacca touch1: Starting backend\n");
|
|
|
|
hr = mercury_dll.touch_init();
|
|
|
|
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
dprintf("Wacca touch: Backend error: %x\n", (int) hr);
|
|
|
|
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
hr = uart_handle_irp(&touch1_uart, irp);
|
|
|
|
|
|
|
|
if (FAILED(hr) || irp->op != IRP_OP_WRITE) {
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (;;) {
|
2022-01-08 03:16:33 +01:00
|
|
|
#if 0
|
2022-01-05 08:03:55 +01:00
|
|
|
dprintf("TX1 Buffer:\n");
|
|
|
|
dump_iobuf(&touch1_uart.written);
|
2022-01-04 09:46:30 +01:00
|
|
|
#endif
|
|
|
|
|
2022-01-05 11:07:29 +01:00
|
|
|
hr = touch_frame_decode(&req, &touch1_uart.written, 1);
|
2022-01-05 08:03:55 +01:00
|
|
|
|
|
|
|
if (hr != S_OK) {
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
dprintf("Wacca touch: Deframe error: %x\n", (int) hr);
|
|
|
|
}
|
|
|
|
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
2022-01-04 09:46:30 +01:00
|
|
|
hr = touch_req_dispatch(&req);
|
|
|
|
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
dprintf("Wacca touch: Processing error: %x\n", (int) hr);
|
|
|
|
}
|
2022-01-05 11:07:29 +01:00
|
|
|
|
|
|
|
return hr;
|
2022-01-04 09:46:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-05 08:03:55 +01:00
|
|
|
static HRESULT touch_req_dispatch(const struct touch_req *req)
|
2022-01-04 09:46:30 +01:00
|
|
|
{
|
2022-01-05 08:03:55 +01:00
|
|
|
switch (req->cmd) {
|
2022-01-08 08:25:42 +01:00
|
|
|
case CMD_GET_SYNC_BOARD_VER:
|
|
|
|
return touch_handle_get_sync_board_ver(req);
|
2022-02-10 18:17:10 +01:00
|
|
|
case CMD_NEXT_READ:
|
|
|
|
return touch_handle_next_read(req);
|
2022-01-08 08:25:42 +01:00
|
|
|
case CMD_GET_UNIT_BOARD_VER:
|
|
|
|
return touch_handle_get_unit_board_ver(req);
|
2022-01-08 03:16:33 +01:00
|
|
|
case CMD_MYSTERY1:
|
|
|
|
return touch_handle_mystery1(req);
|
|
|
|
case CMD_MYSTERY2:
|
|
|
|
return touch_handle_mystery2(req);
|
|
|
|
case CMD_START_AUTO_SCAN:
|
|
|
|
return touch_handle_start_auto_scan(req);
|
|
|
|
case CMD_BEGIN_WRITE:
|
|
|
|
dprintf("Wacca touch: Begin write for side %d\n", req->side);
|
|
|
|
return S_OK;
|
|
|
|
case CMD_NEXT_WRITE:
|
|
|
|
dprintf("Wacca touch: continue write for side %d\n", req->side);
|
|
|
|
return S_OK;
|
2022-01-04 09:46:30 +01:00
|
|
|
default:
|
2022-01-05 08:03:55 +01:00
|
|
|
dprintf("Wacca touch: Unhandled command %02x\n", req->cmd);
|
2022-01-04 09:46:30 +01:00
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-08 08:25:42 +01:00
|
|
|
static HRESULT touch_handle_get_sync_board_ver(const struct touch_req *req)
|
2022-01-04 09:46:30 +01:00
|
|
|
{
|
2022-01-08 08:25:42 +01:00
|
|
|
struct touch_resp_get_sync_board_ver resp;
|
2022-01-08 03:16:33 +01:00
|
|
|
HRESULT hr;
|
|
|
|
|
2022-01-08 08:25:42 +01:00
|
|
|
dprintf("Wacca Touch%d: Get sync board version\n", req->side);
|
2022-01-08 03:16:33 +01:00
|
|
|
|
|
|
|
resp.cmd = 0xa0;
|
2022-05-18 07:33:09 +02:00
|
|
|
memcpy(resp.version, SYNC_BOARD_VER, sizeof(SYNC_BOARD_VER));
|
2022-01-08 23:19:10 +01:00
|
|
|
resp.checksum = 0;
|
|
|
|
resp.checksum = calc_checksum(&resp, sizeof(resp));
|
2022-01-08 08:25:42 +01:00
|
|
|
|
2022-01-05 11:07:29 +01:00
|
|
|
if (req->side == 0) {
|
2022-01-09 04:57:34 +01:00
|
|
|
hr = iobuf_write(&touch0_uart.readable, &resp, sizeof(resp));
|
2022-01-05 11:07:29 +01:00
|
|
|
}
|
|
|
|
else {
|
2022-01-09 04:57:34 +01:00
|
|
|
hr = iobuf_write(&touch1_uart.readable, &resp, sizeof(resp));
|
2022-01-05 11:07:29 +01:00
|
|
|
}
|
2022-05-18 07:33:09 +02:00
|
|
|
|
2022-01-08 03:16:33 +01:00
|
|
|
return hr;
|
2022-01-05 11:07:29 +01:00
|
|
|
}
|
|
|
|
|
2022-02-10 18:17:10 +01:00
|
|
|
static HRESULT touch_handle_next_read(const struct touch_req *req)
|
2022-01-05 11:07:29 +01:00
|
|
|
{
|
2022-01-08 03:16:33 +01:00
|
|
|
struct touch_resp_startup resp;
|
|
|
|
HRESULT hr;
|
2022-05-18 07:33:09 +02:00
|
|
|
char *rev;
|
2022-01-08 03:16:33 +01:00
|
|
|
|
2022-02-10 18:17:10 +01:00
|
|
|
dprintf("Wacca Touch%d: Read section %2hx\n", req->side, req->data[2]);
|
2022-01-08 08:25:42 +01:00
|
|
|
|
2022-01-08 03:16:33 +01:00
|
|
|
|
|
|
|
switch (req->data[2]) {
|
2022-05-18 07:33:09 +02:00
|
|
|
// These can be found in the config file
|
2022-01-08 03:16:33 +01:00
|
|
|
case 0x30:
|
2022-05-18 07:33:09 +02:00
|
|
|
rev = " 0 0 1 2 3 4 5 15 15 15 15 15 15 11 11 11";
|
2022-01-08 03:16:33 +01:00
|
|
|
break;
|
|
|
|
case 0x31:
|
2022-05-18 07:33:09 +02:00
|
|
|
rev = " 11 11 11 128 103 103 115 138 127 103 105 111 126 113 95 100";
|
2022-01-08 03:16:33 +01:00
|
|
|
break;
|
|
|
|
case 0x33:
|
2022-05-18 07:33:09 +02:00
|
|
|
rev = " 101 115 98 86 76 67 68 48 117 0 82 154 0 6 35 4";
|
2022-01-08 03:16:33 +01:00
|
|
|
break;
|
|
|
|
default:
|
2022-02-10 18:17:10 +01:00
|
|
|
dprintf("Wacca touch: BAD READ REQUEST %2hx\n", req->data[2]);
|
2022-01-08 03:16:33 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-05-18 07:33:09 +02:00
|
|
|
memcpy(resp.data, rev, 80 * sizeof(char));
|
2022-01-08 23:19:10 +01:00
|
|
|
resp.checksum = 0;
|
|
|
|
resp.checksum = calc_checksum(&resp, sizeof(resp));
|
2022-01-08 03:16:33 +01:00
|
|
|
|
|
|
|
if (req->side == 0) {
|
2022-01-09 04:57:34 +01:00
|
|
|
hr = iobuf_write(&touch0_uart.readable, &resp, sizeof(resp));
|
2022-01-05 08:03:55 +01:00
|
|
|
}
|
|
|
|
else {
|
2022-01-09 04:57:34 +01:00
|
|
|
hr = iobuf_write(&touch1_uart.readable, &resp, sizeof(resp));
|
2022-01-05 08:03:55 +01:00
|
|
|
}
|
2022-01-08 03:16:33 +01:00
|
|
|
return hr;
|
2022-01-05 08:03:55 +01:00
|
|
|
}
|
2022-01-04 09:46:30 +01:00
|
|
|
|
2022-01-08 08:25:42 +01:00
|
|
|
static HRESULT touch_handle_get_unit_board_ver(const struct touch_req *req)
|
2022-01-08 03:16:33 +01:00
|
|
|
{
|
2022-01-08 08:25:42 +01:00
|
|
|
struct touch_resp_get_unit_board_ver resp;
|
2022-01-08 03:16:33 +01:00
|
|
|
HRESULT hr;
|
|
|
|
|
2022-01-08 08:25:42 +01:00
|
|
|
dprintf("Wacca Touch%d: get unit board version\n", req->side);
|
2022-01-08 03:16:33 +01:00
|
|
|
|
2022-05-18 07:33:09 +02:00
|
|
|
memset(resp.version, 0, sizeof(resp.version));
|
|
|
|
memcpy(resp.version, SYNC_BOARD_VER, sizeof(SYNC_BOARD_VER));
|
|
|
|
|
|
|
|
for (int i = 0; i < 6; i++ )
|
|
|
|
memcpy(&resp.version[7 + (6 * i)], UNIT_BOARD_VER, sizeof(UNIT_BOARD_VER));
|
|
|
|
|
2022-01-08 03:16:33 +01:00
|
|
|
resp.cmd = 0xa8;
|
2022-05-18 07:33:09 +02:00
|
|
|
resp.checksum = 0;
|
2022-01-08 03:16:33 +01:00
|
|
|
|
2022-01-10 07:07:01 +01:00
|
|
|
if (req->side == 0) {
|
2022-05-18 07:33:09 +02:00
|
|
|
resp.version[6] = 'R';
|
2022-01-10 07:07:01 +01:00
|
|
|
resp.checksum = calc_checksum(&resp, sizeof(resp));
|
2022-05-18 07:33:09 +02:00
|
|
|
|
|
|
|
#if 0
|
|
|
|
for (int i = 0; i < sizeof(resp.version); i++) {
|
|
|
|
dprintf("0x%02x ", resp.version[i]);
|
|
|
|
}
|
|
|
|
dprintf("\n");
|
|
|
|
#endif
|
|
|
|
|
2022-01-09 04:57:34 +01:00
|
|
|
hr = iobuf_write(&touch0_uart.readable, &resp, sizeof(resp));
|
2022-01-08 03:16:33 +01:00
|
|
|
}
|
|
|
|
else {
|
2022-05-18 07:33:09 +02:00
|
|
|
resp.version[6] = 'L';
|
2022-01-10 07:07:01 +01:00
|
|
|
resp.checksum = calc_checksum(&resp, sizeof(resp));
|
2022-05-18 07:33:09 +02:00
|
|
|
|
|
|
|
#if 0
|
|
|
|
for (int i = 0; i < sizeof(resp.version); i++) {
|
|
|
|
dprintf("0x%02x ", resp.version[i]);
|
|
|
|
}
|
|
|
|
dprintf("\n");
|
|
|
|
#endif
|
|
|
|
|
2022-01-09 04:57:34 +01:00
|
|
|
hr = iobuf_write(&touch1_uart.readable, &resp, sizeof(resp));
|
2022-01-08 03:16:33 +01:00
|
|
|
}
|
|
|
|
|
2022-05-18 07:33:09 +02:00
|
|
|
return hr;
|
2022-01-08 03:16:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT touch_handle_mystery1(const struct touch_req *req)
|
|
|
|
{
|
|
|
|
struct touch_resp_mystery1 resp;
|
|
|
|
HRESULT hr;
|
|
|
|
|
2022-02-10 18:17:10 +01:00
|
|
|
dprintf("Wacca Touch%d: Command A2\n", req->side);
|
2022-01-08 03:16:33 +01:00
|
|
|
|
|
|
|
resp.cmd = 0xa2;
|
|
|
|
resp.data = 0x3f;
|
2022-01-08 23:19:10 +01:00
|
|
|
resp.checksum = 0;
|
|
|
|
resp.checksum = calc_checksum(&resp, sizeof(resp));
|
2022-01-08 03:16:33 +01:00
|
|
|
|
|
|
|
if (req->side == 0) {
|
2022-01-09 04:57:34 +01:00
|
|
|
hr = iobuf_write(&touch0_uart.readable, &resp, sizeof(resp));
|
2022-01-08 03:16:33 +01:00
|
|
|
}
|
|
|
|
else {
|
2022-01-09 04:57:34 +01:00
|
|
|
hr = iobuf_write(&touch1_uart.readable, &resp, sizeof(resp));
|
2022-01-08 03:16:33 +01:00
|
|
|
}
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT touch_handle_mystery2(const struct touch_req *req)
|
|
|
|
{
|
|
|
|
struct touch_resp_mystery2 resp;
|
|
|
|
HRESULT hr;
|
|
|
|
|
2022-02-10 18:17:10 +01:00
|
|
|
dprintf("Wacca Touch%d: Command 94\n", req->side);
|
2022-01-08 03:16:33 +01:00
|
|
|
|
|
|
|
resp.cmd = 0x94;
|
|
|
|
resp.data = 0;
|
2022-01-08 23:19:10 +01:00
|
|
|
resp.checksum = 0;
|
|
|
|
resp.checksum = calc_checksum(&resp, sizeof(resp));
|
2022-01-08 03:16:33 +01:00
|
|
|
|
|
|
|
if (req->side == 0) {
|
2022-01-09 04:57:34 +01:00
|
|
|
hr = iobuf_write(&touch0_uart.readable, &resp, sizeof(resp));
|
2022-01-10 07:07:01 +01:00
|
|
|
}
|
2022-01-08 03:16:33 +01:00
|
|
|
else {
|
2022-01-09 04:57:34 +01:00
|
|
|
hr = iobuf_write(&touch1_uart.readable, &resp, sizeof(resp));
|
2022-01-08 03:16:33 +01:00
|
|
|
}
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT touch_handle_start_auto_scan(const struct touch_req *req)
|
|
|
|
{
|
|
|
|
struct touch_resp_start_auto resp;
|
|
|
|
HRESULT hr;
|
|
|
|
uint8_t data1[24] = { 0 };
|
2022-05-18 07:33:09 +02:00
|
|
|
// Unsure what this does. It seems to change every request on a real board,
|
|
|
|
// but the game doesn't seem to mind that it's the same
|
2022-01-08 03:16:33 +01:00
|
|
|
uint8_t data2[9] = { 0x0d, 0x03, 0x02, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00 };
|
|
|
|
|
2022-05-18 07:33:09 +02:00
|
|
|
dprintf("Wacca Touch%d: Start Auto", req->side);
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
for (int i = 0; i < req->data_length; i++)
|
|
|
|
dprintf("0x%02x ", req->data[i]);
|
|
|
|
#endif
|
|
|
|
dprintf("\n");
|
2022-01-08 03:16:33 +01:00
|
|
|
|
|
|
|
resp.cmd = 0x9c;
|
|
|
|
resp.data = 0;
|
|
|
|
resp.checksum = 0x49;
|
|
|
|
|
|
|
|
resp.frame.cmd= 0x81;
|
|
|
|
memcpy(resp.frame.data1, data1, sizeof(data1));
|
|
|
|
memcpy(resp.frame.data2, data2, sizeof(data2));
|
2022-01-08 23:19:10 +01:00
|
|
|
resp.frame.checksum = 0;
|
2022-01-08 03:16:33 +01:00
|
|
|
resp.frame.checksum = calc_checksum(&resp.frame, sizeof(resp.frame));
|
|
|
|
|
|
|
|
if (req->side == 0) {
|
2022-01-08 23:19:10 +01:00
|
|
|
resp.frame.count = input_frame_count_0++;
|
2022-01-09 04:57:34 +01:00
|
|
|
hr = iobuf_write(&touch0_uart.readable, &resp, sizeof(resp));
|
2022-01-08 23:19:10 +01:00
|
|
|
touch0_auto = true;
|
2022-01-08 03:16:33 +01:00
|
|
|
}
|
|
|
|
else {
|
2022-01-08 23:19:10 +01:00
|
|
|
resp.frame.count = input_frame_count_1++;
|
2022-01-09 04:57:34 +01:00
|
|
|
hr = iobuf_write(&touch1_uart.readable, &resp, sizeof(resp));
|
2022-01-08 23:19:10 +01:00
|
|
|
touch1_auto = true;
|
2022-01-08 03:16:33 +01:00
|
|
|
}
|
2022-01-08 23:19:10 +01:00
|
|
|
|
2022-01-08 23:42:09 +01:00
|
|
|
mercury_dll.touch_start(touch_res_auto_scan);
|
2022-01-08 03:16:33 +01:00
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
2022-01-09 18:30:31 +01:00
|
|
|
static void touch_res_auto_scan(const bool *state)
|
2022-01-08 23:19:10 +01:00
|
|
|
{
|
|
|
|
struct touch_input_frame frame0;
|
2022-01-11 01:36:11 +01:00
|
|
|
struct touch_input_frame frame1;
|
|
|
|
uint8_t dataR[24] = { 0 };
|
|
|
|
uint8_t dataL[24] = { 0 };
|
|
|
|
// this changes every input on a real board but
|
|
|
|
// the game doesn't seem to care about it...
|
2022-01-08 23:19:10 +01:00
|
|
|
uint8_t data2[9] = { 0x0d, 0x03, 0x02, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00 };
|
2022-01-11 01:36:11 +01:00
|
|
|
uint8_t counter = 0;
|
2022-01-08 23:19:10 +01:00
|
|
|
|
|
|
|
frame0.cmd = 0x81;
|
2022-01-09 04:57:34 +01:00
|
|
|
frame0.count = input_frame_count_0++;
|
|
|
|
input_frame_count_0 %= 0x7f;
|
2022-01-11 01:36:11 +01:00
|
|
|
|
|
|
|
frame1.cmd = 0x81;
|
|
|
|
frame1.count = input_frame_count_1++;
|
|
|
|
input_frame_count_1 %= 0x7f;
|
|
|
|
|
|
|
|
for (int i = 0; i < 24; i++) {
|
|
|
|
for (int j = 0; j < 5; j++) {
|
|
|
|
if (state[counter]) {
|
|
|
|
dataR[i] |= (1 << j);
|
|
|
|
}
|
|
|
|
if (state[counter+120]) {
|
|
|
|
dataL[i] |= (1 << j);
|
|
|
|
}
|
|
|
|
counter++;
|
|
|
|
}
|
|
|
|
}
|
2022-01-10 07:07:01 +01:00
|
|
|
|
2022-01-11 01:36:11 +01:00
|
|
|
memcpy(frame0.data1, dataR, sizeof(dataR));
|
2022-01-08 23:19:10 +01:00
|
|
|
memcpy(frame0.data2, data2, sizeof(data2));
|
2022-01-11 01:36:11 +01:00
|
|
|
|
|
|
|
memcpy(frame1.data1, dataL, sizeof(dataL));
|
|
|
|
memcpy(frame1.data2, data2, sizeof(data2));
|
|
|
|
|
2022-01-08 23:19:10 +01:00
|
|
|
frame0.checksum = 0;
|
|
|
|
frame0.checksum = calc_checksum(&frame0, sizeof(frame0));
|
|
|
|
|
2022-01-11 01:36:11 +01:00
|
|
|
frame1.checksum = 0;
|
|
|
|
frame1.checksum = calc_checksum(&frame1, sizeof(frame1));
|
|
|
|
|
2022-01-08 23:19:10 +01:00
|
|
|
if (touch0_auto) {
|
|
|
|
//dprintf("Wacca touch: Touch0 auto frame #%2hx sent\n", frame0.count);
|
|
|
|
EnterCriticalSection(&touch0_lock);
|
2022-01-09 04:57:34 +01:00
|
|
|
iobuf_write(&touch0_uart.readable, &frame0, sizeof(frame0));
|
2022-01-08 23:19:10 +01:00
|
|
|
LeaveCriticalSection(&touch0_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (touch1_auto) {
|
|
|
|
//dprintf("Wacca touch: Touch1 auto frame #%2hx sent\n", frame0.count);
|
|
|
|
EnterCriticalSection(&touch1_lock);
|
2022-01-11 01:36:11 +01:00
|
|
|
iobuf_write(&touch1_uart.readable, &frame1, sizeof(frame1));
|
2022-01-08 23:19:10 +01:00
|
|
|
LeaveCriticalSection(&touch1_lock);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-08 03:16:33 +01:00
|
|
|
/* Decodes the response into a struct that's easier to work with. */
|
2022-01-05 11:07:29 +01:00
|
|
|
static HRESULT touch_frame_decode(struct touch_req *dest, struct iobuf *iobuf, int side)
|
2022-01-05 08:03:55 +01:00
|
|
|
{
|
|
|
|
dest->side = side;
|
|
|
|
dest->cmd = iobuf->bytes[0];
|
2022-01-05 11:07:29 +01:00
|
|
|
iobuf->pos--;
|
|
|
|
dest->data_length = iobuf->pos;
|
2022-01-04 09:46:30 +01:00
|
|
|
|
2022-01-05 08:03:55 +01:00
|
|
|
if (dest->data_length > 0) {
|
|
|
|
for (int i = 1; i < dest->data_length; i++) {
|
|
|
|
dest->data[i-1] = iobuf->bytes[i];
|
|
|
|
}
|
|
|
|
}
|
2022-01-05 11:07:29 +01:00
|
|
|
iobuf->pos -= dest->data_length;
|
2022-01-05 08:03:55 +01:00
|
|
|
|
|
|
|
return S_OK;
|
2022-01-04 09:46:30 +01:00
|
|
|
}
|
2022-01-08 03:16:33 +01:00
|
|
|
|
2022-01-08 08:25:42 +01:00
|
|
|
/* The last byte of every response is a checksum.
|
|
|
|
* This checksum is calculated by bitwise XORing
|
|
|
|
* every byte in the response, then dropping the MSB.
|
|
|
|
* Thanks the CrazyRedMachine for figuring that out!!
|
|
|
|
*/
|
2022-01-08 03:16:33 +01:00
|
|
|
static uint8_t calc_checksum(const void *ptr, size_t nbytes)
|
|
|
|
{
|
|
|
|
const uint8_t *src;
|
|
|
|
uint8_t checksum = 0;
|
|
|
|
|
|
|
|
src = ptr;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < nbytes; i++) {
|
2022-01-08 23:19:10 +01:00
|
|
|
//dprintf("Wacca touch: Calculating %2hx\n", src[i]);
|
2022-01-08 03:16:33 +01:00
|
|
|
checksum = checksum^(src[i]);
|
|
|
|
}
|
2022-01-08 23:19:10 +01:00
|
|
|
//dprintf("Wacca touch: Checksum is %2hx\n", checksum&0x7f);
|
2022-01-08 03:16:33 +01:00
|
|
|
return checksum&0x7f;
|
|
|
|
}
|