Meadow.Foundation.ICs.IOExpanders.Mcp3008
Mcp3008 | |
---|---|
Status | |
Source code | GitHub |
Datasheet(s) | GitHub |
NuGet package |
Code Example
Mcp3008 mcp;
IAnalogInputPort port;
public override Task Initialize()
{
Resolver.Log.Info("Initialize");
IDigitalOutputPort chipSelectPort = Device.CreateDigitalOutputPort(Device.Pins.D01);
mcp = new Mcp3008(Device.CreateSpiBus(), chipSelectPort);
port = mcp.CreateAnalogInputPort(mcp.Pins.CH0, 32, TimeSpan.FromSeconds(1), new Voltage(3.3, Voltage.UnitType.Volts), Mcp3xxx.InputType.SingleEnded);
port.Updated += (s, result) =>
{
Resolver.Log.Info($"Analog event, new voltage: {result.New.Volts:N2}V, old: {result.Old?.Volts:N2}V");
};
var observer = IAnalogInputPort.CreateObserver(
handler: result =>
{
Resolver.Log.Info($"Analog observer triggered; new: {result.New.Volts:n2}V, old: {result.Old?.Volts:n2}V");
},
filter: result =>
{
if (result.Old is { } oldValue)
{
return (result.New - oldValue).Abs().Volts > 0.1;
}
else { return false; }
}
);
port.Subscribe(observer);
return base.Initialize();
}
public override Task Run()
{
Resolver.Log.Info("Run");
port.StartUpdating();
return Task.CompletedTask;
}
Sample project(s) available on GitHub
Class Mcp3008
MCP3008 Analog to Digital Converter (ADC)
Assembly: Mcp3xxx.dll
View Source
Declaration
public class Mcp3008 : Mcp3xxx, IAnalogInputController, IPinController, ISpiPeripheral, IDisposable
Inheritance: System.Object
-> Meadow.Foundation.ICs.IOExpanders.Mcp3xxx
Implements:
Meadow.Hardware.IAnalogInputController
, Meadow.Hardware.IPinController
, Meadow.Hardware.ISpiPeripheral
, System.IDisposable
Properties
Pins
The pins
View Source
Declaration
public Mcp3008.PinDefinitions Pins { get; }
Implements
Meadow.Hardware.IAnalogInputController
Meadow.Hardware.IPinController
Meadow.Hardware.ISpiPeripheral
System.IDisposable