forked from agrath/Sniper.Lighting.Dmx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDmxController.cs
167 lines (146 loc) · 5.54 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
using System;
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 List<Channel> Channels = new List<Channel>();
private static int _numChannels = 512;
private static Thread runningThread;
private static bool done;
private static T dmxDevice;
static DmxController()
{
dmxDevice = new T();
done = false;
lock (Channels)
{
for (int i = 0; i < _numChannels; i++)
{
Channel c = new Channel(i,
(int channel, byte value) => {
//Console.WriteLine("{0}: DMXProUSB.SetDmxValue({1},{2})", DateTime.Now, channel, value);
dmxDevice.SetDmxValue(channel, value);
});
Channels.Add(c);
}
}
runningThread = new Thread(new ThreadStart(Run));
runningThread.IsBackground = true;
runningThread.Start();
dmxDevice.StateChanged += new StateChangedEventHandler(DMXProUSB_StateChanged);
}
static void DMXProUSB_StateChanged(object sender, StateChangedEventArgs e)
{
if (StateChanged != null)
{
StateChanged(sender, e);
}
}
public static Effect EaseDmxValue(int channel, byte endValue, int duration, EasingType typeIn, EasingType typeOut, EasingExtents extents)
{
Effect handle = new Effect(channel, Channels[channel].Value, endValue, duration, typeIn, typeOut, extents);
Channels[channel].QueueEffect(handle);
handle.Start();
return handle;
}
public static Effect EaseDmxValue(int channel, byte startValue, byte endValue, int duration, EasingType typeIn, EasingType typeOut, EasingExtents extents)
{
Effect handle = new Effect(channel, startValue, endValue, duration, typeIn, typeOut, extents);
Channels[channel].QueueEffect(handle);
handle.Start();
return handle;
}
public static Effect EaseDmxValue(int channel, byte startValue, byte endValue, int duration, EasingType typeIn, EasingType typeOut, EasingExtents extents, DateTime when)
{
Effect handle = new Effect(channel, startValue, endValue, duration, typeIn, typeOut, extents);
Channels[channel].QueueEffect(handle);
handle.StartIn((int)(when - DateTime.Now).TotalMilliseconds);
return handle;
}
public static void SetDmxValue(int channel, byte value, DateTime when)
{
Effect handle = new Effect(channel, Channels[channel].Value, value, 0, EasingType.Linear, EasingType.Linear, EasingExtents.EaseInOut);
Channels[channel].QueueEffect(handle);
handle.StartIn((int)(when - DateTime.Now).TotalMilliseconds);
}
public static void SetDmxValue(int channel, byte value)
{
SetDmxValue(channel, value, DateTime.Now);
}
public static void SetDmxValue(int channel, byte value, int revertInDuration)
{
byte current = GetDmxValue(channel);
SetDmxValue(channel, value);
SetDmxValue(channel, current, DateTime.Now.AddMilliseconds(revertInDuration));
}
public static Effect PulseDmxValue(int channel, byte startValue, byte endValue, int duration, EasingType typeIn, EasingType typeOut, EasingExtents extents)
{
var handle = new Pulse(channel, startValue, endValue, duration, typeIn, typeOut, extents);
Channels[channel].QueueEffect(handle);
handle.Start();
return handle;
}
public static byte GetDmxValue(int channel)
{
return dmxDevice.GetDmxValue(channel);
}
private static void Run()
{
while (!done)
{
lock (Channels)
{
foreach (Channel c in Channels)
{
try
{
c.Tick();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
}
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 ( var c in Channels )
{
c.Stop();
}
dmxDevice.stop();
}
}
}