This repository was archived by the owner on Jun 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathSendSPIData.cs
More file actions
74 lines (60 loc) · 2.67 KB
/
SendSPIData.cs
File metadata and controls
74 lines (60 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
using System;
using System.Management.Automation;
using System.Device.Spi;
using System.Device.Gpio;
[Cmdlet(VerbsCommunications.Send, "SPIData")]
public class SendSPIData : Cmdlet
{
[Parameter(Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, Position = 0)]
public byte[] Data { get; set; }
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, Position = 1)]
public int BusId { get; set; }
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, Position = 2)]
public int ChipSelectLine { get; set; }
[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; }
public SendSPIData()
{
this.BusId = 0;
this.ChipSelectLine = 0;
this.Frequency = 500_000; // 500 KHz default speed
}
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
};
using (var spiDevice = SpiDevice.Create(settings))
{
var response = new byte[this.Data.Length];
spiDevice.TransferFullDuplex(this.Data, response);
if (this.Raw)
{
WriteObject(response);
}
else
{
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);
}
}
}
}