forked from FIRST-Tech-Challenge/FtcRobotController
-
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
1 parent
d0f8f3d
commit 20478ae
Showing
4 changed files
with
66 additions
and
10 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
30 changes: 30 additions & 0 deletions
30
TeamCode/src/main/java/org/firstinspires/ftc/teamcode/HardwareMapper.java
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,30 @@ | ||
package org.firstinspires.ftc.teamcode; | ||
|
||
import com.qualcomm.robotcore.hardware.HardwareMap; | ||
|
||
import org.firstinspires.ftc.teamcode.hardware.HardwareName; | ||
|
||
import java.lang.reflect.Field; | ||
|
||
public abstract class HardwareMapper { | ||
public HardwareMapper(HardwareMap map) { | ||
Field[] fields = this.getClass().getDeclaredFields(); | ||
for (Field field : fields) { | ||
boolean accessible = field.isAccessible(); | ||
if (!accessible) field.setAccessible(true); | ||
try { | ||
Class<?> targetType = field.getType(); | ||
HardwareName annotation = field.getAnnotation(HardwareName.class); | ||
if (annotation == null) continue; | ||
field.set(this, map.get(targetType, field.getName())); | ||
} catch (IllegalAccessException e) { | ||
throw new RuntimeException(e); | ||
} catch (IllegalArgumentException e) { | ||
throw new RuntimeException("Field " + field.getName() + " typecast failed"); | ||
} finally { | ||
// lock the field back if it was locked | ||
field.setAccessible(accessible); | ||
} | ||
} | ||
} | ||
} |
5 changes: 4 additions & 1 deletion
5
...stinspires/ftc/teamcode/HardwareName.java → ...s/ftc/teamcode/hardware/HardwareName.java
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,9 +1,12 @@ | ||
package org.firstinspires.ftc.teamcode; | ||
package org.firstinspires.ftc.teamcode.hardware; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.FIELD) | ||
public @interface HardwareName { | ||
String value(); | ||
} |
10 changes: 10 additions & 0 deletions
10
TeamCode/src/main/java/org/firstinspires/ftc/teamcode/hardware/Reversed.java
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,10 @@ | ||
package org.firstinspires.ftc.teamcode.hardware; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.FIELD) | ||
public @interface Reversed {} |