From 1849ccfaefbbacbb72253e6fc4481479fbf3a980 Mon Sep 17 00:00:00 2001 From: Gitter499 Date: Thu, 18 Jan 2024 19:23:22 -0500 Subject: [PATCH] feat(misc): Added tuning utility --- README.md | 2 +- .../org/robolancers321/util/TunableSet.java | 63 +++++++++++++++++++ 2 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 src/main/java/org/robolancers321/util/TunableSet.java diff --git a/README.md b/README.md index ecf7e73..2212a7a 100644 --- a/README.md +++ b/README.md @@ -1 +1 @@ -## WIP \ No newline at end of file +## WIP diff --git a/src/main/java/org/robolancers321/util/TunableSet.java b/src/main/java/org/robolancers321/util/TunableSet.java new file mode 100644 index 0000000..2181211 --- /dev/null +++ b/src/main/java/org/robolancers321/util/TunableSet.java @@ -0,0 +1,63 @@ +/* (C) Robolancers 2024 */ +package org.robolancers321.util; + +import static edu.wpi.first.wpilibj.smartdashboard.SmartDashboard.*; + +public class TunableSet { + public static class Tunable { + public static int tune(String key, int defaultValue) { + var entry = getEntry(key); + if (!entry.exists()) putNumber(key, defaultValue); + return (int) entry.getInteger(defaultValue); + } + + public static double tune(String key, double defaultValue) { + var entry = getEntry(key); + if (!entry.exists()) putNumber(key, defaultValue); + return entry.getDouble(defaultValue); + } + + public static String tune(String key, String defaultValue) { + var entry = getEntry(key); + if (!entry.exists()) putString(key, defaultValue); + return entry.getString(defaultValue); + } + + public static boolean tune(String key, boolean defaultValue) { + var entry = getEntry(key); + if (!entry.exists()) putBoolean(key, defaultValue); + return entry.getBoolean(defaultValue); + } + } + + private String prefix; + + public TunableSet(String prefix) { + this.prefix = prefix; + } + + // !TODO Add prefixes + public int tune(String key, int defaultValue) { + var entry = getEntry(prefix + " " + key); + if (!entry.exists()) putNumber(key, defaultValue); + return (int) entry.getInteger(defaultValue); + } + + public double tune(String key, double defaultValue) { + var entry = getEntry(prefix + " " + key); + if (!entry.exists()) putNumber(key, defaultValue); + return entry.getDouble(defaultValue); + } + + public String tune(String key, String defaultValue) { + var entry = getEntry(prefix + " " + key); + if (!entry.exists()) putString(key, defaultValue); + return entry.getString(defaultValue); + } + + public boolean tune(String key, boolean defaultValue) { + var entry = getEntry(prefix + " " + key); + if (!entry.exists()) putBoolean(key, defaultValue); + return entry.getBoolean(defaultValue); + } +}