forked from nanoframework/nanoFramework.IoT.Device
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tm1637.cs
360 lines (313 loc) · 12.3 KB
/
Tm1637.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
// 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;
using System.Device.Gpio;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;
namespace Iot.Device.Tm1637
{
/// <summary>
/// Represents Tm1637 segment display.
/// </summary>
public sealed class Tm1637 : IDisposable
{
/// <summary>
/// The number of characters that the TM1637 can handle.
/// </summary>
public static byte MaxCharacters => 6;
// According to the doc, the clock pulse width minimum is 400 ns
// And waiting time between clk up and down is 1 µs
private const byte ClockWidthMicroseconds = 1;
private readonly int _pinClk;
private readonly int _pinDio;
private GpioController _controller;
private bool _shouldDispose;
private byte _brightness;
// Default character order is from 0 to 5
private byte[] _charactersOrder = new byte[6] { 0, 1, 2, 3, 4, 5 };
// To store what has been displayed last. Used when change on brightness or
// screen on/off is used
private byte[] _lastDisplay = new byte[6] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
private bool _screenOn;
/// <summary>
/// Initializes a new instance of the<see cref="Tm1637" /> class.
/// </summary>
/// <param name="pinClk">The clock pin.</param>
/// <param name="pinDio">The data pin.</param>
/// <param name="pinNumberingScheme">Use the logical or physical pin layout.</param>
/// <param name="gpioController">A Gpio Controller if you want to use a specific one.</param>
/// <param name="shouldDispose">True to dispose the Gpio Controller.</param>
public Tm1637(
int pinClk,
int pinDio,
PinNumberingScheme pinNumberingScheme = PinNumberingScheme.Logical,
GpioController? gpioController = null,
bool shouldDispose = true)
{
_pinClk = pinClk;
_pinDio = pinDio;
_controller = gpioController ?? new GpioController(pinNumberingScheme);
_shouldDispose = shouldDispose || gpioController is null;
_controller.OpenPin(_pinClk, PinMode.Output);
_controller.OpenPin(_pinDio, PinMode.Output);
_brightness = 7;
}
/// <summary>
/// Gets or sets order of characters, expect a 6 length byte array
/// 0 to 5, any order. Most of the time 4 segments do
/// not need to be changed but the 6 ones may be in different order
/// like 0 1 2 5 4 3. In this case, this byte array has be be in this order.
/// </summary>
public byte[] CharacterOrder
{
get => _charactersOrder;
set
{
if (value.Length != MaxCharacters)
{
throw new ArgumentException(nameof(CharacterOrder), $"Value must be 6 bytes.");
}
// Check if we have all values from 0 to 5
bool allExist = true;
for (int i = 0; i < MaxCharacters; i++)
{
allExist &= Array.IndexOf(value, i) != -1;
}
if (!allExist)
{
throw new ArgumentException(
$"{nameof(CharacterOrder)} needs to have all existing characters from 0 to 5");
}
value.CopyTo(_charactersOrder, 0);
}
}
/// <summary>
/// Gets or sets a value indicating whether the screen on or off.
/// </summary>
public bool ScreenOn
{
get => _screenOn;
set
{
_screenOn = value;
DisplayRaw(0, _lastDisplay[0]);
}
}
/// <summary>
/// Gets or sets a value indicating whether the screen brightness from 0 to 7.
/// </summary>
public byte Brightness
{
get => _brightness;
set
{
if (value > 7)
{
throw new ArgumentException(nameof(Brightness), "Value must be less than 8.");
}
_brightness = value;
DisplayRaw(0, _lastDisplay[0]);
}
}
private PinValue WriteByte(byte data)
{
// We send data by 8 bits
for (byte i = 0; i < 8; i++)
{
_controller.Write(_pinClk, PinValue.Low);
Thread.SpinWait(ClockWidthMicroseconds);
// LSB first
if ((data & 0x01) == 0x01)
{
_controller.Write(_pinDio, PinValue.High);
}
else
{
_controller.Write(_pinDio, PinValue.Low);
}
// LSB first
data >>= 1;
_controller.Write(_pinClk, PinValue.High);
Thread.SpinWait(ClockWidthMicroseconds);
}
// Wait for the acknowledge
_controller.Write(_pinClk, PinValue.Low);
_controller.Write(_pinDio, PinValue.High);
_controller.Write(_pinClk, PinValue.High);
Thread.SpinWait(ClockWidthMicroseconds);
_controller.SetPinMode(_pinDio, PinMode.Input);
// Wait 1 µs, it's the waiting time between clk up and down
// That's according to the documentation
Thread.SpinWait(ClockWidthMicroseconds);
var ack = _controller.Read(_pinDio);
if (ack == PinValue.Low)
{
// We get acknowledge from the device
_controller.SetPinMode(_pinDio, PinMode.Output);
_controller.Write(_pinDio, PinValue.Low);
}
_controller.Write(_pinClk, PinValue.High);
Thread.SpinWait(ClockWidthMicroseconds);
_controller.Write(_pinClk, PinValue.Low);
Thread.SpinWait(ClockWidthMicroseconds);
_controller.SetPinMode(_pinDio, PinMode.Output);
return ack;
}
private void StartTransmission()
{
_controller.Write(_pinClk, PinValue.High);
_controller.Write(_pinDio, PinValue.High);
Thread.SpinWait(ClockWidthMicroseconds);
_controller.Write(_pinDio, PinValue.Low);
}
private void StopTransmission()
{
_controller.Write(_pinClk, PinValue.Low);
_controller.Write(_pinDio, PinValue.Low);
Thread.SpinWait(ClockWidthMicroseconds);
_controller.Write(_pinClk, PinValue.High);
_controller.Write(_pinDio, PinValue.High);
}
/// <summary>
/// Displays segments starting at first segment with byte array containing raw data for each segment including the dot.
/// <remarks>
/// Segment representation:
/// <para/>
/// bit 0 = a _a_
/// bit 1 = b | |
/// bit 2 = c f b
/// bit 3 = d |_g_|
/// bit 4 = e | |
/// bit 5 = f e c
/// bit 6 = g |_d_| .dp
/// bit 7 = dp
/// <para/>
/// Representation of the number 0 so lighting segments a, b, c, d, e and F is then 0x3f
/// </remarks>
/// </summary>
/// <param name="rawData">The raw data array to display, size of the array has to be 6 maximum.</param>
private void Display(SpanByte rawData)
{
if (rawData.Length > MaxCharacters)
{
throw new ArgumentException(nameof(rawData), $"Maximum number of segments for TM1637 is {MaxCharacters}");
}
// Prepare the buffer with the right order to transfer
byte[] toTransfer = new byte[MaxCharacters];
for (int i = 0; i < rawData.Length; i++)
{
toTransfer[_charactersOrder[i]] = rawData[i];
}
for (int j = rawData.Length; j < MaxCharacters; j++)
{
toTransfer[_charactersOrder[j]] = (byte)Character.Nothing;
}
_lastDisplay = toTransfer;
StartTransmission();
// First command is set data
WriteByte((byte)DataCommand.DataCommandSetting);
StopTransmission();
StartTransmission();
// Second command is set address to automatic
WriteByte((byte)DataCommand.DataCommandSetting);
// Transfer the data
for (int i = 0; i < MaxCharacters; i++)
{
WriteByte(toTransfer[i]);
}
StopTransmission();
StartTransmission();
// Set the display on/off and the brightness
WriteByte((byte)((ScreenOn ? DisplayCommand.DisplayOn : DisplayCommand.DisplayOff) + _brightness));
StopTransmission();
}
/// <summary>
/// Displays a series of prebuild characters including the dot or not
/// You can build your own characters with the primitives like Bottom, Top, Dot.
/// </summary>
/// <param name="rawData">The Character to display.</param>
public void Display(SpanCharacter rawData)
{
var byteArray = new byte[rawData.Length];
for (var j = 0; j < rawData.Length; j++)
{
byteArray[j] = (byte)rawData[j];
}
Display(new SpanByte(byteArray));
}
/// <summary>
/// Displays a raw data at a specific segment position from 0 to 5.
/// </summary>
/// <remarks>
/// Segment representation:
/// <para/>
/// bit 0 = a _a_
/// bit 1 = b | |
/// bit 2 = c f b
/// bit 3 = d |_g_|
/// bit 4 = e | |
/// bit 5 = f e c
/// bit 6 = g |_d_| .dp
/// bit 7 = dp
/// <para/>
/// Representation of the number 0 so lighting segments a, b, c, d, e and F is then 0x3f.
/// </remarks>
/// <param name="characterPosition">The character position from 0 to 5.</param>
/// <param name="rawData">The segment characters to display.</param>
public void Display(byte characterPosition, Character rawData)
{
if (characterPosition > MaxCharacters)
{
throw new ArgumentException(nameof(characterPosition), $"Maximum number of characters for TM1637 is {MaxCharacters}");
}
// Recreate the buffer in correct order
_lastDisplay[_charactersOrder[characterPosition]] = (byte)rawData;
DisplayRaw(_charactersOrder[characterPosition], (byte)rawData);
}
private void DisplayRaw(byte characterAddress, byte rawData)
{
StartTransmission();
// First command for fix address
WriteByte((byte)DataCommand.FixAddress);
StopTransmission();
StartTransmission();
// Set the address to transfer
WriteByte((byte)(DataCommand.AddressCommandSetting + characterAddress));
// Transfer the byte
WriteByte(rawData);
StopTransmission();
StartTransmission();
// Set the display on/off and the brightness
WriteByte((byte)((ScreenOn ? DisplayCommand.DisplayOn : DisplayCommand.DisplayOff) + _brightness));
StopTransmission();
}
/// <summary>
/// Clear the display.
/// </summary>
public void ClearDisplay()
{
// 6 segments with nothing/space displayed
SpanByte clearDisplay = new byte[]
{
(byte)Character.Nothing,
(byte)Character.Nothing,
(byte)Character.Nothing,
(byte)Character.Nothing,
(byte)Character.Nothing,
(byte)Character.Nothing,
};
Display(clearDisplay);
}
/// <inheritdoc/>
public void Dispose()
{
if (_shouldDispose)
{
_controller?.Dispose();
_controller = null;
}
}
}
}