001/*
002 * Copyright (C) Cross The Road Electronics.  All rights reserved.
003 * License information can be found in CTRE_LICENSE.txt
004 * For support and suggestions contact support@ctr-electronics.com or file
005 * an issue tracker at https://github.com/CrossTheRoadElec/Phoenix-Releases
006 */
007package com.ctre.phoenixpro.configs;
008
009import com.ctre.phoenixpro.StatusCode;
010import com.ctre.phoenixpro.hardware.DeviceIdentifier;
011import com.ctre.phoenixpro.configs.jni.ConfigJNI;
012
013/**
014 * Class for CANcoder, a CAN based magnetic encoder that provides absolute and
015 * relative position along with filtered velocity.
016 *
017 * This handles the configurations for the {@link com.ctre.phoenixpro.hardware.CANcoder}
018 */
019public class CANcoderConfigurator extends ParentConfigurator
020{
021    public CANcoderConfigurator (DeviceIdentifier id)
022    {
023        super(id);
024    }
025
026    /**
027     * Refreshes the values of the specified config group.
028     * <p>
029     * This will wait up to {@link #defaultTimeoutSeconds}.
030     * <p>
031     * Call to refresh the selected configs from the device.
032     *
033     * @param configs The configs to refresh
034     * @return StatusCode of refreshing the configs
035     */
036    public StatusCode refresh(CANcoderConfiguration configs)
037    {
038        return refresh(configs, defaultTimeoutSeconds);
039    }
040
041    /**
042     * Refreshes the values of the specified config group.
043     * <p>
044     * Call to refresh the selected configs from the device.
045     *
046     * @param configs The configs to refresh
047     * @param timeoutSeconds Maximum amount of time to wait when performing configuration
048     * @return StatusCode of refreshing the configs
049     */
050    public StatusCode refresh(CANcoderConfiguration configs, double timeoutSeconds)
051    {
052        StringBuilder serializedString = new StringBuilder();
053        StatusCode err = getConfigsPrivate(serializedString, timeoutSeconds);
054        if (err == StatusCode.OK) {
055            /* Only deserialize if we successfully got configs */
056            configs.deserialize(serializedString.toString());
057        }
058        return err;
059    }
060
061    /**
062     * Applies the contents of the specified config to the device.
063     * <p>
064     * This will wait up to {@link #defaultTimeoutSeconds}.
065     * <p>
066     * Call to apply the selected configs.
067     *
068     * @param configs Configs to apply against.
069     * @return StatusCode of the set command
070     */
071    public StatusCode apply(CANcoderConfiguration configs)
072    {
073        return apply(configs, defaultTimeoutSeconds);
074    }
075
076    /**
077     * Applies the contents of the specified config to the device.
078     * <p>
079     * Call to apply the selected configs.
080     *
081     * @param configs Configs to apply against.
082     * @param timeoutSeconds Maximum amount of time to wait when performing configuration
083     * @return StatusCode of the set command
084     */
085    public StatusCode apply(CANcoderConfiguration configs, double timeoutSeconds)
086    {
087        return setConfigsPrivate(configs.serialize(), timeoutSeconds, configs.FutureProofConfigs, false);
088    }
089
090
091    /**
092     * Refreshes the values of the specified config group.
093     * <p>
094     * This will wait up to {@link #defaultTimeoutSeconds}.
095     * <p>
096     * Call to refresh the selected configs from the device.
097     *
098     * @param configs The configs to refresh
099     * @return StatusCode of refreshing the configs
100     */
101    public StatusCode refresh(MagnetSensorConfigs configs)
102    {
103        return refresh(configs, defaultTimeoutSeconds);
104    }
105
106    /**
107     * Refreshes the values of the specified config group.
108     * <p>
109     * Call to refresh the selected configs from the device.
110     *
111     * @param configs The configs to refresh
112     * @param timeoutSeconds Maximum amount of time to wait when performing configuration
113     * @return StatusCode of refreshing the configs
114     */
115    public StatusCode refresh(MagnetSensorConfigs configs, double timeoutSeconds)
116    {
117        StringBuilder serializedString = new StringBuilder();
118        StatusCode err = getConfigsPrivate(serializedString, timeoutSeconds);
119        if (err == StatusCode.OK) {
120            /* Only deserialize if we successfully got configs */
121            configs.deserialize(serializedString.toString());
122        }
123        return err;
124    }
125
126    /**
127     * Applies the contents of the specified config to the device.
128     * <p>
129     * This will wait up to {@link #defaultTimeoutSeconds}.
130     * <p>
131     * Call to apply the selected configs.
132     *
133     * @param configs Configs to apply against.
134     * @return StatusCode of the set command
135     */
136    public StatusCode apply(MagnetSensorConfigs configs)
137    {
138        return apply(configs, defaultTimeoutSeconds);
139    }
140
141    /**
142     * Applies the contents of the specified config to the device.
143     * <p>
144     * Call to apply the selected configs.
145     *
146     * @param configs Configs to apply against.
147     * @param timeoutSeconds Maximum amount of time to wait when performing configuration
148     * @return StatusCode of the set command
149     */
150    public StatusCode apply(MagnetSensorConfigs configs, double timeoutSeconds)
151    {
152        return setConfigsPrivate(configs.serialize(), timeoutSeconds, false, false);
153    }
154
155    
156    /**
157     * The position to set the sensor position to right now.
158     * <p>
159     * This will wait up to {@link #defaultTimeoutSeconds}.
160     * <p>
161     * This is available in the configurator in case the user wants
162     * to initialize their device entirely without passing a device
163     * reference down to the code that performs the initialization.
164     * In this case, the user passes down the configurator object
165     * and performs all the initialization code on the object.
166     *
167     * @param newValue Value to set to.
168     * @return StatusCode of the set command
169     */
170    public StatusCode setPosition(double newValue) {
171        return setPosition(newValue, defaultTimeoutSeconds);
172    }
173    /**
174     * The position to set the sensor position to right now.
175     * <p>
176     * This is available in the configurator in case the user wants
177     * to initialize their device entirely without passing a device
178     * reference down to the code that performs the initialization.
179     * In this case, the user passes down the configurator object
180     * and performs all the initialization code on the object.
181     *
182     * @param newValue Value to set to.
183     * @param timeoutSeconds Maximum time to wait up to in seconds.
184     * @return StatusCode of the set command
185     */
186    public StatusCode setPosition(double newValue, double timeoutSeconds) {
187        String serialized = ConfigJNI.Serializedouble(1010, newValue);
188        return setConfigsPrivate(serialized, timeoutSeconds, false, true);
189    }
190    
191    /**
192     * Clear the sticky faults in the device.
193     * <p>
194     * This typically has no impact on the device functionality.  Instead,
195     * it just clears telemetry faults that are accessible via API and
196     * Tuner Self-Test.
197     * <p>
198     * This will wait up to {@link #defaultTimeoutSeconds}.
199     * <p>
200     * This is available in the configurator in case the user wants
201     * to initialize their device entirely without passing a device
202     * reference down to the code that performs the initialization.
203     * In this case, the user passes down the configurator object
204     * and performs all the initialization code on the object.
205     * @return StatusCode of the set command
206     */
207    public StatusCode clearStickyFaults() {
208        return clearStickyFaults(defaultTimeoutSeconds);
209    }
210    /**
211     * Clear the sticky faults in the device.
212     * <p>
213     * This typically has no impact on the device functionality.  Instead,
214     * it just clears telemetry faults that are accessible via API and
215     * Tuner Self-Test.
216     * <p>
217     * This is available in the configurator in case the user wants
218     * to initialize their device entirely without passing a device
219     * reference down to the code that performs the initialization.
220     * In this case, the user passes down the configurator object
221     * and performs all the initialization code on the object.
222     * @param timeoutSeconds Maximum time to wait up to in seconds.
223     * @return StatusCode of the set command
224     */
225    public StatusCode clearStickyFaults(double timeoutSeconds) {
226        String serialized = ConfigJNI.Serializedouble(1476, 0);
227        return setConfigsPrivate(serialized, timeoutSeconds, false, true);
228    }
229}