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;
}