-
Notifications
You must be signed in to change notification settings - Fork 11
/
DMXControllerInstance.cs
199 lines (176 loc) · 7.14 KB
/
DMXControllerInstance.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
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace Sniper.Lighting.DMX
{
public class DmxControllerInstance
{
private ThreadSafeList<Effect> effectQueue = new ThreadSafeList<Effect>();
private Thread runningThread;
private bool done;
private DMXProUSB dmxDevice;
private const int busLength = 512;
public DmxControllerInstance(DMXDeviceType dmxDeviceType)
{
dmxDevice = dmxDeviceType == DMXDeviceType.OpenDMX ? new OpenDMXUSB() : new DMXProUSB();
done = false;
for (int i = 0; i < busLength; i++)
{
dmxDevice.SetDmxValue(i, 0, Guid.Empty, 1);
}
runningThread = new Thread(new ThreadStart(Run));
runningThread.IsBackground = true;
runningThread.Start();
dmxDevice.StateChanged += new StateChangedEventHandler(DMXProUSB_StateChanged);
}
public void QueueEffect(Effect e)
{
//create a lighting queue for this effect
dmxDevice.CreateQueue(e.Queue, e.Priority);
effectQueue.Add(e);
}
void DMXProUSB_StateChanged(object sender, StateChangedEventArgs e)
{
if (StateChanged != null)
{
StateChanged(sender, e);
}
}
public 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 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 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 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 void SetDmxValue(Guid queue, int channel, int priority, byte value, DateTime when, int stopInMilliseconds)
{
SetDmxValue(queue, channel, priority, value, when);
SetDmxValue(queue, channel, priority, 0, when.AddMilliseconds(stopInMilliseconds));
}
public void SetDmxValue(Guid queue, int channel, int priority, byte value)
{
SetDmxValue(queue, channel, priority, value, DateTime.Now);
}
public void SetDmxValue(Guid queue, int channel, int priority, byte value, int revertInDuration)
{
SetDmxValue(queue, channel, priority, value);
if (revertInDuration != 0)
{
SetDmxValue(queue, channel, priority, 0, DateTime.Now.AddMilliseconds(revertInDuration));
}
}
public 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 byte GetDmxValue(int channel)
{
return dmxDevice.GetDmxValue(channel);
}
private void Run()
{
Queue<Effect> toRemove = new Queue<Effect>();
while (!done)
{
int activeEffectsCount = effectQueue.Count;
foreach (Effect e in effectQueue)
{
DateTime moment = DateTime.Now;
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 void Dispose()
{
done = true;
Thread.Sleep(500);
if (runningThread.ThreadState == ThreadState.Running)
{
runningThread.Abort();
}
runningThread = null;
dmxDevice.Dispose();
}
public byte[] GetCurrentValues()
{
return dmxDevice.GetCurrentBuffer();
}
public bool Start()
{
return dmxDevice.start();
}
public bool Connected { get { return dmxDevice.Connected; } }
public void SetLimits(DmxLimits newLimits)
{
dmxDevice.setLimits(newLimits);
}
public event StateChangedEventHandler StateChanged;
public void Stop()
{
StopEffects();
dmxDevice.stop();
}
public void StopEffects()
{
foreach (Effect e in effectQueue)
{
e.Stop();
}
}
}
}