2022-05-16 23:38:40 +02:00
|
|
|
using System;
|
2022-05-22 06:32:20 +02:00
|
|
|
using System.Collections;
|
2022-05-16 23:38:40 +02:00
|
|
|
using System.Collections.Generic;
|
2022-05-26 01:10:57 +02:00
|
|
|
using System.Threading;
|
2022-05-25 01:29:59 +02:00
|
|
|
using System.IO.Ports;
|
2022-05-16 23:38:40 +02:00
|
|
|
using System.Linq;
|
2022-05-25 01:29:59 +02:00
|
|
|
using UnityEngine;
|
2022-11-05 01:32:17 +01:00
|
|
|
using UnityEngine.UI;
|
2022-10-03 22:48:09 +02:00
|
|
|
public class TouchManager : MonoBehaviour
|
2022-05-16 23:38:40 +02:00
|
|
|
{
|
2022-05-22 06:32:20 +02:00
|
|
|
const byte CMD_GET_SYNC_BOARD_VER = 0xa0;
|
|
|
|
const byte CMD_NEXT_READ = 0x72;
|
|
|
|
const byte CMD_GET_UNIT_BOARD_VER = 0xa8;
|
|
|
|
const byte CMD_MYSTERY1 = 0xa2;
|
|
|
|
const byte CMD_MYSTERY2 = 0x94;
|
|
|
|
const byte CMD_START_AUTO_SCAN = 0xc9;
|
|
|
|
const byte CMD_BEGIN_WRITE = 0x77;
|
|
|
|
const byte CMD_NEXT_WRITE = 0x20;
|
|
|
|
|
2022-05-26 01:10:57 +02:00
|
|
|
private Thread _touchThread;
|
|
|
|
private Queue _touchQueue;
|
2022-07-29 18:49:56 +02:00
|
|
|
|
2022-05-16 23:38:40 +02:00
|
|
|
static SerialPort ComL = new SerialPort ("COM5", 115200);
|
|
|
|
static SerialPort ComR = new SerialPort ("COM6", 115200);
|
2022-05-22 14:58:17 +02:00
|
|
|
|
2022-05-16 23:38:40 +02:00
|
|
|
byte inByte;
|
2022-05-22 06:32:20 +02:00
|
|
|
string SYNC_BOARD_VER = "190523";
|
|
|
|
string UNIT_BOARD_VER = "190514";
|
|
|
|
string read1 = " 0 0 1 2 3 4 5 15 15 15 15 15 15 11 11 11";
|
|
|
|
string read2 = " 11 11 11 128 103 103 115 138 127 103 105 111 126 113 95 100";
|
|
|
|
string read3 = " 101 115 98 86 76 67 68 48 117 0 82 154 0 6 35 4";
|
|
|
|
|
2022-07-26 15:13:45 +02:00
|
|
|
byte[] SettingData_160 = new byte[8] {160, 49, 57, 48, 53, 50, 51, 44};
|
|
|
|
byte[] SettingData_162 = new byte[3] {162, 63, 29};
|
|
|
|
byte[] SettingData_148 = new byte[3] {148, 0, 20};
|
|
|
|
byte[] SettingData_201 = new byte[3] {201, 0, 73};
|
2022-05-16 23:38:40 +02:00
|
|
|
static byte[] TouchPackL = new byte[36];
|
|
|
|
static byte[] TouchPackR = new byte[36];
|
2022-10-05 22:47:49 +02:00
|
|
|
static bool[] TouchPackAll = new bool[240];
|
2022-05-16 23:38:40 +02:00
|
|
|
bool StartUp = false;
|
2022-11-05 01:32:17 +01:00
|
|
|
static bool useIPCTouch = true;
|
2022-05-16 23:38:40 +02:00
|
|
|
void Start()
|
|
|
|
{
|
2022-11-05 01:32:17 +01:00
|
|
|
var widget = ConfigManager.GetConfigPanelWidget("UseIPCTouch");
|
|
|
|
var toggle = widget.GetComponent<Toggle>();
|
|
|
|
toggle.onValueChanged.AddListener((value) => {
|
|
|
|
useIPCTouch = value;
|
|
|
|
});
|
|
|
|
toggle.onValueChanged.Invoke(useIPCTouch);
|
|
|
|
|
2022-05-25 02:50:44 +02:00
|
|
|
try
|
|
|
|
{
|
|
|
|
ComL.Open();
|
|
|
|
ComR.Open();
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
Console.WriteLine($"Failed to Open Serial Ports: {ex}");
|
|
|
|
}
|
2022-10-05 22:47:49 +02:00
|
|
|
Debug.Log("Touch Serial Initializing..");
|
2022-05-25 01:29:59 +02:00
|
|
|
//Send touch update periodically to keep "read" alive
|
2022-05-26 01:10:57 +02:00
|
|
|
_touchQueue = Queue.Synchronized(new Queue());
|
|
|
|
_touchThread = new Thread(TouchThreadLoop);
|
|
|
|
InvokeRepeating("PingTouchThread", 0, 1);
|
2022-05-25 01:29:59 +02:00
|
|
|
//Send touch updates whenever actual state changes to achieve desired update frequency without overloading
|
2022-10-03 22:48:09 +02:00
|
|
|
ColliderToTouch.touchDidChange += PingTouchThread;
|
2022-05-16 23:38:40 +02:00
|
|
|
}
|
|
|
|
|
2022-05-26 01:10:57 +02:00
|
|
|
private void PingTouchThread()
|
|
|
|
{
|
|
|
|
_touchQueue.Enqueue(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void TouchThreadLoop()
|
|
|
|
{
|
|
|
|
while(true)
|
|
|
|
{
|
|
|
|
if(_touchQueue.Count > 0)
|
|
|
|
{
|
|
|
|
_touchQueue.Dequeue();
|
|
|
|
SendTouchState();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-05-25 02:48:15 +02:00
|
|
|
private void OnDestroy()
|
|
|
|
{
|
|
|
|
ComL.Close();
|
|
|
|
ComR.Close();
|
|
|
|
}
|
2022-05-16 23:38:40 +02:00
|
|
|
void Update()
|
|
|
|
{
|
2022-05-22 15:34:23 +02:00
|
|
|
if(ComL.IsOpen)
|
|
|
|
ReadHead(ComL, 0);
|
|
|
|
if (ComR.IsOpen)
|
|
|
|
ReadHead(ComR, 1);
|
2022-10-06 22:38:29 +02:00
|
|
|
//following are touch test code
|
|
|
|
//if (Input.GetKeyDown(KeyCode.M))
|
2022-10-05 22:47:49 +02:00
|
|
|
//StartCoroutine(TouchTest(true));
|
2022-10-06 22:38:29 +02:00
|
|
|
//if (Input.GetKeyDown(KeyCode.M) && StartUp)
|
|
|
|
//SendTouchState();
|
2022-05-16 23:38:40 +02:00
|
|
|
}
|
|
|
|
|
2022-05-25 01:29:59 +02:00
|
|
|
private void SendTouchState()
|
|
|
|
{
|
2022-05-25 02:48:15 +02:00
|
|
|
if(StartUp)
|
|
|
|
{
|
2022-05-28 02:47:03 +02:00
|
|
|
//Debug.Log("Sending Touch State");
|
2022-05-25 02:48:15 +02:00
|
|
|
// Debug.Log("Sending Left");
|
|
|
|
SendTouch(ComL, TouchPackL);
|
|
|
|
// Debug.Log("Sending Right");
|
|
|
|
SendTouch(ComR, TouchPackR);
|
|
|
|
}
|
2022-05-16 23:38:40 +02:00
|
|
|
}
|
|
|
|
|
2022-05-23 23:31:33 +02:00
|
|
|
IEnumerator TouchTest(bool State) //this is a touch test code
|
2022-05-16 23:38:40 +02:00
|
|
|
{
|
|
|
|
for (int i = 0; i < 240; i++)
|
|
|
|
{
|
|
|
|
SetTouch(i, true);
|
|
|
|
Debug.Log(i);
|
|
|
|
yield return new WaitForSeconds(0.05f);
|
|
|
|
SetTouch(i, false);
|
|
|
|
yield return new WaitForSeconds(0.05f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-23 23:31:33 +02:00
|
|
|
void ReadHead(SerialPort Serial, int side) //Read head byte
|
2022-05-16 23:38:40 +02:00
|
|
|
{
|
2022-05-22 06:32:20 +02:00
|
|
|
if(Serial.BytesToRead > 0)
|
2022-05-16 23:38:40 +02:00
|
|
|
{
|
|
|
|
inByte = Convert.ToByte(Serial.ReadByte());
|
2022-05-22 06:32:20 +02:00
|
|
|
var data = Serial.ReadExisting();
|
|
|
|
SendResp(Serial, side, data);
|
2022-05-16 23:38:40 +02:00
|
|
|
}
|
|
|
|
}
|
2022-05-22 06:32:20 +02:00
|
|
|
void SendResp(SerialPort Serial, int side, string data)
|
2022-05-16 23:38:40 +02:00
|
|
|
{
|
|
|
|
switch(inByte)
|
|
|
|
{
|
2022-05-22 06:32:20 +02:00
|
|
|
case CMD_GET_SYNC_BOARD_VER:
|
2022-05-22 14:58:17 +02:00
|
|
|
//Response: cmd byte + sync board ver + checksum
|
2022-05-16 23:38:40 +02:00
|
|
|
StartUp = false;
|
2022-05-22 06:32:20 +02:00
|
|
|
List<byte> syncbytes = new List<byte>();
|
|
|
|
syncbytes.Add(inByte);
|
2022-05-22 14:58:17 +02:00
|
|
|
syncbytes.AddRange(ByteHelper.ConvertStringToByteArray(SYNC_BOARD_VER));
|
|
|
|
byte syncCheckSum = (byte)44;
|
|
|
|
syncbytes.Add(syncCheckSum);
|
2022-05-22 06:32:20 +02:00
|
|
|
Serial.Write(syncbytes.ToArray(), 0, syncbytes.Count);
|
2022-05-28 02:47:03 +02:00
|
|
|
//Debug.Log($"GET SYNC BOARD VER {side}");
|
2022-05-16 23:38:40 +02:00
|
|
|
break;
|
2022-05-22 06:32:20 +02:00
|
|
|
case CMD_NEXT_READ:
|
2022-05-22 14:58:17 +02:00
|
|
|
//Response: corresponding read bytes + checksum
|
2022-05-16 23:38:40 +02:00
|
|
|
StartUp = false;
|
2022-05-28 02:47:03 +02:00
|
|
|
//Debug.Log($"Side {side} NEXT READ {Convert.ToByte(data[2])}");
|
2022-05-22 06:32:20 +02:00
|
|
|
switch (Convert.ToByte(data[2]))
|
|
|
|
{
|
|
|
|
case 0x30:
|
|
|
|
var bytes = ByteHelper.ConvertStringToByteArray(read1);
|
|
|
|
bytes.Add(ByteHelper.CalCheckSum(bytes.ToArray(), bytes.Count));
|
2022-05-28 02:47:03 +02:00
|
|
|
//Debug.Log("Read 1");
|
2022-05-22 06:32:20 +02:00
|
|
|
Serial.Write(bytes.ToArray(), 0, bytes.Count);
|
|
|
|
break;
|
|
|
|
case 0x31:
|
|
|
|
var bytes2 = ByteHelper.ConvertStringToByteArray(read2);
|
|
|
|
bytes2.Add(ByteHelper.CalCheckSum(bytes2.ToArray(), bytes2.Count));
|
2022-05-28 02:47:03 +02:00
|
|
|
//Debug.Log("Read 2");
|
2022-05-22 06:32:20 +02:00
|
|
|
Serial.Write(bytes2.ToArray(), 0, bytes2.Count);
|
|
|
|
break;
|
|
|
|
case 0x33:
|
|
|
|
var bytes3 = ByteHelper.ConvertStringToByteArray(read3);
|
|
|
|
bytes3.Add(ByteHelper.CalCheckSum(bytes3.ToArray(), bytes3.Count));
|
2022-05-28 02:47:03 +02:00
|
|
|
//Debug.Log("Read 3");
|
2022-05-22 06:32:20 +02:00
|
|
|
Serial.Write(bytes3.ToArray(), 0, bytes3.Count);
|
|
|
|
break;
|
|
|
|
default:
|
2022-05-28 02:47:03 +02:00
|
|
|
//Debug.Log("Extra Read");
|
2022-05-22 06:32:20 +02:00
|
|
|
break;
|
|
|
|
}
|
2022-05-16 23:38:40 +02:00
|
|
|
break;
|
2022-05-22 06:32:20 +02:00
|
|
|
case CMD_GET_UNIT_BOARD_VER:
|
2022-05-22 14:58:17 +02:00
|
|
|
//Response: cmd byte + sync board ver bytes + 'L'/'R' based on side + unit board ver bytes x6 + checksum
|
2022-05-16 23:38:40 +02:00
|
|
|
StartUp = false;
|
2022-05-22 06:32:20 +02:00
|
|
|
List<byte> unitBytes = new List<byte>();
|
2022-05-22 14:58:17 +02:00
|
|
|
byte sideByte = (side == 0 ? Convert.ToByte('R') : Convert.ToByte('L'));
|
|
|
|
byte unitCheckSum = (side == 0 ? (byte)118 : (byte)104);
|
|
|
|
unitBytes.Add(inByte);
|
|
|
|
unitBytes.AddRange(ByteHelper.ConvertStringToByteArray(SYNC_BOARD_VER));
|
|
|
|
unitBytes.Add(sideByte);
|
|
|
|
for (int i = 0; i < 6; i++)
|
|
|
|
unitBytes.AddRange(ByteHelper.ConvertStringToByteArray(UNIT_BOARD_VER));
|
|
|
|
unitBytes.Add(unitCheckSum);
|
2022-05-22 06:32:20 +02:00
|
|
|
Serial.Write(unitBytes.ToArray(), 0, unitBytes.Count);
|
2022-05-28 02:47:03 +02:00
|
|
|
//Debug.Log($"GET UNIT BOARD VER {side}");
|
2022-05-16 23:38:40 +02:00
|
|
|
break;
|
2022-05-22 06:32:20 +02:00
|
|
|
case CMD_MYSTERY1:
|
2022-05-16 23:38:40 +02:00
|
|
|
StartUp = false;
|
|
|
|
Serial.Write(SettingData_162, 0, 3);
|
2022-05-28 02:47:03 +02:00
|
|
|
//Debug.Log($"MYSTERY 1 SIDE {side}");
|
2022-05-16 23:38:40 +02:00
|
|
|
break;
|
2022-05-22 06:32:20 +02:00
|
|
|
case CMD_MYSTERY2:
|
2022-05-16 23:38:40 +02:00
|
|
|
StartUp = false;
|
|
|
|
Serial.Write(SettingData_148, 0, 3);
|
2022-05-28 02:47:03 +02:00
|
|
|
//Debug.Log($"MYSTERY 2 SIDE {side}");
|
2022-05-16 23:38:40 +02:00
|
|
|
break;
|
2022-05-22 06:32:20 +02:00
|
|
|
case CMD_START_AUTO_SCAN:
|
2022-05-16 23:38:40 +02:00
|
|
|
Serial.Write(SettingData_201.ToArray(), 0, 3);
|
2022-05-28 02:47:03 +02:00
|
|
|
//Debug.Log($"START AUTO SCAN SIDE {side}");
|
2022-05-16 23:38:40 +02:00
|
|
|
StartUp = true;
|
2022-05-26 01:10:57 +02:00
|
|
|
if (!_touchThread.IsAlive)
|
|
|
|
_touchThread.Start();
|
2022-05-16 23:38:40 +02:00
|
|
|
break;
|
2022-05-22 06:32:20 +02:00
|
|
|
case CMD_BEGIN_WRITE:
|
2022-05-22 14:58:17 +02:00
|
|
|
// Debug.Log($"Begin Write For Side {side}");
|
2022-05-22 06:32:20 +02:00
|
|
|
break;
|
|
|
|
case CMD_NEXT_WRITE:
|
2022-05-22 14:58:17 +02:00
|
|
|
// Debug.Log($"Continue Write For Side {side}");
|
2022-05-22 06:32:20 +02:00
|
|
|
break;
|
2022-05-16 23:38:40 +02:00
|
|
|
case 154:
|
|
|
|
StartUp = false;
|
2022-05-28 02:47:03 +02:00
|
|
|
//Debug.Log("BAD");
|
2022-05-16 23:38:40 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-23 23:31:33 +02:00
|
|
|
byte[] GetTouchPack(byte[] Pack) //convert touch to right format for game
|
2022-05-16 23:38:40 +02:00
|
|
|
{
|
|
|
|
Pack[0] = 129;
|
|
|
|
Pack[34] = Pack[34]++;
|
|
|
|
Pack[35] = 128;
|
|
|
|
Pack[35] = ByteHelper.CalCheckSum(Pack, 36);
|
|
|
|
if (Pack[34] > 127)
|
|
|
|
Pack[34] = 0;
|
|
|
|
return Pack;
|
|
|
|
}
|
2022-10-05 22:47:49 +02:00
|
|
|
void SendTouch(SerialPort Serial, byte[] Pack) //Send touch data to serial
|
2022-05-16 23:38:40 +02:00
|
|
|
{
|
|
|
|
if (StartUp)
|
2022-07-26 15:13:45 +02:00
|
|
|
Serial.Write(GetTouchPack(Pack), 0, 36);
|
2022-05-16 23:38:40 +02:00
|
|
|
}
|
2022-10-05 22:47:49 +02:00
|
|
|
public static void SetTouch(int Area, bool State) //set touch data 1-240
|
2022-05-16 23:38:40 +02:00
|
|
|
{
|
2022-10-05 22:47:49 +02:00
|
|
|
Area -= 1; //0-239
|
|
|
|
if (Area < 120) //right side
|
2022-05-16 23:38:40 +02:00
|
|
|
{
|
2022-10-05 22:47:49 +02:00
|
|
|
TouchPackAll[Area + 120] = State; //save R touch for IPC
|
2022-10-03 22:48:09 +02:00
|
|
|
|
|
|
|
Area += Area / 5 * 3 + 7;
|
2022-05-16 23:38:40 +02:00
|
|
|
ByteHelper.SetBit(TouchPackR, Area, State);
|
|
|
|
}
|
2022-10-05 22:47:49 +02:00
|
|
|
else if (Area >= 120) //left side
|
2022-05-16 23:38:40 +02:00
|
|
|
{
|
2022-10-05 22:47:49 +02:00
|
|
|
TouchPackAll[Area - 120] = State; //save L touch for IPC
|
2022-10-03 22:48:09 +02:00
|
|
|
|
2022-05-16 23:38:40 +02:00
|
|
|
Area -= 120;
|
2022-10-03 22:48:09 +02:00
|
|
|
Area += Area / 5 * 3 + 7;
|
2022-05-16 23:38:40 +02:00
|
|
|
ByteHelper.SetBit(TouchPackL, Area, State);
|
|
|
|
}
|
2022-11-05 01:32:17 +01:00
|
|
|
if (useIPCTouch)
|
2022-10-07 17:12:42 +02:00
|
|
|
IPCManager.SetTouchData(TouchPackAll); //send touch data to IPC
|
2022-05-16 23:38:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static class ByteHelper
|
|
|
|
{
|
|
|
|
public static byte[] SetBit(this byte[] self, int index, bool value)
|
|
|
|
{
|
|
|
|
var bitArray = new BitArray(self);
|
|
|
|
bitArray.Set(index, value);
|
|
|
|
bitArray.CopyTo(self, 0);
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
public static byte CalCheckSum(byte[] _PacketData,int PacketLength)
|
|
|
|
{
|
|
|
|
Byte _CheckSumByte = 0x00;
|
|
|
|
for (int i = 0; i < PacketLength; i++)
|
|
|
|
_CheckSumByte ^= _PacketData[i];
|
|
|
|
return _CheckSumByte;
|
|
|
|
}
|
2022-05-22 06:32:20 +02:00
|
|
|
public static List<byte> ConvertStringToByteArray(string data)
|
|
|
|
{
|
|
|
|
List<byte> tempList = new List<byte>(100);
|
|
|
|
for(int i = 0; i < data.Length; i++)
|
|
|
|
tempList.Add(Convert.ToByte(data[i]));
|
|
|
|
return tempList;
|
|
|
|
}
|
2022-05-16 23:38:40 +02:00
|
|
|
}
|