1
0
mirror of https://github.com/xiaopeng12138/MaiDXR.git synced 2024-11-27 20:40:48 +01:00

v0.6.2 fix, rewritten read serial function (LED)

This commit is contained in:
xpeng 2022-02-26 21:16:34 +01:00
parent b7e7fe3ce6
commit fcd0b49700
7 changed files with 470 additions and 372 deletions

Binary file not shown.

File diff suppressed because it is too large Load Diff

View File

@ -1,22 +1,21 @@

using UnityEngine;
using System.IO.Ports;
using System;
using System.Collections;
using System.Collections.Generic;
public class LedSerial : MonoBehaviour
{
static SerialPort p1Serial = new SerialPort ("COM51", 115200);
// Start is called before the first frame update
byte[] dataPacket = new byte[13];
byte[] incomPacket = new byte[13];
float timer = 0;
public List<byte> dataPacket = new List<byte>();
public List<byte> incomPacket = new List<byte>();
byte recivData;
int packLeng = 0;
public Light[] Lights;
public float LightIntensity = 3f;
Color32 PrevFadeColor;
bool headState = false;
Color32 nowCorlor;
void Start()
{
p1Serial.Open();
@ -26,17 +25,11 @@ public class LedSerial : MonoBehaviour
{
//ReadPack();
ReadData();
UpdateLED();
StartCoroutine(FixLedPower());
}
IEnumerator FixLedPower()
{
for (int i = 0; i < 8; i++)
{
Lights[i].intensity = (LightIntensity / (Lights[i].color.r + Lights[i].color.g + Lights[i].color.b)) +0.1f;
yield return null;
}
}
private void ReadPack()
void ReadPack()
{
ReadData();
Debug.Log("RX: "+dataPacket[0]+"-"+
@ -51,59 +44,50 @@ public class LedSerial : MonoBehaviour
dataPacket[9]+"-"+
dataPacket[10]);
}
private void ReadData()
void ReadData()
{
timer = 0f;
if (p1Serial.BytesToRead >= 3)
while (p1Serial.BytesToRead > 0)
{
if (!headState)
recivData = Convert.ToByte(p1Serial.ReadByte());
headState = false;
if (recivData == 224)
recivData = Convert.ToByte(p1Serial.ReadByte());
if (recivData == 224)
{
do
{
incomPacket[packLeng++] = recivData;
recivData = Convert.ToByte(p1Serial.ReadByte());
}
while (recivData != 224 & p1Serial.BytesToRead >= 1);
headState = true;
dataPacket = incomPacket;
packLeng = 0;
UpdateLED();
dataPacket = new List<byte>(incomPacket);
incomPacket.Clear();
incomPacket.Add(recivData);
break;
}
incomPacket.Add(recivData);
}
}
private void UpdateLED()
void UpdateLED()
{
if (dataPacket.Count < 9)
return;
switch (dataPacket[4])
{
case 49:
if (dataPacket[5] > 7)
dataPacket[5] = 7;
Lights[dataPacket[5]].color = new Color32(dataPacket[6], dataPacket[7], dataPacket[8], 255);
dataPacket.Clear();
break;
case 50:
if (dataPacket[6] > 8)
dataPacket[6] = 8;
for (int i = dataPacket[5]; i < dataPacket[6]; i++)
{
Lights[i].color = new Color32(dataPacket[8], dataPacket[9], dataPacket[10], 255);
}
PrevFadeColor = new Color32(dataPacket[8], dataPacket[9], dataPacket[10], 255);
break;
case 51:
if (dataPacket[6] > 8)
dataPacket[6] = 8;
Color32 nowCorlor = new Color32(dataPacket[8], dataPacket[9], dataPacket[10], 255);
StartCoroutine(Fade(dataPacket[5], dataPacket[6], Lights,PrevFadeColor, nowCorlor, dataPacket[11]));
nowCorlor = new Color32(dataPacket[8], dataPacket[9], dataPacket[10], 255);
if (dataPacket[4]==50)
StartCoroutine(Switch(dataPacket[5], dataPacket[6], Lights, nowCorlor));
else
StartCoroutine(Fade(dataPacket[5], dataPacket[6], Lights,PrevFadeColor, nowCorlor, dataPacket[11]));
PrevFadeColor = new Color32(dataPacket[8], dataPacket[9], dataPacket[10], 255);
//Debug.Log("Fade");
dataPacket.Clear();
break;
}
}
private IEnumerator Fade(byte start, byte end, Light[] Lights, Color32 prevColor, Color32 nowColor, float duration)
IEnumerator Fade(byte start, byte end, Light[] Lights, Color32 prevColor, Color32 nowColor, float duration)
{
duration = 4095 / duration * 8 / 1000;
//Debug.Log(duration);
@ -113,8 +97,25 @@ public class LedSerial : MonoBehaviour
for (int i = start; i < end; i++)
{
Lights[i].color = Color.Lerp(prevColor, nowColor, progress);
//yield return null;
}
yield return null;
}
}
IEnumerator Switch(byte start, byte end, Light[] Lights, Color32 Color)
{
for (int i = start; i < end; i++)
{
Lights[i].color = Color;
yield return null;
}
}
IEnumerator FixLedPower()
{
for (int i = 0; i < 8; i++)
{
Lights[i].intensity = (LightIntensity / (Lights[i].color.r + Lights[i].color.g + Lights[i].color.b)) +0.1f;
yield return null;
}
}
}

