forked from dotnet/iot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
496 lines (447 loc) · 15.4 KB
/
Program.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
489
490
491
492
493
494
495
496
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Device.Spi;
using System.Device.Gpio;
using System.Device.Pwm.Drivers;
using System.IO;
using System.Device.Pwm;
using System;
using System.Threading;
using System.Threading.Tasks;
using System.Globalization;
using System.Drawing;
using Iot.Device.Display;
using Iot.Device.Ft4222;
using Iot.Device.CharacterLcd;
using Iot.Device.Graphics;
using Iot.Device.Graphics.SkiaSharpAdapter;
SkiaSharpAdapter.Register();
Console.WriteLine("Hello PCD8544, screen of Nokia 5110!");
Console.WriteLine("Please select the platform you want to use:");
Console.WriteLine(" 1. Native device like a Raspberry Pi");
Console.WriteLine(" 2. FT4222");
var platformChoice = Console.ReadKey();
Console.WriteLine();
if (platformChoice is not object or { KeyChar: not ('1' or '2') })
{
Console.WriteLine("You have to choose a valid platform");
return;
}
SpiConnectionSettings spiConnection = new(0, 1) { ClockFrequency = 5_000_000, Mode = SpiMode.Mode0, DataFlow = DataFlow.MsbFirst, ChipSelectLineActiveState = PinValue.Low };
Pcd8544 lcd;
int pwmPin = -1;
Console.WriteLine("Which pin/channel do you want to use for PWM? (use negative number for none)");
var pinChoice = Console.ReadLine();
try
{
pwmPin = Convert.ToInt32(pinChoice);
}
catch
{
Console.WriteLine("Can't convert the pin number, will assume you don't want to use PWM.");
}
int resetPin = -1;
Console.WriteLine("Which pin do you want to use for Reset? (use negative number for none)");
pinChoice = Console.ReadLine();
try
{
resetPin = Convert.ToInt32(pinChoice);
}
catch
{
Console.WriteLine("Can't convert the pin number, will assume you don't want to use Reset.");
}
int dataCommandPin = -1;
Console.WriteLine("Which pin do you want to use for Data Command?");
pinChoice = Console.ReadLine();
try
{
dataCommandPin = Convert.ToInt32(pinChoice);
}
catch
{
}
if (dataCommandPin < 0)
{
Console.WriteLine("Can't continue as Data Command pin has to be valid.");
return;
}
GpioController? gpio = null;
if (platformChoice.KeyChar == '1')
{
PwmChannel? pwmChannel = pwmPin >= 0 ? PwmChannel.Create(0, pwmPin) : null;
SpiDevice spi = SpiDevice.Create(spiConnection);
lcd = new(dataCommandPin, spi, resetPin, pwmChannel);
}
else
{
gpio = new(PinNumberingScheme.Logical, new Ft4222Gpio());
Ft4222Spi ft4222Spi = new(spiConnection);
SoftwarePwmChannel? softPwm = pwmPin >= 0 ? new(pwmPin, 1000, usePrecisionTimer: true, controller: gpio, shouldDispose: false, dutyCycle: 0) : null;
lcd = new(dataCommandPin, ft4222Spi, resetPin, softPwm, gpio, false);
}
if (lcd is not object)
{
Console.WriteLine("Something went wrong, can't initialize PCD8544");
return;
}
lcd.Enabled = true;
Console.WriteLine("Choose the demonstration you want to see:");
Console.WriteLine(" 1. All");
Console.WriteLine(" 2. Brightness, Contrast, Temperature, Bias");
Console.WriteLine(" 3. Display text, change cursor position");
Console.WriteLine(" 4. Display images, resize images");
Console.WriteLine(" 5. Display lines, points, rectangles");
Console.WriteLine(" 6. Use the LcdConsole and display texts");
var demoChoice = Console.ReadKey();
Console.WriteLine();
if (demoChoice is not object or { KeyChar: < '1' or > '6' })
{
Console.WriteLine("You have to choose a demonstration");
return;
}
switch (demoChoice.KeyChar)
{
case '1':
BrightnessContrastTemperatureBias();
DisplayTextChangePositionBlink();
DisplayingBitmap();
DisplayLinesPointsRectabngles();
LcdConsole();
break;
case '2':
BrightnessContrastTemperatureBias();
break;
case '3':
DisplayTextChangePositionBlink();
break;
case '4':
DisplayingBitmap();
break;
case '5':
DisplayLinesPointsRectabngles();
break;
case '6':
LcdConsole();
break;
}
void BrightnessContrastTemperatureBias()
{
Console.WriteLine("Adjusting brightness from 0 to 1.0");
for (int i = 0; i < 10; i++)
{
lcd.SetCursorPosition(0, 0);
lcd.WriteLine("Test for brightness");
lcd.Write($"{i * 10} %");
lcd.BacklightBrightness = i / 10.0f;
Thread.Sleep(1000);
}
lcd.Clear();
Console.WriteLine("Reseting brightness to 0.2 (20%)");
lcd.BacklightBrightness = 0.2f;
Console.WriteLine("Adjusting contrast from 0 to 127");
for (byte i = 0; i < 128; i++)
{
lcd.SetCursorPosition(0, 0);
lcd.WriteLine("Test for contrast");
lcd.Write($"{i}");
lcd.Contrast = i;
Thread.Sleep(100);
}
Console.WriteLine("Reseting contrast to recommended value 40");
lcd.Contrast = 40;
lcd.Clear();
Console.WriteLine("Testing the 4 different temperature modes");
lcd.WriteLine("Test temp 0");
lcd.Temperature = Iot.Device.Display.Pcd8544Enums.ScreenTemperature.Coefficient0;
Thread.Sleep(1500);
lcd.WriteLine("Test temp 1");
lcd.Temperature = Iot.Device.Display.Pcd8544Enums.ScreenTemperature.Coefficient1;
Thread.Sleep(1500);
lcd.WriteLine("Test temp 2");
lcd.Temperature = Iot.Device.Display.Pcd8544Enums.ScreenTemperature.Coefficient2;
Thread.Sleep(1500);
lcd.WriteLine("Test temp 3");
lcd.Temperature = Iot.Device.Display.Pcd8544Enums.ScreenTemperature.Coefficient3;
Thread.Sleep(1500);
lcd.Temperature = Iot.Device.Display.Pcd8544Enums.ScreenTemperature.Coefficient0;
lcd.Clear();
Console.WriteLine("Adjusting bias from 0 to 6");
// Adjusting the bias
for (byte i = 0; i < 7; i++)
{
// Adjusting the bias
lcd.Bias = i;
lcd.SetCursorPosition(0, 0);
lcd.WriteLine("Adjusting bias");
lcd.Write($"bias = {i}");
Thread.Sleep(2000);
lcd.Clear();
}
Console.WriteLine("Reseting bias to recommended value 4");
lcd.Bias = 4;
}
void DisplayTextChangePositionBlink()
{
Console.WriteLine("Display is on and will switch on and off");
lcd.Write("Display is on and will switch on and off");
for (int i = 0; i < 5; i++)
{
Thread.Sleep(1000);
lcd.Enabled = !lcd.Enabled;
}
lcd.Enabled = true;
lcd.Clear();
Console.WriteLine("Displaying multi line with WriteLine");
lcd.SetCursorPosition(0, 0);
lcd.Write("First line");
lcd.SetCursorPosition(0, 1);
lcd.Write("Second one");
lcd.SetCursorPosition(0, 2);
lcd.Write("3rd");
lcd.SetCursorPosition(0, 3);
lcd.Write("Guess!");
lcd.SetCursorPosition(0, 4);
lcd.Write("One more...");
lcd.SetCursorPosition(0, 5);
lcd.Write("last line");
Thread.Sleep(1500);
Console.WriteLine("Inverting the color screen");
// this will blink the screen
for (int i = 0; i < 6; i++)
{
lcd.InvertedColors = !lcd.InvertedColors;
Thread.Sleep(1000);
}
Console.WriteLine("Activating the cursor, writting numbers in a raw");
lcd.Clear();
lcd.SetCursorPosition(0, 0);
lcd.UnderlineCursorVisible = true;
for (int i = 0; i < 50; i++)
{
lcd.Write($"{i}");
Thread.Sleep(500);
}
lcd.Clear();
Console.WriteLine("Testing backspace to remove character");
lcd.Write("Basckspace");
for (int i = 0; i < 5; i++)
{
Thread.Sleep(2000);
lcd.Write("\b");
}
Console.WriteLine("Displaying more text and moving the cursor around");
lcd.Clear();
lcd.Write("More text");
Thread.Sleep(1500);
lcd.SetCursorPosition(0, 0);
Thread.Sleep(1500);
lcd.SetCursorPosition(5, 0);
Thread.Sleep(1500);
lcd.SetCursorPosition(0, 5);
Thread.Sleep(1500);
lcd.UnderlineCursorVisible = false;
Console.WriteLine("This will display a line of random bits");
lcd.Clear();
lcd.WriteLine("This will display a line of random characters");
Thread.Sleep(1500);
char[] textToSend = new char[lcd.Size.Height * lcd.Size.Width];
var rand = new Random(123456);
for (int i = 0; i < textToSend.Length; i++)
{
textToSend[i] = (char)rand.Next(0, 255);
}
lcd.Clear();
lcd.SetCursorPosition(0, 0);
lcd.Write(textToSend);
Thread.Sleep(1000);
lcd.Clear();
}
void DisplayingBitmap()
{
Console.WriteLine("Displaying bitmap, text, resizing them");
using BitmapImage bitmapMe = BitmapImage.CreateFromFile(Path.Combine("me.bmp"));
var bitmap1 = BitmapToByteArray(bitmapMe);
using BitmapImage bitmapNokia = BitmapImage.CreateFromFile(Path.Combine("nokia_bw.bmp"));
var bitmap2 = BitmapToByteArray(bitmapNokia);
// Open a non bitmap and resize it
BitmapImage bitmapLarge = BitmapImage.CreateFromFile(Path.Combine("nonbmp.jpg"));
bitmapLarge = bitmapLarge.Resize(Pcd8544.PixelScreenSize);
bitmapLarge.Clear();
var bitmap3 = BitmapToByteArray(bitmapLarge);
for (byte i = 0; i < 2; i++)
{
lcd.Clear();
lcd.Write(" Bonjour .NET IoT!");
Thread.Sleep(2000);
lcd.Clear();
lcd.SetCursorPosition(0, 0);
lcd.Write("This is me");
Thread.Sleep(1000);
// Shows the first bitmap
lcd.SetByteMap(bitmap1);
lcd.Draw();
Thread.Sleep(1500);
lcd.SetCursorPosition(0, 0);
lcd.WriteLine("A nice");
lcd.WriteLine("picture with");
lcd.WriteLine("a heart");
lcd.WriteLine("displayed");
lcd.WriteLine("on the screen");
Thread.Sleep(1000);
// Shows the second bitmap
lcd.SetByteMap(bitmap2);
lcd.Draw();
Thread.Sleep(1500);
lcd.SetCursorPosition(0, 0);
lcd.WriteLine("Large picture");
lcd.WriteLine("resized and");
lcd.WriteLine("changed to");
lcd.WriteLine("monochrome");
Thread.Sleep(1000);
// Shows the second bitmap
lcd.SetByteMap(bitmap3);
lcd.Draw();
Thread.Sleep(1500);
}
bitmapLarge.Dispose();
}
void DisplayLinesPointsRectabngles()
{
Console.WriteLine("Drawing point, line and rectangles");
lcd.DrawPoint(5, 5, true);
lcd.DrawLine(0, 0, 15, 35, true);
lcd.DrawRectangle(10, 30, 10, 20, true, true);
lcd.DrawRectangle(12, 32, 6, 16, false, true);
// You should not forget to refresh to draw everything
lcd.Draw();
Thread.Sleep(2000);
lcd.Clear();
Console.WriteLine("Drawing 4 points at the 4 edges");
lcd.DrawPoint(0, 0, true);
lcd.DrawPoint(Pcd8544.PixelScreenSize.Width - 1, 0, true);
lcd.DrawPoint(Pcd8544.PixelScreenSize.Width - 1, Pcd8544.PixelScreenSize.Height - 1, true);
lcd.DrawPoint(0, Pcd8544.PixelScreenSize.Height - 1, true);
lcd.Draw();
Thread.Sleep(2000);
Console.WriteLine("Drawing a rectangle at 2 pixels from the edge");
lcd.DrawRectangle(2, 2, Pcd8544.PixelScreenSize.Width - 4, Pcd8544.PixelScreenSize.Height - 4, true, false);
lcd.Draw();
Thread.Sleep(2000);
lcd.Clear();
Console.WriteLine("Drawing 2 diagonal lines");
lcd.DrawLine(0, 0, Pcd8544.PixelScreenSize.Width - 1, Pcd8544.PixelScreenSize.Height - 1, true);
lcd.DrawLine(0, Pcd8544.PixelScreenSize.Height - 1, Pcd8544.PixelScreenSize.Width - 1, 0, true);
lcd.Draw();
Thread.Sleep(2000);
}
void LcdConsole()
{
LcdConsole console = new LcdConsole(lcd, string.Empty, false);
console.LineFeedMode = LineWrapMode.Truncate;
Console.WriteLine("Nowrap test:");
console.Write("This is a long text that should not wrap and just extend beyond the display");
console.WriteLine("This has CRLF\r\nin it and should \r\n wrap.");
console.Write("This goes to the last line of the display");
console.WriteLine("This isn't printed, because it's off the screen");
Thread.Sleep(1500);
Console.WriteLine("Autoscroll test:");
console.LineFeedMode = LineWrapMode.Wrap;
console.WriteLine();
console.WriteLine("Now the display should move up.");
console.WriteLine("And more up.");
for (int i = 0; i < 20; i++)
{
console.WriteLine($"This is line {i + 1}/{20}, but longer than the screen but you really have to add a lot of text to make it big enough");
Thread.Sleep(500);
}
console.LineFeedMode = LineWrapMode.Wrap;
console.WriteLine("Same again, this time with full wrapping.");
for (int i = 0; i < 20; i++)
{
console.Write($"This is string {i + 1}/{20} longer than the screen but you really have to add a lot of text to make it big enough");
Thread.Sleep(500);
}
Thread.Sleep(1500);
Console.WriteLine("Intelligent wrapping test");
console.LineFeedMode = LineWrapMode.WordWrap;
console.WriteLine("Now intelligent wrapping should wrap this long sentence at word borders and ommit spaces at the start of lines.");
Console.WriteLine("Not wrappable test");
Thread.Sleep(1500);
console.WriteLine("NowThisIsOneSentenceInOneWordThatCannotBeWrappedButStillAppearAllOverUpToTheEnd");
Thread.Sleep(1500);
Console.WriteLine("Individual line test");
console.Clear();
console.LineFeedMode = LineWrapMode.Truncate;
console.ReplaceLine(0, "This is all garbage that will be replaced");
console.ReplaceLine(0, "Running clock test, press enter to continue");
int left = console.Size.Width;
Task? alertTask = null;
// Let the current time move trought the display on line 1
while (!Console.KeyAvailable)
{
DateTime now = DateTime.Now;
String time = String.Format(CultureInfo.CurrentCulture, "{0}", now.ToLongTimeString());
string printTime = time;
if (left > 0)
{
printTime = new string(' ', left) + time;
}
else if (left < 0)
{
printTime = time.Substring(-left);
}
console.ReplaceLine(1, printTime);
left--;
// Each full minute, blink the display (but continue writing the time)
if (now.Second == 0 && alertTask is null)
{
alertTask = console.BlinkDisplayAsync(3);
}
if (alertTask is object && alertTask.IsCompleted)
{
// Ensure we catch any exceptions (there shouldn't be any...)
alertTask.Wait();
alertTask = null;
}
Thread.Sleep(500);
// Restart when the time string has left the display
if (left < -time.Length)
{
left = console.Size.Width;
}
}
alertTask?.Wait();
Console.ReadKey();
console.Dispose();
}
Console.WriteLine("Thank you for your attention!");
lcd.WriteLine("Thank you for your attention!");
Thread.Sleep(2000);
lcd.Dispose();
// In case we're using FT4222, gpio needs to be disposed after the screen
gpio?.Dispose();
byte[] BitmapToByteArray(BitmapImage bitmap)
{
if (bitmap is not object)
{
throw new ArgumentNullException(nameof(bitmap));
}
if ((bitmap.Width != Pcd8544.PixelScreenSize.Width) || (bitmap.Height != Pcd8544.PixelScreenSize.Height))
{
throw new ArgumentException($"{nameof(bitmap)} should be same size as the screen {Pcd8544.PixelScreenSize.Width}x{Pcd8544.PixelScreenSize.Height}");
}
byte[] toReturn = new byte[Pcd8544.ScreenBufferByteSize];
int width = Pcd8544.PixelScreenSize.Width;
Color colWhite = Color.White;
for (int position = 0; position < Pcd8544.ScreenBufferByteSize; position++)
{
byte toStore = 0;
for (int bit = 0; bit < 8; bit++)
{
toStore = (byte)(toStore | ((bitmap[position % width, position / width * 8 + bit] == colWhite ? 0 : 1) << bit));
}
toReturn[position] = toStore;
}
return toReturn;
}