Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions hardware/src/main/kotlin/dev/nextftc/hardware/RobotController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ object RobotController : OnCreateEventLoop, OpModeManagerNotifier.Notifications
hardwareObjects += hardware
}

private fun resetHardware() {
synchronized(hardwareObjects) { hardwareObjects.toList() }.forEach { it.reset() }
}

/**
* Application context captured when the event loop is created.
*
Expand Down Expand Up @@ -145,14 +149,16 @@ object RobotController : OnCreateEventLoop, OpModeManagerNotifier.Notifications
ftcEventLoop.opModeManager.registerListener(this)
}

override fun onOpModePreInit(opMode: OpMode?) {}
override fun onOpModePreInit(opMode: OpMode?) {
resetHardware()
}

override fun onOpModePreStart(opMode: OpMode?) {}

/**
* Discards all cached hardware objects so they are re-initialized in the next op mode.
*/
override fun onOpModePostStop(opMode: OpMode?) {
synchronized(hardwareObjects) { hardwareObjects.toList() }.forEach { it.reset() }
resetHardware()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,19 @@ class NextLynxModule internal constructor(initializer: () -> LynxModule, @JvmFie
}

/** Tracks used ports. Resets each OpMode. */
private val usedPorts by LazyHardware { HashSet<Pair<PortKind, Int>>() }
private val usedPorts by LazyHardware { HashMap<Pair<PortKind, Int>, Throwable>() }

/** Throws if this port is already used on this hub. */
private fun claim(kind: PortKind, port: Int) {
check(usedPorts.add(kind to port)) {
"Port conflict on $type: ${kind.name.lowercase()} port $port is used by more than one device"
val key = kind to port
val first = usedPorts[key]
if (first != null) {
throw IllegalStateException(
"Port conflict on $type: ${kind.name.lowercase()} port $port is used by more than one device",
first,
)
}
usedPorts[key] = Throwable("Port first claimed here")
}

/** Current module temperature. */
Expand Down
Loading