I'm writing code to bring R to Raspberry Pi. Currently I'm working on adding SPI (serial peripheral interface) support to the package. But don't get hung up on that just yet.
SPI lends itself to OOP, so I'm using S7 (formerly R7)
To speak to an SPI device, you have to first open it (similar to a file). I'd like to do that when creating a new_class("spi", etc....). I suspect the best way is to do that with a constructor (vs a function in new_property() ). But there will also be values passed in at the time of creation of the object.
Consider the following code...
spi <- new_class("spi",
properties = list(
spiChan = class_integer,
spiBus = class_integer,
max_speed_hz = new_property(class_integer, default = 125000000),
spiDeviceID = class_integer
)
,
validator = function(self) {
if (self@spiChan != 0 || self@spiChan != 1) {
"@spiChan must be zero or one"
} else if (TRUE) {
"@spiBus must be something"
}
}
)
...then, to create an object...
mySPI <- spi(spiChan = 0, spiBus = 0)
At the time of object creation, the spi
class should use spiChan
and spiBus
to open a connection to the spi device, then return (and save) spiDeviceID
as a property of the new object.
Question: Can I just define the spiDeviceID value under properties
as new_property(class_integer, default = function(x) {#spi open code goes here})
Question: If I should instead create a constructor, do I really need to define ALL of the property values in the constructor? Default values is easy enough, but what about the values passed in for spiChan
and spiBus
?
Question: If so, how do I pass values to the constructor?**
Thank you, oh thank you oh geni of R
MNR