forked from dotnet/iot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArduinoI2cBus.cs
57 lines (49 loc) · 1.87 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
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;
private bool _busInitialized;
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");
}
if (!_busInitialized)
{
// Ensure the corresponding pins are set to I2C (not strictly necessary, but consistent)
foreach (SupportedPinConfiguration supportedPinConfiguration in _board.SupportedPinConfigurations.Where(x => x.PinModes.Contains(SupportedMode.I2c)))
{
_board.Firmata.SetPinMode(supportedPinConfiguration.Pin, SupportedMode.I2c);
}
_board.Firmata.SendI2cConfigCommand();
_busInitialized = true;
}
var device = new ArduinoI2cDevice(_board, this, new I2cConnectionSettings(_busId, deviceAddress));
_usedAddresses.Add(deviceAddress);
return device;
}
public override void RemoveDevice(int deviceAddress)
{
_usedAddresses.Remove(deviceAddress);
}
}
}