Skip to content

Commit

Permalink
Implented JSONSync
Browse files Browse the repository at this point in the history
  • Loading branch information
avidraccoon committed Sep 6, 2024
1 parent 1d5cda3 commit d0997b7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 20 deletions.
1 change: 1 addition & 0 deletions wpi_interface/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ dependencies {
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'

implementation 'com.googlecode.json-simple:json-simple:1.1.1'
implementation project(':parameter_tools')
}

test {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
package coppercore.wpi_interface;

import coppercore.parameter_tools.JSONSync;
import edu.wpi.first.wpilibj2.command.button.CommandGenericHID;
import edu.wpi.first.wpilibj2.command.button.CommandJoystick;
import edu.wpi.first.wpilibj2.command.button.CommandXboxController;
Expand All @@ -8,17 +11,20 @@
import java.util.function.IntSupplier;

public class Controllers {
public static Map<String, Integer> buttonShorthands;
public static Map<String, Integer> axesShorthands;
public static List<Controller> controllers = null;
public Map<String, Integer> buttonShorthands;
public Map<String, Integer> axesShorthands;
public List<Controller> controllers = null;
public static transient JSONSync<Controllers> synced =
new JSONSync<Controllers>(
new Controllers(), "filePath", new JSONSync.JSONSyncConfigBuilder().build());

static class Controller {
public int port = -1;
public String type = null;
public boolean hasPov = false;
public List<Button> buttons = null;
public List<Axis> axes = null;
//public transient JsonSync synced = ------;
// public transient JsonSync synced = ------;
public transient CommandGenericHID commandHID;

public IntSupplier getPov() {
Expand All @@ -28,11 +34,12 @@ public IntSupplier getPov() {
public void setupController() {
if (port < 1 || port > 5)
throw new RuntimeException("Invalid port must be between 1 and 5 " + port);
commandHID = switch (type) {
case "joystick" -> commandHID = new CommandJoystick(port);
case "xbox" -> commandHID = new CommandXboxController(port);
default -> throw new RuntimeException("Invalid controller type " + type);
};
commandHID =
switch (type) {
case "joystick" -> commandHID = new CommandJoystick(port);
case "xbox" -> commandHID = new CommandXboxController(port);
default -> throw new RuntimeException("Invalid controller type " + type);
};
setupControlInputs();
}

Expand Down Expand Up @@ -74,10 +81,10 @@ public void setupTrigger(CommandGenericHID commandHID) {
try {
id = Integer.valueOf(button, 10);
} catch (NumberFormatException e) {
if (!buttonShorthands.containsKey(button))
if (!synced.getObject().buttonShorthands.containsKey(button))
throw new RuntimeException(
"Button Id not found as interger or in shorthands " + button);
id = buttonShorthands.get(button);
id = synced.getObject().buttonShorthands.get(button);
}
if (isPov) trigger = commandHID.pov(id);
else trigger = commandHID.button(id);
Expand All @@ -95,12 +102,11 @@ public void setupAxis(CommandGenericHID commandHID) {
try {
axisNum = Integer.valueOf(axis, 10);
} catch (NumberFormatException e) {
if (!buttonShorthands.containsKey(axis))
throw new RuntimeException(
"Axis Id not found " + axis);
axisNum = buttonShorthands.get(axis);
if (!synced.getObject().axesShorthands.containsKey(axis))
throw new RuntimeException("Axis Id not found " + axis);
axisNum = synced.getObject().axesShorthands.get(axis);
}
supplier = () -> ((negate)? 1: -1) * commandHID.getRawAxis(axisNum);
supplier = () -> ((negate) ? 1 : -1) * commandHID.getRawAxis(axisNum);
}

public DoubleSupplier getSupplier() {
Expand All @@ -109,18 +115,17 @@ public DoubleSupplier getSupplier() {
}

public static List<Controller> getControllers() {
return controllers;
return synced.getObject().controllers;
}

public static void setupControllers() {
for (Controller controller : controllers) {
for (Controller controller : synced.getObject().controllers) {
controller.setupController();
}
}

public static void loadControllers() {

//synced.loadData();
synced.loadData();

setupControllers();
}
Expand Down

0 comments on commit d0997b7

Please sign in to comment.