forked from nanoframework/nanoFramework.IoT.Device
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SparkfunLcd.cs
586 lines (515 loc) · 20.3 KB
/
SparkfunLcd.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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
namespace Iot.Device.SparkFunLcd
{
using System;
using System.Device.I2c;
using System.Drawing;
using System.Threading;
using Iot.Device.CharacterLcd;
/// <summary>
/// LCD library for SparkFun RGB Serial Open LCD display (sizes 20x4 or 16x2) with I2C connection
/// for product information see https://www.sparkfun.com/products/16398
/// code based on https://github.com/sparkfun/OpenLCD.
/// </summary>
public partial class SparkFunLcd : ICharacterLcd
{
/// <summary>
/// Default I2C address.
/// </summary>
public const byte DefaultI2cAddress = 0x72;
/// <summary>
/// I2C connection to display.
/// </summary>
private readonly I2cDevice _i2cDevice; // I2C connection
/// <summary>
/// Display status.
/// </summary>
private OpenLcdCommandEnum _displayControl = OpenLcdCommandEnum.DisplayOn | OpenLcdCommandEnum.CursorOff | OpenLcdCommandEnum.BlinkOff;
/// <summary>
/// Display auto-scrolling and flow state.
/// </summary>
private OpenLcdCommandEnum _displayMode = OpenLcdCommandEnum.EntryLeft | OpenLcdCommandEnum.EntryShiftDecrement;
/// <summary>
/// Backing store for <see cref="DisplayOn"/>.
/// </summary>
private bool _displayOn = true;
/// <summary>
/// Backing store for <see cref="UnderlineCursorVisible"/>.
/// </summary>
private bool _underlineCursorVisible = false;
/// <summary>
/// Backing store for <see cref="BlinkingCursorVisible"/>.
/// </summary>
private bool _blinkingCursorVisible = false;
/// <summary>
/// Backing store for <see cref="BacklightOn"/>
/// </summary>
private bool _backlightOn = false;
/// <summary>
/// Backlight color if backlight is on, see also <see cref="BacklightOn"/>.
/// </summary>
private Color _backlightColor = Color.FromArgb(0, 255, 0);
/// <summary>
/// Initializes a new instance of the <see cref="SparkFunLcd" /> class.
/// </summary>
/// <param name="i2cDevice">Existing I2C connection to display.</param>
/// <param name="displaySize">Display size.</param>
public SparkFunLcd(I2cDevice i2cDevice, DisplaySizeEnum displaySize = DisplaySizeEnum.Size20x4)
{
if (i2cDevice == null)
{
throw new ArgumentNullException("i2cDevice", "argument 'i2cDevice' cannot be null");
}
_i2cDevice = i2cDevice;
switch (displaySize)
{
default:
case DisplaySizeEnum.Size20x4:
Size = new CharacterLcd.Size() { Width = 20, Height = 4 };
break;
case DisplaySizeEnum.Size16x2:
Size = new CharacterLcd.Size() { Width = 16, Height = 2 };
break;
}
// initialize display
Transmit(OpenLcdCommandEnum.SpecialCommand);
Transmit((byte)(OpenLcdCommandEnum.DisplayControl | _displayControl));
Transmit(OpenLcdCommandEnum.SpecialCommand);
Transmit((byte)(OpenLcdCommandEnum.EntryModeSet | _displayMode));
Transmit(OpenLcdCommandEnum.SettingCommand);
Transmit(OpenLcdCommandEnum.ClearCommand);
Thread.Sleep(50);
}
/// <summary>
/// Finalizes an instance of the <see cref="SparkFunLcd" /> class.
/// </summary>
~SparkFunLcd()
{
Dispose();
}
/// <summary>
/// Gets the size of the display.
/// </summary>
public CharacterLcd.Size Size { get; private set; }
/// <summary>
/// Gets or sets a value indicating whether the display is turned on.
/// </summary>
public bool DisplayOn
{
get { return _displayOn; }
set { SetDisplayState(value); }
}
/// <summary>
/// Gets or sets a value indicating whether the underline cursor is enabled.
/// </summary>
public bool UnderlineCursorVisible
{
get { return _underlineCursorVisible; }
set { SetCursorUnderlineState(value); }
}
/// <summary>
/// Gets or sets a value indicating whether the cursor is blinking.
/// </summary>
public bool BlinkingCursorVisible
{
get { return _blinkingCursorVisible; }
set { SetCursorBlinkState(value); }
}
/// <summary>
/// Gets the number of custom characters supported.
/// </summary>
public int NumberOfCustomCharactersSupported
{
get { return 8; }
}
/// <summary>
/// Gets or sets a value indicating whether the backlight is turned on.
/// To set the backlight color use <see cref="SetBacklight"/>.
/// </summary>
public bool BacklightOn
{
get
{
return _backlightOn;
}
set
{
_backlightOn = value && (_backlightColor != Color.Black);
SetBacklight(_backlightOn ? _backlightColor : Color.Black);
}
}
/// <summary>
/// Disposes of an instance of the class, implements <see cref="IDisposable"/>.
/// </summary>
public void Dispose()
{
}
/// <summary>
/// Clear display and force cursor to beginning.
/// </summary>
/// <seealso cref="Home"/>
public void Clear()
{
Command(OpenLcdCommandEnum.ClearCommand);
Thread.Sleep(10);
}
/// <summary>
/// Return cursor to beginning of the display, without clearing the display.
/// </summary>
/// <seealso cref="Clear"/>
public void Home()
{
TransmitSpecialCommand(OpenLcdCommandEnum.ReturnHome);
}
/// <summary>
/// Set the cursor position to a particular column and row.
/// </summary>
/// <param name="col">Column 0 to 19.</param>
/// <param name="row">Row 0 to 3.</param>
public void SetCursorPosition(int col, int row)
{
SetCursorPosition((byte)col, (byte)row);
}
/// <summary>
/// Set the cursor position to a particular column and row.
/// </summary>
/// <param name="col">Column 0 to 19.</param>
/// <param name="row">Row 0 to 3.</param>
public void SetCursorPosition(byte col, byte row)
{
int[] row_offsets = { 0x00, 0x40, 0x14, 0x54 };
// keep variables in bounds
row = (byte)Math.Min(row, Size.Width - 1); // row cannot be greater than max rows
// send the command
TransmitSpecialCommand(OpenLcdCommandEnum.SetDdramAddress | (OpenLcdCommandEnum)(col + row_offsets[row]));
}
/// <summary>
/// Create a custom character, eight custom characters are available numbered 0 through 7.
/// After character is created in LCD memory it can be written to display by sending byte in the range 0x0 to 0x7 using <see cref="Write(byte)"/>.
/// </summary>
/// <param name="location">Character number 0 thru 7, 8 locations are available.</param>
/// <param name="characterMap">Byte array for custom character refer to datasheet for specific information, or see https://www.quinapalus.com/hd44780udg.html.</param>
/// <seealso cref="CreateCustomCharacter(int, byte[])"/>
/// <seealso cref="CreateCustomCharacter(int, SpanByte)"/>
public void CreateCustomCharacter(int location, byte[] characterMap)
{
if (location < 0 || location > 7)
{
throw new ArgumentOutOfRangeException("location", "argument location must have range 0 thru 7, but input was '" + location + "'");
}
if (characterMap == null)
{
throw new ArgumentNullException("characterMap", "argument characterMap cannot be null");
}
Transmit(OpenLcdCommandEnum.SettingCommand);
Transmit((byte)(27 + (location & 0x07)));
for (int i = 0; i < characterMap.Length; i++)
{
Transmit(characterMap[i]);
}
Thread.Sleep(50);
}
/// <summary>
/// Create a custom character.
/// </summary>
/// <param name="location">Character number 0 thru 7, 8 locations are available.</param>
/// <param name="characterMap">Byte array for custom character refer to datasheet for specific information.</param>
/// <seealso cref="CreateCustomCharacter(int, byte[])"/>
/// <seealso cref="CreateCustomCharacter(int, SpanByte)"/>
public void CreateCustomCharacter(int location, SpanByte characterMap)
{
CreateCustomCharacter(location, characterMap.ToArray());
}
/// <summary>
/// Write a byte to the display.
/// </summary>
/// <param name="b">Byte to write.</param>
public void Write(byte b)
{
Transmit(b);
Thread.Sleep(10);
}
/// <summary>
/// Write a character buffer to the display.
/// </summary>
/// <param name="buffer">Buffer to write.</param>
public void Write(SpanChar buffer)
{
Write(buffer.ToArray());
}
/// <summary>
/// Write a character buffer to the display.
/// </summary>
/// <param name="buffer">Buffer to write.</param>
public void Write(char[] buffer)
{
Write(buffer, buffer.Length);
}
/// <summary>
/// Write a character buffer to the display.
/// </summary>
/// <param name="buffer">Buffer to write.</param>
/// <param name="bufferSize">Size of buffer to write.</param>
public void Write(char[] buffer, int bufferSize)
{
int writeSize = Math.Min(bufferSize, buffer.Length);
for (int i = 0; i < writeSize; i++)
{
Transmit((byte)buffer[i]);
}
Thread.Sleep(10);
}
/// <summary>
/// Set cursor position and write string.
/// </summary>
/// <param name="col">Column 0 to 19.</param>
/// <param name="row">Row 0 to 3.</param>
/// <param name="str">String to write.</param>
public void Write(byte col, byte row, string str)
{
SetCursorPosition(col, row);
Write(str);
}
/// <summary>
/// Write a string to the display.
/// </summary>
/// <param name="str">String to write.</param>
public void Write(string str)
{
if (string.IsNullOrEmpty(str) == false)
{
char[] buffer = str.ToCharArray();
Write(buffer, buffer.Length);
}
}
/// <summary>
/// Change state of display.
/// </summary>
/// <param name="enable"><c>true</c> to enable display, else <c>false</c> to disable.</param>
public void SetDisplayState(bool enable)
{
if (enable)
{
_displayControl |= OpenLcdCommandEnum.DisplayOn;
}
else
{
OpenLcdCommandEnum tmp = OpenLcdCommandEnum.DisplayOn;
_displayControl &= ~tmp;
}
TransmitSpecialCommand(OpenLcdCommandEnum.DisplayControl | _displayControl);
_displayOn = enable;
}
/// <summary>
/// Set state of cursor.
/// </summary>
/// <param name="enable"><c>true</c> to enable cursor, else <c>false</c> to disable.</param>
public void SetCursorUnderlineState(bool enable)
{
if (enable)
{
_displayControl |= OpenLcdCommandEnum.CursorOn;
}
else
{
OpenLcdCommandEnum tmp = OpenLcdCommandEnum.CursorOn;
_displayControl &= ~tmp;
}
TransmitSpecialCommand(OpenLcdCommandEnum.DisplayControl | _displayControl);
_underlineCursorVisible = enable;
}
/// <summary>
/// Set the state of the blink cursor.
/// </summary>
/// <param name="enable"><c>true</c> to enable cursor blink, else <c>false</c> to disable.</param>
public void SetCursorBlinkState(bool enable)
{
if (enable)
{
_displayControl |= OpenLcdCommandEnum.BlinkOn;
}
else
{
OpenLcdCommandEnum tmp = OpenLcdCommandEnum.BlinkOn;
_displayControl &= ~tmp;
}
TransmitSpecialCommand(OpenLcdCommandEnum.DisplayControl | _displayControl);
_blinkingCursorVisible = enable;
}
/// <summary>
/// Scroll the display multiple characters to the left, without changing the text.
/// </summary>
/// <param name="count">Number of characters to scroll.</param>
public void ScrollDisplayLeft(byte count = 1)
{
TransmitSpecialCommand(OpenLcdCommandEnum.CursorShift | OpenLcdCommandEnum.DisplayMove | OpenLcdCommandEnum.MoveLeft, count);
}
/// <summary>
/// Scroll the display multiple characters to the right, without changing the text.
/// </summary>
/// <param name="count">Number of characters to scroll.</param>
public void ScrollDisplayRight(byte count = 1)
{
TransmitSpecialCommand(OpenLcdCommandEnum.CursorShift | OpenLcdCommandEnum.DisplayMove | OpenLcdCommandEnum.MoveRight, count);
}
/// <summary>
/// Move the cursor multiple characters to the left.
/// </summary>
/// <param name="count">Number of characters to move.</param>
public void MoveCursorLeft(byte count = 1)
{
TransmitSpecialCommand(OpenLcdCommandEnum.CursorShift | OpenLcdCommandEnum.CursorMove | OpenLcdCommandEnum.MoveLeft, count);
}
/// <summary>
/// Move the cursor multiple characters to the right.
/// </summary>
/// <param name="count">Number of characters to move.</param>
public void MoveCursorRight(byte count = 1)
{
TransmitSpecialCommand(OpenLcdCommandEnum.CursorShift | OpenLcdCommandEnum.CursorMove | OpenLcdCommandEnum.MoveRight, count);
}
/// <summary>
/// Set backlight color.
/// </summary>
/// <param name="color">Color to set.</param>
/// see also <see cref="BacklightOn"/>
public void SetBacklight(Color color)
{
Transmit(OpenLcdCommandEnum.SettingCommand);
Transmit(OpenLcdCommandEnum.SetRgbCommand);
Transmit(color.R);
Transmit(color.G);
Transmit(color.B);
_backlightOn = color != Color.Black; // record if backlight state for BacklightOn property
Thread.Sleep(10);
}
/// <summary>
/// Change state of system messages.
/// </summary>
/// <param name="state"><c>true</c> to enable system messages, else <c>false</c> to disable.</param>
public void SetSystemMessagesState(bool state)
{
Transmit(OpenLcdCommandEnum.SettingCommand);
Transmit(state ? OpenLcdCommandEnum.EnableSystemMessageDisplay : OpenLcdCommandEnum.DisableSystemMessageDisplay);
Thread.Sleep(10);
}
/// <summary>
/// Change state of splash screen at power on, setting takes effect at next power on.
/// </summary>
/// <param name="state"><c>true</c> to enable splash screen at power on, else <c>false</c> to disable.</param>
public void SetSplashScreenState(bool state)
{
Transmit(OpenLcdCommandEnum.SettingCommand);
Transmit(state ? OpenLcdCommandEnum.EnableSplashDisplay : OpenLcdCommandEnum.DisableSpashDisplay);
Thread.Sleep(10);
}
/// <summary>
/// Save the current display as the splash screen at power on, setting takes effect at next power on.
/// </summary>
/// <seealso cref="SetSplashScreenState"/>
public void SaveSplashScreen()
{
Transmit(OpenLcdCommandEnum.SettingCommand);
Transmit(OpenLcdCommandEnum.SaveCurrentDisplayAsSplash);
Thread.Sleep(10);
}
/// <summary>
/// Set state of text flow direction.
/// </summary>
/// <param name="textFlowDirection">Text flow direction, note that left to right is the direction common to most Western languages.</param>
public void SetTextFlowDirectionState(TextFlowDirectionEnum textFlowDirection)
{
switch (textFlowDirection)
{
default:
case TextFlowDirectionEnum.LeftToRight:
_displayMode |= OpenLcdCommandEnum.EntryLeft;
break;
case TextFlowDirectionEnum.RightToLeft:
OpenLcdCommandEnum tmp = OpenLcdCommandEnum.EntryLeft;
_displayMode &= ~tmp;
break;
}
TransmitSpecialCommand(OpenLcdCommandEnum.EntryModeSet | _displayMode);
}
/// <summary>
/// Change state of auto-scrolling, when enabled text will be right justified.
/// </summary>
/// <param name="state"><c>true</c> to enable auto-scrolling, else <c>false</c> to disable.</param>
public void SetAutoScrollingState(bool state)
{
if (state)
{
_displayMode |= OpenLcdCommandEnum.EntryShiftIncrement;
}
else
{
OpenLcdCommandEnum tmp = OpenLcdCommandEnum.EntryShiftIncrement;
_displayMode &= ~tmp;
}
TransmitSpecialCommand(OpenLcdCommandEnum.EntryModeSet | _displayMode);
}
/// <summary>
/// Set display contrast, default value is 120.
/// </summary>
/// <param name="contrastValue">New contrast value.</param>
public void SetContrast(byte contrastValue)
{
Transmit(OpenLcdCommandEnum.SettingCommand);
Transmit(OpenLcdCommandEnum.ContrastCommand);
Transmit(contrastValue);
Thread.Sleep(10);
}
/// <summary>
/// Send data to the device.
/// </summary>
/// <param name="data">Data to send.</param>
private void Transmit(byte data)
{
_i2cDevice.WriteByte(data);
}
/// <summary>
/// Send data to the device.
/// </summary>
/// <param name="data">Data to send.</param>
private void Transmit(OpenLcdCommandEnum data)
{
Transmit((byte)data);
}
/// <summary>
/// Send a command to the display.
/// </summary>
/// <param name="command">Command to send.</param>
private void Command(OpenLcdCommandEnum command)
{
Transmit(OpenLcdCommandEnum.SettingCommand);
Transmit(command);
Thread.Sleep(10);
}
/// <summary>
/// Send a special command to the display.
/// </summary>
/// <param name="command">Command to send.</param>
private void TransmitSpecialCommand(OpenLcdCommandEnum command)
{
Transmit(OpenLcdCommandEnum.SpecialCommand);
Transmit(command);
Thread.Sleep(50);
}
/// <summary>
/// Send multiple special commands to the display.
/// </summary>
/// <param name="command">Command to send.</param>
/// <param name="count">Number of times to send.</param>
private void TransmitSpecialCommand(OpenLcdCommandEnum command, byte count)
{
for (int i = 0; i < count; i++)
{
Transmit(OpenLcdCommandEnum.SpecialCommand);
Transmit(command);
}
Thread.Sleep(50);
}
}
}