Skip to content
This repository was archived by the owner on Jun 14, 2024. It is now read-only.

SPI cmdlet #13

Merged
merged 1 commit into from
Mar 26, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions src/Microsoft.PowerShell.IoT/Microsoft.PowerShell.IoT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,22 @@ public GpioPinData(int id, SignalLevel value, Unosquare.RaspberryIO.Gpio.GpioPin
this.Value = value;
this.PinInfo = pinInfo;
}
}

public class SPIData
{
public uint Channel { get; set; }
public uint Frequency { get; set; }
public byte[] Data { get; set; }
public byte[] Responce { get; set; }

public SPIData(uint channel, uint frequency, byte[] data, byte[] responce)
{
this.Channel = channel;
this.Frequency = frequency;
this.Data = data;
this.Responce = responce;
}
}

[Cmdlet(VerbsCommon.Get, "I2CDevice")]
Expand Down Expand Up @@ -276,4 +292,51 @@ protected override void ProcessRecord()
}
}
}

[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 uint Channel { get; set; }

[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, Position = 2)]
public uint Frequency { get; set; }

[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true)]
public SwitchParameter Raw { get; set; }

public SendSPIData()
{
this.Channel = 0;
this.Frequency = Unosquare.RaspberryIO.Gpio.SpiChannel.MinFrequency;
}

protected override void ProcessRecord()
{
var spiChannel = Unosquare.RaspberryIO.Pi.Spi.Channel0;
if (this.Channel == 1)
{
spiChannel = Unosquare.RaspberryIO.Pi.Spi.Channel1;
Unosquare.RaspberryIO.Pi.Spi.Channel1Frequency = (int)this.Frequency;
}
else
{
Unosquare.RaspberryIO.Pi.Spi.Channel0Frequency = (int)this.Frequency;
};

var responce = spiChannel.SendReceive(this.Data);
if (this.Raw)
{
WriteObject(responce);
}
else
{
SPIData spiData = new SPIData(this.Channel, this.Frequency, this.Data, responce);
WriteObject(spiData);
}
}
}
}