001/************************ PROJECT ZIXI ************************/
002/* Copyright (c) 2026 StuyPulse Robotics. All rights reserved.*/
003/* This work is licensed under the terms of the MIT license.  */
004/**************************************************************/
005package com.stuypulse.robot.util.talonfx.logged;
006
007import static org.wpilib.units.Units.*;
008
009import org.wpilib.units.measure.*;
010
011import java.util.HashMap;
012import java.util.Map;
013import java.util.Map.Entry;
014import org.littletonrobotics.junction.LogTable;
015import org.littletonrobotics.junction.inputs.LoggableInputs;
016
017/**
018 *
019 *
020 * <h2>TalonFX Inputs</h2>
021 *
022 * <p>Contains logged data collected from a {@link LoggedTalonFX}.
023 *
024 * <p>A {@code TalonFXInputs} instance is typically stored as a field in a subsystem's {@code
025 * IOInputs} class:
026 *
027 * <pre>{@code
028 * @AutoLog
029 * public static class IntakeIOInputs {
030 *     public TalonFXInputsAutoLogged pivotSignals = new TalonFXInputsAutoLogged();
031 *     public TalonFXInputsAutoLogged rollerSignals = new TalonFXInputsAutoLogged();
032 * }
033 * }</pre>
034 *
035 * <p>The values are populated by {@link LoggedTalonFX#updateInputs(TalonFXInputs)}.
036 *
037 * @see LoggedTalonFX
038 * @see LogTableUtil
039 * @author Faizaan (https://github.com/Faizaan-J)
040 */
041public class TalonFXInputs implements LoggableInputs, Cloneable {
042    public boolean connected;
043    public Current supplyCurrent = Amps.zero();
044    public Current statorCurrent = Amps.zero();
045    public Temperature temperature = Celsius.zero();
046    public Angle position = Degrees.zero();
047    public Voltage appliedVoltage = Volts.zero();
048    public AngularVelocity velocity = DegreesPerSecond.zero();
049
050    private Map<String, Object> additionalSignalValues = new HashMap<>();
051
052    /**
053     * Records additional StatusSignals to be logged.
054     *
055     * @param name The name of the signal to log.
056     * @param value The value of the signal to log.
057     */
058    void recordAdditionalSignal(String name, Object value) {
059        this.additionalSignalValues.put(name, value);
060    }
061
062    /**
063     * Returns the value of an additional logged StatusSignal, or null if signal is not logged.
064     *
065     * @param <T> The type of the signal value.
066     * @param name The name of the StatusSignal to retrieve.
067     * @param type The type of the signal value.
068     * @return The value of the signal, or null if not logged.
069     */
070    public <T> T getAdditionalSignal(String name, Class<T> type) {
071        Object value = additionalSignalValues.get(name);
072        if (value == null) {
073            return null;
074        }
075
076        return type.cast(value);
077    }
078
079    @Override
080    public void toLog(LogTable table) {
081        table.put("Connected", connected);
082        table.put("SupplyCurrent", supplyCurrent);
083        table.put("StatorCurrent", statorCurrent);
084        table.put("Temperature", temperature);
085        table.put("Position", position);
086        table.put("AppliedVoltage", appliedVoltage);
087        table.put("Velocity", velocity);
088
089        for (Entry<String, Object> entry : additionalSignalValues.entrySet()) {
090            LogTableUtil.put(table, entry.getKey(), entry.getValue());
091        }
092    }
093
094    @Override
095    public void fromLog(LogTable table) {
096        connected = table.get("Connected", connected);
097        supplyCurrent = table.get("SupplyCurrent", supplyCurrent);
098        statorCurrent = table.get("StatorCurrent", statorCurrent);
099        temperature = table.get("Temperature", temperature);
100        position = table.get("Position", position);
101        appliedVoltage = table.get("AppliedVoltage", appliedVoltage);
102        velocity = table.get("Velocity", velocity);
103
104        for (Entry<String, Object> entry : additionalSignalValues.entrySet()) {
105            additionalSignalValues.put(
106                    entry.getKey(), LogTableUtil.get(table, entry.getKey(), entry.getValue()));
107        }
108    }
109
110    public TalonFXInputs clone() {
111        TalonFXInputs copy = new TalonFXInputs();
112        copy.connected = connected;
113        copy.supplyCurrent = this.supplyCurrent;
114        copy.statorCurrent = this.statorCurrent;
115        copy.temperature = this.temperature;
116        copy.position = this.position;
117        copy.appliedVoltage = this.appliedVoltage;
118        copy.velocity = this.velocity;
119        copy.additionalSignalValues = new HashMap<>(this.additionalSignalValues);
120        return copy;
121    }
122}