-
Notifications
You must be signed in to change notification settings - Fork 11
/
DmxController.cs
259 lines (227 loc) · 9.25 KB
/
DmxController.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
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace Sniper.Lighting.DMX
{
public static class DmxController<T> where T : DMXProUSB, new()
{
private static ThreadSafeList<Effect> effectQueue = new ThreadSafeList<Effect>();
private static Thread runningThread;
private static bool done;
private static T dmxDevice;
private readonly static int busLength = 512;
static DmxController()
{
dmxDevice = new T();
done = false;
for (int i = 0; i < busLength; i++)
{
byte value = dmxDevice.getDefaultForChannel(i);
dmxDevice.SetDmxValue(i, value, Guid.Empty, 1);
}
runningThread = new Thread(new ThreadStart(Run));
runningThread.IsBackground = true;
runningThread.Start();
dmxDevice.StateChanged += new StateChangedEventHandler(DMXProUSB_StateChanged);
}
public static void QueueEffect(Effect e)
{
//create a lighting queue for this effect
dmxDevice.CreateQueue(e.Queue, e.Priority);
effectQueue.Add(e);
}
public static byte GetDefaultForChannel(int channel)
{
return dmxDevice.getDefaultForChannel(channel);
}
static void DMXProUSB_StateChanged(object sender, StateChangedEventArgs e)
{
if (StateChanged != null)
{
StateChanged(sender, e);
}
}
public static Effect EaseDmxValue(Guid queue, int channel, int priority, byte endValue, int duration, EasingType typeIn, EasingType typeOut, EasingExtents extents)
{
byte startValue = dmxDevice.GetDmxValue(channel);
Effect handle = new Effect(queue, channel, priority, startValue, endValue, duration, typeIn, typeOut, extents);
QueueEffect(handle);
handle.Start();
return handle;
}
public static void SetDefaults(DmxDefaults newDefaults)
{
dmxDevice.setDefaults(newDefaults);
}
public static Effect EaseDmxValue(Guid queue, int channel, int priority, byte startValue, byte endValue, int duration, EasingType typeIn, EasingType typeOut, EasingExtents extents)
{
Effect handle = new Effect(queue, channel, priority, startValue, endValue, duration, typeIn, typeOut, extents);
QueueEffect(handle);
handle.Start();
return handle;
}
public static Effect EaseDmxValue(Guid queue, int channel, int priority, byte startValue, byte endValue, int duration, EasingType typeIn, EasingType typeOut, EasingExtents extents, DateTime when)
{
Effect handle = new Effect(queue, channel, priority, startValue, endValue, duration, typeIn, typeOut, extents);
QueueEffect(handle);
handle.StartIn((int)(when - DateTime.Now).TotalMilliseconds);
return handle;
}
public static void SetDmxValue(Guid queue, int channel, int priority, byte value, DateTime when)
{
Effect handle = new Effect(queue, channel, priority, null, value, 0, EasingType.Linear, EasingType.Linear, EasingExtents.EaseInOut);
QueueEffect(handle);
handle.StartIn((int)(when - DateTime.Now).TotalMilliseconds);
}
public static void SetDmxValue(Guid queue, int channel, int priority, byte value)
{
SetDmxValue(queue, channel, priority, value, DateTime.Now);
}
public static void SetDmxValue(Guid queue, int channel, int priority, byte value, int revertInDuration)
{
var currentValue = GetDmxValue(channel);
SetDmxValue(queue, channel, priority, value);
if (revertInDuration != 0)
{
SetDmxValue(queue, channel, priority, currentValue, DateTime.Now.AddMilliseconds(revertInDuration));
}
}
public static void SetDmxValue(Guid queue, int channel, int priority, byte value, int revertInDuration, int delayDuration)
{
var currentValue = GetDmxValue(channel);
SetDmxValue(queue, channel, priority, value, DateTime.Now.AddMilliseconds(delayDuration));
if (revertInDuration != 0)
{
SetDmxValue(queue, channel, priority, currentValue, DateTime.Now.AddMilliseconds(delayDuration).AddMilliseconds(revertInDuration));
}
}
public static Effect PulseDmxValue(Guid queue, int channel, int priority, byte startValue, byte endValue, int duration, EasingType typeIn, EasingType typeOut, EasingExtents extents)
{
var handle = new Pulse(queue, channel, priority, startValue, endValue, duration, typeIn, typeOut, extents);
QueueEffect(handle);
handle.Start();
return handle;
}
public static Effect HoldDmxValue(Guid queue, int channel, int priority, byte newValue, byte revertValue, int duration, int delayDuration)
{
var handle = new Hold(queue, channel, priority, newValue, revertValue, duration);
QueueEffect(handle);
handle.StartIn(delayDuration);
return handle;
}
public static byte GetDmxValue(int channel)
{
return dmxDevice.GetDmxValue(channel);
}
public static void ClearFutureQueueForDmxChannel(Guid queue, int channel)
{
Queue<Effect> toRemove = new Queue<Effect>();
DateTime moment = DateTime.Now;
foreach (Effect e in effectQueue)
{
if (e.Queue == queue && e.Channel == channel && e.FromTimestamp > moment)
{
toRemove.Enqueue(e);
}
}
while (toRemove.Count > 0)
{
Effect e = toRemove.Dequeue();
effectQueue.Remove(e);
}
}
public static void ClearEntireQueueForDmxChannel(Guid queue, int channel)
{
Queue<Effect> toRemove = new Queue<Effect>();
DateTime moment = DateTime.Now;
foreach (Effect e in effectQueue)
{
if (e.Queue == queue && e.Channel == channel)
{
toRemove.Enqueue(e);
}
}
while (toRemove.Count > 0)
{
Effect e = toRemove.Dequeue();
effectQueue.Remove(e);
}
}
private static void Run()
{
Queue<Effect> toRemove = new Queue<Effect>();
while (!done)
{
int activeEffectsCount = effectQueue.Count;
DateTime moment = DateTime.Now;
foreach (Effect e in effectQueue)
{
if (e.FromTimestamp <= moment) //don't check the upper bound otherwise if it's an instant effect, it's already passed by the time this is evaluated
{
byte value = e.GetCurrentValue();
Guid queue = e.Queue;
int channel = e.Channel;
int priority = e.Priority; //it's our pie-ority
dmxDevice.SetDmxValue(channel, value, queue, priority);
}
if (moment > e.ToTimestamp)
{
toRemove.Enqueue(e);
}
}
while (toRemove.Count > 0)
{
Effect finishedEffect = toRemove.Dequeue();
effectQueue.Remove(finishedEffect);
}
var activeQueues = effectQueue.Select(x => x.Queue).Distinct();
var currentQueues = dmxDevice.GetCurrentQueueIds();
var inactiveQueues = currentQueues.Except(activeQueues);
foreach (var queue in inactiveQueues)
{
if (queue != Guid.Empty) //never delete empty guid queue, used for test UI
{
dmxDevice.DeleteQueue(queue);
}
}
Thread.Sleep(5);
}
}
public static void Dispose()
{
done = true;
Thread.Sleep(500);
if (runningThread.ThreadState == ThreadState.Running)
{
runningThread.Abort();
}
runningThread = null;
dmxDevice.Dispose();
}
public static byte[] GetCurrentValues()
{
return dmxDevice.GetCurrentBuffer();
}
public static bool Start()
{
return dmxDevice.start();
}
public static bool Connected { get { return dmxDevice.Connected; } }
public static void SetLimits(DmxLimits newLimits)
{
dmxDevice.setLimits(newLimits);
}
public static event StateChangedEventHandler StateChanged;
public static void Stop()
{
foreach (Effect e in effectQueue)
{
e.Stop();
}
dmxDevice.stop();
}
}
}