This repository has been archived by the owner on Mar 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
194 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 11 additions & 23 deletions
34
TeamCode/src/main/java/org/firstinspires/ftc/teamcode/api/linear/LinearAPI.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,21 @@ | ||
package org.firstinspires.ftc.teamcode.api.linear | ||
|
||
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode | ||
import org.firstinspires.ftc.teamcode.api.API | ||
import org.firstinspires.ftc.teamcode.utils.Resettable | ||
|
||
/** | ||
* A linear API is shared code used by linear components. | ||
* | ||
* It is specifically designed to only work in autonomous scenarios. It allows linear components to | ||
* call specific methods unique to [LinearOpMode]. | ||
* | ||
* If a linear API depends on another API, it should say so in the documentation. | ||
* | ||
* @see API | ||
*/ | ||
@Deprecated( | ||
message = "LinearAPI is deprecated. Please use the API class and override isLinear to true.", | ||
replaceWith = | ||
ReplaceWith( | ||
expression = "API", | ||
imports = arrayOf("org.firstinspires.ftc.teamcode.api.API"), | ||
), | ||
level = DeprecationLevel.ERROR, | ||
) | ||
abstract class LinearAPI { | ||
private var uninitializedOpMode: LinearOpMode? by Resettable { null } | ||
|
||
protected val linearOpMode: LinearOpMode | ||
get() = | ||
uninitializedOpMode | ||
?: throw NullPointerException("<LinearAPI> has not been initialized with the OpMode before being used.") | ||
get() = throw NotImplementedError() | ||
|
||
open fun init(linearOpMode: LinearOpMode) { | ||
// You can only initialize an API once. If it is initialized more than once, throw an error. | ||
if (uninitializedOpMode != null) { | ||
throw IllegalStateException("Tried to initialize a <LinearAPI> more than once.") | ||
} | ||
|
||
this.uninitializedOpMode = linearOpMode | ||
throw NotImplementedError("LinearAPI is deprecated.") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
TeamCode/src/main/java/org/firstinspires/ftc/teamcode/utils/APIDependencies.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package org.firstinspires.ftc.teamcode.utils | ||
|
||
import android.content.Context | ||
import com.qualcomm.ftccommon.FtcEventLoop | ||
import com.qualcomm.robotcore.eventloop.opmode.OpMode | ||
import com.qualcomm.robotcore.eventloop.opmode.OpModeManagerNotifier | ||
import org.firstinspires.ftc.ftccommon.external.OnCreateEventLoop | ||
import org.firstinspires.ftc.teamcode.api.API | ||
|
||
/** | ||
* An object that orchestrates API dependency checking. | ||
*/ | ||
object APIDependencies : OpModeManagerNotifier.Notifications { | ||
/** A list of all APIs initialized for the current opmode, populated when "Init" is pressed. */ | ||
private val initializedAPIs = mutableSetOf<API>() | ||
|
||
@OnCreateEventLoop | ||
@JvmStatic | ||
fun register( | ||
@Suppress("UNUSED_PARAMETER") context: Context, | ||
ftcEventLoop: FtcEventLoop, | ||
) { | ||
ftcEventLoop.opModeManager.registerListener(this) | ||
} | ||
|
||
override fun onOpModePreInit(opMode: OpMode) { | ||
// Clear initialized API list when a new opmode is selected. | ||
this.initializedAPIs.clear() | ||
} | ||
|
||
// Checks dependencies when the start button is pressed. | ||
// It will only do this if debug mode is active, so as not to waste time during competitions. | ||
override fun onOpModePreStart(opMode: OpMode) { | ||
if (RobotConfig.debug) { | ||
this.checkDependencies() | ||
} | ||
} | ||
|
||
override fun onOpModePostStop(opMode: OpMode) { | ||
} | ||
|
||
/** | ||
* Registers that an API has been initialized. | ||
* | ||
* You should never call this method manually. It is automatically done when `API.init` is | ||
* called. (Which could also look like `super.init(opMode)`.) | ||
*/ | ||
internal fun registerAPI(api: API) { | ||
this.initializedAPIs.add(api) | ||
} | ||
|
||
/** | ||
* Checks that the dependencies of initialized APIs are also initialized. | ||
* | ||
* @throws RuntimeException If a dependency is not initialized. | ||
*/ | ||
private fun checkDependencies() { | ||
// Go through every single API dependency and check if it is in the list. | ||
for (api in this.initializedAPIs) { | ||
for (dep in api.dependencies()) { | ||
if (!this.initializedAPIs.contains(dep)) { | ||
// Report the error with as much information as possible to debug the issue. | ||
val apiName = api::class.simpleName | ||
val depName = dep::class.simpleName | ||
|
||
throw RuntimeException( | ||
""" | ||
API $apiName requires dependency $depName, but it was not initialized. | ||
Please call $depName.init(this) in the initialization phase of the opmode. | ||
Initialized APIs: ${this.initializedAPIs}. | ||
""".trimIndent(), | ||
) | ||
} | ||
} | ||
} | ||
} | ||
} |