-
Notifications
You must be signed in to change notification settings - Fork 28
Add more settings to SPI, and possibly add a Get-SPIDevice #54
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
using System; | ||
using System.Management.Automation; | ||
using System.Device.Spi; | ||
using System.Device.Gpio; | ||
|
||
[Cmdlet(VerbsCommon.Get, "SPIDevice")] | ||
public class GetSPIDevice : Cmdlet | ||
{ | ||
|
||
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, Position = 0)] | ||
public int BusId { get; set; } | ||
|
||
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, Position = 1)] | ||
public int ChipSelectLine { get; set; } | ||
|
||
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, Position = 2)] | ||
public int Frequency { get; set; } | ||
|
||
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, Position = 3)] | ||
public int DataBitLength { get; set;} | ||
|
||
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, Position = 4)] | ||
public SpiMode Mode { get; set; } | ||
|
||
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, Position = 5)] | ||
public DataFlow DataFlow { get; set; } | ||
|
||
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, Position = 6)] | ||
public PinValue ChipSelectLineActiveState { get; set; } | ||
|
||
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true)] | ||
public SwitchParameter Raw { get; set; } | ||
|
||
public GetSPIDevice() | ||
{ | ||
this.BusId = 0; | ||
this.ChipSelectLine = 0; | ||
this.Frequency = 500_000; // 500 KHz default speed | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. defaults for other properties also have to be set here. They can be taken from |
||
} | ||
|
||
protected override void ProcessRecord() | ||
{ | ||
var settings = new SpiConnectionSettings(this.BusId, this.ChipSelectLine) | ||
{ | ||
ClockFrequency = this.Frequency, | ||
DataBitLength = this.DataBitLength, | ||
Mode = this.Mode, | ||
DataFlow = this.DataFlow, | ||
ChipSelectLineActiveState = this.ChipSelectLineActiveState | ||
}; | ||
|
||
SpiDevice spiDevice = SpiDevice.Create(settings); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this should return our |
||
//TODO: This can be done this way if we follow the same logic as in I2C where we access the device by doing device.Device | ||
// WriteObject(new SPIDevice(spiDevice, this.BusId, this.ChipSelectLine, this.Frequency, | ||
// this.DataBitLength, this.Mode, this.DataFlow, | ||
// this.ChipSelectLineActiveState)); | ||
//Because I'm currently testing this like this : $device = Get-SPIDevice -Frequency 2400000 -Mode Mode0 -DataBitLength 8 -BusId 0 -ChipSelectLine 0 | ||
// I want the returned object to be the spiDevice created. | ||
WriteObject(spiDevice); | ||
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,34 @@ | ||
public class SPIData | ||
// public class SPIData | ||
// { | ||
// public int BusId { get; set; } | ||
// public int ChipSelectLine { get; set; } | ||
// public int Frequency { get; set; } | ||
// public byte[] Data { get; set; } | ||
// public byte[] Response { get; set; } | ||
|
||
// public SPIData(int busId, int chipSelectLine, int frequency, byte[] data, byte[] response) | ||
// { | ||
// this.BusId = busId; | ||
// this.ChipSelectLine = chipSelectLine; | ||
// this.Frequency = frequency; | ||
// this.Data = data; | ||
// this.Response = response; | ||
// } | ||
// } | ||
|
||
using System.Device.Gpio; | ||
anmenaga marked this conversation as resolved.
Show resolved
Hide resolved
|
||
using System.Device.Spi; | ||
|
||
public class SPIData : SPIDevice | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just have another There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not familiar with best practices for such cases. Why shouldn't it inherit from There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, it is very tempting to inherit |
||
{ | ||
public int BusId { get; set; } | ||
public int ChipSelectLine { get; set; } | ||
public int Frequency { get; set; } | ||
public byte[] Data { get; set; } | ||
public byte[] Response { get; set; } | ||
|
||
public SPIData(int busId, int chipSelectLine, int frequency, byte[] data, byte[] response) | ||
public SPIData(SpiDevice device, int busId, int chipSelectLine, int frequency, | ||
int dataBitLength, SpiMode mode, DataFlow dataFlow, | ||
PinValue chipSelectLineActiveState, byte[] data, byte[] response | ||
): base(device, busId, chipSelectLine, frequency, dataBitLength, mode, dataFlow, chipSelectLineActiveState) | ||
{ | ||
this.BusId = busId; | ||
this.ChipSelectLine = chipSelectLine; | ||
this.Frequency = frequency; | ||
this.Data = data; | ||
this.Response = response; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using System.Device.Gpio; | ||
using System.Device.Spi; | ||
|
||
public class SPIDevice | ||
{ | ||
internal SpiDevice device = null; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't see a problem if this class just holds 2 public properties:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree with this, but it would make sense to "expand" the connectionSettings when they are being displayed, like overriding the ToString There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can probably be done in a type format file for |
||
public int BusId { get; set; } | ||
|
||
public int ChipSelectLine { get; set; } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should these properties be public settable? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've followed the same implementation as we have for I2C (here). What should happen if the user changed their hardware from busId 0 to 1, or the ChipSelectLine? Should he create a new device? Or should we allow to change the busId and ChipSelectLine? If we want to bind a device to a busId and ChipSelectLine, this should not be settable. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. High level thinking is that these properties here are There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just to clarify, this means that all properties should be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you consider readonly struct for this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Correct.
This seems alright. |
||
|
||
public int Frequency { get; set; } | ||
|
||
public int DataBitLength { get; set;} | ||
|
||
public SpiMode Mode { get; set; } | ||
|
||
public DataFlow DataFlow { get; set; } | ||
|
||
public PinValue ChipSelectLineActiveState { get; set; } | ||
|
||
public SPIDevice(SpiDevice device, int busId, int chipSelectLine, int frequency, | ||
int dataBitLength, SpiMode mode, DataFlow dataFlow, | ||
PinValue chipSelectLineActiveState) | ||
{ | ||
this.device = device; | ||
this.BusId = busId; | ||
this.ChipSelectLine = chipSelectLine; | ||
this.Frequency = frequency; | ||
this.DataBitLength = dataBitLength; | ||
this.Mode = mode; | ||
this.DataFlow = dataFlow; | ||
this.ChipSelectLineActiveState = chipSelectLineActiveState; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
using System; | ||
using System.Management.Automation; | ||
using System.Device.Spi; | ||
using System.Device.Gpio; | ||
|
||
[Cmdlet(VerbsCommunications.Send, "SPIData")] | ||
public class SendSPIData : Cmdlet | ||
|
@@ -17,6 +18,18 @@ public class SendSPIData : Cmdlet | |
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, Position = 3)] | ||
public int Frequency { get; set; } | ||
|
||
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, Position = 4)] | ||
public int DataBitLength { get; set;} | ||
|
||
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, Position = 5)] | ||
public SpiMode Mode { get; set; } | ||
|
||
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, Position = 6)] | ||
public DataFlow DataFlow { get; set; } | ||
|
||
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, Position = 7)] | ||
public PinValue ChipSelectLineActiveState { get; set; } | ||
|
||
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true)] | ||
public SwitchParameter Raw { get; set; } | ||
|
||
|
@@ -29,10 +42,16 @@ public SendSPIData() | |
|
||
protected override void ProcessRecord() | ||
{ | ||
var settings = new SpiConnectionSettings(this.BusId, this.ChipSelectLine); | ||
settings.ClockFrequency = this.Frequency; | ||
|
||
using(var spiDevice = SpiDevice.Create(settings)) | ||
var settings = new SpiConnectionSettings(this.BusId, this.ChipSelectLine) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Constructing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This cmdlet can just take |
||
{ | ||
ClockFrequency = this.Frequency, | ||
DataBitLength = this.DataBitLength, | ||
Mode = this.Mode, | ||
DataFlow = this.DataFlow, | ||
ChipSelectLineActiveState = this.ChipSelectLineActiveState | ||
}; | ||
Comment on lines
+50
to
+57
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we be concerned about overwriting defaults here if they aren't specified via the cmdlet? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we should be concerned because if those fields are not specified, they will have their defaults (0 for int, etc), which already happens with our current version. DataBitLength wasn't specified, hence it was always 0, etc There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should Not rely on types' default values. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Totally agree. I was relying on the defaults because from what I saw they seemed to be the .NET defaults. Checking again, I was wrong. I was probably checking the wrong file . |
||
|
||
using (var spiDevice = SpiDevice.Create(settings)) | ||
{ | ||
var response = new byte[this.Data.Length]; | ||
|
||
|
@@ -44,7 +63,10 @@ protected override void ProcessRecord() | |
} | ||
else | ||
{ | ||
SPIData spiData = new SPIData(this.BusId, this.ChipSelectLine, this.Frequency, this.Data, response); | ||
SPIData spiData = new SPIData(spiDevice, this.BusId, this.ChipSelectLine, this.Frequency, | ||
this.DataBitLength, this.Mode, this.DataFlow, | ||
this.ChipSelectLineActiveState, this.Data, response); | ||
//SPIData spiData = new SPIData(this.BusId, this.ChipSelectLine, this.Frequency, this.Data, response); | ||
WriteObject(spiData); | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks like this is not used anywhere so can be removed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't understand from GH interface what property you are referring to. The Raw?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes;
Raw
.