-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathLedSerial.cs
119 lines (115 loc) · 3.72 KB
/
LedSerial.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
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
public List<byte> dataPacket = new List<byte>();
public List<byte> incomPacket = new List<byte>();
byte recivData;
public Light[] Lights;
public float LightIntensity = 3f;
Color32 PrevFadeColor;
Color32 nowCorlor;
void Start()
{
p1Serial.Open();
Debug.Log("LED Serial Started");
}
void Update()
{
//ReadPack();
ReadData();
UpdateLED();
//FixLedPower();
}
void ReadPack()
{
ReadData();
Debug.Log("RX: "+dataPacket[0]+"-"+
dataPacket[1]+"-"+
dataPacket[2]+"-"+
dataPacket[3]+"-"+
dataPacket[4]+"-"+
dataPacket[5]+"-"+
dataPacket[6]+"-"+
dataPacket[7]+"-"+
dataPacket[8]+"-"+
dataPacket[9]+"-"+
dataPacket[10]);
}
void ReadData()
{
while (p1Serial.BytesToRead > 0)
{
recivData = Convert.ToByte(p1Serial.ReadByte());
if (recivData == 224)
{
dataPacket = new List<byte>(incomPacket);
incomPacket.Clear();
incomPacket.Add(recivData);
return;
}
incomPacket.Add(recivData);
}
}
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:
case 51:
if (dataPacket[6] > 8)
dataPacket[6] = 8;
nowCorlor = new Color32(dataPacket[8], dataPacket[9], dataPacket[10], 255);
if (dataPacket[4]==50)
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);
dataPacket.Clear();
break;
}
}
IEnumerator Fade(byte start, byte end, Light[] Lights, Color32 prevColor, Color32 nowColor, float duration)
{
duration = 4095 / duration * 8 / 1000;
//Debug.Log(duration);
for (float time = 0f; time < duration; time += Time.deltaTime)
{
float progress = time / duration;
for (int i = start; i < end; i++)
{
Lights[i].color = Color.Lerp(prevColor, nowColor, progress);
//yield return null;
}
yield return null;
}
}
void Switch(byte start, byte end, Light[] Lights, Color32 Color)
{
for (int i = start; i < end; i++)
{
Lights[i].color = Color;
}
}
void FixLedPower()
{
for (int i = 0; i < 8; i++)
{
Lights[i].intensity = LightIntensity / (Lights[i].color.r / 2 + Lights[i].color.g / 2 + Lights[i].color.b / 2 + 0.1f) ;
}
}
}