Skip to content

Commit

Permalink
LED Light thing, not tested yet. Also RunForTask
Browse files Browse the repository at this point in the history
  • Loading branch information
lachlanPaul committed Aug 9, 2024
1 parent 5d0981a commit 00315e3
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package org.murraybridgebunyips.bunyipslib.subsystems;

import com.qualcomm.hardware.rev.RevBlinkinLedDriver;

import org.murraybridgebunyips.bunyipslib.BunyipsSubsystem;
import org.murraybridgebunyips.bunyipslib.external.units.Measure;
import org.murraybridgebunyips.bunyipslib.external.units.Time;
import org.murraybridgebunyips.bunyipslib.tasks.RunForTask;
import org.murraybridgebunyips.bunyipslib.tasks.bases.Task;

public class BlinkinLights extends BunyipsSubsystem {

This comment has been minimized.

Copy link
@bubner

bubner Aug 9, 2024

Collaborator

add docs

private final RevBlinkinLedDriver lights;
private final RevBlinkinLedDriver.BlinkinPattern defaultPattern;
private RevBlinkinLedDriver.BlinkinPattern currentPattern;

public BlinkinLights(RevBlinkinLedDriver light, RevBlinkinLedDriver.BlinkinPattern defaultBlinkPattern) {
lights = light;
defaultPattern = defaultBlinkPattern;
currentPattern = defaultBlinkPattern;

lights.setPattern(defaultPattern);
}

public void setPattern(RevBlinkinLedDriver.BlinkinPattern pattern) {
currentPattern = pattern;
}

public Task setPatternForTask(Measure<Time> duration, RevBlinkinLedDriver.BlinkinPattern pattern) {
return new RunForTask(duration, () -> currentPattern = pattern, this::resetPattern)
.onSubsystem(this, true)
.withName("Lights:" + pattern.name());
}

public RevBlinkinLedDriver.BlinkinPattern getPattern() {
return currentPattern;
}

public void resetPattern() {
currentPattern = defaultPattern;
}

public void turnOff() {
currentPattern = RevBlinkinLedDriver.BlinkinPattern.BLACK;
}

@Override
protected void onDisable() {
turnOff();
}

@Override
protected void onEnable() {
currentPattern = defaultPattern;
}

@Override
protected void periodic() {
opMode.telemetry.add("Current Pattern:" + currentPattern.name());
lights.setPattern(currentPattern);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@
*/
public class RunForTask extends Task {
private final Runnable callback;
private Runnable finishCallback;

/**
* Subsystem-independent task.
* A runnable that will run until timeout.
*
* @param timeout The time to run the task for
* @param callback The callback to run every loop
Expand All @@ -24,9 +25,29 @@ public RunForTask(Measure<Time> timeout, Runnable callback) {
withName("Run For");
}

/**
* A runnable that will run until timeout, with a finish callback.
*
* @param timeout The time to run the task for
* @param callback The callback to run every loop
* @param finishCallback The callback to run after this task finishes
*/
public RunForTask(Measure<Time> timeout, Runnable callback, Runnable finishCallback) {
super(timeout);
this.callback = callback;
this.finishCallback = finishCallback;
withName("Run For");
}

@Override
protected void periodic() {
// no-op
callback.run();
}

@Override
protected void onFinish() {
if (finishCallback != null)
finishCallback.run();
}

@Override
Expand Down

0 comments on commit 00315e3

Please sign in to comment.