Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Dairy): changed to use Dairy Features #15

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
7 changes: 5 additions & 2 deletions PhotonCore/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ plugins {

repositories {
mavenCentral()
maven { url 'https://jitpack.io' }
}

android {
Expand Down Expand Up @@ -42,8 +43,10 @@ afterEvaluate {
}

dependencies {
compileOnly 'org.jetbrains:annotations:15.0'
compileOnly "androidx.annotation:annotation:1.7.0"
compileOnly 'org.jetbrains:annotations:13.0'
compileOnly "androidx.annotation:annotation:1.1.0"

compileOnly('com.github.Dairy-Foundation.Dairy:Core:pre-release-2-0')

compileOnly 'org.firstinspires.ftc:RobotCore:9.0.1'
compileOnly 'org.firstinspires.ftc:Hardware:9.0.1'
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import android.content.Context;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.outoftheboxrobotics.photoncore.hardware.PhotonLynxVoltageSensor;
import com.outoftheboxrobotics.photoncore.hardware.motor.PhotonDcMotor;
import com.outoftheboxrobotics.photoncore.hardware.motor.PhotonLynxDcMotorController;
Expand All @@ -15,10 +18,8 @@
import com.qualcomm.hardware.lynx.LynxModuleIntf;
import com.qualcomm.hardware.lynx.LynxServoController;
import com.qualcomm.hardware.lynx.LynxVoltageSensor;
import com.qualcomm.robotcore.eventloop.opmode.OpMode;
import com.qualcomm.robotcore.eventloop.opmode.OpModeManager;
import com.qualcomm.robotcore.eventloop.opmode.OpModeManagerImpl;
import com.qualcomm.robotcore.eventloop.opmode.OpModeManagerNotifier;
import com.qualcomm.robotcore.exception.RobotCoreException;
import com.qualcomm.robotcore.hardware.CRServo;
import com.qualcomm.robotcore.hardware.CRServoImpl;
Expand All @@ -31,19 +32,48 @@

import org.firstinspires.ftc.ftccommon.external.OnCreateEventLoop;

import java.lang.annotation.Annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.Semaphore;

public class PhotonCore implements OpModeManagerNotifier.Notifications {
import dev.frozenmilk.dairy.core.Feature;
import dev.frozenmilk.dairy.core.dependencyresolution.dependencies.Dependency;
import dev.frozenmilk.dairy.core.dependencyresolution.dependencyset.DependencySet;
import dev.frozenmilk.dairy.core.wrapper.Wrapper;
import dev.frozenmilk.util.cell.LateInitCell;

public class PhotonCore implements Feature {
private final LateInitCell<Annotation> attach = new LateInitCell<>();
private final DependencySet dependencySet = new DependencySet(this)
.includesExactlyOneOf(Attach.class)
.bindOutputTo(attach);
@NonNull
@Override
public Set<Dependency<?, ?>> getDependencies() {
return dependencySet;
}

// Configuration and singleton values
private static final String TAG = "PhotonCore";
private static final PhotonCore instance = new PhotonCore();
public static final boolean DEBUG=true;
public static Photon photon;
public static boolean attached() {
return instance.isAttached();
}
@Nullable
public static Attach photon() {
return (Attach) instance.attach.safeGet();
}

private OpModeManagerImpl opModeManager;

Expand All @@ -54,8 +84,7 @@ public class PhotonCore implements OpModeManagerNotifier.Notifications {
public static void attachEventLoop(Context context, FtcEventLoop eventLoop)
{
if(DEBUG) RobotLog.ii(TAG, "attachEventLoop: Attached PhotonCore to event loop");
eventLoop.getOpModeManager().registerListener(instance);
instance.opModeManager=eventLoop.getOpModeManager();
instance.opModeManager = eventLoop.getOpModeManager();
}

public static void acquire(LynxModuleIntf module) throws InterruptedException {
Expand All @@ -67,18 +96,17 @@ public static void release(LynxModuleIntf module) throws InterruptedException {
}

@Override
public void onOpModePreInit(OpMode opMode) {
public void preUserInitHook(@NonNull Wrapper opMode) {
if(opModeManager.getActiveOpModeName().equals(OpModeManager.DEFAULT_OP_MODE_NAME))
return;

photon = opMode.getClass().getAnnotation(Photon.class);
if(photon!=null) {
if(photon()!=null) {

if(DEBUG) RobotLog.ii(TAG, "onOpModePreInit: Enabling PhotonCore optimizations for opMode %s", opModeManager.getActiveOpModeName());

HardwareMap hardwareMap = opMode.hardwareMap;
HardwareMap hardwareMap = opMode.getOpMode().hardwareMap;
// Get the names of devices using reflection
Map<HardwareDevice, Set<String>> deviceNames = ReflectionUtils.getFieldValue(opMode.hardwareMap, "deviceNames");
Map<HardwareDevice, Set<String>> deviceNames = ReflectionUtils.getFieldValue(opMode.getOpMode().hardwareMap, "deviceNames");
assert deviceNames!=null;

// The first step is to replace all LynxModules with PhotonLynxModules
Expand Down Expand Up @@ -238,8 +266,17 @@ public void onOpModePreInit(OpMode opMode) {
}*/
}
}
@Override
public void onOpModePreStart(OpMode opMode) {}
@Override
public void onOpModePostStop(OpMode opMode) {}

/**
* Add this to a {@link com.qualcomm.robotcore.eventloop.opmode.OpMode} in order to apply PhotonCore optimizations
*/
@Documented
@Inherited
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Attach {
int maximumParallelCommands() default 8;

boolean singleThreadOptimized() default true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import android.content.Context;

import com.outoftheboxrobotics.photoncore.Photon;
import com.outoftheboxrobotics.photoncore.PhotonLastKnown;
import com.outoftheboxrobotics.photoncore.hardware.motor.commands.PhotonLynxGetADCCommand;
import com.qualcomm.hardware.lynx.LynxModule;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void onNackReceived(LynxNack nack) {

@Override
public void acquireNetworkLock() throws InterruptedException {
if(PhotonCore.photon == null){
if(!PhotonCore.attached()){
super.acquireNetworkLock();
}else {
PhotonCore.acquire(module);
Expand All @@ -48,7 +48,7 @@ public void acquireNetworkLock() throws InterruptedException {

@Override
public void releaseNetworkLock() throws InterruptedException {
if(PhotonCore.photon == null){
if(!PhotonCore.attached()){
super.releaseNetworkLock();
}else {
PhotonCore.release(module);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void onNackReceived(LynxNack nack) {

@Override
public void acquireNetworkLock() throws InterruptedException {
if(PhotonCore.photon == null){
if(!PhotonCore.attached()){
super.acquireNetworkLock();
}else {
PhotonCore.acquire(module);
Expand All @@ -47,7 +47,7 @@ public void acquireNetworkLock() throws InterruptedException {

@Override
public void releaseNetworkLock() throws InterruptedException {
if(PhotonCore.photon == null){
if(!PhotonCore.attached()){
super.releaseNetworkLock();
}else {
PhotonCore.release(module);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void onNackReceived(LynxNack nack) {

@Override
public void acquireNetworkLock() throws InterruptedException {
if(PhotonCore.photon == null){
if(!PhotonCore.attached()){
super.acquireNetworkLock();
}else {
PhotonCore.acquire(module);
Expand All @@ -47,7 +47,7 @@ public void acquireNetworkLock() throws InterruptedException {

@Override
public void releaseNetworkLock() throws InterruptedException {
if(PhotonCore.photon == null){
if(!PhotonCore.attached()){
super.releaseNetworkLock();
}else {
PhotonCore.release(module);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void onNackReceived(LynxNack nack) {

@Override
public void acquireNetworkLock() throws InterruptedException {
if(PhotonCore.photon == null){
if(!PhotonCore.attached()){
super.acquireNetworkLock();
}else {
PhotonCore.acquire(module);
Expand All @@ -43,7 +43,7 @@ public void acquireNetworkLock() throws InterruptedException {

@Override
public void releaseNetworkLock() throws InterruptedException {
if(PhotonCore.photon == null){
if(!PhotonCore.attached()){
super.releaseNetworkLock();
}else {
PhotonCore.release(module);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void onNackReceived(LynxNack nack) {

@Override
public void acquireNetworkLock() throws InterruptedException {
if(PhotonCore.photon == null){
if(!PhotonCore.attached()){
super.acquireNetworkLock();
}else {
PhotonCore.acquire(module);
Expand All @@ -47,7 +47,7 @@ public void acquireNetworkLock() throws InterruptedException {

@Override
public void releaseNetworkLock() throws InterruptedException {
if(PhotonCore.photon == null){
if(!PhotonCore.attached()){
super.releaseNetworkLock();
}else {
PhotonCore.release(module);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void onNackReceived(LynxNack nack) {

@Override
public void acquireNetworkLock() throws InterruptedException {
if(PhotonCore.photon == null){
if(!PhotonCore.attached()){
super.acquireNetworkLock();
}else {
PhotonCore.acquire(module);
Expand All @@ -47,7 +47,7 @@ public void acquireNetworkLock() throws InterruptedException {

@Override
public void releaseNetworkLock() throws InterruptedException {
if(PhotonCore.photon == null){
if(!PhotonCore.attached()){
super.releaseNetworkLock();
}else {
PhotonCore.release(module);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void onNackReceived(LynxNack nack) {

@Override
public void acquireNetworkLock() throws InterruptedException {
if(PhotonCore.photon == null){
if(!PhotonCore.attached()){
super.acquireNetworkLock();
}else {
PhotonCore.acquire(module);
Expand All @@ -44,7 +44,7 @@ public void acquireNetworkLock() throws InterruptedException {

@Override
public void releaseNetworkLock() throws InterruptedException {
if(PhotonCore.photon == null){
if(!PhotonCore.attached()){
super.releaseNetworkLock();
}else {
PhotonCore.release(module);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void onNackReceived(LynxNack nack) {

@Override
public void acquireNetworkLock() throws InterruptedException {
if(PhotonCore.photon == null){
if(!PhotonCore.attached()){
super.acquireNetworkLock();
}else {
PhotonCore.acquire(module);
Expand All @@ -44,7 +44,7 @@ public void acquireNetworkLock() throws InterruptedException {

@Override
public void releaseNetworkLock() throws InterruptedException {
if(PhotonCore.photon == null){
if(!PhotonCore.attached()){
super.releaseNetworkLock();
}else {
PhotonCore.release(module);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void onNackReceived(LynxNack nack) {

@Override
public void acquireNetworkLock() throws InterruptedException {
if(PhotonCore.photon == null){
if(!PhotonCore.attached()){
super.acquireNetworkLock();
}else {
PhotonCore.acquire(module);
Expand All @@ -47,7 +47,7 @@ public void acquireNetworkLock() throws InterruptedException {

@Override
public void releaseNetworkLock() throws InterruptedException {
if(PhotonCore.photon == null){
if(!PhotonCore.attached()){
super.releaseNetworkLock();
}else {
PhotonCore.release(module);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void onNackReceived(LynxNack nack) {

@Override
public void acquireNetworkLock() throws InterruptedException {
if(PhotonCore.photon == null){
if(!PhotonCore.attached()){
super.acquireNetworkLock();
}else {
PhotonCore.acquire(module);
Expand All @@ -47,7 +47,7 @@ public void acquireNetworkLock() throws InterruptedException {

@Override
public void releaseNetworkLock() throws InterruptedException {
if(PhotonCore.photon == null){
if(!PhotonCore.attached()){
super.releaseNetworkLock();
}else {
PhotonCore.release(module);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void onNackReceived(LynxNack nack) {

@Override
public void acquireNetworkLock() throws InterruptedException {
if(PhotonCore.photon == null){
if(!PhotonCore.attached()){
super.acquireNetworkLock();
}else {
PhotonCore.acquire(module);
Expand All @@ -47,7 +47,7 @@ public void acquireNetworkLock() throws InterruptedException {

@Override
public void releaseNetworkLock() throws InterruptedException {
if(PhotonCore.photon == null){
if(!PhotonCore.attached()){
super.releaseNetworkLock();
}else {
PhotonCore.release(module);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void onNackReceived(LynxNack nack) {

@Override
public void acquireNetworkLock() throws InterruptedException {
if(PhotonCore.photon == null){
if(!PhotonCore.attached()){
super.acquireNetworkLock();
}else {
PhotonCore.acquire(module);
Expand All @@ -47,7 +47,7 @@ public void acquireNetworkLock() throws InterruptedException {

@Override
public void releaseNetworkLock() throws InterruptedException {
if(PhotonCore.photon == null){
if(!PhotonCore.attached()){
super.releaseNetworkLock();
}else {
PhotonCore.release(module);
Expand Down
Loading