|
| 1 | +package frc.team5115.subsystems.lights; |
| 2 | +import java.util.function.Function; |
| 3 | + |
| 4 | +import edu.wpi.first.wpilibj.AddressableLED; |
| 5 | +import edu.wpi.first.wpilibj.AddressableLEDBuffer; |
| 6 | +import edu.wpi.first.wpilibj.DigitalInput; |
| 7 | +import edu.wpi.first.wpilibj2.command.SubsystemBase; |
| 8 | +import frc.team5115.Constants; |
| 9 | + |
| 10 | +public class Lights extends SubsystemBase { |
| 11 | + private enum AnimationState { Idle, ControllingNote, Aligning, InRange } |
| 12 | + |
| 13 | + private final int period = 2; |
| 14 | + private final double tailLength = 5; |
| 15 | + private final double decay = 1d / tailLength; |
| 16 | + private final int minPower = 10; |
| 17 | + private final int maxPower = 250; |
| 18 | + |
| 19 | + private final AddressableLED leds; |
| 20 | + private final AddressableLEDBuffer buffer; |
| 21 | + public final int ledCount; |
| 22 | + |
| 23 | + private int timer_idle; |
| 24 | + private int counter_idle; |
| 25 | + private int direction_idle; |
| 26 | + |
| 27 | + private int timer_align; |
| 28 | + private int position_align; |
| 29 | + |
| 30 | + private DigitalInput reflectiveSensor; |
| 31 | + |
| 32 | + public Lights(int port, int ledCount){ |
| 33 | + this.ledCount = ledCount; |
| 34 | + leds = new AddressableLED(port); |
| 35 | + buffer = new AddressableLEDBuffer(ledCount); |
| 36 | + reflectiveSensor = new DigitalInput(Constants.SHOOTER_SENSOR_ID); |
| 37 | + |
| 38 | + } |
| 39 | + |
| 40 | + public void start(){ |
| 41 | + leds.start(); |
| 42 | + } |
| 43 | + |
| 44 | + public void stop(){ |
| 45 | + leds.stop(); |
| 46 | + } |
| 47 | + |
| 48 | + public void update(boolean aligning, boolean inRange) { |
| 49 | + AnimationState state = AnimationState.Idle; |
| 50 | + |
| 51 | + if (inRange) { |
| 52 | + state = AnimationState.InRange; |
| 53 | + } else if (aligning) { |
| 54 | + state = AnimationState.Aligning; |
| 55 | + } else if (!reflectiveSensor.get()) { |
| 56 | + state = AnimationState.ControllingNote; |
| 57 | + } |
| 58 | + |
| 59 | + switch (state) { |
| 60 | + case ControllingNote: |
| 61 | + setUniformColor(0, 150, 0); |
| 62 | + break; |
| 63 | + case Aligning: |
| 64 | + updateAlignAnimation(); |
| 65 | + break; |
| 66 | + case InRange: |
| 67 | + timer_align = 0; |
| 68 | + position_align = 0; |
| 69 | + setUniformColor(0xA5, 0x10, 0x90); |
| 70 | + break; |
| 71 | + case Idle: |
| 72 | + default: |
| 73 | + updateIdleAnimation(); |
| 74 | + break; |
| 75 | + } |
| 76 | + } |
| 77 | + private void updateAlignAnimation() { |
| 78 | + timer_align ++; |
| 79 | + if (timer_align >= period) { |
| 80 | + timer_align = 0; |
| 81 | + } else { |
| 82 | + return; |
| 83 | + } |
| 84 | + |
| 85 | + position_align++; |
| 86 | + if (position_align == ledCount / 2) { |
| 87 | + position_align = 0; |
| 88 | + } |
| 89 | + iterateAllLeds((index) -> { |
| 90 | + final boolean powered = |
| 91 | + index == position_align || // the current position |
| 92 | + index == ledCount-1 - position_align || // the opposite side |
| 93 | + index == position_align - 1 || // the left of the current position |
| 94 | + index == ledCount-1 - position_align + 1; // the right of the opposite side |
| 95 | + |
| 96 | + if (powered) { |
| 97 | + return new Integer[] { 0xA5, 0x10, 0x90 }; |
| 98 | + } else { |
| 99 | + return new Integer[] { 0, 0, 0 }; |
| 100 | + } |
| 101 | + }); |
| 102 | + } |
| 103 | + |
| 104 | + public void updateIdleAnimation() { |
| 105 | + timer_idle ++; |
| 106 | + if (timer_idle >= period) { |
| 107 | + timer_idle = 0; |
| 108 | + } else { |
| 109 | + return; |
| 110 | + } |
| 111 | + |
| 112 | + counter_idle += direction_idle; |
| 113 | + if (counter_idle == ledCount || counter_idle == -1) { |
| 114 | + direction_idle = -direction_idle; |
| 115 | + counter_idle += 2 * direction_idle; |
| 116 | + } |
| 117 | + iterateAllLeds((index) -> { |
| 118 | + double percent = buffer.getLED(index).red - minPower / 1d / maxPower - decay; |
| 119 | + percent = Math.max(percent, 0); |
| 120 | + |
| 121 | + if (index == counter_idle) { |
| 122 | + percent = 1.0; |
| 123 | + } |
| 124 | + |
| 125 | + final double power = (percent * (maxPower - minPower)) + minPower; |
| 126 | + return new Integer[] { (int)power, 0, 0 }; |
| 127 | + }); |
| 128 | + } |
| 129 | + public void setUniformColor(int r, int g, int b){ |
| 130 | + iterateAllLeds((index) -> { |
| 131 | + return new Integer[] {r, g, b}; |
| 132 | + }); |
| 133 | + } |
| 134 | + public void iterateAllLeds(Function<Integer, Integer[]> function){ |
| 135 | + for(int i = 0; i <ledCount; i++){ |
| 136 | + Integer[] color = function.apply(i); |
| 137 | + buffer.setRGB(i, color[0], color[1], color[2]); |
| 138 | + } |
| 139 | + leds.setData(buffer); |
| 140 | + } |
| 141 | + |
| 142 | +} |
0 commit comments