-
Notifications
You must be signed in to change notification settings - Fork 586
/
ArduinoI2cBus.cs
70 lines (60 loc) · 2.43 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
58
59
60
61
62
63
64
65
66
67
68
69
70
// 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.Concurrent;
using System.Collections.Generic;
using System.Device;
using System.Device.I2c;
using System.Globalization;
using System.Linq;
namespace Iot.Device.Arduino
{
internal class ArduinoI2cBus : I2cBus
{
private readonly ArduinoBoard _board;
private readonly int _busId;
private readonly ConcurrentDictionary<int, ArduinoI2cDevice> _usedAddresses;
private bool _busInitialized;
public ArduinoI2cBus(ArduinoBoard board, int busId)
{
_board = board;
_busId = busId;
_usedAddresses = new ConcurrentDictionary<int, ArduinoI2cDevice>();
}
/// <inheritdoc />
public override I2cDevice CreateDevice(int deviceAddress)
{
if (_usedAddresses.ContainsKey(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.TryAdd(deviceAddress, device);
return device;
}
public override void RemoveDevice(int deviceAddress)
{
_usedAddresses.TryRemove(deviceAddress, out _);
}
public override ComponentInformation QueryComponentInformation()
{
var self = new ComponentInformation(this, "Arduino I2C Bus driver");
self.Properties["BusNo"] = _busId.ToString(CultureInfo.InvariantCulture);
foreach (var device in _usedAddresses)
{
self.AddSubComponent(device.Value.QueryComponentInformation());
}
return self;
}
}
}