Skip to content

Commit ef77c7a

Browse files
authored
Add default extra methods to board (#44)
1 parent 0b2ac34 commit ef77c7a

File tree

11 files changed

+417
-459
lines changed

11 files changed

+417
-459
lines changed

core/sdk/src/main/kotlin/com/viam/sdk/core/component/board/Board.kt

+120-25
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ typealias Tick = StreamTicksResponse
2121
abstract class Board(name: String) : Component(SUBTYPE, named(name)) {
2222
companion object {
2323
@JvmField
24-
val SUBTYPE =
25-
Subtype(Subtype.NAMESPACE_RDK, Subtype.RESOURCE_TYPE_COMPONENT, "board")
24+
val SUBTYPE = Subtype(Subtype.NAMESPACE_RDK, Subtype.RESOURCE_TYPE_COMPONENT, "board")
2625

2726
/**
2827
* Get the ResourceName of the component
@@ -51,94 +50,190 @@ abstract class Board(name: String) : Component(SUBTYPE, named(name)) {
5150
* @param pin the name of the GPIO pin
5251
* @param high when true, sets the pin to high. When false, sets the pin to low.
5352
*/
54-
abstract fun setGpioState(pin: String, high: Boolean, extra: Optional<Struct>)
53+
abstract fun setGpioState(pin: String, high: Boolean, extra: Struct)
54+
55+
/**
56+
* Set the high/low state of the given pin of a board.
57+
* @param pin the name of the GPIO pin
58+
* @param high when true, sets the pin to high. When false, sets the pin to low.
59+
*/
60+
fun setGpioState(pin: String, high: Boolean) {
61+
return setGpioState(pin, high, Struct.getDefaultInstance())
62+
}
5563

5664
/**
5765
* Get the high/low state of the given pin of a board.
5866
* @param pin the name of the GPIO pin
5967
* @return the state of the pin: true if high, false otherwise.
6068
*/
61-
abstract fun getGpioState(pin: String, extra: Optional<Struct>): Boolean
69+
abstract fun getGpioState(pin: String, extra: Struct): Boolean
70+
71+
/**
72+
* Get the high/low state of the given pin of a board.
73+
* @param pin the name of the GPIO pin
74+
* @return the state of the pin: true if high, false otherwise.
75+
*/
76+
fun getGpioState(pin: String): Boolean {
77+
return getGpioState(pin, Struct.getDefaultInstance())
78+
}
79+
80+
/**
81+
* Set the duty cycle of the given pin of a board.
82+
* @param pin the name of the GPIO pin
83+
* @param dutyCyclePct the duty cycle percent
84+
*/
85+
abstract fun setPwm(pin: String, dutyCyclePct: Double, extra: Struct)
6286

6387
/**
6488
* Set the duty cycle of the given pin of a board.
6589
* @param pin the name of the GPIO pin
6690
* @param dutyCyclePct the duty cycle percent
6791
*/
68-
abstract fun setPwm(pin: String, dutyCyclePct: Double, extra: Optional<Struct>)
92+
fun setPwm(pin: String, dutyCyclePct: Double) {
93+
return setPwm(pin, dutyCyclePct, Struct.getDefaultInstance())
94+
}
95+
96+
/**
97+
* Get the duty cycle of the given pin of a board.
98+
* @param pin the name of the pin
99+
* @returns the duty cycle percent
100+
*/
101+
abstract fun getPwm(pin: String, extra: Struct): Double
69102

70103
/**
71104
* Get the duty cycle of the given pin of a board.
72105
* @param pin the name of the pin
73106
* @returns the duty cycle percent
74107
*/
75-
abstract fun getPwm(pin: String, extra: Optional<Struct>): Double
108+
fun getPwm(pin: String): Double {
109+
return getPwm(pin, Struct.getDefaultInstance())
110+
}
76111

77112
/**
78113
* Set the PWM frequency of the given pin of a board.
79114
* @param pin the name of the pin
80115
* @param frequencyHz the frequency to set
81116
*/
82-
abstract fun setPwmFrequency(pin: String, frequencyHz: Int, extra: Optional<Struct>)
117+
abstract fun setPwmFrequency(pin: String, frequencyHz: Int, extra: Struct)
118+
119+
/**
120+
* Set the PWM frequency of the given pin of a board.
121+
* @param pin the name of the pin
122+
* @param frequencyHz the frequency to set
123+
*/
124+
fun setPwmFrequency(pin: String, frequencyHz: Int) {
125+
return setPwmFrequency(pin, frequencyHz, Struct.getDefaultInstance())
126+
}
127+
128+
/**
129+
* Get the PWM frequency of the given pin of a board.
130+
* @param pin the name of the pin
131+
* @returns the frequency of the pin in Hz
132+
*/
133+
abstract fun getPwmFrequency(pin: String, extra: Struct): Int
83134

84135
/**
85136
* Get the PWM frequency of the given pin of a board.
86137
* @param pin the name of the pin
87138
* @returns the frequency of the pin in Hz
88139
*/
89-
abstract fun getPwmFrequency(pin: String, extra: Optional<Struct>): Int
140+
fun getPwmFrequency(pin: String): Int {
141+
return getPwmFrequency(pin, Struct.getDefaultInstance())
142+
}
90143

91144
/**
92145
* Write analog value to pin.
93146
* @param pin the name of the pin
94147
* @param value the value to set
95148
*/
96-
abstract fun writeAnalog(pin: String, value: Int, extra: Optional<Struct>)
149+
abstract fun writeAnalog(pin: String, value: Int, extra: Struct)
150+
151+
/**
152+
* Write analog value to pin.
153+
* @param pin the name of the pin
154+
* @param value the value to set
155+
*/
156+
fun writeAnalog(pin: String, value: Int) {
157+
return writeAnalog(pin, value, Struct.getDefaultInstance())
158+
}
97159

98160
/**
99161
* Read the current value of an analog reader of a board.
100162
* @param analogReader the name of the analog reader
101163
* @returns the current value of the analog reader
102164
*/
103-
abstract fun getAnalogReaderValue(analogReader: String, extra: Optional<Struct>): Int
165+
abstract fun getAnalogReaderValue(analogReader: String, extra: Struct): Int
166+
167+
/**
168+
* Read the current value of an analog reader of a board.
169+
* @param analogReader the name of the analog reader
170+
* @returns the current value of the analog reader
171+
*/
172+
fun getAnalogReaderValue(analogReader: String): Int {
173+
return getAnalogReaderValue(analogReader, Struct.getDefaultInstance())
174+
}
175+
176+
/**
177+
* Return the current value of the interrupt which is based on the type of Interrupt.
178+
* @param digitalInterrupt the name of the digital interrupt
179+
* @returns the current value of the digital reader
180+
*/
181+
abstract fun getDigitalInterruptValue(digitalInterrupt: String, extra: Struct): Int
104182

105183
/**
106184
* Return the current value of the interrupt which is based on the type of Interrupt.
107185
* @param digitalInterrupt the name of the digital interrupt
108186
* @returns the current value of the digital reader
109187
*/
110-
abstract fun getDigitalInterruptValue(
111-
digitalInterrupt: String,
112-
extra: Optional<Struct>
113-
): Int
188+
fun getDigitalInterruptValue(digitalInterrupt: String): Int {
189+
return getDigitalInterruptValue(digitalInterrupt, Struct.getDefaultInstance())
190+
}
114191

115192
/**
116193
* Stream digital interrupts ticks.
117194
* @param interrupts the list of digital interrupts names from which to receive ticks
118195
* @returns a [Stream] of [Tick] objects
119196
*/
120-
abstract fun streamTicks(interrupts: List<String>, extra: Optional<Struct>): Iterator<Tick>
197+
abstract fun streamTicks(interrupts: List<String>, extra: Struct): Iterator<Tick>
198+
199+
/**
200+
* Stream digital interrupts ticks.
201+
* @param interrupts the list of digital interrupts names from which to receive ticks
202+
* @returns a [Stream] of [Tick] objects
203+
*/
204+
fun streamTicks(interrupts: List<String>): Iterator<Tick> {
205+
return streamTicks(interrupts, Struct.getDefaultInstance())
206+
}
207+
208+
/**
209+
* Add a listener for the digital interrupts.
210+
* @param interrupts the list of digital interrupts names from which to receive ticks
211+
* @param tickQueue an object to receive values from the callback
212+
*/
213+
abstract fun addCallbacks(interrupts: List<String>, tickQueue: Queue<Tick>, extra: Struct)
121214

122215
/**
123216
* Add a listener for the digital interrupts.
124217
* @param interrupts the list of digital interrupts names from which to receive ticks
125218
* @param tickQueue an object to receive values from the callback
126219
*/
127-
abstract fun addCallbacks(
128-
interrupts: List<String>,
129-
tickQueue: Queue<Tick>,
130-
extra: Optional<Struct>
131-
)
220+
fun addCallbacks(interrupts: List<String>, tickQueue: Queue<Tick>) {
221+
return addCallbacks(interrupts, tickQueue, Struct.getDefaultInstance())
222+
}
132223

133224
/**
134225
* Set the board to the indicated power mode.
135226
* @param powerMode the power mode to set
136227
* @param duration if provided, the board will exit the given power mode after this duration
137228
*/
138-
abstract fun setPowerMode(
139-
powerMode: PowerMode,
140-
duration: Duration,
141-
extra: Optional<Struct>
142-
)
229+
abstract fun setPowerMode(powerMode: PowerMode, duration: Duration, extra: Struct)
143230

231+
/**
232+
* Set the board to the indicated power mode.
233+
* @param powerMode the power mode to set
234+
* @param duration if provided, the board will exit the given power mode after this duration
235+
*/
236+
fun setPowerMode(powerMode: PowerMode, duration: Duration) {
237+
return setPowerMode(powerMode, duration, Struct.getDefaultInstance())
238+
}
144239
}

core/sdk/src/main/kotlin/com/viam/sdk/core/component/board/BoardRPCClient.kt

+34-75
Original file line numberDiff line numberDiff line change
@@ -29,118 +29,77 @@ class BoardRPCClient(name: String, channel: Channel) : Board(name) {
2929
}
3030
}
3131

32-
override fun setGpioState(pin: String, high: Boolean, extra: Optional<Struct>) {
33-
val request = SetGPIORequest.newBuilder()
34-
.setName(this.name.name)
35-
.setPin(pin).setHigh(high)
36-
.setExtra(extra.getOrDefault(Struct.getDefaultInstance()))
37-
.build()
32+
override fun setGpioState(pin: String, high: Boolean, extra: Struct) {
33+
val request =
34+
SetGPIORequest.newBuilder().setName(this.name.name).setPin(pin).setHigh(high).setExtra(extra).build()
3835
this.client.setGPIO(request)
3936
}
4037

41-
override fun getGpioState(pin: String, extra: Optional<Struct>): Boolean {
42-
val request = GetGPIORequest.newBuilder()
43-
.setName(this.name.name)
44-
.setPin(pin)
45-
.setExtra(extra.getOrDefault(Struct.getDefaultInstance()))
46-
.build()
38+
override fun getGpioState(pin: String, extra: Struct): Boolean {
39+
val request = GetGPIORequest.newBuilder().setName(this.name.name).setPin(pin).setExtra(extra).build()
4740
return this.client.getGPIO(request).high
4841
}
4942

50-
override fun setPwm(pin: String, dutyCyclePct: Double, extra: Optional<Struct>) {
51-
val request = SetPWMRequest.newBuilder()
52-
.setName(this.name.name)
53-
.setPin(pin)
54-
.setDutyCyclePct(dutyCyclePct)
55-
.setExtra(extra.getOrDefault(Struct.getDefaultInstance()))
56-
.build()
43+
override fun setPwm(pin: String, dutyCyclePct: Double, extra: Struct) {
44+
val request =
45+
SetPWMRequest.newBuilder().setName(this.name.name).setPin(pin).setDutyCyclePct(dutyCyclePct).setExtra(extra)
46+
.build()
5747
this.client.setPWM(request)
5848
}
5949

60-
override fun getPwm(pin: String, extra: Optional<Struct>): Double {
61-
val request = PWMRequest.newBuilder()
62-
.setName(this.name.name)
63-
.setPin(pin)
64-
.setExtra(extra.getOrDefault(Struct.getDefaultInstance()))
65-
.build()
50+
override fun getPwm(pin: String, extra: Struct): Double {
51+
val request = PWMRequest.newBuilder().setName(this.name.name).setPin(pin).setExtra(extra).build()
6652
return this.client.pWM(request).dutyCyclePct
6753
}
6854

69-
override fun setPwmFrequency(pin: String, frequencyHz: Int, extra: Optional<Struct>) {
70-
val request = SetPWMFrequencyRequest.newBuilder()
71-
.setName(this.name.name)
72-
.setPin(pin)
73-
.setFrequencyHz(frequencyHz.toLong())
74-
.setExtra(extra.getOrDefault(Struct.getDefaultInstance()))
75-
.build()
55+
override fun setPwmFrequency(pin: String, frequencyHz: Int, extra: Struct) {
56+
val request =
57+
SetPWMFrequencyRequest.newBuilder().setName(this.name.name).setPin(pin).setFrequencyHz(frequencyHz.toLong())
58+
.setExtra(extra).build()
7659
this.client.setPWMFrequency(request)
7760
}
7861

79-
override fun getPwmFrequency(pin: String, extra: Optional<Struct>): Int {
80-
val request = PWMFrequencyRequest.newBuilder()
81-
.setName(this.name.name)
82-
.setPin(pin)
83-
.setExtra(extra.getOrDefault(Struct.getDefaultInstance()))
84-
.build()
62+
override fun getPwmFrequency(pin: String, extra: Struct): Int {
63+
val request = PWMFrequencyRequest.newBuilder().setName(this.name.name).setPin(pin).setExtra(extra).build()
8564
return this.client.pWMFrequency(request).frequencyHz.toInt()
8665
}
8766

88-
override fun writeAnalog(pin: String, value: Int, extra: Optional<Struct>) {
89-
val request = WriteAnalogRequest.newBuilder()
90-
.setName(this.name.name)
91-
.setPin(pin)
92-
.setValue(value)
93-
.setExtra(extra.getOrDefault(Struct.getDefaultInstance()))
94-
.build()
67+
override fun writeAnalog(pin: String, value: Int, extra: Struct) {
68+
val request =
69+
WriteAnalogRequest.newBuilder().setName(this.name.name).setPin(pin).setValue(value).setExtra(extra).build()
9570
this.client.writeAnalog(request)
9671
}
9772

98-
override fun getAnalogReaderValue(analogReader: String, extra: Optional<Struct>): Int {
99-
val request = ReadAnalogReaderRequest.newBuilder()
100-
.setBoardName(this.name.name)
101-
.setAnalogReaderName(analogReader)
102-
.setExtra(extra.getOrDefault(Struct.getDefaultInstance()))
103-
.build()
73+
override fun getAnalogReaderValue(analogReader: String, extra: Struct): Int {
74+
val request =
75+
ReadAnalogReaderRequest.newBuilder().setBoardName(this.name.name).setAnalogReaderName(analogReader)
76+
.setExtra(extra).build()
10477
return this.client.readAnalogReader(request).value
10578
}
10679

107-
override fun getDigitalInterruptValue(digitalInterrupt: String, extra: Optional<Struct>): Int {
108-
val request = GetDigitalInterruptValueRequest.newBuilder()
109-
.setBoardName(this.name.name)
110-
.setDigitalInterruptName(digitalInterrupt)
111-
.setExtra(extra.getOrDefault(Struct.getDefaultInstance()))
112-
.build()
80+
override fun getDigitalInterruptValue(digitalInterrupt: String, extra: Struct): Int {
81+
val request = GetDigitalInterruptValueRequest.newBuilder().setBoardName(this.name.name)
82+
.setDigitalInterruptName(digitalInterrupt).setExtra(extra).build()
11383
return this.client.getDigitalInterruptValue(request).value.toInt()
11484
}
11585

116-
override fun streamTicks(interrupts: List<String>, extra: Optional<Struct>): Iterator<Tick> {
117-
val request = StreamTicksRequest.newBuilder()
118-
.setName(this.name.name)
119-
.addAllPinNames(interrupts)
120-
.setExtra(extra.getOrDefault(Struct.getDefaultInstance()))
121-
.build()
86+
override fun streamTicks(interrupts: List<String>, extra: Struct): Iterator<Tick> {
87+
val request =
88+
StreamTicksRequest.newBuilder().setName(this.name.name).addAllPinNames(interrupts).setExtra(extra).build()
12289
return this.client.streamTicks(request)
12390
}
12491

12592
override fun addCallbacks(
126-
interrupts: List<String>,
127-
tickQueue: Queue<Tick>,
128-
extra: Optional<Struct>
93+
interrupts: List<String>, tickQueue: Queue<Tick>, extra: Struct
12994
) {
13095
throw MethodNotImplementedException("BoardRPCClient.addCallbacks")
13196
}
13297

13398
override fun setPowerMode(
134-
powerMode: PowerMode,
135-
duration: Duration,
136-
extra: Optional<Struct>
99+
powerMode: PowerMode, duration: Duration, extra: Struct
137100
) {
138-
val request = SetPowerModeRequest.newBuilder()
139-
.setName(this.name.name)
140-
.setPowerMode(powerMode)
141-
.setDuration(Durations.fromNanos(duration.inWholeNanoseconds))
142-
.setExtra(extra.getOrDefault(Struct.getDefaultInstance()))
143-
.build()
101+
val request = SetPowerModeRequest.newBuilder().setName(this.name.name).setPowerMode(powerMode)
102+
.setDuration(Durations.fromNanos(duration.inWholeNanoseconds)).setExtra(extra).build()
144103
this.client.setPowerMode(request)
145104
}
146105

0 commit comments

Comments
 (0)