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.phoenix6.configs;
008
009import com.ctre.phoenix6.StatusCode;
010import com.ctre.phoenix6.hardware.DeviceIdentifier;
011import com.ctre.phoenix6.configs.jni.ConfigJNI;
012import com.ctre.phoenix6.spns.*;
013
014/**
015 * Class for CANcoder, a CAN based magnetic encoder that provides absolute and
016 * relative position along with filtered velocity.
017 *
018 * This handles the configurations for the {@link com.ctre.phoenix6.hardware.CANcoder}
019 */
020public class CANcoderConfigurator extends ParentConfigurator
021{
022    public CANcoderConfigurator (DeviceIdentifier id)
023    {
024        super(id);
025    }
026
027    /**
028     * Refreshes the values of the specified config group.
029     * <p>
030     * This will wait up to {@link #DefaultTimeoutSeconds}.
031     * <p>
032     * Call to refresh the selected configs from the device.
033     *
034     * @param configs The configs to refresh
035     * @return StatusCode of refreshing the configs
036     */
037    public StatusCode refresh(CANcoderConfiguration configs)
038    {
039        return refresh(configs, DefaultTimeoutSeconds);
040    }
041
042    /**
043     * Refreshes the values of the specified config group.
044     * <p>
045     * Call to refresh the selected configs from the device.
046     *
047     * @param configs The configs to refresh
048     * @param timeoutSeconds Maximum amount of time to wait when performing configuration
049     * @return StatusCode of refreshing the configs
050     */
051    public StatusCode refresh(CANcoderConfiguration configs, double timeoutSeconds)
052    {
053        StringBuilder serializedString = new StringBuilder();
054        StatusCode err = getConfigsPrivate(serializedString, timeoutSeconds);
055        if (err == StatusCode.OK) {
056            /* Only deserialize if we successfully got configs */
057            configs.deserialize(serializedString.toString());
058        }
059        return err;
060    }
061
062    /**
063     * Applies the contents of the specified config to the device.
064     * <p>
065     * This will wait up to {@link #DefaultTimeoutSeconds}.
066     * <p>
067     * Call to apply the selected configs.
068     *
069     * @param configs Configs to apply against.
070     * @return StatusCode of the set command
071     */
072    public StatusCode apply(CANcoderConfiguration configs)
073    {
074        return apply(configs, DefaultTimeoutSeconds);
075    }
076
077    /**
078     * Applies the contents of the specified config to the device.
079     * <p>
080     * Call to apply the selected configs.
081     *
082     * @param configs Configs to apply against.
083     * @param timeoutSeconds Maximum amount of time to wait when performing configuration
084     * @return StatusCode of the set command
085     */
086    public StatusCode apply(CANcoderConfiguration configs, double timeoutSeconds)
087    {
088        return setConfigsPrivate(configs.serialize(), timeoutSeconds, configs.FutureProofConfigs, false);
089    }
090
091
092    /**
093     * Refreshes the values of the specified config group.
094     * <p>
095     * This will wait up to {@link #DefaultTimeoutSeconds}.
096     * <p>
097     * Call to refresh the selected configs from the device.
098     *
099     * @param configs The configs to refresh
100     * @return StatusCode of refreshing the configs
101     */
102    public StatusCode refresh(MagnetSensorConfigs configs)
103    {
104        return refresh(configs, DefaultTimeoutSeconds);
105    }
106
107    /**
108     * Refreshes the values of the specified config group.
109     * <p>
110     * Call to refresh the selected configs from the device.
111     *
112     * @param configs The configs to refresh
113     * @param timeoutSeconds Maximum amount of time to wait when performing configuration
114     * @return StatusCode of refreshing the configs
115     */
116    public StatusCode refresh(MagnetSensorConfigs configs, double timeoutSeconds)
117    {
118        StringBuilder serializedString = new StringBuilder();
119        StatusCode err = getConfigsPrivate(serializedString, timeoutSeconds);
120        if (err == StatusCode.OK) {
121            /* Only deserialize if we successfully got configs */
122            configs.deserialize(serializedString.toString());
123        }
124        return err;
125    }
126
127    /**
128     * Applies the contents of the specified config to the device.
129     * <p>
130     * This will wait up to {@link #DefaultTimeoutSeconds}.
131     * <p>
132     * Call to apply the selected configs.
133     *
134     * @param configs Configs to apply against.
135     * @return StatusCode of the set command
136     */
137    public StatusCode apply(MagnetSensorConfigs configs)
138    {
139        return apply(configs, DefaultTimeoutSeconds);
140    }
141
142    /**
143     * Applies the contents of the specified config to the device.
144     * <p>
145     * Call to apply the selected configs.
146     *
147     * @param configs Configs to apply against.
148     * @param timeoutSeconds Maximum amount of time to wait when performing configuration
149     * @return StatusCode of the set command
150     */
151    public StatusCode apply(MagnetSensorConfigs configs, double timeoutSeconds)
152    {
153        return setConfigsPrivate(configs.serialize(), timeoutSeconds, false, false);
154    }
155
156    
157    /**
158     * Sets the current position of the device.
159     * <p>
160     * This will wait up to {@link #DefaultTimeoutSeconds}.
161     * <p>
162     * This is available in the configurator in case the user wants
163     * to initialize their device entirely without passing a device
164     * reference down to the code that performs the initialization.
165     * In this case, the user passes down the configurator object
166     * and performs all the initialization code on the object.
167     * 
168     * @param newValue Value to set to. Units are in rotations.
169     * @return StatusCode of the set command
170     */
171    public StatusCode setPosition(double newValue) {
172        return setPosition(newValue, DefaultTimeoutSeconds);
173    }
174    /**
175     * Sets the current position of the device.
176     * <p>
177     * This is available in the configurator in case the user wants
178     * to initialize their device entirely without passing a device
179     * reference down to the code that performs the initialization.
180     * In this case, the user passes down the configurator object
181     * and performs all the initialization code on the object.
182     * 
183     * @param newValue Value to set to. Units are in rotations.
184     * @param timeoutSeconds Maximum time to wait up to in seconds.
185     * @return StatusCode of the set command
186     */
187    public StatusCode setPosition(double newValue, double timeoutSeconds) {
188        String serialized = ConfigJNI.Serializedouble(SpnValue.CANCoder_SetSensorPosition.value, newValue);
189        return setConfigsPrivate(serialized, timeoutSeconds, false, true);
190    }
191    
192    /**
193     * Clear the sticky faults in the device.
194     * <p>
195     * This typically has no impact on the device functionality.  Instead,
196     * it just clears telemetry faults that are accessible via API and
197     * Tuner Self-Test.
198     * <p>
199     * This will wait up to {@link #DefaultTimeoutSeconds}.
200     * <p>
201     * This is available in the configurator in case the user wants
202     * to initialize their device entirely without passing a device
203     * reference down to the code that performs the initialization.
204     * In this case, the user passes down the configurator object
205     * and performs all the initialization code on the object.
206     * 
207     * @return StatusCode of the set command
208     */
209    public StatusCode clearStickyFaults() {
210        return clearStickyFaults(DefaultTimeoutSeconds);
211    }
212    /**
213     * Clear the sticky faults in the device.
214     * <p>
215     * This typically has no impact on the device functionality.  Instead,
216     * it just clears telemetry faults that are accessible via API and
217     * Tuner Self-Test.
218     * <p>
219     * This is available in the configurator in case the user wants
220     * to initialize their device entirely without passing a device
221     * reference down to the code that performs the initialization.
222     * In this case, the user passes down the configurator object
223     * and performs all the initialization code on the object.
224     * 
225     * @param timeoutSeconds Maximum time to wait up to in seconds.
226     * @return StatusCode of the set command
227     */
228    public StatusCode clearStickyFaults(double timeoutSeconds) {
229        String serialized = ConfigJNI.Serializedouble(SpnValue.SPN_ClearStickyFaults.value, 0);
230        return setConfigsPrivate(serialized, timeoutSeconds, false, true);
231    }
232    
233    /**
234     * Clear sticky fault: Hardware fault occurred
235     * <p>
236     * This will wait up to {@link #DefaultTimeoutSeconds}.
237     * <p>
238     * This is available in the configurator in case the user wants
239     * to initialize their device entirely without passing a device
240     * reference down to the code that performs the initialization.
241     * In this case, the user passes down the configurator object
242     * and performs all the initialization code on the object.
243     * 
244     * @return StatusCode of the set command
245     */
246    public StatusCode clearStickyFault_Hardware() {
247        return clearStickyFault_Hardware(DefaultTimeoutSeconds);
248    }
249    /**
250     * Clear sticky fault: Hardware fault occurred
251     * <p>
252     * This is available in the configurator in case the user wants
253     * to initialize their device entirely without passing a device
254     * reference down to the code that performs the initialization.
255     * In this case, the user passes down the configurator object
256     * and performs all the initialization code on the object.
257     * 
258     * @param timeoutSeconds Maximum time to wait up to in seconds.
259     * @return StatusCode of the set command
260     */
261    public StatusCode clearStickyFault_Hardware(double timeoutSeconds) {
262        String serialized = ConfigJNI.Serializedouble(SpnValue.ClearStickyFault_Hardware.value, 0);
263        return setConfigsPrivate(serialized, timeoutSeconds, false, true);
264    }
265    
266    /**
267     * Clear sticky fault: Device supply voltage dropped to near brownout
268     * levels
269     * <p>
270     * This will wait up to {@link #DefaultTimeoutSeconds}.
271     * <p>
272     * This is available in the configurator in case the user wants
273     * to initialize their device entirely without passing a device
274     * reference down to the code that performs the initialization.
275     * In this case, the user passes down the configurator object
276     * and performs all the initialization code on the object.
277     * 
278     * @return StatusCode of the set command
279     */
280    public StatusCode clearStickyFault_Undervoltage() {
281        return clearStickyFault_Undervoltage(DefaultTimeoutSeconds);
282    }
283    /**
284     * Clear sticky fault: Device supply voltage dropped to near brownout
285     * levels
286     * <p>
287     * This is available in the configurator in case the user wants
288     * to initialize their device entirely without passing a device
289     * reference down to the code that performs the initialization.
290     * In this case, the user passes down the configurator object
291     * and performs all the initialization code on the object.
292     * 
293     * @param timeoutSeconds Maximum time to wait up to in seconds.
294     * @return StatusCode of the set command
295     */
296    public StatusCode clearStickyFault_Undervoltage(double timeoutSeconds) {
297        String serialized = ConfigJNI.Serializedouble(SpnValue.ClearStickyFault_Undervoltage.value, 0);
298        return setConfigsPrivate(serialized, timeoutSeconds, false, true);
299    }
300    
301    /**
302     * Clear sticky fault: Device boot while detecting the enable signal
303     * <p>
304     * This will wait up to {@link #DefaultTimeoutSeconds}.
305     * <p>
306     * This is available in the configurator in case the user wants
307     * to initialize their device entirely without passing a device
308     * reference down to the code that performs the initialization.
309     * In this case, the user passes down the configurator object
310     * and performs all the initialization code on the object.
311     * 
312     * @return StatusCode of the set command
313     */
314    public StatusCode clearStickyFault_BootDuringEnable() {
315        return clearStickyFault_BootDuringEnable(DefaultTimeoutSeconds);
316    }
317    /**
318     * Clear sticky fault: Device boot while detecting the enable signal
319     * <p>
320     * This is available in the configurator in case the user wants
321     * to initialize their device entirely without passing a device
322     * reference down to the code that performs the initialization.
323     * In this case, the user passes down the configurator object
324     * and performs all the initialization code on the object.
325     * 
326     * @param timeoutSeconds Maximum time to wait up to in seconds.
327     * @return StatusCode of the set command
328     */
329    public StatusCode clearStickyFault_BootDuringEnable(double timeoutSeconds) {
330        String serialized = ConfigJNI.Serializedouble(SpnValue.ClearStickyFault_BootDuringEnable.value, 0);
331        return setConfigsPrivate(serialized, timeoutSeconds, false, true);
332    }
333    
334    /**
335     * Clear sticky fault: The magnet distance is not correct or magnet is
336     * missing
337     * <p>
338     * This will wait up to {@link #DefaultTimeoutSeconds}.
339     * <p>
340     * This is available in the configurator in case the user wants
341     * to initialize their device entirely without passing a device
342     * reference down to the code that performs the initialization.
343     * In this case, the user passes down the configurator object
344     * and performs all the initialization code on the object.
345     * 
346     * @return StatusCode of the set command
347     */
348    public StatusCode clearStickyFault_BadMagnet() {
349        return clearStickyFault_BadMagnet(DefaultTimeoutSeconds);
350    }
351    /**
352     * Clear sticky fault: The magnet distance is not correct or magnet is
353     * missing
354     * <p>
355     * This is available in the configurator in case the user wants
356     * to initialize their device entirely without passing a device
357     * reference down to the code that performs the initialization.
358     * In this case, the user passes down the configurator object
359     * and performs all the initialization code on the object.
360     * 
361     * @param timeoutSeconds Maximum time to wait up to in seconds.
362     * @return StatusCode of the set command
363     */
364    public StatusCode clearStickyFault_BadMagnet(double timeoutSeconds) {
365        String serialized = ConfigJNI.Serializedouble(SpnValue.ClearStickyFault_CANCODER_BadMagnet.value, 0);
366        return setConfigsPrivate(serialized, timeoutSeconds, false, true);
367    }
368}