-
Notifications
You must be signed in to change notification settings - Fork 598
/
Copy pathCustomBoard.cs
85 lines (76 loc) · 3.01 KB
/
CustomBoard.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// 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;
using System.Device.Gpio;
using System.Device.I2c;
using System.Device.Pwm;
using System.Device.Spi;
using System.Text;
namespace Iot.Device.Board
{
/// <summary>
/// A board that can be customized with user-specified drivers.
/// This should only be used if the drivers can't be auto-detected properly.
/// </summary>
public class CustomBoard : GenericBoard
{
private readonly GpioDriver _gpioDriver;
private readonly Func<int, I2cBus> _i2CBusCreator;
private readonly Func<SpiConnectionSettings, SpiDevice> _spiDeviceCreator;
private readonly Func<int, PwmChannel> _pwmChannelCreator;
/// <summary>
/// Creates a new custom board.
/// </summary>
/// <param name="gpioDriver">GPIO driver to use</param>
/// <param name="i2cBusCreator">Function to create an I2C bus instance</param>
/// <param name="spiDeviceCreator">Function to create an SPI device</param>
/// <param name="pwmChannelCreator">Function to create a PWM channel</param>
public CustomBoard(GpioDriver gpioDriver, Func<int, I2cBus> i2cBusCreator,
Func<SpiConnectionSettings, SpiDevice> spiDeviceCreator, Func<int, PwmChannel> pwmChannelCreator)
{
_gpioDriver = gpioDriver;
_i2CBusCreator = i2cBusCreator;
_spiDeviceCreator = spiDeviceCreator;
_pwmChannelCreator = pwmChannelCreator;
}
/// <summary>
/// Returns the GPIO driver
/// </summary>
/// <returns></returns>
protected override GpioDriver? TryCreateBestGpioDriver()
{
return _gpioDriver ?? throw new NotSupportedException("No GPIO Driver specified");
}
/// <inheritdoc />
protected override I2cBusManager CreateI2cBusCore(int busNumber, int[]? pins)
{
return new I2cBusManager(this, busNumber, pins, _i2CBusCreator(busNumber));
}
/// <inheritdoc />
public override int GetDefaultI2cBusNumber()
{
throw new NotSupportedException("The custom board does not have a default bus number");
}
/// <inheritdoc />
protected override SpiDevice CreateSimpleSpiDevice(SpiConnectionSettings settings, int[] pins)
{
return _spiDeviceCreator(settings);
}
/// <inheritdoc />
protected override PwmChannel CreateSimplePwmChannel(int chip, int channel, int frequency, double dutyCyclePercentage)
{
return _pwmChannelCreator(channel);
}
/// <inheritdoc />
public override ComponentInformation QueryComponentInformation()
{
var ret = base.QueryComponentInformation();
return ret with
{
Description = "Custom Board"
};
}
}
}