-
Notifications
You must be signed in to change notification settings - Fork 598
/
Copy pathLarge4Digit14SegmentDisplay.cs
330 lines (284 loc) · 12 KB
/
Large4Digit14SegmentDisplay.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
// 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.Runtime.InteropServices;
namespace Iot.Device.Display
{
/// <summary>
/// PIMORONI FOUR LETTER PHAT 14 segment display
/// </summary>
/// <remarks>
/// https://shop.pimoroni.com/products/four-letter-phat
/// Sources:
/// Derived from /src/devices/Display/Large4Digit7SegmentDisplay.cs
/// https://github.com/adafruit/Adafruit_LED_Backpack/blob/master/Adafruit_LEDBackpack.cpp
/// https://github.com/sobek1985/Adafruit_LEDBackpack/blob/master/Adafruit_LEDBackpack/AlphaNumericFourCharacters.cs
/// </remarks>
public partial class Large4Digit14SegmentDisplay : Ht16k33
{
#region Private members
#region Constants
/// <summary>
/// Number of digits supported by display
/// </summary>
private const int MaxNumberOfDigits = 4;
#endregion
#region Enums
/// <summary>
/// Digit address within display buffer
/// </summary>
private enum Address
{
/// <summary>
/// First digit
/// </summary>
Digit1 = 1,
/// <summary>
/// Second digit
/// </summary>
Digit2 = 3,
/// <summary>
/// Third digit
/// </summary>
Digit3 = 5,
/// <summary>
/// Fourth digit
/// </summary>
Digit4 = 7
}
#endregion
/// <summary>
/// List of digit addresses for sequential writing
/// </summary>
private static readonly Address[] s_digitAddressList = { Address.Digit1, Address.Digit2, Address.Digit3, Address.Digit4 };
/// <summary>
/// Empty display buffer
/// </summary>
private static readonly byte[] s_clearBuffer =
{
0b0000_0000, // Command
0b0000_0000, // Data Disp 1
0b0000_0000,
0b0000_0000, // Data Disp 2
0b0000_0000,
0b0000_0000, // Data Disp 3
0b0000_0000,
0b0000_0000, // Data Disp 4
0b0000_0000
};
/// <summary>
/// Display buffer
/// </summary>
private readonly byte[] _displayBuffer =
{
0b0000_0000, // Command
0b0000_0000, // Data Disp 1
0b0000_0000,
0b0000_0000, // Data Disp 2
0b0000_0000,
0b0000_0000, // Data Disp 3
0b0000_0000,
0b0000_0000, // Data Disp 4
0b0000_0000
};
/// <summary>
/// Translate digit number to buffer address
/// </summary>
/// <param name="digit">digit to translate</param>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="digit"/></exception>
private static int TranslateDigitToBufferAddress(int digit) => digit switch
{
0 => (int)Address.Digit1,
1 => (int)Address.Digit2,
2 => (int)Address.Digit3,
3 => (int)Address.Digit4,
_ => throw new ArgumentOutOfRangeException(nameof(digit)),
};
#endregion
#region Public members
/// <summary>
/// Initialize 7-Segment display
/// </summary>
/// <param name="i2cDevice">The <see cref="I2cDevice"/> to create with.</param>
public Large4Digit14SegmentDisplay(I2cDevice i2cDevice)
: base(i2cDevice)
{
}
/// <summary>
/// Gets the number of digits supported by the display
/// </summary>
public int NumberOfDigits { get; } = MaxNumberOfDigits;
/// <summary>
/// Gets or sets a single digit's segments by id
/// </summary>
/// <param name="address">digit address (0..3)</param>
/// <returns>Segment in display buffer for the given address</returns>
public Segment14 this[int address]
{
get => (Segment14)_displayBuffer[TranslateDigitToBufferAddress(address)];
set
{
_displayBuffer[TranslateDigitToBufferAddress(address)] = (byte)value;
AutoFlush();
}
}
/// <inheritdoc/>
/// <remarks>Write clears dots, you'll have to reset them afterwards</remarks>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="startAddress"/></exception>
/// <exception cref="ArgumentOutOfRangeException"><see cref="Large4Digit7SegmentDisplay"/> only supports <see cref="MaxNumberOfDigits"/> digits</exception>
public override void Write(ReadOnlySpan<byte> digits, int startAddress = 0)
{
if (digits.Length == 0)
{
// Nothing to write
return;
}
if (startAddress < 0 || startAddress >= (MaxNumberOfDigits * 2))
{
throw new ArgumentOutOfRangeException(nameof(startAddress));
}
if (digits.Length + startAddress > (MaxNumberOfDigits * 2))
{
throw new ArgumentOutOfRangeException(nameof(digits), $"{nameof(Large4Digit7SegmentDisplay)} only supports {MaxNumberOfDigits - startAddress} digits starting from address {startAddress}");
}
foreach (byte digit in digits)
{
_displayBuffer[(int)s_digitAddressList[startAddress++]] = (byte)(digit);
}
AutoFlush();
}
/// <summary>
/// Write raw data to display buffer
/// </summary>
/// <param name="digits">Array of ushort to write to the display</param>
/// <param name="startAddress">Address to start writing from</param>
private void Write(ReadOnlySpan<ushort> digits, int startAddress = 0)
{
if (digits.Length == 0)
{
// Nothing to write
return;
}
if (startAddress < 0 || startAddress >= (MaxNumberOfDigits * 2))
{
throw new ArgumentOutOfRangeException(nameof(startAddress));
}
if (digits.Length + startAddress > (MaxNumberOfDigits * 2))
{
throw new ArgumentOutOfRangeException(nameof(digits), $"{nameof(Large4Digit14SegmentDisplay)} only supports {MaxNumberOfDigits - startAddress} digits starting from address {startAddress}");
}
foreach (ushort digit in digits)
{
var asBytes = BitConverter.GetBytes(digit);
_displayBuffer[(int)s_digitAddressList[startAddress]] = (byte)(asBytes[1]);
_displayBuffer[(int)s_digitAddressList[startAddress] + 1] = (byte)(asBytes[0]);
startAddress++;
}
AutoFlush();
}
/// <summary>
/// Write a series of digits to the display buffer
/// </summary>
/// <param name="digits">a list of digits represented in segments</param>
/// <param name="startAddress">Address to start writing from</param>
public void Write(ReadOnlySpan<Segment14> digits, int startAddress = 0) =>
Write(MemoryMarshal.Cast<Segment14, ushort>(digits), startAddress);
/// <summary>
/// Write a series of characters to the display buffer
/// </summary>
/// <param name="characters">a list of characters represented in fonts</param>
/// <param name="startAddress">Address to start writing from</param>
public void Write(ReadOnlySpan<Font14> characters, int startAddress = 0) =>
Write(MemoryMarshal.Cast<Font14, ushort>(characters), startAddress);
/// <inheritdoc/>
public override void Clear() =>
s_clearBuffer.CopyTo(_displayBuffer, 0);
/// <inheritdoc/>
public override void Flush() =>
_i2cDevice.Write(_displayBuffer);
#region Write overloads
/// <summary>
/// Write integer value as decimal digits
/// </summary>
/// <param name="value">integer value</param>
/// <param name="alignment">alignment on display (left or right, right is default)</param>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="value"/> must be between -999..9999</exception>
public void Write(int value, Alignment alignment = Alignment.Right)
{
if (value > 9999 || value < -999)
{
throw new ArgumentOutOfRangeException(nameof(value), $"{nameof(value)} must be between -999..9999");
}
Write(value.ToString(), alignment);
}
/// <summary>
/// Write string value to display
/// </summary>
/// <param name="value">value to display, max 4 characters, or 5 characters if the 3rd character is ':' (this also turns on the center colon), or 6 characters if 1st character is also ':'</param>
/// <remarks>
/// * Unsupported characters will be replaced as whitespace
/// * This method clears the buffer before writing, so dots have to be reset afterwards
/// </remarks>
/// <param name="alignment">alignment on display (left or right, right is default)</param>
/// <exception cref="ArgumentException"><paramref name="value"/>[2] must be a ':'</exception>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="value"/> can contain maximum 5 characters</exception>
public void Write(string value, Alignment alignment = Alignment.Left)
{
Clear();
if (string.IsNullOrWhiteSpace(value))
{
return;
}
switch (value.Length)
{
case 1 when alignment == Alignment.Right:
case 2 when alignment == Alignment.Right:
case 3 when alignment == Alignment.Right:
value = value.PadLeft(MaxNumberOfDigits);
break;
case 1 when alignment == Alignment.Left:
case 2 when alignment == Alignment.Left:
case 3 when alignment == Alignment.Left:
case 4:
case 5:
// Nothing to do
break;
default:
throw new ArgumentOutOfRangeException(nameof(value), $"{nameof(value)} can contain maximum 5 characters");
}
Write(FontHelper14.GetString(value));
}
/// <summary>
/// Write an array of up to 4 bytes as hex
/// </summary>
/// <param name="values">Array of bytes</param>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="values"/>Length of array must be greater 0 and less than MaxNumberOfDigits</exception>
public void WriteHex(byte[] values)
{
if (values.Length > MaxNumberOfDigits)
{
throw new ArgumentOutOfRangeException(nameof(values), $"{nameof(values)} can contain maximum {MaxNumberOfDigits} bytes");
}
Write(FontHelper14.GetHexDigits(values));
}
/// <summary>
/// Write a single char to a spcific display digit
/// </summary>
/// <param name="c">The character to display</param>
/// <param name="pos">zero-based index of character position from the left</param>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="pos"/>position must be between 0 and MaxNumberOfDigits-1</exception>
public void WriteChar(char c, int pos)
{
if (pos >= MaxNumberOfDigits || pos < 0)
{
throw new ArgumentOutOfRangeException(nameof(pos), $"{nameof(pos)} must be between 0 and {MaxNumberOfDigits - 1} bytes");
}
Font14[] ch = { FontHelper14.GetCharacter(c) };
var tp = new ReadOnlySpan<Font14>(ch);
Write(tp, pos);
}
#endregion
#endregion
}
}