diff --git a/TeamCode/src/main/java/org/firstinspires/ftc/teamcode/Hardware.java b/TeamCode/src/main/java/org/firstinspires/ftc/teamcode/Hardware.java index 231c2e09d19..9af5d3e1584 100644 --- a/TeamCode/src/main/java/org/firstinspires/ftc/teamcode/Hardware.java +++ b/TeamCode/src/main/java/org/firstinspires/ftc/teamcode/Hardware.java @@ -3,19 +3,31 @@ import com.qualcomm.robotcore.hardware.DcMotor; import com.qualcomm.robotcore.hardware.HardwareMap; -public class Hardware { +import org.firstinspires.ftc.teamcode.hardware.HardwareName; +import org.firstinspires.ftc.teamcode.hardware.Reversed; + +public class Hardware extends HardwareMapper { // left = left motor = exp 0 frontLeft // right = right motor = ctr 0 frontRight // center = ctr 3 intake - public final DcMotor frontLeft; - public final DcMotor frontRight; - public final DcMotor backLeft; - public final DcMotor backRight; - - public final DcMotor encoderLeft; - public final DcMotor encoderCenter; - public final DcMotor encoderRight; + @HardwareName("frontLeft") + @Reversed + public DcMotor frontLeft; + @HardwareName("frontRight") + public DcMotor frontRight; + @HardwareName("backLeft") + @Reversed + public DcMotor backLeft; + @HardwareName("backRight") + public DcMotor backRight; + + @HardwareName("frontLeft") + public DcMotor encoderLeft; + @HardwareName("intake") + public DcMotor encoderCenter; + @HardwareName("frontRight") + public DcMotor encoderRight; private void resetEncoder(DcMotor target) { DcMotor.RunMode mode = target.getMode(); @@ -24,6 +36,7 @@ private void resetEncoder(DcMotor target) { } public Hardware(HardwareMap hwMap) { + super(hwMap); frontLeft = hwMap.dcMotor.get("frontLeft"); frontRight = hwMap.dcMotor.get("frontRight"); backLeft = hwMap.dcMotor.get("backLeft"); diff --git a/TeamCode/src/main/java/org/firstinspires/ftc/teamcode/HardwareMapper.java b/TeamCode/src/main/java/org/firstinspires/ftc/teamcode/HardwareMapper.java new file mode 100644 index 00000000000..22188c4fe05 --- /dev/null +++ b/TeamCode/src/main/java/org/firstinspires/ftc/teamcode/HardwareMapper.java @@ -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); + } + } + } +} diff --git a/TeamCode/src/main/java/org/firstinspires/ftc/teamcode/HardwareName.java b/TeamCode/src/main/java/org/firstinspires/ftc/teamcode/hardware/HardwareName.java similarity index 53% rename from TeamCode/src/main/java/org/firstinspires/ftc/teamcode/HardwareName.java rename to TeamCode/src/main/java/org/firstinspires/ftc/teamcode/hardware/HardwareName.java index 6ec070dfe72..6134c6ebd65 100644 --- a/TeamCode/src/main/java/org/firstinspires/ftc/teamcode/HardwareName.java +++ b/TeamCode/src/main/java/org/firstinspires/ftc/teamcode/hardware/HardwareName.java @@ -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(); } diff --git a/TeamCode/src/main/java/org/firstinspires/ftc/teamcode/hardware/Reversed.java b/TeamCode/src/main/java/org/firstinspires/ftc/teamcode/hardware/Reversed.java new file mode 100644 index 00000000000..123682038c0 --- /dev/null +++ b/TeamCode/src/main/java/org/firstinspires/ftc/teamcode/hardware/Reversed.java @@ -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 {}