PowerShell-IoT icon indicating copy to clipboard operation
PowerShell-IoT copied to clipboard

Set-i2cdevice overload to support arrays

Open DanielSSilva opened this issue 7 years ago • 8 comments

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

DanielSSilva avatar Apr 21 '18 10:04 DanielSSilva

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

TylerLeonhardt avatar Apr 21 '18 23:04 TylerLeonhardt

Oh yes... I wrote it from my memory 😅

DanielSSilva avatar Apr 21 '18 23:04 DanielSSilva

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 avatar Apr 21 '18 23:04 TylerLeonhardt

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.

TylerLeonhardt avatar Apr 21 '18 23:04 TylerLeonhardt

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...

DanielSSilva avatar Apr 22 '18 18:04 DanielSSilva

You want to do 1 value to many registers?

TylerLeonhardt avatar Apr 22 '18 19:04 TylerLeonhardt

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

DanielSSilva avatar Apr 23 '18 08:04 DanielSSilva

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.

TylerLeonhardt avatar Apr 23 '18 14:04 TylerLeonhardt