S7 constructors & default values for properties

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

I assume it's the journey, not the destination? If it's the destination that wheel has been invented by R4PI, but since you're deep in the rough, it's got to be the journey.

The geni locii here are thin on the ground of S7 and its R7 former name except for one guy: user name andresrcs. (Just add @ to direct message him for a request to look at the question here—etiquette is to refrain from bringing in users to a thread by direct linking.)

Thanks!

I'm using R4pi as well - great stuff. I should have been more specific - I'm writing a package to provide access to the Raspberry Pi GPIO, including PWM, GPIO, I2C, and now SPI. UART to come.

I'll contact andresrcs - Thanks for the contact

MNR

1 Like

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.