-
Notifications
You must be signed in to change notification settings - Fork 0
/
StatusPool.cs
80 lines (73 loc) · 3.64 KB
/
StatusPool.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
namespace Iot.Raspi.MultiThreading
{
// The StatusPool encapsulates status information used by different tasks/threads.
// It holds the current delay values for the red, yellow and green LEDs. A delay value is equal to
// the half period time and defines the blinking frequency. The access to delay values is protected
// by mutex, even this isn't neccessary in this case, because integer variables are read or written
// using atomic operations (without interruption by another thread). Each time a value is changed,
// an EventWaitHandle is set to signal TaskLcd that a delay value has been changed.
public class StatusPool
{
// Colors of the 3 LEDs.
private enum Color {Red, Yellow, Green};
// Delay time for red LED. This half-period time determines the flashing frequency for the LED.
private TimeSpan delayRed;
// Delay time for yellow LED. This half-period time determines the flashing frequency for the LED.
private TimeSpan delayYellow;
// Delay time for green LED. This half-period time determines the flashing frequency for the LED.
private TimeSpan delayGreen;
// Mutex used to protect access to delay times.
private Mutex mutex = new Mutex();
// This EventWaitHandle is set each time a delay has been changed. Used to inform TaskLcd about changes.
private EventWaitHandle delayChangedEvent;
// Ctor.
// parameters:
// delayRed - initial delay for red LED
// delayYellow - initial delay for yellow LED
// delayGreen - initial delay for green LED
// delayChangedEvent - EventWaitHandle to be signaled upon changes of delay times.
public StatusPool(TimeSpan delayRed, TimeSpan delayYellow, TimeSpan delayGreen, EventWaitHandle delayChangedEvent)
{
this.delayRed = delayRed;
this.delayYellow = delayYellow;
this.delayGreen = delayGreen;
this.delayChangedEvent = delayChangedEvent;
}
public TimeSpan GetDelayRed() { return GetDelay(Color.Red); }
public void SetDelayRed(TimeSpan delay) { SetDelay(Color.Red, delay); }
public TimeSpan GetDelayYellow() { return GetDelay(Color.Yellow); }
public void SetDelayYellow(TimeSpan delay) { SetDelay(Color.Yellow, delay); }
public TimeSpan GetDelayGreen() { return GetDelay(Color.Green); }
public void SetDelayGreen(TimeSpan delay) { SetDelay(Color.Green, delay); }
private void SetDelay (Color color, TimeSpan delay)
{
mutex.WaitOne();
bool delayChanged = false;
switch (color)
{
case Color.Red: if (delay != delayRed) { delayRed = delay; delayChanged = true; } break;
case Color.Yellow: if (delay != delayYellow) { delayYellow = delay; delayChanged = true; } break;
case Color.Green: if (delay != delayGreen) { delayGreen = delay; delayChanged = true; } break;
}
mutex.ReleaseMutex();
if (delayChanged)
{
delayChangedEvent.Set();
}
}
private TimeSpan GetDelay (Color color)
{
TimeSpan delay;
mutex.WaitOne();
switch (color)
{
case Color.Red: delay = delayRed; break;
case Color.Yellow: delay = delayYellow; break;
case Color.Green: delay = delayGreen; break;
default: delay = TimeSpan.FromSeconds(1); break;
}
mutex.ReleaseMutex();
return delay;
}
}
}