-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArduinoController.cs
More file actions
169 lines (149 loc) · 6.05 KB
/
Copy pathArduinoController.cs
File metadata and controls
169 lines (149 loc) · 6.05 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
using Microsoft.Maker.RemoteWiring;
using Microsoft.Maker.Serial;
using Microsoft.Maker.Firmata;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Networking;
using Windows.Devices.Enumeration;
namespace Arduino_Iot_PinMapping_UWP
{
public class ArduinoController
{
public delegate void ConnectionLostFailHandler(string message);
public delegate void PinUpdateHandler(AModule module,APin pin,ushort value);
public event ConnectionLostFailHandler ConnectionLostEvent;
public event ConnectionLostFailHandler ConnectionFailEvent;
public event PinUpdateHandler onPinUpdateEvent;
RemoteDevice arduino;
BluetoothSerial bluetoothConnection;
NetworkSerial networkConnection;
UsbSerial usbConnection;
Dictionary<string, APin> _pinNum = new Dictionary<string, APin>();
Dictionary<string, AModule> _moduleNum = new Dictionary<string, AModule>();
bool deviceReady = false;
private ConnectionType _connType;
public enum ConnectionType { Bluetooth, Network, Usb }
public ConnectionType connectionType
{
get { return _connType; }
}
private ArduinoController(IStream connection)
{
arduino = new RemoteDevice(connection);
arduino.DeviceReady += Setup;
}
private void Setup()
{
lock(_moduleNum)
{
foreach(AModule am in _moduleNum.Values)
{
BindPins(am);
}
deviceReady = true;
}
}
/// <summary>
/// Connect to arduino over bloetooth
/// </summary>
/// <param name="DeviceID">Device name or ID</param>
/// <returns></returns>
public static ArduinoController BluetoothConnection(string DeviceID, uint baudRate = 115200, SerialConfig serialConfig = SerialConfig.SERIAL_8N1)
{
BluetoothSerial Connection1 = new BluetoothSerial(DeviceID);
var res = new ArduinoController(Connection1);
Connection1.begin(baudRate, serialConfig);
res.bluetoothConnection = Connection1;
res._connType = ConnectionType.Bluetooth;
return res;
}
public static ArduinoController NetworkConnection(HostName host,ushort port, uint baudRate = 115200, SerialConfig serialConfig = SerialConfig.SERIAL_8N1)
{
NetworkSerial Connection1 = new NetworkSerial(host, port);
var res = new ArduinoController(Connection1);
Connection1.begin(baudRate, serialConfig);
res.networkConnection = Connection1;
res._connType = ConnectionType.Network;
return res;
}
public static ArduinoController UsbConnection(DeviceInformation device,uint baudRate = 115200,SerialConfig serialConfig = SerialConfig.SERIAL_8N1)
{
UsbSerial Connection1 = new UsbSerial(device);
Connection1.begin(baudRate, serialConfig);
var res = new ArduinoController(Connection1);
res.usbConnection = Connection1;
res._connType = ConnectionType.Usb;
return res;
}
public void AddModule(AModule mod)
{
lock(_moduleNum)
{
foreach(APin apm in mod.pins) if (_pinNum.ContainsKey(apm.pinNumber)) throw new ModuleConflictException("Pin " + apm.pinNumber + " already used in '" + _moduleNum[apm.pinNumber].name + "' module.");
if(deviceReady) BindPins(mod);
foreach (APin apm in mod.pins)
{
_pinNum.Add(apm.pinNumber, apm);
_moduleNum.Add(apm.pinNumber, mod);
}
}
}
private void ArduinoEvents()
{
arduino.AnalogPinUpdated += Arduino_AnalogPinUpdated;
arduino.DigitalPinUpdated += Arduino_DigitalPinUpdated;
arduino.DeviceConnectionFailed += Arduino_DeviceConnectionFailed;
arduino.DeviceConnectionLost += Arduino_DeviceConnectionLost;
arduino.StringMessageReceived += Arduino_StringMessageReceived;
arduino.SysexMessageReceived += Arduino_SysexMessageReceived;
}
private void Arduino_SysexMessageReceived(byte command, Windows.Storage.Streams.DataReader message)
{
throw new NotImplementedException();
}
private void Arduino_StringMessageReceived(string message)
{
throw new NotImplementedException();
}
private void Arduino_DeviceConnectionLost(string message)
{
if (ConnectionLostEvent != null) ConnectionLostEvent(message);
}
private void Arduino_DeviceConnectionFailed(string message)
{
if (ConnectionFailEvent != null) ConnectionFailEvent(message);
}
private void Arduino_DigitalPinUpdated(byte pin, PinState state)
{
string pinNumber = ((short)pin).ToString();
AModule module = _moduleNum[pinNumber];
APin p = _pinNum[pinNumber];
if (onPinUpdateEvent != null) onPinUpdateEvent(module, p, (ushort)state);
}
private void Arduino_AnalogPinUpdated(string pin, ushort value)
{
AModule module = _moduleNum[pin];
APin p = _pinNum[pin];
if (onPinUpdateEvent != null) onPinUpdateEvent(module, p, value);
}
private void BindPins(AModule mod)
{
foreach (APin pin in mod.pins)
{
//Analogic or Digital pin
if (char.IsLetter(pin.pinNumber[0])) arduino.pinMode(pin.pinNumber, pin.pinMode);
else arduino.pinMode((byte)short.Parse(pin.pinNumber), pin.pinMode);
}
}
}
public class ModuleConflictException : Exception
{
public ModuleConflictException(string message)
: base(message)
{
}
}
}