View File

@ -1,6 +1,6 @@
{
"dependencies": {
"com.unity.collab-proxy": "1.15.7",
"com.unity.collab-proxy": "1.15.12",
"com.unity.ide.rider": "2.0.7",
"com.unity.ide.visualstudio": "2.0.14",
"com.unity.ide.vscode": "1.2.4",

View File

@ -1,11 +1,10 @@
{
"dependencies": {
"com.unity.collab-proxy": {
"version": "1.15.7",
"version": "1.15.12",
"depth": 0,
"source": "registry",
"dependencies": {
"com.unity.nuget.newtonsoft-json": "2.0.0",
"com.unity.services.core": "1.0.1"
},
"url": "https://packages.unity.com"

View File

@ -167,9 +167,9 @@ PlayerSettings:
- {fileID: 0}
- {fileID: 0}
- {fileID: 0}
- {fileID: 1708333901713763777, guid: c32bea96a9d497047888cff51152a827, type: 2}
- {fileID: 0}
- {fileID: -9008817870737138221, guid: dac1673e162f8724aada57cd5b44f68b, type: 2}
- {fileID: 0}
- {fileID: 0}
metroInputSource: 0
wsaTransparentSwapchain: 0
m_HolographicPauseOnTrackingLoss: 1
@ -377,7 +377,9 @@ PlayerSettings:
Android: 1
iPhone: 1
tvOS: 1
m_BuildTargetGroupLightmapEncodingQuality: []
m_BuildTargetGroupLightmapEncodingQuality:
- m_BuildTarget: Standalone
m_EncodingQuality: 1
m_BuildTargetGroupLightmapSettings: []
m_BuildTargetNormalMapEncoding: []
playModeTestRunnerEnabled: 0
@ -528,7 +530,9 @@ PlayerSettings:
switchPlayerConnectionEnabled: 1
switchUseNewStyleFilepaths: 0
switchUseMicroSleepForYield: 1
switchEnableRamDiskSupport: 0
switchMicroSleepForYieldTime: 25
switchRamDiskSpaceSize: 12
ps4NPAgeRating: 12
ps4NPTitleSecret:
ps4NPTrophyPackPath:

View File

@ -1,2 +1,2 @@
m_EditorVersion: 2020.3.28f1
m_EditorVersionWithRevision: 2020.3.28f1 (f5400f52e03f)
m_EditorVersion: 2020.3.30f1
m_EditorVersionWithRevision: 2020.3.30f1 (1fb1bf06830e)