/
GpioCharacterDisplay.cs
400 lines (352 loc) · 13.3 KB
/
GpioCharacterDisplay.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
using Meadow.Hardware;
using Meadow.Peripherals.Displays;
using System;
using System.Threading;
namespace Meadow.Foundation.Displays.Lcd
{
/// <summary>
/// Represents a GPIO character display
/// </summary>
public class GpioCharacterDisplay : ICharacterDisplay, IDisposable
{
/// <summary>
/// Is the object disposed
/// </summary>
public bool IsDisposed { get; private set; }
readonly bool createdPorts = false;
private readonly byte LCD_LINE_1 = 0x80; // # LCD RAM address for the 1st line
private readonly byte LCD_LINE_2 = 0xC0; // # LCD RAM address for the 2nd line
private readonly byte LCD_LINE_3 = 0x94; // # LCD RAM address for the 3rd line
private readonly byte LCD_LINE_4 = 0xD4; // # LCD RAM address for the 4th line
private byte cursorLine = 0;
private byte cursorColumn = 0;
private const byte LCD_SETDDRAMADDR = 0x80;
private const byte LCD_SETCGRAMADDR = 0x40;
readonly IPwmPort? LCD_V0;
readonly IDigitalOutputPort LCD_E;
readonly IDigitalOutputPort LCD_RS;
readonly IDigitalOutputPort LCD_D4;
readonly IDigitalOutputPort LCD_D5;
readonly IDigitalOutputPort LCD_D6;
readonly IDigitalOutputPort LCD_D7;
readonly bool LCD_INSTRUCTION = false;
readonly bool LCD_DATA = true;
static readonly object _lock = new();
/// <summary>
/// The text display menu configuration
/// </summary>
public TextDisplayConfig DisplayConfig { get; protected set; }
/// <summary>
/// Create a new GpioCharacterDisplay
/// </summary>
/// <param name="pinRS">The RS pin</param>
/// <param name="pinE">The E pin</param>
/// <param name="pinD4">The D4 pin</param>
/// <param name="pinD5">The D5 pin</param>
/// <param name="pinD6">The D6 pin</param>
/// <param name="pinD7">The D7 pin</param>
/// <param name="rows">The number of character rows</param>
/// <param name="columns">The number of character columns</param>
public GpioCharacterDisplay(
IPin pinRS,
IPin pinE,
IPin pinD4,
IPin pinD5,
IPin pinD6,
IPin pinD7,
byte rows = 4, byte columns = 20) :
this(
pinRS.CreateDigitalOutputPort(),
pinE.CreateDigitalOutputPort(),
pinD4.CreateDigitalOutputPort(),
pinD5.CreateDigitalOutputPort(),
pinD6.CreateDigitalOutputPort(),
pinD7.CreateDigitalOutputPort(),
rows, columns)
{
createdPorts = true;
}
/// <summary>
/// Create a new GpioCharacterDisplay object
/// </summary>
/// <param name="portRS">Port for RS pin</param>
/// <param name="portE">Port for W pin</param>
/// <param name="portD4">Port for D4 pin</param>
/// <param name="portD5">Port for D5 pin</param>
/// <param name="portD6">Port for D6 pin</param>
/// <param name="portD7">Port for D7 pin</param>
/// <param name="rows">Number of character rows</param>
/// <param name="columns">Number of character columns</param>
public GpioCharacterDisplay(
IDigitalOutputPort portRS,
IDigitalOutputPort portE,
IDigitalOutputPort portD4,
IDigitalOutputPort portD5,
IDigitalOutputPort portD6,
IDigitalOutputPort portD7,
byte rows = 4, byte columns = 20)
{
DisplayConfig = new TextDisplayConfig { Height = rows, Width = columns };
LCD_RS = portRS;
LCD_E = portE;
LCD_D4 = portD4;
LCD_D5 = portD5;
LCD_D6 = portD6;
LCD_D7 = portD7;
Initialize();
}
/// <summary>
/// Create a new GpioCharacterDisplay object
/// </summary>
/// <param name="pinV0">V0 pin</param>
/// <param name="pinRS">RS pin</param>
/// <param name="pinE">W pin</param>
/// <param name="pinD4">D4 pin</param>
/// <param name="pinD5">D5 pin</param>
/// <param name="pinD6">D6 pin</param>
/// <param name="pinD7">D7 pin</param>
/// <param name="rows">Number of character rows</param>
/// <param name="columns">Number of character columns</param>
public GpioCharacterDisplay(
IPin pinV0,
IPin pinRS,
IPin pinE,
IPin pinD4,
IPin pinD5,
IPin pinD6,
IPin pinD7,
byte rows = 4, byte columns = 20) :
this(
pinV0.CreatePwmPort(new Units.Frequency(IPwmOutputController.DefaultPwmDutyCycle, Units.Frequency.UnitType.Hertz), 0.5f, true),
pinRS.CreateDigitalOutputPort(),
pinE.CreateDigitalOutputPort(),
pinD4.CreateDigitalOutputPort(),
pinD5.CreateDigitalOutputPort(),
pinD6.CreateDigitalOutputPort(),
pinD7.CreateDigitalOutputPort(),
rows, columns)
{ }
/// <summary>
/// Create a new GpioCharacterDisplay object
/// </summary>
/// <param name="portV0">PWM port for backlight</param>
/// <param name="portRS">Port for RS pin</param>
/// <param name="portE">Port for W pin</param>
/// <param name="portD4">Port for D4 pin</param>
/// <param name="portD5">Port for D5 pin</param>
/// <param name="portD6">Port for D6 pin</param>
/// <param name="portD7">Port for D7 pin</param>
/// <param name="rows">Number of character rows</param>
/// <param name="columns">Number of character columns</param>
public GpioCharacterDisplay(
IPwmPort portV0,
IDigitalOutputPort portRS,
IDigitalOutputPort portE,
IDigitalOutputPort portD4,
IDigitalOutputPort portD5,
IDigitalOutputPort portD6,
IDigitalOutputPort portD7,
byte rows = 4, byte columns = 20)
{
DisplayConfig = new TextDisplayConfig { Height = rows, Width = columns };
LCD_V0 = portV0; LCD_V0.Start();
LCD_RS = portRS;
LCD_E = portE;
LCD_D4 = portD4;
LCD_D5 = portD5;
LCD_D6 = portD6;
LCD_D7 = portD7;
Initialize();
}
private void Initialize()
{
SendByte(0x33, LCD_INSTRUCTION); // 110011 Initialize
SendByte(0x32, LCD_INSTRUCTION); // 110010 Initialize
SendByte(0x06, LCD_INSTRUCTION); // 000110 Cursor move direction
SendByte(0x0C, LCD_INSTRUCTION); // 001100 Display On,Cursor Off, Blink Off
SendByte(0x28, LCD_INSTRUCTION); // 101000 Data length, number of lines, font size
SendByte(0x01, LCD_INSTRUCTION); // 000001 Clear display
Thread.Sleep(5);
}
private void SendByte(byte value, bool mode)
{
lock (_lock)
{
LCD_RS.State = (mode);
// high bits
LCD_D4.State = ((value & 0x10) == 0x10);
LCD_D5.State = ((value & 0x20) == 0x20);
LCD_D6.State = ((value & 0x40) == 0x40);
LCD_D7.State = ((value & 0x80) == 0x80);
ToggleEnable();
// low bits
LCD_D4.State = ((value & 0x01) == 0x01);
LCD_D5.State = ((value & 0x02) == 0x02);
LCD_D6.State = ((value & 0x04) == 0x04);
LCD_D7.State = ((value & 0x08) == 0x08);
ToggleEnable();
Thread.Sleep(5);
}
}
private void ToggleEnable()
{
LCD_E.State = false;
LCD_E.State = true;
LCD_E.State = false;
}
private byte GetLineAddress(int line)
{
return line switch
{
0 => LCD_LINE_1,
1 => LCD_LINE_2,
2 => LCD_LINE_3,
3 => LCD_LINE_4,
_ => throw new ArgumentOutOfRangeException(),
};
}
private void SetLineAddress(int line)
{
SendByte(GetLineAddress(line), LCD_INSTRUCTION);
}
/// <summary>
/// Write text to a line
/// </summary>
/// <param name="text">The text to display</param>
/// <param name="lineNumber">The target line</param>
/// <param name="showCursor">If true, show the cursor</param>
public void WriteLine(string text, byte lineNumber, bool showCursor = false)
{
SetLineAddress(lineNumber);
// Instead of clearing the line first, pad it with empty space on the end
var screenText = text.PadRight(DisplayConfig.Width, ' ');
if (screenText.Length > DisplayConfig.Width)
{
screenText = screenText.Substring(0, DisplayConfig.Width);
}
var bytes = System.Text.Encoding.UTF8.GetBytes(screenText);
foreach (var b in bytes)
{
SendByte(b, LCD_DATA);
}
}
/// <summary>
/// Write a string to the display
/// </summary>
/// <param name="text">The text to show as a string</param>
public void Write(string text)
{
string screentText = text;
if (screentText.Length + cursorColumn > DisplayConfig.Width)
{
screentText = screentText.Substring(0, DisplayConfig.Width - cursorColumn);
}
var bytes = System.Text.Encoding.UTF8.GetBytes(screentText);
foreach (var b in bytes)
{
SendByte(b, LCD_DATA);
}
}
/// <summary>
/// Set the cursor position
/// </summary>
/// <param name="column">The cursor column</param>
/// <param name="line">The cursor line</param>
public void SetCursorPosition(byte column, byte line)
{
if (column >= DisplayConfig.Width || line >= DisplayConfig.Height)
{
throw new Exception($"CharacterDisplay: cursor out of bounds {column}, {line}");
}
cursorColumn = column;
cursorLine = line;
byte lineAddress = GetLineAddress(line);
var address = column + lineAddress;
SendByte(((byte)(LCD_SETDDRAMADDR | address)), LCD_INSTRUCTION);
}
/// <summary>
/// Clear all lines
/// </summary>
public void ClearLines()
{
SendByte(0x01, LCD_INSTRUCTION);
SetCursorPosition(1, 0);
Thread.Sleep(5);
}
/// <summary>
/// Clear a line of text
/// </summary>
/// <param name="lineNumber">The line to clear (0 indexed)</param>
public void ClearLine(byte lineNumber)
{
SetLineAddress(lineNumber);
for (int i = 0; i < DisplayConfig.Width; i++)
{
Write(" ");
}
}
/// <summary>
/// Set the display contrast
/// </summary>
/// <param name="contrast">The contrast as a float (0-1)</param>
/// <exception cref="ArgumentOutOfRangeException"></exception>
public void SetContrast(float contrast = 0.5f)
{
if (contrast < 0 || contrast > 1)
{
throw new ArgumentOutOfRangeException(nameof(contrast), "err: contrast must be between 0 and 1, inclusive.");
}
Resolver.Log.Info($"Contrast: {contrast}");
if (LCD_V0 != null)
{
LCD_V0.DutyCycle = contrast;
}
}
/// <summary>
/// Save a custom character to the display
/// </summary>
/// <param name="characterMap">The character data</param>
/// <param name="address">The display character address (0-7)</param>
public void SaveCustomCharacter(byte[] characterMap, byte address)
{
address &= 0x7; // we only have 8 locations 0-7
SendByte((byte)(LCD_SETCGRAMADDR | (address << 3)), LCD_INSTRUCTION);
for (var i = 0; i < 8; i++)
{
SendByte(characterMap[i], LCD_DATA);
}
}
/// <summary>
/// Update the display
/// </summary>
public void Show()
{ //can safely ignore
} //required for ITextDisplayMenu
///<inheritdoc/>
public void Dispose()
{
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Dispose of the object
/// </summary>
/// <param name="disposing">Is disposing</param>
protected virtual void Dispose(bool disposing)
{
if (!IsDisposed)
{
if (disposing && createdPorts)
{
LCD_E.Dispose();
LCD_RS.Dispose();
LCD_D4.Dispose();
LCD_D5.Dispose();
LCD_D6.Dispose();
LCD_D7.Dispose();
}
IsDisposed = true;
}
}
}
}