forked from agrath/Sniper.Lighting.Dmx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Channel.cs
99 lines (81 loc) · 2.62 KB
/
Channel.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Sniper.Lighting.DMX
{
class Channel
{
public int Id { get; set; }
public byte Value { get; set; }
public List<Effect> Effects { get; set; }
private Action<int, byte> Set;
public Channel(int id, Action<int, byte> setValueAction)
{
this.Id = id;
this.Set = setValueAction; // use this outside the channel to update the main buffer
this.Effects = new List<Effect>();
}
public void QueueEffect(Effect e)
{
lock (Effects)
{
Effects.Add(e);
}
}
public void Tick()
{
lock (Effects)
{
if (Effects.Count == 0) return;
List<Effect> toRemove = new List<Effect>();
int sum = 0;
bool hasValue = false;
foreach (Effect e in Effects)
{
if (!e.Running)
{
continue;
}
DateTime moment = DateTime.Now;
byte value = e.GetCurrentValue();
if (moment >= e.FromTimestamp && moment <= e.ToTimestamp)
{
sum += value;
//Console.WriteLine("{0}: Effect {1} produced value {2} for channel {3}", DateTime.Now, e.UniqueIdentifier, value, e.Channel);
hasValue = true;
}
if (moment > e.ToTimestamp)
{
sum += e.NewValue;
toRemove.Add(e);
//Console.WriteLine("{0}: Effect {1} produced value {2} for channel {3} (Finalize)", DateTime.Now, e.UniqueIdentifier, e.NewValue, e.Channel);
hasValue = true;
}
}
Effects.RemoveAll(x => toRemove.Contains(x));
if (hasValue)
{
if (sum > 255)
{
sum = 255;
}
if (sum < 0)
{
sum = 0;
}
Value = (byte)sum;
Set(this.Id, Value);
}
}
}
internal void Stop()
{
foreach ( var e in Effects )
{
e.Stop();
}
//this.Effects.Clear();
}
}
}