RobotCore 1.0
WPILib port to raspberry Pi
Loading...
Searching...
No Matches
UnitTelemetry.java
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5package org.wpilib.internal;
6
7import io.avaje.jsonb.Jsonb;
8import java.util.HashMap;
9import java.util.IdentityHashMap;
10import java.util.Map;
11import org.wpilib.telemetry.TelemetryTable;
12import org.wpilib.units.Measure;
13import org.wpilib.units.Unit;
14
16public final class UnitTelemetry {
17 private static final Jsonb s_json = Jsonb.instance();
18 private static final Map<Unit, String> s_unitMetadata = new IdentityHashMap<>();
19 private static final Map<String, String> s_symbolMetadata = new HashMap<>();
20
21 private UnitTelemetry() {
22 throw new UnsupportedOperationException("This is a utility class!");
23 }
24
33 public static String getUnitMetadata(Unit unit) {
34 synchronized (s_unitMetadata) {
35 String metadata = s_unitMetadata.get(unit);
36 if (metadata != null) {
37 return metadata;
38 }
39 metadata = s_json.toJson(unit.symbol());
40 s_unitMetadata.put(unit, metadata);
41 return metadata;
42 }
43 }
44
53 public static String getUnitMetadata(String unitSymbol) {
54 synchronized (s_symbolMetadata) {
55 String metadata = s_symbolMetadata.get(unitSymbol);
56 if (metadata != null) {
57 return metadata;
58 }
59 metadata = s_json.toJson(unitSymbol);
60 s_symbolMetadata.put(unitSymbol, metadata);
61 return metadata;
62 }
63 }
64
74 public static void log(TelemetryTable table, String name, Measure<?> value) {
75 Unit baseUnit = value.unit().getBaseUnit();
76 table.setProperty(name, "unit", getUnitMetadata(baseUnit));
77 table.log(name, value.baseUnitMagnitude());
78 }
79
90 public static void log(TelemetryTable table, String name, double value, Unit unit) {
91 table.setProperty(name, "unit", getUnitMetadata(unit.getBaseUnit()));
92 table.log(name, unit.toBaseUnits(value));
93 }
94
103 public static void log(TelemetryTable table, String name, double value, String unitSymbol) {
104 table.setProperty(name, "unit", getUnitMetadata(unitSymbol));
105 table.log(name, value);
106 }
107
118 public static void log(TelemetryTable table, String name, int value, Unit unit) {
119 log(table, name, (double) value, unit);
120 }
121
132 public static void log(TelemetryTable table, String name, long value, Unit unit) {
133 log(table, name, (double) value, unit);
134 }
135}
static void log(TelemetryTable table, String name, long value, Unit unit)
static String getUnitMetadata(Unit unit)
static String getUnitMetadata(String unitSymbol)
static void log(TelemetryTable table, String name, int value, Unit unit)
static void log(TelemetryTable table, String name, double value, String unitSymbol)
static void log(TelemetryTable table, String name, double value, Unit unit)
static void log(TelemetryTable table, String name, Measure<?> value)
void setProperty(String name, String key, String value)
double toBaseUnits(double valueInNativeUnits)
Definition Unit.java:166