-
Notifications
You must be signed in to change notification settings - Fork 0
/
AnalogPinState.cs
158 lines (147 loc) · 3.81 KB
/
AnalogPinState.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
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace NanoPanel
{
class AnalogPinState
{
private int _A0 = 0;
public int A0
{
get => _A0;
set
{
int prev = _A0;
_A0 = value;
OnAnalogPinChanged(new(0, _A0, prev));
}
}
private int _A1 = 0;
public int A1
{
get => _A1;
set
{
int prev = _A1;
_A1 = value;
OnAnalogPinChanged(new(1, _A1, prev));
}
}
private int _A2 = 0;
public int A2
{
get => _A2;
set
{
int prev = _A2;
_A2 = value;
OnAnalogPinChanged(new(2, _A2, prev));
}
}
private int _A3 = 0;
public int A3
{
get => _A3;
set
{
int prev = _A3;
_A3 = value;
OnAnalogPinChanged(new(3, _A3, prev));
}
}
private int _A4 = 0;
public int A4
{
get => _A4;
set
{
int prev = _A4;
_A4 = value;
OnAnalogPinChanged(new(4, _A4, prev));
}
}
private int _A5 = 0;
public int A5
{
get => _A5;
set
{
int prev = _A5;
_A5 = value;
OnAnalogPinChanged(new(5, _A5, prev));
}
}
private int _A6 = 0;
public int A6
{
get => _A6;
set
{
int prev = _A6;
_A6 = value;
OnAnalogPinChanged(new(6, _A6, prev));
}
}
private int _A7 = 0;
public int A7
{
get => _A7;
set
{
int prev = _A7;
_A7 = value;
OnAnalogPinChanged(new(7, _A7, prev));
}
}
public event EventHandler<AnalogPinChangedArgs> AnalogPinChanged;
public AnalogPinState()
{
}
public AnalogPinState(Serial serial)
{
connectEvents(serial);
}
public void connectEvents(Serial serial)
{
serial.ReceivedLine += Serial_ReceivedLine;
}
static Regex re = new Regex(@"^1\s+A(?<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);
int value = int.Parse(match.Groups["value"].Value);
switch (pin)
{
case 0: A0 = value; break;
case 1: A1 = value; break;
case 2: A2 = value; break;
case 3: A3 = value; break;
case 4: A4 = value; break;
case 5: A5 = value; break;
case 6: A6 = value; break;
case 7: A7 = value; break;
}
}
}
public virtual void OnAnalogPinChanged(AnalogPinChangedArgs e)
{
AnalogPinChanged?.Invoke(this, e);
}
}
class AnalogPinChangedArgs : EventArgs
{
public int previous;
public int next;
public int pin;
public AnalogPinChangedArgs(int pin, int next, int prev = -1)
{
this.pin = pin;
this.next = next;
this.previous = prev;
}
}
}