-
Notifications
You must be signed in to change notification settings - Fork 594
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Board as base class of existing ArduinoBoard
- Loading branch information
Showing
8 changed files
with
225 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.