1
0
mirror of synced 2024-11-14 18:17:40 +01:00

Merge pull request #21 from Kylemc1413/main

Improve Touch Input Delay
This commit is contained in:
Kyle1413 2022-05-24 21:08:45 -04:00 committed by GitHub
commit cc66b70b58
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 164 additions and 3158 deletions

File diff suppressed because it is too large Load Diff

View File

@ -9,6 +9,7 @@ public class ColliderToSerial : MonoBehaviour
{
private Renderer cr;
private int _insideColliderCount = 0;
public static event Action touchDidChange;
private void Start()
{
@ -19,6 +20,7 @@ public class ColliderToSerial : MonoBehaviour
{
_insideColliderCount += 1;
Serial.SetTouch(Convert.ToInt32(gameObject.name), true);
touchDidChange?.Invoke();
cr.material.color = new Color(1f, 1f, 1f, 1f);
}
@ -26,10 +28,12 @@ public class ColliderToSerial : MonoBehaviour
{
_insideColliderCount -= 1;
_insideColliderCount = Mathf.Max(0, _insideColliderCount);
cr.material.color = new Color(0f, 0f, 0f, 0f);
if (_insideColliderCount == 0)
{
Serial.SetTouch(Convert.ToInt32(gameObject.name), false);
touchDidChange?.Invoke();
cr.material.color = new Color(0f, 0f, 0f, 0f);
}
}
}

View File

@ -1,9 +1,9 @@
using UnityEngine;
using System.IO.Ports;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using UnityEngine;
public class Serial : MonoBehaviour
{
@ -35,28 +35,56 @@ public class Serial : MonoBehaviour
bool StartUp = false;
void Start()
{
ComL.Open();
ComR.Open();
try
{
ComL.Open();
ComR.Open();
}
catch (Exception ex)
{
Console.WriteLine($"Failed to Open Serial Ports: {ex}");
}
Debug.Log("Touch Serial Initializing..");
SetSettingData_160();
SetSettingData_201();
SetSettingData_162();
SetSettingData_148();
//Send touch update periodically to keep "read" alive
InvokeRepeating("SendTouchState", 0, 1);
//Send touch updates whenever actual state changes to achieve desired update frequency without overloading
ColliderToSerial.touchDidChange += SendTouchState;
}
private void OnDestroy()
{
ComL.Close();
ComR.Close();
}
void Update()
{
if(ComL.IsOpen)
ReadHead(ComL, 0);
if (ComR.IsOpen)
ReadHead(ComR, 1);
if (Input.GetKeyDown(KeyCode.M)) //this is a touch test code
StartCoroutine(TouchTest(true));
// if (Input.GetKeyDown(KeyCode.M)) //this is a touch test code
// StartCoroutine(TouchTest(true));
if (Input.GetKeyDown(KeyCode.M) && StartUp)
SendTouchState();
}
private void SendTouchState()
{
if(StartUp)
{
Debug.Log("Sending Touch State");
// Debug.Log("Sending Left");
SendTouch(ComL, TouchPackL);
// Debug.Log("Sending Right");
SendTouch(ComR, TouchPackR);
}
}
private void FixedUpdate() {
SendTouch(ComL, TouchPackL);
SendTouch(ComR, TouchPackR);
//SendTouchState();
}
IEnumerator TouchTest(bool State) //this is a touch test code
@ -181,7 +209,13 @@ public class Serial : MonoBehaviour
void SendTouch(SerialPort Serial, byte[] Pack) //Send touch data
{
if (StartUp)
Serial.Write(GetTouchPack(Pack), 0, 36);
{
// Debug.Log($"Pack {string.Join(" ", Pack)}");
var output = GetTouchPack(Pack);
// Debug.Log($"Output {string.Join(" ", output)}");
Serial.Write(output, 0, 36);
}
}
public static void SetTouch(int Area, bool State) //set touch data 0-239
{