-
-
Notifications
You must be signed in to change notification settings - Fork 114
/
Copy pathHd44780.ExtendedSample.cs
380 lines (342 loc) · 11.8 KB
/
Hd44780.ExtendedSample.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
// 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.Device.I2c;
using System.Diagnostics;
using System.Text;
using System.Threading;
using System.Globalization;
using System.Drawing;
using Iot.Device.Graphics;
namespace Iot.Device.CharacterLcd.Samples
{
internal class ExtendedSample
{
private const string Twenty = "123456789\u0008123456789\u0009";
private const string Thirty = Twenty + "123456789\u000a";
private const string Fourty = Thirty + "123456789\u000b";
private const string Eighty = Fourty + "123456789\u000c123456789\u000d123456789\u000e123456789\u000f";
private static object _myLock = new object();
public static void Test(Hd44780 lcd)
{
Debug.WriteLine("Starting...");
lcd.Clear();
Debug.WriteLine("Initialized");
TestPrompt("SetCursor", lcd, SetCursorTest);
TestPrompt("Underline", lcd, l => l.UnderlineCursorVisible = true);
lcd.UnderlineCursorVisible = false;
TestPrompt("Walker", lcd, WalkerTest);
CreateTensCharacters(lcd);
TestPrompt("CharacterSet", lcd, CharacterSet);
TestPrompt("DisplayEnable", lcd, DisplayAndBackLightOnOff);
// Shifting
TestPrompt("Autoshift", lcd, AutoShift);
TestPrompt("DisplayLeft", lcd, l => ShiftDisplayTest(l, a => a.ShiftDisplayLeft()));
TestPrompt("DisplayRight", lcd, l => ShiftDisplayTest(l, a => a.ShiftDisplayRight()));
TestPrompt("CursorLeft", lcd, l => ShiftCursorTest(l, a => a.ShiftCursorLeft()));
TestPrompt("CursorRight", lcd, l => ShiftCursorTest(l, a => a.ShiftCursorRight()));
// Long string
TestPrompt("Twenty", lcd, l => l.Write(Twenty));
TestPrompt("Fourty", lcd, l => l.Write(Fourty));
TestPrompt("Eighty", lcd, l => l.Write(Eighty));
TestPrompt("Twenty-", lcd, l => WriteFromEnd(l, Twenty));
TestPrompt("Fourty-", lcd, l => WriteFromEnd(l, Fourty));
TestPrompt("Eighty-", lcd, l => WriteFromEnd(l, Eighty));
TestPrompt("Wrap", lcd, l => l.Write(new string('*', 80) + ">>>>>"));
TestPrompt("Perf", lcd, PerfTests);
TestPrompt("Colors", lcd, SetBacklightColorTest);
TestPrompt("Time", lcd, TestClock);
lcd.DisplayOn = false;
lcd.BacklightOn = false;
Debug.WriteLine("Done...");
}
private static void CharacterSet(Hd44780 lcd)
{
StringBuilder sb = new StringBuilder(256);
for (int i = 0; i < 256; i++)
{
sb.Append((char)i);
}
int character = 0;
int line = 0;
Size size = lcd.Size;
while (character < 256)
{
lcd.SetCursorPosition(0, line);
lcd.Write(sb.ToString(character, Math.Min(size.Width, 256 - character)));
line++;
character += size.Width;
if (line >= size.Height)
{
line = 0;
System.Threading.Thread.Sleep(1000);
}
}
}
private static void DisplayAndBackLightOnOff(Hd44780 lcd)
{
lcd.Clear();
lcd.Write("This is some text");
lcd.DisplayOn = false;
Thread.Sleep(1000);
lcd.DisplayOn = true;
lcd.BacklightOn = false;
Thread.Sleep(1000);
lcd.BacklightOn = true;
}
private static void AutoShift(Hd44780 lcd)
{
lcd.AutoShift = true;
Size size = lcd.Size;
lcd.Write(Eighty.Substring(0, size.Width + size.Width / 2));
lcd.AutoShift = false;
}
private static void ShiftTest(Hd44780 lcd, HdFunction action)
{
Size size = lcd.Size;
for (int i = 0; i <= size.Width; i++)
{
action(lcd);
System.Threading.Thread.Sleep(250);
}
}
private static void ShiftDisplayTest(Hd44780 lcd, HdFunction action)
{
Size size = lcd.Size;
lcd.Write(Eighty.Substring(0, size.Height * size.Width));
ShiftTest(lcd, action);
}
private static void ShiftCursorTest(Hd44780 lcd, HdFunction action)
{
lcd.BlinkingCursorVisible = true;
ShiftTest(lcd, action);
lcd.BlinkingCursorVisible = false;
}
private static void WriteFromEnd(Hd44780 lcd, string value)
{
lcd.Increment = false;
lcd.SetCursorPosition(lcd.Size.Width - 1, lcd.Size.Height - 1);
lcd.Write(value);
lcd.Increment = true;
}
private static void WalkerTest(Hd44780 lcd)
{
CreateWalkCharacters(lcd);
string walkOne = new string('\x8', lcd.Size.Width);
string walkTwo = new string('\x9', lcd.Size.Width);
for (int i = 0; i < 5; i++)
{
lcd.SetCursorPosition(0, 0);
lcd.Write(walkOne);
System.Threading.Thread.Sleep(500);
lcd.SetCursorPosition(0, 0);
lcd.Write(walkTwo);
System.Threading.Thread.Sleep(500);
}
}
private static void SetCursorTest(Hd44780 lcd)
{
Size size = lcd.Size;
int number = 0;
for (int i = 0; i < size.Height; i++)
{
lcd.SetCursorPosition(0, i);
lcd.Write($"{number++}");
lcd.SetCursorPosition(size.Width - 1, i);
lcd.Write($"{number++}");
}
}
private static void PerfTests(Hd44780 lcd)
{
string stars = new string('*', 80);
Stopwatch stopwatch = Stopwatch.StartNew();
lcd.Clear();
for (int i = 0; i < 25; i++)
{
lcd.Write(Eighty);
lcd.Write(stars);
}
lcd.Clear();
stopwatch.Stop();
string result = $"Elapsed ms: {stopwatch.ElapsedMilliseconds}";
lcd.Write(result);
Debug.WriteLine(result);
}
private static void SetBacklightColorTest(Hd44780 lcd)
{
if (lcd is not LcdRgb colorLcd)
{
Debug.WriteLine("Color backlight not supported");
return;
}
Color[] colors =
{
Color.Red, Color.Green, Color.Blue, Color.Aqua, Color.Azure,
Color.Brown, Color.Chocolate, Color.LemonChiffon, Color.Lime, Color.Tomato, Color.Yellow
};
foreach (var color in colors)
{
lcd.Clear();
lcd.Write(color.ToHex());
colorLcd.SetBacklightColor(color);
System.Threading.Thread.Sleep(1000);
}
lcd.Clear();
colorLcd.SetBacklightColor(Color.White);
}
/// <summary>
/// A small test that shows something useful. It may not work optimally due to wrong character mappings if the month names contain non-ascii characters.
/// </summary>
private static void TestClock(Hd44780 lcd)
{
using (Timer timer = new Timer(TimerCallbackClock, lcd, 0, 100))
{
// Adjust for something else
Thread.Sleep(Timeout.Infinite);
}
}
private static void TimerCallbackClock(object callback)
{
Hd44780 lcd = (Hd44780)callback;
// The callback may be executed in parallel several times, but the display component is not reentrant!
try
{
Monitor.Enter(_myLock);
var now = DateTime.UtcNow;
lcd.SetCursorPosition(0, 0);
lcd.Write(String.Format("{0:dddd}", now));
lcd.SetCursorPosition(0, 1);
lcd.Write(String.Format("{0:M} {0:yyyy}", now, now));
lcd.SetCursorPosition(0, 2);
lcd.Write("It is now ");
lcd.SetCursorPosition(0, 3);
lcd.Write(String.Format("{0}", now.ToString()));
Monitor.Exit(_myLock);
}
catch (Exception)
{
// Nothing to be done
}
}
private delegate void HdFunction(Hd44780 lcd);
private static void TestPrompt(string test, Hd44780 lcd, HdFunction action)
{
string prompt = $"Test {test}:";
lcd.Clear();
lcd.Write(prompt);
lcd.BlinkingCursorVisible = true;
Debug.Write(prompt);
lcd.BlinkingCursorVisible = false;
lcd.Clear();
action(lcd);
Debug.Write("Test Complete.");
lcd.Clear();
}
private static void CreateWalkCharacters(Hd44780 lcd)
{
// Walk 1
lcd.CreateCustomCharacter(0,
0b_00110,
0b_00110,
0b_01100,
0b_10111,
0b_00100,
0b_01110,
0b_01010,
0b_10001);
// Walk 2
lcd.CreateCustomCharacter(1,
0b_00110,
0b_00110,
0b_01100,
0b_01100,
0b_00110,
0b_00110,
0b_01010,
0b_01010);
}
private static void CreateTensCharacters(Hd44780 lcd)
{
// 10
lcd.CreateCustomCharacter(0,
0b_10000,
0b_10000,
0b_10000,
0b_10000,
0b_10111,
0b_00101,
0b_00101,
0b_00111);
// 20
lcd.CreateCustomCharacter(1,
0b_11100,
0b_00100,
0b_11100,
0b_10000,
0b_11111,
0b_00101,
0b_00101,
0b_00111);
// 30
lcd.CreateCustomCharacter(2,
0b_11100,
0b_00100,
0b_11100,
0b_00100,
0b_11111,
0b_00101,
0b_00101,
0b_00111);
// 40
lcd.CreateCustomCharacter(3,
0b_10100,
0b_10100,
0b_11100,
0b_00100,
0b_00111,
0b_00101,
0b_00101,
0b_00111);
// 50
lcd.CreateCustomCharacter(4,
0b_11100,
0b_10000,
0b_11100,
0b_00100,
0b_11111,
0b_00101,
0b_00101,
0b_00111);
// 60
lcd.CreateCustomCharacter(5,
0b_11100,
0b_10000,
0b_11100,
0b_10100,
0b_11111,
0b_00101,
0b_00101,
0b_00111);
// 70
lcd.CreateCustomCharacter(6,
0b_11100,
0b_00100,
0b_01000,
0b_01000,
0b_01111,
0b_00101,
0b_00101,
0b_00111);
// 80
lcd.CreateCustomCharacter(7,
0b_11100,
0b_10100,
0b_11100,
0b_10100,
0b_11111,
0b_00101,
0b_00101,
0b_00111);
}
}
}