forked from Montellese/xbmc-on-imon
-
Notifications
You must be signed in to change notification settings - Fork 5
/
DisplayHandler.cs
472 lines (372 loc) · 15.5 KB
/
DisplayHandler.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
using System;
using System.Collections.Generic;
using System.ComponentModel;
using iMon.DisplayApi;
using iMon.XBMC.Properties;
using System.Threading;
using System.Timers;
namespace iMon.XBMC
{
internal partial class DisplayHandler : BackgroundWorker
{
#region Private variables
private Semaphore semReady;
private Semaphore semWork;
private iMonWrapperApi imon;
private bool lcd;
private bool vfd;
private object displayLock = new object();
private object queueLock = new object();
private List<Text> queue;
private int position;
private Dictionary<iMonLcdIcons, bool> icons;
private System.Timers.Timer discRotation;
private const int DefaultDelay = 1000;
private const string LoggingArea = "Display Handler";
private const bool LogDisplayingIcons = false;
private const bool LogDisplayingDiscIcons = false;
#endregion
#region Public variables
#endregion
#region Constructor
public DisplayHandler(iMonWrapperApi imon)
{
if (imon == null)
{
throw new ArgumentNullException("imon");
}
this.imon = imon;
this.imon.StateChanged += stateChanged;
this.queue = new List<Text>();
this.icons = new Dictionary<iMonLcdIcons, bool>(Enum.GetValues(typeof(iMonLcdIcons)).Length);
foreach (iMonLcdIcons icon in Enum.GetValues(typeof(iMonLcdIcons)))
{
this.icons.Add(icon, false);
}
this.discRotation = new System.Timers.Timer();
this.discRotation.AutoReset = true;
this.discRotation.Interval = 300;
this.discRotation.Elapsed += discRotationElapsed;
this.WorkerReportsProgress = false;
this.WorkerSupportsCancellation = true;
this.semReady = new Semaphore(0, 1);
this.semWork = new Semaphore(0, 1);
}
#endregion
#region Overrides of BackgroundWorker
protected override void OnDoWork(DoWorkEventArgs e)
{
while (!this.CancellationPending)
{
// Wait until a connection has been established
this.semReady.WaitOne();
Logging.Log(LoggingArea, "Start working");
if (this.lcd)
{
foreach (KeyValuePair<iMonLcdIcons, bool> icon in this.icons)
{
this.imon.LCD.Icons.Set(icon.Key, icon.Value);
}
}
if (this.queue.Count > 0)
{
this.display(this.queue[0]);
if (this.queue.Count > 1)
{
this.position = 1;
}
}
while (!this.CancellationPending && (this.lcd || this.vfd))
{
this.semWork.WaitOne();
lock (this.queueLock)
{
if (this.position >= this.queue.Count)
{
this.position = 0;
}
this.display(this.queue[this.position]);
if (this.queue.Count > this.position + 1)
{
this.position += 1;
}
}
}
Logging.Log(LoggingArea, "Stop working");
}
Logging.Log(LoggingArea, "Cancelled");
this.imon.LCD.ScrollFinished -= lcdScrollFinished;
}
#endregion
#region Public functions
public void SetText(string text)
{
this.SetText(text, text, string.Empty, DefaultDelay);
}
public void SetText(string text, int delay)
{
// TODO: Indroduce line breaker to be able to correctly (as desired) display 2 lines on VFD displays
this.SetText(text, text, string.Empty, delay);
}
public void SetText(string lcd, string vfdUpper, string vfdLower)
{
this.SetText(lcd, vfdUpper, vfdLower, DefaultDelay);
}
public void SetText(string lcd, string vfdUpper, string vfdLower, int delay)
{
lock (this.queueLock)
{
Logging.Log(LoggingArea, "DisplayHandler.SetText(" + lcd + ", " + vfdUpper + ", " + vfdLower + ", " + delay.ToString() + ")");
this.queue.Clear();
this.queue.Add(new Text(lcd, vfdUpper, vfdLower, delay));
this.position = 0;
this.update();
}
}
public void AddText(string text)
{
this.AddText(text, text, string.Empty, DefaultDelay);
}
public void AddText(string text, int delay)
{
this.AddText(text, text, string.Empty, delay);
}
public void AddText(string lcd, string vfdUpper, string vfdLower)
{
this.AddText(lcd, vfdUpper, vfdLower, DefaultDelay);
}
public void AddText(string lcd, string vfdUpper, string vfdLower, int delay)
{
lock (this.queueLock)
{
Logging.Log(LoggingArea, "Adding text \"" + lcd + "\" to the queue with delay " + delay.ToString());
this.queue.Add(new Text(lcd, vfdUpper, vfdLower, delay));
Logging.Log(LoggingArea, "Queue length after addition: " + this.queue.Count.ToString());
if (this.queue.Count == 1)
{
this.update();
}
}
}
public void SetProgress(int position, int total)
{
//Logging.Log(LoggingArea, "Trying to set the progress bar to " + position.ToString() + "/" + total.ToString());
if (this.lcd)
{
Logging.Log(LoggingArea, "Setting the progress bar to " + position.ToString() + "/" + total.ToString());
this.imon.LCD.SetProgress(position, total);
}
}
public void SetProgress(TimeSpan position, TimeSpan total)
{
//this.imon.LCD.SetProgress(Convert.ToInt32(position.TotalMilliseconds), Convert.ToInt32(total.TotalMilliseconds));
this.SetProgress(Convert.ToInt32(position.TotalSeconds), Convert.ToInt32(total.TotalSeconds));
}
public void SetIcon(iMonLcdIcons icon, bool show)
{
if (LogDisplayingIcons)
Logging.Log(LoggingArea, "Trying to set LCD icon " + icon + " to " + show);
this.icons[icon] = show;
if (this.lcd)
{
if (LogDisplayingIcons)
Logging.Log(LoggingArea, "Setting LCD icon " + icon + " to " + show);
this.imon.LCD.Icons.Set(icon, show);
}
}
public void SetIcons(IEnumerable<iMonLcdIcons> iconList, bool show)
{
foreach (iMonLcdIcons icon in iconList)
{
if (LogDisplayingIcons)
Logging.Log(LoggingArea, "Trying to set LCD icon " + icon + " to " + show);
if (this.lcd)
{
if (LogDisplayingIcons)
Logging.Log(LoggingArea, "Setting LCD icon " + icon + " to " + show);
}
this.icons[icon] = show;
}
if (this.lcd)
{
this.imon.LCD.Icons.Set(iconList, show);
}
}
public void HideAllIcons()
{
if (LogDisplayingIcons)
Logging.Log(LoggingArea, "Trying to hide all LCD icons");
foreach (iMonLcdIcons icon in Enum.GetValues(typeof(iMonLcdIcons)))
{
this.icons[icon] = false;
}
if (this.lcd)
{
if (LogDisplayingIcons)
Logging.Log(LoggingArea, "Hiding all LCD icons");
this.imon.LCD.Icons.HideAll();
}
}
public void ShowDisc(bool bottomCircle)
{
this.PauseDisc();
List<iMonLcdIcons> iconList = new List<iMonLcdIcons>() { iMonLcdIcons.DiscBottomLeft, iMonLcdIcons.DiscBottomCenter,
iMonLcdIcons.DiscBottomRight, iMonLcdIcons.DiscMiddleLeft,
iMonLcdIcons.DiscMiddleRight, iMonLcdIcons.DiscTopLeft,
iMonLcdIcons.DiscTopCenter, iMonLcdIcons.DiscTopRight };
if (bottomCircle)
{
iconList.Add(iMonLcdIcons.DiscCircle);
}
this.SetIcons(iconList, true);
}
public void HideDisc()
{
this.PauseDisc();
List<iMonLcdIcons> iconList = new List<iMonLcdIcons>() { iMonLcdIcons.DiscBottomLeft, iMonLcdIcons.DiscBottomCenter,
iMonLcdIcons.DiscBottomRight, iMonLcdIcons.DiscMiddleLeft,
iMonLcdIcons.DiscMiddleRight, iMonLcdIcons.DiscTopLeft,
iMonLcdIcons.DiscTopCenter, iMonLcdIcons.DiscTopRight,
iMonLcdIcons.DiscCircle };
this.SetIcons(iconList, false);
}
public void RotateDisc(bool bottomCircle)
{
this.HideDisc();
List<iMonLcdIcons> iconList = new List<iMonLcdIcons>() { iMonLcdIcons.DiscBottomLeft, iMonLcdIcons.DiscBottomRight,
iMonLcdIcons.DiscTopRight, iMonLcdIcons.DiscTopLeft };
if (bottomCircle)
{
iconList.Add(iMonLcdIcons.DiscCircle);
}
this.SetIcons(iconList, true);
this.discRotation.Start();
}
public void PauseDisc()
{
this.discRotation.Stop();
}
#endregion
#region Event handlers
private void update()
{
if (!this.imon.IsInitialized)
{
Logging.Error(LoggingArea, "iMON not initialized.");
return;
}
try
{
this.semWork.Release();
}
catch (SemaphoreFullException)
{
Logging.Error(LoggingArea, "Error: 'SemaphoreFullException' for semWork in DisplayHandler.update()");
}
}
private void stateChanged(object sender, iMonStateChangedEventArgs e)
{
lock (this.displayLock)
{
if (e.IsInitialized)
{
iMonDisplayType display = this.imon.DisplayType;
if ((display & iMonDisplayType.LCD) == iMonDisplayType.LCD)
{
this.imon.LCD.ScrollFinished += lcdScrollFinished;
this.lcd = true;
}
if ((display & iMonDisplayType.VFD) == iMonDisplayType.VFD)
{
this.vfd = true;
}
try
{
this.semReady.Release();
}
catch (SemaphoreFullException)
{
Logging.Error(LoggingArea, "Error: 'SemaphoreFullException' for semReady in DisplayHandler.stateChanged()");
}
}
else
{
this.lcd = false;
this.vfd = false;
this.update();
}
}
}
private void lcdScrollFinished(object sender, EventArgs e)
{
Thread.Sleep(Settings.Default.ImonLcdScrollingDelay);
Logging.Log(LoggingArea, "Scrolling finished");
lock (this.queueLock)
{
if (this.position >= this.queue.Count)
{
this.position = 0;
}
if (this.queue.Count == 0)
{
return;
}
this.update();
}
}
private void discRotationElapsed(object sender, ElapsedEventArgs e)
{
List<iMonLcdIcons> hideIcons = new List<iMonLcdIcons>();
List<iMonLcdIcons> showIcons = new List<iMonLcdIcons>();
if (this.icons[iMonLcdIcons.DiscBottomLeft])
{
hideIcons.AddRange(new iMonLcdIcons[] { iMonLcdIcons.DiscBottomLeft, iMonLcdIcons.DiscBottomRight,
iMonLcdIcons.DiscTopRight, iMonLcdIcons.DiscTopLeft });
showIcons.AddRange(new iMonLcdIcons[] { iMonLcdIcons.DiscBottomCenter, iMonLcdIcons.DiscMiddleRight,
iMonLcdIcons.DiscTopCenter, iMonLcdIcons.DiscMiddleLeft });
}
else
{
hideIcons.AddRange(new iMonLcdIcons[] { iMonLcdIcons.DiscBottomCenter, iMonLcdIcons.DiscMiddleRight,
iMonLcdIcons.DiscTopCenter, iMonLcdIcons.DiscMiddleLeft });
showIcons.AddRange(new iMonLcdIcons[] { iMonLcdIcons.DiscBottomLeft, iMonLcdIcons.DiscBottomRight,
iMonLcdIcons.DiscTopRight, iMonLcdIcons.DiscTopLeft });
}
this.SetIcons(hideIcons, false);
this.SetIcons(showIcons, true);
}
#endregion
#region Private functions
private void display(Text text)
{
bool shown = false;
Logging.Log(LoggingArea, "Trying to display a text");
lock (this.displayLock)
{
if (this.lcd)
{
Logging.Log(LoggingArea, "LCD.SetText: " + text.Lcd);
shown = this.imon.LCD.SetText(text.Lcd.Substring(0, text.Lcd.Length <= 256 ? text.Lcd.Length : 256));
}
if (this.vfd)
{
Logging.Log(LoggingArea, "VFD.SetText: " + text.VfdUpper + "; " + text.VfdLower);
shown = this.imon.VFD.SetText(text.VfdUpper, text.VfdLower);
}
if (shown)
{
if (text.Delay > 0)
{
Logging.Log(LoggingArea, "Showing text for " + text.Delay + "ms");
Thread.Sleep(text.Delay);
}
}
else
{
Logging.Error(LoggingArea, "The text was not displayed succesfully");
}
}
}
#endregion
}
}