This commit is contained in:
Tyler Wilcox 2023-03-30 19:32:29 +00:00 committed by GitHub
commit 4986d57ab1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 98 additions and 39 deletions

View File

@ -4,7 +4,7 @@ apply plugin: 'maven-publish'
ext.licenseFile = files("$rootDir/LICENSE.txt") ext.licenseFile = files("$rootDir/LICENSE.txt")
def pubVersion = '2023.3.3' def pubVersion = '2023.4.0'
def outputsFolder = file("$buildDir/allOutputs") def outputsFolder = file("$buildDir/allOutputs")

View File

@ -7,4 +7,5 @@ public class Library {
public boolean someLibraryMethod() { public boolean someLibraryMethod() {
return true; return true;
} }
} }

View File

@ -1,7 +1,6 @@
package com.fairportrobotics.frc.poe.controllers.lighting; package com.fairportrobotics.frc.poe.controllers.lighting;
import java.util.regex.Matcher; import java.util.ArrayList;
import java.util.regex.Pattern;
import edu.wpi.first.wpilibj.SerialPort; import edu.wpi.first.wpilibj.SerialPort;
@ -9,54 +8,113 @@ import edu.wpi.first.wpilibj.SerialPort;
public class ArduinoLightingController { public class ArduinoLightingController {
final SerialPort bus; final SerialPort bus;
final Pattern colorPattern = Pattern.compile("\\d{9}"); private final static String NULL_COLOR = "000000000";
private final String NULL_COLOR = "000000000"; private final static String RAINBOW_VALUE = NULL_COLOR + "254";
private final String RAINBOW_VALUE = NULL_COLOR + "254"; private final static String SHIFT_VALUE = NULL_COLOR + "251";
private final String SHIFT_VALUE = NULL_COLOR + "251"; private final static String SHIFT_WRAP_VALUE = NULL_COLOR + "250";
private final String SHIFT_WRAP_VALUE = NULL_COLOR + "250";
private Thread lightingRunnerThread;
private boolean running = true;
private ArrayList<String> commandQueue = new ArrayList<>();
public ArduinoLightingController(int baudRate, SerialPort.Port port) { public ArduinoLightingController(int baudRate, SerialPort.Port port) {
this.bus = new SerialPort(baudRate, port); this.bus = new SerialPort(baudRate, port);
this.lightingRunnerThread = new Thread(new Runnable() {
public void run(){
while(running){
for(String command : commandQueue){
bus.writeString(command);
try{
Thread.sleep(500);
}catch(InterruptedException e){
e.printStackTrace();
}
} }
private boolean validateColor(String colorValue) {
Matcher m = colorPattern.matcher(colorValue);
return m.find();
} }
public int executeColor(String colorValue, String command) {
if ( validateColor(colorValue) ) {
return bus.writeString( colorValue + command );
} }
return 0; });
lightingRunnerThread.start();
} }
public int fillAll(String colorValue) { public void executeCommand(LightingCommand command) {
return executeColor(colorValue, "255");
commandQueue.addAll(command.getCommands());
// if ( validateColor(colorValue) ) {
// return bus.writeString( colorValue + command );
// }
// return 0;
} }
public int fillRainbow() { public static class LightingCommandBuilder{
return bus.writeString(RAINBOW_VALUE);
private ArrayList<String> commands;
public LightingCommandBuilder(){
commands= new ArrayList<>();
} }
public int switchGradient(String colorValue) { public LightingCommandBuilder setSpecificLED(String colorValue, String numberLED){
return executeColor(colorValue, "253"); commands.add(colorValue + numberLED);
return this;
} }
public int fillShift(String colorValue) { public LightingCommandBuilder fillAll(String colorValue){
return executeColor(colorValue, "252"); commands.add(colorValue + "255");
return this;
} }
public int shift() { public LightingCommandBuilder shiftWrap(){
return bus.writeString(SHIFT_VALUE); commands.add(SHIFT_WRAP_VALUE);
return this;
} }
public int shiftWrap() { public LightingCommandBuilder fillShift(String colorValue){
return bus.writeString(SHIFT_WRAP_VALUE); commands.add(colorValue + "252");
return this;
} }
public int flash(String colorValue) { public LightingCommandBuilder shift(){
return executeColor(colorValue, "249"); commands.add(SHIFT_VALUE);
return this;
} }
public LightingCommandBuilder fillRainbow(){
commands.add(RAINBOW_VALUE);
return this;
}
public LightingCommandBuilder flash(String colorValue){
commands.add(colorValue + "249");
return this;
}
public LightingCommandBuilder switchGradient(String colorValue){
commands.add(colorValue + "253");
return this;
}
public LightingCommand build(){
return new LightingCommand(commands);
}
}
public static class LightingCommand{
private ArrayList<String> commands;
public LightingCommand(ArrayList<String> commands){
this.commands = commands;
}
public ArrayList<String> getCommands(){
return commands;
}
}
} }