-
Notifications
You must be signed in to change notification settings - Fork 599
/
Copy pathSetupHelpers.cs
97 lines (83 loc) · 2.72 KB
/
SetupHelpers.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
86
87
88
89
90
91
92
93
94
95
96
97
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Threading;
using System.Runtime.InteropServices;
using System.Device.Spi;
using System.Device.I2c;
using System.Device.Pwm;
using System.Device.Pwm.Drivers;
using Iot.Device.Adc;
using Iot.Device.Bmxx80;
using Iot.Device.Bmxx80.PowerMode;
using Xunit;
namespace System.Device.Gpio.Tests;
public static class SetupHelpers
{
public const int MinAdc = 0;
public const int MaxAdc = 1023;
public const int HalfAdc = MaxAdc / 2;
private const int Bme280I2cBusId = 1;
public static void AdcValueAround(int expected, int actual, int error = 50)
{
Assert.InRange(
actual,
Math.Max(MinAdc, expected - error),
Math.Min(MaxAdc, expected + error));
}
public static void DutyCycleAround(double expected, double actual, double error = 0.01)
{
Assert.InRange(
actual,
Math.Max(0, expected - error),
Math.Min(1, expected + error));
}
public static Mcp3008 CreateAdc()
{
return new Mcp3008(SpiDevice.Create(new SpiConnectionSettings(0, 0)));
}
public static Bme280 CreateBme280()
{
var settings = new I2cConnectionSettings(Bme280I2cBusId, Bme280.DefaultI2cAddress);
var bme280 = new Bme280(I2cDevice.Create(settings));
SetupBme280(bme280);
return bme280;
}
public static I2cBus CreateI2cBusForBme280()
{
return I2cBus.Create(Bme280I2cBusId);
}
public static Bme280 CreateBme280(I2cBus i2cBus)
{
var bme280 = new Bme280(i2cBus.CreateDevice(Bme280.DefaultI2cAddress));
SetupBme280(bme280);
return bme280;
}
private static void SetupBme280(Bme280 bme280)
{
// https://github.com/dotnet/iot/issues/753
bme280.SetPowerMode(Bmx280PowerMode.Normal);
Thread.Sleep(100);
}
private static PwmChannel CreatePwmChannelCore(int frequency, double dutyCycle)
{
try
{
return PwmChannel.Create(0, 0, frequency, dutyCycle);
}
catch (ArgumentException) when (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
// PWM is likely not enabled
// Let's try to run against software PWM implementation
return new SoftwarePwmChannel(18, frequency, dutyCycle, usePrecisionTimer: true);
}
}
public static PwmChannel CreatePwmChannel(int frequency = 10000, bool stopped = false, double dutyCycle = 0.5)
{
var pwm = CreatePwmChannelCore(frequency, dutyCycle);
if (!stopped)
{
pwm.Start();
}
return pwm;
}
}