-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLightController.cs
More file actions
199 lines (161 loc) · 5.27 KB
/
Copy pathLightController.cs
File metadata and controls
199 lines (161 loc) · 5.27 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO.Ports;
namespace FluxLightUI
{
class LightController
{
Interval[] intervals;
SerialPort port;
bool paired;
int size = 0;
struct Commands {
static public byte SETCOLOR = 0;
static public byte INTERVALON = 1;
static public byte INTERVALOFF = 2;
static public byte MUSICON = 3;
static public byte MUSICOFF = 4;
static public byte SETINTERVALS = 5;
static public byte SETINTERVALTIMES = 6;
static public byte PRINTINTERVALS = 7;
static public byte PRINTINTERVALTIMES = 8;
static public byte SETTIME = 9;
static public byte PRINTTIME = 10;
static public byte CLEARINTERVALS = 11;
static public byte HANDSHAKE = 255;
};
Commands commands;
public LightController()
{
commands = new Commands();
paired = false;
intervals = new Interval[10];
if(handshake())
{
port.DataReceived += new SerialDataReceivedEventHandler(DataReceived);
}
}
private bool handshake()
{
foreach (String portName in SerialPort.GetPortNames())
{
try
{
port = new SerialPort(portName, 9600);
port.Open();
write(new byte[1] { Commands.HANDSHAKE }, 1);
if (port.BytesToRead > 0)
{
paired = true;
return true;
}
}
catch (Exception e) { }
}
return false;
}
private void DataReceived(object sender, SerialDataReceivedEventArgs e)
{
Console.WriteLine(port.ReadExisting());
}
public void addInterval(Interval interval)
{
if (size < 10)
{
intervals[size] = interval;
size++;
}
}
public void clearIntervals()
{
intervals = new Interval[] { };
size = 0;
write(new byte[1] { Commands.CLEARINTERVALS }, 1);
}
public void setTime()
{
DateTime now = DateTime.Now;
byte[] buffer = new byte[3] {
Commands.SETTIME,
Convert.ToByte(now.Hour),
Convert.ToByte(now.Minute)
};
write(buffer, 3);
}
public void getTime()
{
write(new byte[] { Commands.PRINTTIME }, 1);
}
public void setColor(int r, int g, int b)
{
byte[] buffer = new byte[4] {
Commands.SETCOLOR,
Convert.ToByte(r),
Convert.ToByte(g),
Convert.ToByte(b)
};
write(buffer, 4);
}
public void intervalModeOn()
{
write(new byte[1] { Commands.INTERVALON }, 1);
}
public void intervalModeOff()
{
write(new byte[1] { Commands.INTERVALOFF }, 1);
}
public void getIntervalTimes()
{
write(new byte[1] { Commands.PRINTINTERVALTIMES }, 1);
}
public void getIntervals()
{
write(new byte[1] { Commands.PRINTINTERVALS }, 1);
}
public void setIntervals()
{
byte[] buffer = new byte[61];
byte[] buffer2 = new byte[41];
buffer[0] = Commands.SETINTERVALS;
buffer2[0] = Commands.SETINTERVALTIMES;
for (int i = 0; i < 10; i++)
{
for (int j = i * 6; j <= (i * 6); j+=6)
{
if (intervals[i] != null)
{
buffer[j + 1] = Convert.ToByte(intervals[i].startColor[0]);
buffer[j + 2] = Convert.ToByte(intervals[i].startColor[1]);
buffer[j + 3] = Convert.ToByte(intervals[i].startColor[2]);
buffer[j + 4] = Convert.ToByte(intervals[i].endColor[0]);
buffer[j + 5] = Convert.ToByte(intervals[i].endColor[1]);
buffer[j + 6] = Convert.ToByte(intervals[i].endColor[2]);
}
}
for (int j = (i * 4); j <= (i*4); j+=4)
{
if (intervals[i] != null)
{
buffer2[j + 1] = Convert.ToByte(intervals[i].start.Hour);
buffer2[j + 2] = Convert.ToByte(intervals[i].start.Minute);
buffer2[j + 3] = Convert.ToByte(intervals[i].end.Hour);
buffer2[j + 4] = Convert.ToByte(intervals[i].end.Minute);
}
}
}
write(buffer, 61);
write(buffer2, 41);
}
private void write(byte[] buffer, int size)
{
if(paired)
{
port.Write(buffer, 0, size);
}
}
}
}