PowerShell / PowerShell-IoT

Interact with I2C, SPI & GPIO devices using PowerShell Core!
https://www.powershellgallery.com/packages/Microsoft.PowerShell.IoT
MIT License
130 stars 28 forks source link

Set-i2cdevice overload to support arrays #35

Open DanielSSilva opened 6 years ago

DanielSSilva commented 6 years ago

I think it would be good to have an overload that would accept an array of registers and an array of values to be set on those registers, 'freeing' the developer to have the necessity to create a loop to iterate each array and set each value

TylerLeonhardt commented 6 years ago

Thanks for this Daniel! Did you mean Set-I2CRegister?

DanielSSilva commented 6 years ago

Oh yes... I wrote it from my memory ๐Ÿ˜…

TylerLeonhardt commented 6 years ago

This is the case of really bad documentation but this is already supported!

https://github.com/PowerShell/PowerShell-IoT/blob/master/src/Microsoft.PowerShell.IoT/Microsoft.PowerShell.IoT.cs#L185-L189

Device, Register and Data are ValueFromPipelineByPropertyName ๐Ÿ˜„

TylerLeonhardt commented 6 years ago

So if you have a custom object array and pass that into Set-I2CRegister it should work:

$device = Get-I2CDevice -Id 0x44 -FriendlyName Foo

$arr = @(
[pscustomobject]@{
    Register = 0x12
    Data = @(1,2,3)
    Device = $device
},
[pscustomobject]@{
    Register = 0x77
    Data = @(2,3,4)
    Device = $device
})

$arr | Set-I2CRegister

Note: haven't tested that code ^ but I'm pretty sure it works.

DanielSSilva commented 6 years ago

Oh I see... although it's only valid to set an array of values into a register, not a 1 to 1 relation, aka, 1 register 1 value. But this might only be useful in my case, i don't know...

TylerLeonhardt commented 6 years ago

You want to do 1 value to many registers?

DanielSSilva commented 6 years ago

No. I want an array of 5 registers and an array of 5 values and use register[i] value[i]. But then again, I don't have much experience to say that this would be a common situation on I2C protocol... Might only be useful in my case

TylerLeonhardt commented 6 years ago

Wait I just realized that you said "not a 1 to 1 relation" to my example above.

My example above IS a 1 to 1 example.

You have an array of custom objects that contain a register and a value property. When you pipe the array into Set-I2CRegister it handles each item in the array one by one and each item in the array has a register and a value sรณ that's a 1 register to 1 value operation.