Skip to main content

Meadow.Foundation.ICs.IOExpanders.Mcp3204

Mcp3204
StatusStatus badge: working
Source codeGitHub
Datasheet(s)GitHub
NuGet packageNuGet Gallery for Meadow.Foundation.ICs.ADCs.Mcp3xxx

Code Example

Mcp3204 mcp;

IAnalogInputPort port;

public override Task Initialize()
{
Resolver.Log.Info("Initialize");

IDigitalOutputPort chipSelectPort = Device.CreateDigitalOutputPort(Device.Pins.D01);

mcp = new Mcp3204(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