-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLM7SegDirect.cs
More file actions
187 lines (161 loc) · 6.25 KB
/
LM7SegDirect.cs
File metadata and controls
187 lines (161 loc) · 6.25 KB
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
using System;
using Windows.Devices.Gpio;
using Windows.System.Threading;
namespace LM7Seg
{
public sealed class LM7SegDirect
{
private bool _isAnode;
private byte qtyDigits = 1;
private ThreadPoolTimer timer = null;
private GpioPin[] pins = new GpioPin[8];
private GpioPin[] digits = new GpioPin[4];
private GpioController gpio;
//--------
// Define each valid number configuration to show in the display
private byte[,] seven_seg_digits = new byte[10, 7]
{
{ 0,0,0,0,0,0,1 }, // = 0
{ 1,0,0,1,1,1,1 }, // = 1
{ 0,0,1,0,0,1,0 }, // = 2
{ 0,0,0,0,1,1,0 }, // = 3
{ 1,0,0,1,1,0,0 }, // = 4
{ 0,1,0,0,1,0,0 }, // = 5
{ 0,1,0,0,0,0,0 }, // = 6
{ 0,0,0,1,1,1,1 }, // = 7
{ 0,0,0,0,0,0,0 }, // = 8
{ 0,0,0,1,1,0,0 } // = 9
};
public int CurrentValue { get; set; }
public LM7SegDirect(byte segA, byte segB, byte segC, byte segD, byte segE, byte segF, byte segG, byte segDotn, bool isAnode)
{
byte[] pin_order = new byte[8];
_isAnode = isAnode;
pin_order[0] = segA;
pin_order[1] = segB;
pin_order[2] = segC;
pin_order[3] = segD;
pin_order[4] = segE;
pin_order[5] = segF;
pin_order[6] = segG;
pin_order[7] = segDotn;
gpio = GpioController.GetDefault();
// Show an error if there is no GPIO controller
if (gpio == null)
{
//@@
}
//----------------
// Open each pin and turn the light of for it
for (int i = 0; i < 8; i++)
{
pins[i] = gpio.OpenPin(pin_order[i]);
pins[i].Write(GpioPinValue.High);
pins[i].SetDriveMode(GpioPinDriveMode.Output);
}
//----------
// Create a timer that redraw the currentvalue each second
// OBS: If you want to change numbers faster than that, just fix the timespan for this timer
timer = ThreadPoolTimer.CreatePeriodicTimer(Timer_Tick, TimeSpan.FromMilliseconds(1));
}
//-----------------------------
// defineDigits
// Define how many digits the 7-segment have and what are the pin numbers
public void defineDigits(byte digitsQty, byte dig1, byte dig2, byte dig3, byte dig4)
{
byte[] digit_order = new byte[4];
qtyDigits = digitsQty;
digit_order[0] = dig1;
digit_order[1] = dig2;
digit_order[2] = dig3;
digit_order[3] = dig4;
//----------------
// Open each digit and turn the light of for it
for (int i = 0; i < digitsQty; i++)
{
digits[i] = gpio.OpenPin(digit_order[i]);
digits[i].Write(GpioPinValue.High);
digits[i].SetDriveMode(GpioPinDriveMode.Output);
}
}
//---------------------------------------------------------------------
// Write one number to a specific digit in the 7-Segment led
public void digitWrite(byte digit, int number)
{
// If the desired digit does not exist, return
if (digit > qtyDigits)
return;
// Activate the digit
pickDigit(digit);
// Activate each led in the digit
int pinseq = 0;
for (byte segCount = 0; segCount < 7; ++segCount)
{
byte finalValue = seven_seg_digits[number, segCount];
if (!_isAnode)
finalValue = finalValue == (byte)1 ? (byte)0 : (byte)1;
pins[pinseq].Write(finalValue == 0 ? GpioPinValue.Low : GpioPinValue.High);
pinseq++;
}
// Here I am always turning off the DOT, because I dont want it
pins[pinseq].Write(_isAnode ? GpioPinValue.High : GpioPinValue.Low);
}
//---------------------------------------------------------------------
// Dump the CurrentValue to the display.
// Up to 4 digits
public void valueWrite(int number)
{
CurrentValue = number;
if (timer == null)
timer = ThreadPoolTimer.CreatePeriodicTimer(Timer_Tick, TimeSpan.FromMilliseconds(500));
}
//--------------
// Each timer interval redraw the number in the display
private void Timer_Tick(ThreadPoolTimer timer)
{
if (CurrentValue < 10)
{
digitWrite(4, 0);
digitWrite(3, 0);
digitWrite(2, 0);
digitWrite(1, CurrentValue);
}
else if (CurrentValue < 100)
{
digitWrite(4, 0);
digitWrite(3, 0);
digitWrite(2, CurrentValue / 10);
digitWrite(1, CurrentValue % 10);
}
else if (CurrentValue < 1000)
{
digitWrite(4, 0);
digitWrite(3, CurrentValue / 100);
digitWrite(2, (CurrentValue % 100) / 10);
digitWrite(1, CurrentValue % 10);
}
else
{
digitWrite(4, CurrentValue / 1000);
digitWrite(3, (CurrentValue % 1000) / 100);
digitWrite(2, (CurrentValue % 100) / 10);
digitWrite(1, CurrentValue % 10);
}
}
//---------------------------------------------------------------------
// Activate a specific digit in the 7-Segment.
private void pickDigit(int x)
{
// If only one digit (7-Segment 1 Digit) then no need to continue here
if (qtyDigits == 1)
return;
// Turn off ALL digits
for (int i = 0; i < qtyDigits; i++)
{
digits[i].Write(_isAnode ? GpioPinValue.Low : GpioPinValue.High);
}
// Turn ON only the desired digit
digits[x - 1].Write(_isAnode ? GpioPinValue.High : GpioPinValue.Low);
}
}
}