Skip to content

Commit 43cdbd2

Browse files
camearle20frcteam401
authored andcommitted
Added pneumatic and digital output channel objects
1 parent 449945a commit 43cdbd2

File tree

9 files changed

+137
-8
lines changed

9 files changed

+137
-8
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.snakeskin.component.provider
2+
3+
/**
4+
* Represents a component that can output a digital signal
5+
*/
6+
interface IDigitalOutputProvider {
7+
/**
8+
* Sets the digital state of the component
9+
*/
10+
fun setState(state: Boolean)
11+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package org.snakeskin.component
2+
3+
import org.snakeskin.component.provider.IDigitalOutputProvider
4+
5+
interface IDigitalOutputChannel : IDigitalOutputProvider
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package org.snakeskin.component
2+
3+
import org.snakeskin.component.provider.IDigitalOutputProvider
4+
5+
interface IPneumaticChannel: IDigitalOutputProvider
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.snakeskin.component.impl
2+
3+
import edu.wpi.first.wpilibj.DigitalOutput
4+
import org.snakeskin.component.IDigitalOutputChannel
5+
6+
class HardwareDigitalOutputChannel(val channel: DigitalOutput): IDigitalOutputChannel {
7+
override fun setState(state: Boolean) {
8+
channel.set(state)
9+
}
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.snakeskin.component.impl
2+
3+
import edu.wpi.first.wpilibj.Solenoid
4+
import org.snakeskin.component.IPneumaticChannel
5+
6+
class HardwarePneumaticChannel(val channel: Solenoid): IPneumaticChannel {
7+
override fun setState(state: Boolean) {
8+
channel.set(state)
9+
}
10+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.snakeskin.component.impl
2+
3+
import org.snakeskin.component.IDigitalOutputChannel
4+
5+
class NullDigitalOutputChannel private constructor(): IDigitalOutputChannel {
6+
companion object {
7+
val INSTANCE = NullDigitalOutputChannel()
8+
val producer = { INSTANCE }
9+
}
10+
11+
override fun setState(state: Boolean) {
12+
//no-op
13+
}
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.snakeskin.component.impl
2+
3+
import org.snakeskin.component.IPneumaticChannel
4+
5+
class NullPneumaticChannel private constructor(): IPneumaticChannel {
6+
companion object {
7+
val INSTANCE = NullPneumaticChannel()
8+
val producer = { INSTANCE }
9+
}
10+
11+
override fun setState(state: Boolean) {
12+
//no-op
13+
}
14+
}

SnakeSkin-FRC/src/main/kotlin/org/snakeskin/dsl/FRCHardwareExtensions.kt

Lines changed: 62 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
package org.snakeskin.dsl
22

3-
import edu.wpi.first.wpilibj.CounterBase
4-
import edu.wpi.first.wpilibj.DigitalInput
5-
import edu.wpi.first.wpilibj.Encoder
3+
import edu.wpi.first.wpilibj.*
64
import org.snakeskin.component.IDIOEncoderDevice
75
import org.snakeskin.component.IDigitalInputChannel
8-
import org.snakeskin.component.impl.HardwareDIOEncoderDevice
9-
import org.snakeskin.component.impl.HardwareDigitalInputChannel
10-
import org.snakeskin.component.impl.NullDIOEncoderDevice
11-
import org.snakeskin.component.impl.NullDigitalInputChannel
6+
import org.snakeskin.component.IDigitalOutputChannel
7+
import org.snakeskin.component.IPneumaticChannel
8+
import org.snakeskin.component.impl.*
129
import org.snakeskin.runtime.SnakeskinPlatform
1310
import org.snakeskin.runtime.SnakeskinRuntime
1411

@@ -52,6 +49,42 @@ inline fun Hardware.createDigitalInputChannel(
5249
else -> mockProducer()
5350
}
5451

52+
/**
53+
* Creates a new pneumatic channel object.
54+
* @param channel The ID of the channel
55+
* @param pcmId The ID of the PCM (defaults to 0)
56+
* @param halMock If true, WPI HAL mocks will be used in WPI Sim mode. If false, mockProducer will be called.
57+
* @param mockProducer Function which returns a mock object representing the device, used for emulating hardware
58+
*/
59+
inline fun Hardware.createPneumaticChannel(
60+
channel: Int,
61+
pcmId: Int,
62+
halMock: Boolean = false,
63+
mockProducer: () -> IPneumaticChannel = { throw NotImplementedError("No mock pneumatic channel implementation provided") }
64+
) = when (SnakeskinRuntime.platform) {
65+
SnakeskinPlatform.UNDEFINED -> NullPneumaticChannel.INSTANCE
66+
SnakeskinPlatform.FRC_ROBORIO -> HardwarePneumaticChannel(Solenoid(pcmId, channel))
67+
SnakeskinPlatform.FRC_WPISIM -> if (halMock) HardwarePneumaticChannel(Solenoid(pcmId, channel)) else mockProducer()
68+
else -> mockProducer()
69+
}
70+
71+
/**
72+
* Creates a new digital output channel object.
73+
* @param channel The ID of the channel
74+
* @param halMock If true, WPI HAL mocks will be used in WPI Sim mode. If false, mockProducer will be called.
75+
* @param mockProducer Function which returns a mock object representing the device, used for emulating hardware
76+
*/
77+
inline fun Hardware.createDigitalOutputChannel(
78+
channel: Int,
79+
halMock: Boolean = false,
80+
mockProducer: () -> IDigitalOutputChannel = { throw NotImplementedError("No mock digital output implementation provided") }
81+
) = when (SnakeskinRuntime.platform) {
82+
SnakeskinPlatform.UNDEFINED -> NullDigitalOutputChannel.INSTANCE
83+
SnakeskinPlatform.FRC_ROBORIO -> HardwareDigitalOutputChannel(DigitalOutput(channel))
84+
SnakeskinPlatform.FRC_WPISIM -> if (halMock) HardwareDigitalOutputChannel(DigitalOutput(channel)) else mockProducer()
85+
else -> mockProducer()
86+
}
87+
5588

5689
/**
5790
* Allows access to hardware device functions of a DIO Encoder device
@@ -73,4 +106,26 @@ inline fun useHardware(digitalInputChannel: IDigitalInputChannel, action: Digita
73106
if (digitalInputChannel is HardwareDigitalInputChannel) {
74107
action(digitalInputChannel.channel)
75108
}
109+
}
110+
111+
/**
112+
* Allows access to hardware device functions of a pneumatic channel
113+
* @param pneumaticChannel The pneumatic channel object
114+
* @param action The action to run on the hardware. If the runtime is not hardware, the action will not be run
115+
*/
116+
inline fun useHardware(pneumaticChannel: IPneumaticChannel, action: Solenoid.() -> Unit) {
117+
if (pneumaticChannel is HardwarePneumaticChannel) {
118+
action(pneumaticChannel.channel)
119+
}
120+
}
121+
122+
/**
123+
* Allows access to hardware device functions of a digital output channel
124+
* @param digitalOutputChannel The digital output channel object
125+
* @param action The action to run on the hardware. If the runtime is not hardware, the action will not be run
126+
*/
127+
inline fun useHardware(digitalOutputChannel: IDigitalOutputChannel, action: DigitalOutput.() -> Unit) {
128+
if (digitalOutputChannel is HardwareDigitalOutputChannel) {
129+
action(digitalOutputChannel.channel)
130+
}
76131
}

build.gradle

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ subprojects { subproj ->
2323
repositories {
2424
mavenCentral()
2525

26+
//401 maven
27+
maven {
28+
url "http://maven.team401.org/artifactory/frc"
29+
}
30+
2631
//SnakeSkin bintray
2732
maven {
2833
url "http://dl.bintray.com/team401/SnakeSkin"
@@ -41,7 +46,7 @@ subprojects { subproj ->
4146
}
4247
//REV Maven
4348
maven {
44-
url "https://www.revrobotics.com/content/sw/max/sdk/maven/"
49+
url "http://www.revrobotics.com/content/sw/max/sdk/maven/"
4550
}
4651
}
4752

0 commit comments

Comments
 (0)