forked from dotnet/iot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLcdValueUnitDisplay.cs
488 lines (446 loc) · 17.6 KB
/
LcdValueUnitDisplay.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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Iot.Device.Graphics;
namespace Iot.Device.CharacterLcd
{
/// <summary>
/// Displays a value and an unit in a big font on an LCD Display.
/// Requires a display with at least 20x4 characters
/// </summary>
public class LcdValueUnitDisplay
{
private const string BigFontMap = "BigFontMap.txt";
private readonly object _lock;
private readonly ICharacterLcd _lcd;
private readonly CultureInfo _culture;
private readonly Dictionary<char, byte[]> _font;
private char _currentSeparationChar;
private LcdCharacterEncoding? _encoding;
/// <summary>
/// Creates an instance of <see cref="LcdValueUnitDisplay"/>
/// </summary>
/// <param name="lcd">Interface to the display</param>
/// <param name="culture">User culture</param>
public LcdValueUnitDisplay(ICharacterLcd lcd, CultureInfo culture)
{
_lock = new object();
_lcd = lcd;
_culture = culture;
_font = new Dictionary<char, byte[]>();
if (lcd.Size.Width < 20 || lcd.Size.Height < 4)
{
throw new NotSupportedException("This class can only run on displays with at least 20x4 characters.");
}
if (lcd.NumberOfCustomCharactersSupported < 8)
{
throw new NotSupportedException("This class can only run on displays with 8 or more custom character slots");
}
}
/// <summary>
/// Returns the active culture.
/// </summary>
public CultureInfo Culture
{
get
{
return _culture;
}
}
/// <summary>
/// Initializes the display for use as a big-number display.
/// Configures the display with some graphic blocks for the display of big numbers.
/// </summary>
/// <param name="romName">Name of the character Rom, required to properly print culture-specific characters in the small text display</param>
/// <param name="factory">Encoding factory or null</param>
public void InitForRom(string romName, LcdCharacterEncodingFactory? factory = null)
{
if (factory == null)
{
factory = new LcdCharacterEncodingFactory();
}
lock (_lock)
{
// Create the default encoding for the current ROM and culture, but leave the custom characters away.
var encoding = factory.Create(_culture, romName, ' ', 0);
Dictionary<byte, byte[]> specialGraphicsRequired = new Dictionary<byte, byte[]>();
CreateSpecialChars(specialGraphicsRequired);
for (byte i = 0; i < specialGraphicsRequired.Count; i++)
{
_lcd.CreateCustomCharacter(i, specialGraphicsRequired[i]);
}
_currentSeparationChar = ' '; // To make sure the next function doesn't just skip the initialization
LoadSeparationChar(_currentSeparationChar);
_encoding = encoding;
_lcd.BlinkingCursorVisible = false;
_lcd.UnderlineCursorVisible = false;
_lcd.BacklightOn = true;
_lcd.DisplayOn = true;
_lcd.Clear();
ReadCharacterMap();
}
}
/// <summary>
/// Stop showing big characters.
/// This method can be used to signal that the display will be used for different purposes. Before a Display method is used,
/// <see cref="InitForRom"/> needs to be called again. This clears the display.
/// </summary>
public void StopShowing()
{
lock (_lock)
{
_lcd.Clear();
_font.Clear();
_encoding = null;
}
}
private void ReadCharacterMap()
{
_font.Clear();
var assembly = Assembly.GetExecutingAssembly();
// Get our resource file (independent of default namespace of project)
string resourceName = assembly.GetManifestResourceNames().Single(str => str.EndsWith("BigFontMap.txt"));
string mapFile;
using (Stream stream = assembly.GetManifestResourceStream(resourceName)!)
{
using (StreamReader reader = new StreamReader(stream))
{
mapFile = reader.ReadToEnd();
}
}
// -1: init
// 0: char seen, awaiting first line
// 1.. 3 next line
int characterStep = -1;
char currentChar = ' ';
byte[] currentCharMap = new byte[0];
int currentCharMapPos = 0;
// Parse the character map file (syntax see there)
using (StringReader r = new StringReader(mapFile))
{
string? line;
int lineNo = 0;
while ((line = r.ReadLine()) != null)
{
lineNo++;
if (line.StartsWith("//") || string.IsNullOrWhiteSpace(line))
{
continue;
}
if (characterStep == -1)
{
line = line.TrimEnd();
if (line.Length != 2 || line[1] != ':')
{
throw new InvalidDataException($"Line {lineNo}: Expected character followed by ':', found {line}");
}
currentChar = line[0];
characterStep = 0;
continue;
}
string[] splits = line.Split(new char[] { ',' }, StringSplitOptions.None);
if (characterStep == 0)
{
currentCharMap = new byte[splits.Length * 4];
currentCharMapPos = 0;
}
for (int i = 0; i < splits.Length; i++)
{
byte b;
if (!byte.TryParse(splits[i], out b))
{
throw new InvalidDataException($"Line {lineNo}: Expected byte, found {splits[i]}");
}
if (currentCharMapPos >= currentCharMap.Length)
{
throw new InvalidDataException($"Line {lineNo}: Character is expected to have {currentCharMap.Length} bytes, but found more");
}
currentCharMap[currentCharMapPos] = b;
currentCharMapPos++;
}
characterStep++;
if (characterStep == 4)
{
_font.Add(currentChar, currentCharMap);
characterStep = -1;
}
}
}
}
/// <summary>
/// Display the current time
/// </summary>
/// <param name="dateTime">Time to display</param>
/// <param name="format">Time format specifier, default "t" (default short time format with hours and minutes and eventually AM/PM).
/// Anything after the first space in the formatted string is printed as small text. This will for instance be AM/PM when the format specifier "T" is used,
/// since only 6 chars (and two separators) fit on the display.</param>
public void DisplayTime(DateTime dateTime, string format = "t")
{
string toDisplay = dateTime.ToString(format, _culture);
string smallText = string.Empty;
int spaceIdx = toDisplay.IndexOf(' ');
if (spaceIdx > 0)
{
smallText = toDisplay.Substring(spaceIdx + 1);
toDisplay = toDisplay.Substring(0, spaceIdx);
}
StringBuilder[] lines = CreateLinesFromText(toDisplay, smallText, 0, out _);
UpdateDisplay(lines);
}
/// <summary>
/// Display the given value/unit pair. The value must be pre-formatted with the required number of digits, ie. "2.01".
/// The value should only contain one of ".", ":" or ",", or the printed result may be unexpected.
/// </summary>
/// <param name="formattedValue">Pre-formatted value to print</param>
/// <param name="unitText">Unit or name of value. This is printed in normal small font on the bottom right corner of the display. </param>
public void DisplayValue(string formattedValue, string unitText = "")
{
var lines = CreateLinesFromText(formattedValue, unitText, 0, out _);
UpdateDisplay(lines);
}
/// <summary>
/// Scrolls a text in big font trough the display
/// </summary>
/// <param name="text">Text to display</param>
/// <param name="scrollSpeed">Speed between scroll steps (one step being one display cell width)</param>
/// <param name="cancellationToken">Token for cancelling the operation</param>
/// <returns>A task handle</returns>
public Task DisplayBigTextAsync(string text, TimeSpan scrollSpeed, CancellationToken cancellationToken)
{
return Task.Factory.StartNew(() =>
{
int startPosition = 0;
int totalColumnsUsed;
CreateLinesFromText(text, string.Empty, 0, out totalColumnsUsed);
if (totalColumnsUsed == 0)
{
return;
}
while (startPosition < totalColumnsUsed)
{
var displayContent = CreateLinesFromText(text, string.Empty, -startPosition, out _);
UpdateDisplay(displayContent);
if (WaitHandle.WaitAny(new[] { cancellationToken.WaitHandle }, (int)scrollSpeed.TotalMilliseconds) != WaitHandle.WaitTimeout)
{
break;
}
startPosition++;
}
});
}
private StringBuilder[] CreateLinesFromText(string bigText, string smallText, int startPosition, out int totalColumnsUsed)
{
if (_encoding == null)
{
throw new InvalidOperationException("Font and encoding not loaded. Use InitForRom first.");
}
int xPosition = startPosition; // The current x position during drawing. We draw 4-chars high letters, but with variable width
int totalColumnsUsedInternal = 0;
StringBuilder[] ret = new StringBuilder[_lcd.Size.Height];
// Insert a 2-column char
void Insert(int columns, params byte[] ci)
{
int nextByte = 0;
for (int row = 0; row < 4; row++)
{
for (int column = 0; column < columns; column++)
{
// Otherwise, there's no room for this character column
int realColumn = xPosition + column;
if (realColumn < _lcd.Size.Width && realColumn >= 0)
{
byte b = ci[nextByte];
// We use 9 as space, makes the formatting of the character table a bit more consistent, as everything is 1 digit only
if (b == 9)
{
b = 32;
}
ret[row][realColumn] = (char)b;
}
nextByte++;
}
}
xPosition += columns;
totalColumnsUsedInternal += columns;
}
for (int i = 0; i < _lcd.Size.Height; i++)
{
ret[i] = new StringBuilder();
ret[i].Append(new string(' ', _lcd.Size.Width));
}
// Only one of these can be printed simultaneously
if (bigText.Contains(':'))
{
LoadSeparationChar(':');
}
else if (bigText.Contains('.'))
{
LoadSeparationChar('.');
}
else if (bigText.Contains(','))
{
LoadSeparationChar(',');
}
foreach (var c in bigText)
{
if (_font.TryGetValue(c, out byte[]? value))
{
Insert(value.Length / 4, value);
}
}
// Right allign the small text (i.e. an unit)
// It will eventually overwrite the last row of the rightmost digits, but that presumably is still readable.
int unitPosition = _lcd.Size.Width - smallText.Length;
xPosition = Math.Max(unitPosition, 0); // Safety check, if unit is longer than display width
var encodedSmallText = _encoding.GetBytes(smallText);
for (int i = 0; i < Math.Min(encodedSmallText.Length, _lcd.Size.Width); i++)
{
ret[3][xPosition + i] = (char)encodedSmallText[i];
}
totalColumnsUsed = totalColumnsUsedInternal;
return ret;
}
private void UpdateDisplay(StringBuilder[] lines)
{
lock (_lock)
{
for (int i = 0; i < lines.Length; i++)
{
_lcd.SetCursorPosition(0, i);
// Will again do a character translation, but that shouldn't hurt, as all characters in the input strings should be printable now.
_lcd.Write(lines[i].ToString());
}
}
}
/// <summary>
/// Clears the display
/// </summary>
public void Clear()
{
lock (_lock)
{
_lcd.Clear();
}
}
private void CreateSpecialChars(Dictionary<byte, byte[]> graphicChars)
{
graphicChars.Add(0, new byte[]
{
0b00001,
0b00011,
0b00011,
0b00111,
0b00111,
0b01111,
0b01111,
0b11111
});
graphicChars.Add(1, new byte[]
{
0b11111,
0b01111,
0b01111,
0b00111,
0b00111,
0b00011,
0b00011,
0b00001,
});
graphicChars.Add(2, new byte[]
{
0b10000,
0b11000,
0b11000,
0b11100,
0b11100,
0b11110,
0b11110,
0b11111,
});
graphicChars.Add(3, new byte[]
{
0b11111,
0b11110,
0b11110,
0b11100,
0b11100,
0b11000,
0b11000,
0b10000,
});
graphicChars.Add(4, new byte[]
{
0b00000,
0b00000,
0b00000,
0b00000,
0b11111,
0b11111,
0b11111,
0b11111
});
graphicChars.Add(5, new byte[]
{
0b11111,
0b11111,
0b11111,
0b11111,
0b00000,
0b00000,
0b00000,
0b00000,
});
graphicChars.Add(6, new byte[]
{
0b11111,
0b11111,
0b11111,
0b11111,
0b11111,
0b11111,
0b11111,
0b11111,
});
}
/// <summary>
/// Character code 7 is always used for the separation char, which is one of ":", "." or ",".
/// </summary>
/// <param name="separationChar">Separation character</param>
private void LoadSeparationChar(char separationChar)
{
if (separationChar == _currentSeparationChar)
{
return;
}
lock (_lock)
{
switch (separationChar)
{
case ':':
_lcd.CreateCustomCharacter(7,
new byte[] { 0b00000, 0b00000, 0b01110, 0b01110, 0b01110, 0b00000, 0b00000, 0b00000 });
break;
case '.':
_lcd.CreateCustomCharacter(7,
new byte[] { 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b01110, 0b01110, 0b01110 });
break;
case ',':
_lcd.CreateCustomCharacter(7,
new byte[] { 0b00000, 0b00000, 0b00000, 0b00000, 0b01110, 0b01110, 0b01100, 0b01000 });
break;
default:
throw new NotImplementedException("Unknown separation char: " + separationChar);
}
_currentSeparationChar = separationChar;
}
}
}
}