forked from dotnet/iot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArduinoI2cBus.cs
41 lines (36 loc) · 1.15 KB
/
ArduinoI2cBus.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
using System;
using System.Collections.Generic;
using System.Device.I2c;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Iot.Device.Arduino
{
internal class ArduinoI2cBus : I2cBus
{
private readonly ArduinoBoard _board;
private readonly int _busId;
private readonly HashSet<int> _usedAddresses;
public ArduinoI2cBus(ArduinoBoard board, int busId)
{
_board = board;
_busId = busId;
_usedAddresses = new HashSet<int>();
}
/// <inheritdoc />
public override I2cDevice CreateDevice(int deviceAddress)
{
if (_usedAddresses.Contains(deviceAddress))
{
throw new InvalidOperationException($"Device number {deviceAddress} is already in use");
}
var device = new ArduinoI2cDevice(_board, this, new I2cConnectionSettings(_busId, deviceAddress));
_usedAddresses.Add(deviceAddress);
return device;
}
public override void RemoveDevice(int deviceAddress)
{
_usedAddresses.Remove(deviceAddress);
}
}
}