Remarks
Mcp3201 | |
---|---|
Status | |
Source code | GitHub |
Datasheet(s) | GitHub |
NuGet package |
Code Example
Mcp3201 mcp;
IAnalogInputPort port;
public override Task Initialize()
{
Resolver.Log.Info("Initialize");
IDigitalOutputPort chipSelectPort = Device.CreateDigitalOutputPort(Device.Pins.D14);
mcp = new Mcp3201(Device.CreateSpiBus(), chipSelectPort);
port = mcp.CreateAnalogInputPort();
port.Updated += (s, result) =>
{
Resolver.Log.Info($"Analog event, new voltage: {result.New.Volts}V, old: {result.Old?.Volts}V");
};
var observer = IAnalogInputPort.CreateObserver(
handler: result =>
{
Resolver.Log.Info($"Analog observer triggered; new: {result.New.Volts}V, old: {result.Old?.Volts}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();
Resolver.Log.Info("Run complete");
return Task.CompletedTask;
}
Sample project(s) available on GitHub
Syntax
public class Mcp3201 : Mcp3xxx, IAnalogInputController, IPinController, ISpiPeripheral, IDisposable
Constructors
Mcp3201(ISpiBus, IDigitalOutputPort)
Constructs Mcp3201 instance
Declaration
public Mcp3201(ISpiBus spiBus, IDigitalOutputPort chipSelectPort)
Parameters
Type | Name | Description |
---|---|---|
ISpiBus | spiBus | The SPI bus |
IDigitalOutputPort | chipSelectPort | Chip select port |
Remarks
Mcp3201 | |
---|---|
Status | |
Source code | GitHub |
Datasheet(s) | GitHub |
NuGet package |
Code Example
Mcp3201 mcp;
IAnalogInputPort port;
public override Task Initialize()
{
Resolver.Log.Info("Initialize");
IDigitalOutputPort chipSelectPort = Device.CreateDigitalOutputPort(Device.Pins.D14);
mcp = new Mcp3201(Device.CreateSpiBus(), chipSelectPort);
port = mcp.CreateAnalogInputPort();
port.Updated += (s, result) =>
{
Resolver.Log.Info($"Analog event, new voltage: {result.New.Volts}V, old: {result.Old?.Volts}V");
};
var observer = IAnalogInputPort.CreateObserver(
handler: result =>
{
Resolver.Log.Info($"Analog observer triggered; new: {result.New.Volts}V, old: {result.Old?.Volts}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();
Resolver.Log.Info("Run complete");
return Task.CompletedTask;
}
Mcp3201(ISpiBus, IPin)
Constructs Mcp3201 instance
Declaration
public Mcp3201(ISpiBus spiBus, IPin chipSelectPin)
Parameters
Type | Name | Description |
---|---|---|
ISpiBus | spiBus | The SPI bus |
IPin | chipSelectPin | Chip select pin |
Remarks
Mcp3201 | |
---|---|
Status | |
Source code | GitHub |
Datasheet(s) | GitHub |
NuGet package |
Code Example
Mcp3201 mcp;
IAnalogInputPort port;
public override Task Initialize()
{
Resolver.Log.Info("Initialize");
IDigitalOutputPort chipSelectPort = Device.CreateDigitalOutputPort(Device.Pins.D14);
mcp = new Mcp3201(Device.CreateSpiBus(), chipSelectPort);
port = mcp.CreateAnalogInputPort();
port.Updated += (s, result) =>
{
Resolver.Log.Info($"Analog event, new voltage: {result.New.Volts}V, old: {result.Old?.Volts}V");
};
var observer = IAnalogInputPort.CreateObserver(
handler: result =>
{
Resolver.Log.Info($"Analog observer triggered; new: {result.New.Volts}V, old: {result.Old?.Volts}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();
Resolver.Log.Info("Run complete");
return Task.CompletedTask;
}
Properties
Pins
The pins
Declaration
public Mcp3201.PinDefinitions Pins { get; }
Property Value
Type | Description |
---|---|
Mcp3201.PinDefinitions |
Remarks
Mcp3201 | |
---|---|
Status | |
Source code | GitHub |
Datasheet(s) | GitHub |
NuGet package |
Code Example
Mcp3201 mcp;
IAnalogInputPort port;
public override Task Initialize()
{
Resolver.Log.Info("Initialize");
IDigitalOutputPort chipSelectPort = Device.CreateDigitalOutputPort(Device.Pins.D14);
mcp = new Mcp3201(Device.CreateSpiBus(), chipSelectPort);
port = mcp.CreateAnalogInputPort();
port.Updated += (s, result) =>
{
Resolver.Log.Info($"Analog event, new voltage: {result.New.Volts}V, old: {result.Old?.Volts}V");
};
var observer = IAnalogInputPort.CreateObserver(
handler: result =>
{
Resolver.Log.Info($"Analog observer triggered; new: {result.New.Volts}V, old: {result.Old?.Volts}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();
Resolver.Log.Info("Run complete");
return Task.CompletedTask;
}
Methods
CreateAnalogInputPort(int)
Create an analog input port for a pin
Declaration
public IAnalogInputPort CreateAnalogInputPort(int sampleCount = 64)
Parameters
Type | Name | Description |
---|---|---|
int | sampleCount |
Returns
Type | Description |
---|---|
IAnalogInputPort |
Remarks
Mcp3201 | |
---|---|
Status | |
Source code | GitHub |
Datasheet(s) | GitHub |
NuGet package |
Code Example
Mcp3201 mcp;
IAnalogInputPort port;
public override Task Initialize()
{
Resolver.Log.Info("Initialize");
IDigitalOutputPort chipSelectPort = Device.CreateDigitalOutputPort(Device.Pins.D14);
mcp = new Mcp3201(Device.CreateSpiBus(), chipSelectPort);
port = mcp.CreateAnalogInputPort();
port.Updated += (s, result) =>
{
Resolver.Log.Info($"Analog event, new voltage: {result.New.Volts}V, old: {result.Old?.Volts}V");
};
var observer = IAnalogInputPort.CreateObserver(
handler: result =>
{
Resolver.Log.Info($"Analog observer triggered; new: {result.New.Volts}V, old: {result.Old?.Volts}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();
Resolver.Log.Info("Run complete");
return Task.CompletedTask;
}
CreateAnalogInputPort(int, TimeSpan, Voltage)
Create an analog input port for a pin
Declaration
public IAnalogInputPort CreateAnalogInputPort(int sampleCount, TimeSpan sampleInterval, Voltage voltageReference)
Parameters
Type | Name | Description |
---|---|---|
int | sampleCount | |
TimeSpan | sampleInterval | |
Voltage | voltageReference |
Returns
Type | Description |
---|---|
IAnalogInputPort |
Remarks
Mcp3201 | |
---|---|
Status | |
Source code | GitHub |
Datasheet(s) | GitHub |
NuGet package |
Code Example
Mcp3201 mcp;
IAnalogInputPort port;
public override Task Initialize()
{
Resolver.Log.Info("Initialize");
IDigitalOutputPort chipSelectPort = Device.CreateDigitalOutputPort(Device.Pins.D14);
mcp = new Mcp3201(Device.CreateSpiBus(), chipSelectPort);
port = mcp.CreateAnalogInputPort();
port.Updated += (s, result) =>
{
Resolver.Log.Info($"Analog event, new voltage: {result.New.Volts}V, old: {result.Old?.Volts}V");
};
var observer = IAnalogInputPort.CreateObserver(
handler: result =>
{
Resolver.Log.Info($"Analog observer triggered; new: {result.New.Volts}V, old: {result.Old?.Volts}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();
Resolver.Log.Info("Run complete");
return Task.CompletedTask;
}
IsInputTypeSupported(InputType)
Is the input type supported on this MCP3xxx version
Declaration
public override bool IsInputTypeSupported(Mcp3xxx.InputType inputType)
Parameters
Type | Name | Description |
---|---|---|
Mcp3xxx.InputType | inputType | The input type |
Returns
Type | Description |
---|---|
bool | True if supported, false if not supported |
Overrides
Remarks
Mcp3201 | |
---|---|
Status | |
Source code | GitHub |
Datasheet(s) | GitHub |
NuGet package |
Code Example
Mcp3201 mcp;
IAnalogInputPort port;
public override Task Initialize()
{
Resolver.Log.Info("Initialize");
IDigitalOutputPort chipSelectPort = Device.CreateDigitalOutputPort(Device.Pins.D14);
mcp = new Mcp3201(Device.CreateSpiBus(), chipSelectPort);
port = mcp.CreateAnalogInputPort();
port.Updated += (s, result) =>
{
Resolver.Log.Info($"Analog event, new voltage: {result.New.Volts}V, old: {result.Old?.Volts}V");
};
var observer = IAnalogInputPort.CreateObserver(
handler: result =>
{
Resolver.Log.Info($"Analog observer triggered; new: {result.New.Volts}V, old: {result.Old?.Volts}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();
Resolver.Log.Info("Run complete");
return Task.CompletedTask;
}
ReadInternal(int, InputType, int)
Reads a value from the device
Declaration
protected override int ReadInternal(int channel, Mcp3xxx.InputType inputType, int adcResolutionBits)
Parameters
Type | Name | Description |
---|---|---|
int | channel | Channel to read - for differential inputs this represents a channel pair (valid values: 0 - channelcount - 1 or 0 - channelcount / 2 - 1 with differential inputs) |
Mcp3xxx.InputType | inputType | The type of input channel to read |
int | adcResolutionBits | The number of bits in the returned value |
Returns
Type | Description |
---|---|
int | A value corresponding to relative voltage level on specified device channel |
Overrides
Remarks
Mcp3201 | |
---|---|
Status | |
Source code | GitHub |
Datasheet(s) | GitHub |
NuGet package |
Code Example
Mcp3201 mcp;
IAnalogInputPort port;
public override Task Initialize()
{
Resolver.Log.Info("Initialize");
IDigitalOutputPort chipSelectPort = Device.CreateDigitalOutputPort(Device.Pins.D14);
mcp = new Mcp3201(Device.CreateSpiBus(), chipSelectPort);
port = mcp.CreateAnalogInputPort();
port.Updated += (s, result) =>
{
Resolver.Log.Info($"Analog event, new voltage: {result.New.Volts}V, old: {result.Old?.Volts}V");
};
var observer = IAnalogInputPort.CreateObserver(
handler: result =>
{
Resolver.Log.Info($"Analog observer triggered; new: {result.New.Volts}V, old: {result.Old?.Volts}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();
Resolver.Log.Info("Run complete");
return Task.CompletedTask;
}