Add LED IPC

This commit is contained in:
xpeng
2022-07-29 18:49:56 +02:00
parent 54c392fb39
commit 9919fabcf6
8 changed files with 235 additions and 89 deletions
@@ -27,6 +27,7 @@ public class SettingsManager : MonoBehaviour
{
FocusChecked=true;
UpdateAllConfigs();
Debug.Log("Configs Updated");
}
}
if (!Application.isFocused)
@@ -38,7 +39,7 @@ public class SettingsManager : MonoBehaviour
UwcConfig.UpdateConfigs();
UpdatePhysicFPS();
UpdateHands();
Debug.Log("Configs Updated");
}
void UpdatePhysicFPS()
+15 -2
View File
@@ -1,3 +1,16 @@
{
"name": "LightManager"
}
"name": "LightManager",
"rootNamespace": "",
"references": [
"GUID:80de51a1f88203a4cb129a5922de311f"
],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}
+66 -6
View File
@@ -1,25 +1,85 @@
using System.Collections;
using System.Collections.Generic;
using System.IO.MemoryMappedFiles;
using System.Security.Principal;
using UnityEngine;
public class LightManager : MonoBehaviour
{
public List<GameObject> Lights = new List<GameObject>();
public List<Material> Materials = new List<Material>();
public float FadeDuration = 0.5f;
List<Material> Materials = new List<Material>();
public static bool useIPC = true;
static Texture2D RGBColor2D;
public static MemoryMappedFile sharedBuffer;
public static MemoryMappedViewAccessor sharedBufferAccessor;
private IEnumerator[] coroutines = new IEnumerator[240];
public float FadeDuration = 0.5f;
private void Start()
{
if (JsonConfiguration.HasKey("useIPC"))
useIPC = JsonConfiguration.GetBoolean("useIPC");
else
JsonConfiguration.SetBoolean("useIPC", useIPC);
for (int i = 0; i < Lights.Count; i++)
Materials.Add(Lights[i].GetComponent<Renderer>().material);
if (useIPC)
{
Materials[i] = Lights[i].GetComponent<Renderer>().material;
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
}
}
public void UpdateLight(int Area, bool State)
private void Update()
{
if (useIPC)
UpdateLED();
}
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()
{
GetTextureFromBytes(GetBytesFromMemory());
int index = 0;
for (int i = 0; i < 30; i++)
{
for (int ii = 0; ii < 4; ii++)
{
Materials[119 - i - ii * 30].SetColor("_EmissionColor", RGBColor2D.GetPixel(index * 2, 0));
Materials[210 + i - ii * 30].SetColor("_EmissionColor", RGBColor2D.GetPixel((index + 120) * 2, 0));
index++;
}
}
}
void GetTextureFromBytes(byte[] bytes)
{
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(useIPC)
return;
Area -= 1;
if (State)
{
Materials[Area].SetColor("_EmissionColor", new Color(1f, 1f, 1f, 1f));
+2 -2
View File
@@ -22,7 +22,7 @@ public class ColliderToSerial : MonoBehaviour
_insideColliderCount += 1;
Serial.SetTouch(Area, true);
touchDidChange?.Invoke();
lightManager.UpdateLight(Area, true);
lightManager.UpdateLightFade(Area, true);
}
private void OnTriggerExit(Collider other)
@@ -33,7 +33,7 @@ public class ColliderToSerial : MonoBehaviour
{
Serial.SetTouch(Area, false);
touchDidChange?.Invoke();
lightManager.UpdateLight(Area, false);
lightManager.UpdateLightFade(Area, false);
}
}
}
+1 -1
View File
@@ -19,6 +19,7 @@ public class Serial : MonoBehaviour
private Thread _touchThread;
private Queue _touchQueue;
static SerialPort ComL = new SerialPort ("COM5", 115200);
static SerialPort ComR = new SerialPort ("COM6", 115200);
@@ -122,7 +123,6 @@ public class Serial : MonoBehaviour
var data = Serial.ReadExisting();
SendResp(Serial, side, data);
}
}
void SendResp(SerialPort Serial, int side, string data)
{