-
Notifications
You must be signed in to change notification settings - Fork 0
/
DigitalPinState.cs
209 lines (195 loc) · 5.17 KB
/
DigitalPinState.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
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace NanoPanel
{
class DigitalPinState
{
private bool _D2 = false;
public bool D2
{
get => _D2;
set
{
bool prev = _D2;
_D2 = value;
OnDigitalPinChanged(new(2, _D2, prev));
}
}
private bool _D3 = false;
public bool D3
{
get => _D3;
set
{
bool prev = _D3;
_D3 = value;
OnDigitalPinChanged(new(3, _D3, prev));
}
}
private bool _D4 = false;
public bool D4
{
get => _D4;
set
{
bool prev = _D4;
_D4 = value;
OnDigitalPinChanged(new(4, _D4, prev));
}
}
private bool _D5 = false;
public bool D5
{
get => _D5;
set
{
bool prev = _D5;
_D5 = value;
OnDigitalPinChanged(new(5, _D5, prev));
}
}
private bool _D6 = false;
public bool D6
{
get => _D6;
set
{
bool prev = _D6;
_D6 = value;
OnDigitalPinChanged(new(6, _D6, prev));
}
}
private bool _D7 = false;
public bool D7
{
get => _D7;
set
{
bool prev = _D7;
_D7 = value;
OnDigitalPinChanged(new(7, _D7, prev));
}
}
private bool _D8 = false;
public bool D8
{
get => _D8;
set
{
bool prev = _D8;
_D8 = value;
OnDigitalPinChanged(new(8, _D8, prev));
}
}
private bool _D9 = false;
public bool D9
{
get => _D9;
set
{
bool prev = _D9;
_D9 = value;
OnDigitalPinChanged(new(9, _D9, prev));
}
}
private bool _D10 = false;
public bool D10
{
get => _D10;
set
{
bool prev = _D10;
_D10 = value;
OnDigitalPinChanged(new(10, _D10, prev));
}
}
private bool _D11 = false;
public bool D11
{
get => _D11;
set
{
bool prev = _D11;
_D11 = value;
OnDigitalPinChanged(new(11, _D11, prev));
}
}
private bool _D12 = false;
public bool D12
{
get => _D12;
set
{
bool prev = _D12;
_D12 = value;
OnDigitalPinChanged(new(12, _D12, prev));
}
}
private bool _D13 = false;
public bool D13
{
get => _D13;
set
{
bool prev = _D13;
_D13 = value;
OnDigitalPinChanged(new(13, _D13, prev));
}
}
public event EventHandler<DigitalPinChangedArgs> DigitalPinChanged;
public DigitalPinState()
{
}
public DigitalPinState(Serial serial)
{
connectEvents(serial);
}
public void connectEvents(Serial serial)
{
serial.ReceivedLine += Serial_ReceivedLine;
}
static Regex re = new Regex(@"^2\s+D(?<pin>\d+)\s+(?<value>\d+)");
private void Serial_ReceivedLine(object sender, ReceivedLineArgs e)
{
MatchCollection matches = re.Matches(e.line);
foreach(Match match in matches)
{
int pin = int.Parse(match.Groups["pin"].Value);
bool value = int.Parse(match.Groups["value"].Value) == 0 ? false : true;
switch (pin)
{
case 2: D2 = value; break;
case 3: D3 = value; break;
case 4: D4 = value; break;
case 5: D5 = value; break;
case 6: D6 = value; break;
case 7: D7 = value; break;
case 8: D8 = value; break;
case 9: D9 = value; break;
case 10: D10 = value; break;
case 11: D11 = value; break;
case 12: D12 = value; break;
case 13: D13 = value; break;
}
}
}
public virtual void OnDigitalPinChanged(DigitalPinChangedArgs e)
{
DigitalPinChanged?.Invoke(this, e);
}
}
class DigitalPinChangedArgs : EventArgs
{
public bool? previous;
public bool next;
public int pin;
public DigitalPinChangedArgs(int pin, bool next, bool? prev=null)
{
this.pin = pin;
this.next = next;
this.previous = prev;
}
}
}