add IPC touch support and fix some issue

This commit is contained in:
xpeng
2022-10-03 22:48:09 +02:00
parent a01f6c9152
commit 99deee5ab6
28 changed files with 523 additions and 87 deletions
+8
View File
@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: e174a01615947664f9e6caee3808ad5e
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,3 @@
{
"name": "IPCManager"
}
@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 4b68ab8531bd5864db8bb4af74c61824
AssemblyDefinitionImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
+67
View File
@@ -0,0 +1,67 @@
using System.Collections;
using System.Collections.Generic;
using System.IO.MemoryMappedFiles;
using System.Security.Principal;
using UnityEngine;
public class IPCManager : MonoBehaviour
{
public static MemoryMappedFile sharedBuffer;
public static MemoryMappedViewAccessor sharedBufferAccessor;
public static bool isInitialized = false;
private void Awake()
{
EnsureInitialization();
}
private static void EnsureInitialization()
{
if (!isInitialized)
InitializeIPC("Local\\WACVR_SHARED_BUFFER", 2164);
}
private IEnumerator ReconnectWait()
{
yield return new WaitForSeconds(5);
InitializeIPC("Local\\WACVR_SHARED_BUFFER", 2164);
}
private void Reconnect()
{
InitializeIPC("Local\\WACVR_SHARED_BUFFER", 2164);
}
private static void InitializeIPC(string sharedMemoryName, int sharedMemorySize)
{
MemoryMappedFileSecurity CustomSecurity = new MemoryMappedFileSecurity();
SecurityIdentifier sid = new SecurityIdentifier(WellKnownSidType.WorldSid, null);
var acct = sid.Translate(typeof(NTAccount)) as NTAccount;
CustomSecurity.AddAccessRule(new System.Security.AccessControl.AccessRule<MemoryMappedFileRights>(acct.ToString(), MemoryMappedFileRights.FullControl, System.Security.AccessControl.AccessControlType.Allow));
sharedBuffer = MemoryMappedFile.CreateOrOpen(sharedMemoryName, sharedMemorySize, MemoryMappedFileAccess.ReadWrite, MemoryMappedFileOptions.None, CustomSecurity, System.IO.HandleInheritability.Inheritable);
sharedBufferAccessor = sharedBuffer.CreateViewAccessor();
isInitialized = true;
}
public static byte[] GetLightData()
{
EnsureInitialization();
byte[] bytes = new byte[1920];
IPCManager.sharedBufferAccessor.ReadArray<byte>(244, bytes, 0, 1920);
return bytes;
}
public static void SetTouchData(bool[] bytes)
{
EnsureInitialization();
IPCManager.sharedBufferAccessor.WriteArray<bool>(4, bytes, 0, 240);
}
public static void SetTouch(int index, bool value)
{
EnsureInitialization();
if (value)
IPCManager.sharedBufferAccessor.Write(4 + index, 1);
else
IPCManager.sharedBufferAccessor.Write(4 + index, 0);
}
}
@@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: c08c82d8bfafe7b4bb4386ab2dd79d9c
guid: ddced15d8eef61f4d8c51a05cff9f958
MonoImporter:
externalObjects: {}
serializedVersion: 2
@@ -2,7 +2,8 @@
"name": "LightManager",
"rootNamespace": "",
"references": [
"GUID:80de51a1f88203a4cb129a5922de311f"
"GUID:80de51a1f88203a4cb129a5922de311f",
"GUID:4b68ab8531bd5864db8bb4af74c61824"
],
"includePlatforms": [],
"excludePlatforms": [],
+18 -30
View File
@@ -8,13 +8,12 @@ public class LightManager : MonoBehaviour
{
public List<GameObject> Lights = new List<GameObject>();
List<Material> Materials = new List<Material>();
public static bool isIPCIdle = false;
public static bool IsUseIPC = true;
[SerializeField]
public bool isIPCIdle = true;
[SerializeField]
public bool useIPCLighting = true;
static Texture2D RGBColor2D;
public static MemoryMappedFile sharedBuffer;
public static MemoryMappedViewAccessor sharedBufferAccessor;
private IEnumerator[] coroutines = new IEnumerator[240];
public float FadeDuration = 0.5f;
@@ -27,9 +26,8 @@ public class LightManager : MonoBehaviour
for (int i = 0; i < Lights.Count; i++)
Materials.Add(Lights[i].GetComponent<Renderer>().material);
if (IsUseIPC)
if (useIPCLighting)
{
InitializeIPC("Local\\WACVR_SHARED_BUFFER", 2164);
RGBColor2D = new Texture2D(480, 1, TextureFormat.RGBA32, false);
//RGBColor2D.filterMode = FilterMode.Point; //for debugging
//GetComponent<Renderer>().material.mainTexture = RGBColor2D; //for debugging
@@ -37,18 +35,23 @@ public class LightManager : MonoBehaviour
}
private void Update()
{
if (sharedBuffer != null)
GetTextureFromBytes(GetBytesFromMemory());
else
if (!useIPCLighting)
return;
if (IsUseIPC)
if (IPCManager.sharedBuffer != null)
{
GetTextureFromBytes(IPCManager.GetLightData());
CheckIPCState();
if (!isIPCIdle)
UpdateLED();
if (!isIPCIdle)
UpdateLED();
}
else
{
isIPCIdle = true;
}
}
void UpdateConfig()
{
IsUseIPC = ConfigManager.config.useIPCLighting;
useIPCLighting = ConfigManager.config.useIPCLighting;
}
private void CheckIPCState()
{
@@ -57,15 +60,6 @@ public class LightManager : MonoBehaviour
else
isIPCIdle = true;
}
private void InitializeIPC(string sharedMemoryName, int sharedMemorySize)
{
MemoryMappedFileSecurity CustomSecurity = new MemoryMappedFileSecurity();
SecurityIdentifier sid = new SecurityIdentifier(WellKnownSidType.WorldSid, null);
var acct = sid.Translate(typeof(NTAccount)) as NTAccount;
CustomSecurity.AddAccessRule(new System.Security.AccessControl.AccessRule<MemoryMappedFileRights>(acct.ToString(), MemoryMappedFileRights.FullControl, System.Security.AccessControl.AccessControlType.Allow));
sharedBuffer = MemoryMappedFile.CreateOrOpen(sharedMemoryName, sharedMemorySize, MemoryMappedFileAccess.ReadWrite, MemoryMappedFileOptions.None, CustomSecurity, System.IO.HandleInheritability.Inheritable);
sharedBufferAccessor = sharedBuffer.CreateViewAccessor();
}
private void UpdateLED()
{
int index = 0;
@@ -86,15 +80,9 @@ public class LightManager : MonoBehaviour
RGBColor2D.LoadRawTextureData(bytes);
RGBColor2D.Apply();
}
byte[] GetBytesFromMemory()
{
byte[] bytes = new byte[1920];
sharedBufferAccessor.ReadArray<byte>(244, bytes, 0, 1920);
return bytes;
}
public void UpdateLightFade(int Area, bool State)
{
if(!isIPCIdle)
if(!isIPCIdle || useIPCLighting)
return;
Area -= 1;
@@ -5,7 +5,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Collections;
public class ColliderToSerial : MonoBehaviour
public class ColliderToTouch : MonoBehaviour
{
public LightManager LightManager;
private int _insideColliderCount = 0;
@@ -18,7 +18,7 @@ public class ColliderToSerial : MonoBehaviour
private void OnTriggerEnter(Collider other)
{
_insideColliderCount += 1;
Serial.SetTouch(Area, true);
TouchManager.SetTouch(Area, true);
touchDidChange?.Invoke();
LightManager.UpdateLightFade(Area, true);
}
@@ -29,7 +29,7 @@ public class ColliderToSerial : MonoBehaviour
_insideColliderCount = Mathf.Max(0, _insideColliderCount);
if (_insideColliderCount == 0)
{
Serial.SetTouch(Area, false);
TouchManager.SetTouch(Area, false);
touchDidChange?.Invoke();
LightManager.UpdateLightFade(Area, false);
}
@@ -2,7 +2,8 @@
"name": "Serial",
"rootNamespace": "",
"references": [
"GUID:476196b3fd6829e45b839eb138f356ed"
"GUID:476196b3fd6829e45b839eb138f356ed",
"GUID:4b68ab8531bd5864db8bb4af74c61824"
],
"includePlatforms": [],
"excludePlatforms": [],
@@ -5,7 +5,7 @@ using System.Threading;
using System.IO.Ports;
using System.Linq;
using UnityEngine;
public class Serial : MonoBehaviour
public class TouchManager : MonoBehaviour
{
const byte CMD_GET_SYNC_BOARD_VER = 0xa0;
@@ -55,7 +55,7 @@ public class Serial : MonoBehaviour
_touchThread = new Thread(TouchThreadLoop);
InvokeRepeating("PingTouchThread", 0, 1);
//Send touch updates whenever actual state changes to achieve desired update frequency without overloading
ColliderToSerial.touchDidChange += PingTouchThread;
ColliderToTouch.touchDidChange += PingTouchThread;
}
private void PingTouchThread()
@@ -85,8 +85,8 @@ public class Serial : MonoBehaviour
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();
}
@@ -230,19 +230,23 @@ public class Serial : MonoBehaviour
}
public static void SetTouch(int Area, bool State) //set touch data 0-239
{
//Area +=1;
if (Area < 121)
Area -= 1;
if (Area < 120)
{
Area += (Area-1) / 5 * 3 + 7;
TouchPackAll[Area + 120] = State;
Area += Area / 5 * 3 + 7;
ByteHelper.SetBit(TouchPackR, Area, State);
}
else if (Area >= 120)
{
TouchPackAll[Area - 120] = State;
Area -= 120;
Area += (Area-1) / 5 * 3 + 7;
Area += Area / 5 * 3 + 7;
ByteHelper.SetBit(TouchPackL, Area, State);
}
TouchPackAll[Area] = State;
IPCManager.SetTouchData(TouchPackAll);
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: e001316a6dab3f04cbb2525904cfd6c8
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: