Skip to content

Commit

Permalink
Add default extra methods to board
Browse files Browse the repository at this point in the history
  • Loading branch information
njooma committed Jun 27, 2024
1 parent 0b2ac34 commit 8b5bbe9
Show file tree
Hide file tree
Showing 11 changed files with 417 additions and 459 deletions.
145 changes: 120 additions & 25 deletions core/sdk/src/main/kotlin/com/viam/sdk/core/component/board/Board.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ typealias Tick = StreamTicksResponse
abstract class Board(name: String) : Component(SUBTYPE, named(name)) {
companion object {
@JvmField
val SUBTYPE =
Subtype(Subtype.NAMESPACE_RDK, Subtype.RESOURCE_TYPE_COMPONENT, "board")
val SUBTYPE = Subtype(Subtype.NAMESPACE_RDK, Subtype.RESOURCE_TYPE_COMPONENT, "board")

/**
* Get the ResourceName of the component
Expand Down Expand Up @@ -51,94 +50,190 @@ abstract class Board(name: String) : Component(SUBTYPE, named(name)) {
* @param pin the name of the GPIO pin
* @param high when true, sets the pin to high. When false, sets the pin to low.
*/
abstract fun setGpioState(pin: String, high: Boolean, extra: Optional<Struct>)
abstract fun setGpioState(pin: String, high: Boolean, extra: Struct)

/**
* Set the high/low state of the given pin of a board.
* @param pin the name of the GPIO pin
* @param high when true, sets the pin to high. When false, sets the pin to low.
*/
fun setGpioState(pin: String, high: Boolean) {
return setGpioState(pin, high, Struct.getDefaultInstance())
}

/**
* Get the high/low state of the given pin of a board.
* @param pin the name of the GPIO pin
* @return the state of the pin: true if high, false otherwise.
*/
abstract fun getGpioState(pin: String, extra: Optional<Struct>): Boolean
abstract fun getGpioState(pin: String, extra: Struct): Boolean

/**
* Get the high/low state of the given pin of a board.
* @param pin the name of the GPIO pin
* @return the state of the pin: true if high, false otherwise.
*/
fun getGpioState(pin: String): Boolean {
return getGpioState(pin, Struct.getDefaultInstance())
}

/**
* Set the duty cycle of the given pin of a board.
* @param pin the name of the GPIO pin
* @param dutyCyclePct the duty cycle percent
*/
abstract fun setPwm(pin: String, dutyCyclePct: Double, extra: Struct)

/**
* Set the duty cycle of the given pin of a board.
* @param pin the name of the GPIO pin
* @param dutyCyclePct the duty cycle percent
*/
abstract fun setPwm(pin: String, dutyCyclePct: Double, extra: Optional<Struct>)
fun setPwm(pin: String, dutyCyclePct: Double) {
return setPwm(pin, dutyCyclePct, Struct.getDefaultInstance())
}

/**
* Get the duty cycle of the given pin of a board.
* @param pin the name of the pin
* @returns the duty cycle percent
*/
abstract fun getPwm(pin: String, extra: Struct): Double

/**
* Get the duty cycle of the given pin of a board.
* @param pin the name of the pin
* @returns the duty cycle percent
*/
abstract fun getPwm(pin: String, extra: Optional<Struct>): Double
fun getPwm(pin: String): Double {
return getPwm(pin, Struct.getDefaultInstance())
}

/**
* Set the PWM frequency of the given pin of a board.
* @param pin the name of the pin
* @param frequencyHz the frequency to set
*/
abstract fun setPwmFrequency(pin: String, frequencyHz: Int, extra: Optional<Struct>)
abstract fun setPwmFrequency(pin: String, frequencyHz: Int, extra: Struct)

/**
* Set the PWM frequency of the given pin of a board.
* @param pin the name of the pin
* @param frequencyHz the frequency to set
*/
fun setPwmFrequency(pin: String, frequencyHz: Int) {
return setPwmFrequency(pin, frequencyHz, Struct.getDefaultInstance())
}

/**
* Get the PWM frequency of the given pin of a board.
* @param pin the name of the pin
* @returns the frequency of the pin in Hz
*/
abstract fun getPwmFrequency(pin: String, extra: Struct): Int

/**
* Get the PWM frequency of the given pin of a board.
* @param pin the name of the pin
* @returns the frequency of the pin in Hz
*/
abstract fun getPwmFrequency(pin: String, extra: Optional<Struct>): Int
fun getPwmFrequency(pin: String): Int {
return getPwmFrequency(pin, Struct.getDefaultInstance())
}

/**
* Write analog value to pin.
* @param pin the name of the pin
* @param value the value to set
*/
abstract fun writeAnalog(pin: String, value: Int, extra: Optional<Struct>)
abstract fun writeAnalog(pin: String, value: Int, extra: Struct)

/**
* Write analog value to pin.
* @param pin the name of the pin
* @param value the value to set
*/
fun writeAnalog(pin: String, value: Int) {
return writeAnalog(pin, value, Struct.getDefaultInstance())
}

/**
* Read the current value of an analog reader of a board.
* @param analogReader the name of the analog reader
* @returns the current value of the analog reader
*/
abstract fun getAnalogReaderValue(analogReader: String, extra: Optional<Struct>): Int
abstract fun getAnalogReaderValue(analogReader: String, extra: Struct): Int

/**
* Read the current value of an analog reader of a board.
* @param analogReader the name of the analog reader
* @returns the current value of the analog reader
*/
fun getAnalogReaderValue(analogReader: String): Int {
return getAnalogReaderValue(analogReader, Struct.getDefaultInstance())
}

/**
* Return the current value of the interrupt which is based on the type of Interrupt.
* @param digitalInterrupt the name of the digital interrupt
* @returns the current value of the digital reader
*/
abstract fun getDigitalInterruptValue(digitalInterrupt: String, extra: Struct): Int

/**
* Return the current value of the interrupt which is based on the type of Interrupt.
* @param digitalInterrupt the name of the digital interrupt
* @returns the current value of the digital reader
*/
abstract fun getDigitalInterruptValue(
digitalInterrupt: String,
extra: Optional<Struct>
): Int
fun getDigitalInterruptValue(digitalInterrupt: String): Int {
return getDigitalInterruptValue(digitalInterrupt, Struct.getDefaultInstance())
}

/**
* Stream digital interrupts ticks.
* @param interrupts the list of digital interrupts names from which to receive ticks
* @returns a [Stream] of [Tick] objects
*/
abstract fun streamTicks(interrupts: List<String>, extra: Optional<Struct>): Iterator<Tick>
abstract fun streamTicks(interrupts: List<String>, extra: Struct): Iterator<Tick>

/**
* Stream digital interrupts ticks.
* @param interrupts the list of digital interrupts names from which to receive ticks
* @returns a [Stream] of [Tick] objects
*/
fun streamTicks(interrupts: List<String>): Iterator<Tick> {
return streamTicks(interrupts, Struct.getDefaultInstance())
}

/**
* Add a listener for the digital interrupts.
* @param interrupts the list of digital interrupts names from which to receive ticks
* @param tickQueue an object to receive values from the callback
*/
abstract fun addCallbacks(interrupts: List<String>, tickQueue: Queue<Tick>, extra: Struct)

/**
* Add a listener for the digital interrupts.
* @param interrupts the list of digital interrupts names from which to receive ticks
* @param tickQueue an object to receive values from the callback
*/
abstract fun addCallbacks(
interrupts: List<String>,
tickQueue: Queue<Tick>,
extra: Optional<Struct>
)
fun addCallbacks(interrupts: List<String>, tickQueue: Queue<Tick>) {
return addCallbacks(interrupts, tickQueue, Struct.getDefaultInstance())
}

/**
* Set the board to the indicated power mode.
* @param powerMode the power mode to set
* @param duration if provided, the board will exit the given power mode after this duration
*/
abstract fun setPowerMode(
powerMode: PowerMode,
duration: Duration,
extra: Optional<Struct>
)
abstract fun setPowerMode(powerMode: PowerMode, duration: Duration, extra: Struct)

/**
* Set the board to the indicated power mode.
* @param powerMode the power mode to set
* @param duration if provided, the board will exit the given power mode after this duration
*/
fun setPowerMode(powerMode: PowerMode, duration: Duration) {
return setPowerMode(powerMode, duration, Struct.getDefaultInstance())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,118 +29,77 @@ class BoardRPCClient(name: String, channel: Channel) : Board(name) {
}
}

override fun setGpioState(pin: String, high: Boolean, extra: Optional<Struct>) {
val request = SetGPIORequest.newBuilder()
.setName(this.name.name)
.setPin(pin).setHigh(high)
.setExtra(extra.getOrDefault(Struct.getDefaultInstance()))
.build()
override fun setGpioState(pin: String, high: Boolean, extra: Struct) {
val request =
SetGPIORequest.newBuilder().setName(this.name.name).setPin(pin).setHigh(high).setExtra(extra).build()
this.client.setGPIO(request)
}

override fun getGpioState(pin: String, extra: Optional<Struct>): Boolean {
val request = GetGPIORequest.newBuilder()
.setName(this.name.name)
.setPin(pin)
.setExtra(extra.getOrDefault(Struct.getDefaultInstance()))
.build()
override fun getGpioState(pin: String, extra: Struct): Boolean {
val request = GetGPIORequest.newBuilder().setName(this.name.name).setPin(pin).setExtra(extra).build()
return this.client.getGPIO(request).high
}

override fun setPwm(pin: String, dutyCyclePct: Double, extra: Optional<Struct>) {
val request = SetPWMRequest.newBuilder()
.setName(this.name.name)
.setPin(pin)
.setDutyCyclePct(dutyCyclePct)
.setExtra(extra.getOrDefault(Struct.getDefaultInstance()))
.build()
override fun setPwm(pin: String, dutyCyclePct: Double, extra: Struct) {
val request =
SetPWMRequest.newBuilder().setName(this.name.name).setPin(pin).setDutyCyclePct(dutyCyclePct).setExtra(extra)
.build()
this.client.setPWM(request)
}

override fun getPwm(pin: String, extra: Optional<Struct>): Double {
val request = PWMRequest.newBuilder()
.setName(this.name.name)
.setPin(pin)
.setExtra(extra.getOrDefault(Struct.getDefaultInstance()))
.build()
override fun getPwm(pin: String, extra: Struct): Double {
val request = PWMRequest.newBuilder().setName(this.name.name).setPin(pin).setExtra(extra).build()
return this.client.pWM(request).dutyCyclePct
}

override fun setPwmFrequency(pin: String, frequencyHz: Int, extra: Optional<Struct>) {
val request = SetPWMFrequencyRequest.newBuilder()
.setName(this.name.name)
.setPin(pin)
.setFrequencyHz(frequencyHz.toLong())
.setExtra(extra.getOrDefault(Struct.getDefaultInstance()))
.build()
override fun setPwmFrequency(pin: String, frequencyHz: Int, extra: Struct) {
val request =
SetPWMFrequencyRequest.newBuilder().setName(this.name.name).setPin(pin).setFrequencyHz(frequencyHz.toLong())
.setExtra(extra).build()
this.client.setPWMFrequency(request)
}

override fun getPwmFrequency(pin: String, extra: Optional<Struct>): Int {
val request = PWMFrequencyRequest.newBuilder()
.setName(this.name.name)
.setPin(pin)
.setExtra(extra.getOrDefault(Struct.getDefaultInstance()))
.build()
override fun getPwmFrequency(pin: String, extra: Struct): Int {
val request = PWMFrequencyRequest.newBuilder().setName(this.name.name).setPin(pin).setExtra(extra).build()
return this.client.pWMFrequency(request).frequencyHz.toInt()
}

override fun writeAnalog(pin: String, value: Int, extra: Optional<Struct>) {
val request = WriteAnalogRequest.newBuilder()
.setName(this.name.name)
.setPin(pin)
.setValue(value)
.setExtra(extra.getOrDefault(Struct.getDefaultInstance()))
.build()
override fun writeAnalog(pin: String, value: Int, extra: Struct) {
val request =
WriteAnalogRequest.newBuilder().setName(this.name.name).setPin(pin).setValue(value).setExtra(extra).build()
this.client.writeAnalog(request)
}

override fun getAnalogReaderValue(analogReader: String, extra: Optional<Struct>): Int {
val request = ReadAnalogReaderRequest.newBuilder()
.setBoardName(this.name.name)
.setAnalogReaderName(analogReader)
.setExtra(extra.getOrDefault(Struct.getDefaultInstance()))
.build()
override fun getAnalogReaderValue(analogReader: String, extra: Struct): Int {
val request =
ReadAnalogReaderRequest.newBuilder().setBoardName(this.name.name).setAnalogReaderName(analogReader)
.setExtra(extra).build()
return this.client.readAnalogReader(request).value
}

override fun getDigitalInterruptValue(digitalInterrupt: String, extra: Optional<Struct>): Int {
val request = GetDigitalInterruptValueRequest.newBuilder()
.setBoardName(this.name.name)
.setDigitalInterruptName(digitalInterrupt)
.setExtra(extra.getOrDefault(Struct.getDefaultInstance()))
.build()
override fun getDigitalInterruptValue(digitalInterrupt: String, extra: Struct): Int {
val request = GetDigitalInterruptValueRequest.newBuilder().setBoardName(this.name.name)
.setDigitalInterruptName(digitalInterrupt).setExtra(extra).build()
return this.client.getDigitalInterruptValue(request).value.toInt()
}

override fun streamTicks(interrupts: List<String>, extra: Optional<Struct>): Iterator<Tick> {
val request = StreamTicksRequest.newBuilder()
.setName(this.name.name)
.addAllPinNames(interrupts)
.setExtra(extra.getOrDefault(Struct.getDefaultInstance()))
.build()
override fun streamTicks(interrupts: List<String>, extra: Struct): Iterator<Tick> {
val request =
StreamTicksRequest.newBuilder().setName(this.name.name).addAllPinNames(interrupts).setExtra(extra).build()
return this.client.streamTicks(request)
}

override fun addCallbacks(
interrupts: List<String>,
tickQueue: Queue<Tick>,
extra: Optional<Struct>
interrupts: List<String>, tickQueue: Queue<Tick>, extra: Struct
) {
throw MethodNotImplementedException("BoardRPCClient.addCallbacks")
}

override fun setPowerMode(
powerMode: PowerMode,
duration: Duration,
extra: Optional<Struct>
powerMode: PowerMode, duration: Duration, extra: Struct
) {
val request = SetPowerModeRequest.newBuilder()
.setName(this.name.name)
.setPowerMode(powerMode)
.setDuration(Durations.fromNanos(duration.inWholeNanoseconds))
.setExtra(extra.getOrDefault(Struct.getDefaultInstance()))
.build()
val request = SetPowerModeRequest.newBuilder().setName(this.name.name).setPowerMode(powerMode)
.setDuration(Durations.fromNanos(duration.inWholeNanoseconds)).setExtra(extra).build()
this.client.setPowerMode(request)
}

Expand Down
Loading

0 comments on commit 8b5bbe9

Please sign in to comment.