-
-
Notifications
You must be signed in to change notification settings - Fork 114
/
Copy pathSeesawAdc.cs
36 lines (31 loc) · 1.39 KB
/
SeesawAdc.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
// 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.Buffers.Binary;
namespace Iot.Device.Seesaw
{
/// <summary>
/// The Seesaw ADC class.
/// </summary>
public partial class Seesaw : IDisposable
{
/// <summary>
/// Reads the value of an analog pin.
/// </summary>
/// <param name="pin">The pin number in the devices numbering scheme.</param>
/// <returns>A value between 0..1023 that represents the analog value.</returns>
public ushort AnalogRead(byte pin)
{
const int AdcConversionDelayMicroseconds = 500; // 500 microSeconds delay between read and write to allow Adc conversion time.
if (!HasModule(SeesawModule.Gpio))
{
throw new InvalidOperationException($"The hardware on I2C Bus {I2cDevice.ConnectionSettings.BusId}, Address 0x{I2cDevice.ConnectionSettings.DeviceAddress:X2} does not support Adafruit SeeSaw ADC functionality");
}
if (pin < 2 || pin > 5)
{
throw new ArgumentOutOfRangeException("ADC pin must be within 2-5 range.");
}
return BinaryPrimitives.ReadUInt16BigEndian(Read(SeesawModule.Adc, SeesawFunction.AdcChannelOffset + pin - 2, 2, AdcConversionDelayMicroseconds));
}
}
}