Package com.reduxrobotics.canand
Class CanandSettingsManager<T extends CanandSettings>
java.lang.Object
com.reduxrobotics.canand.CanandSettingsManager<T>
Common logic for settings management for
CanandDevice
s.
This class holds a CanandSettings
cache of known settings received from the CAN bus,
and offers a series of helper functions that provide common logic for bulk settings operations.-
Nested Class Summary
Modifier and TypeClassDescriptionstatic interface
Functional interface for a CanandSettings constructor.static enum
Setting result codes.static final record
Record of setting results. -
Field Summary
Modifier and TypeFieldDescriptionstatic final int
Bit flag specifying this setting should be set ephemeral., -
Constructor Summary
ConstructorDescriptionConstruct a new CanandSettingsManager. -
Method Summary
Modifier and TypeMethodDescriptionconfirmSetSetting
(int settingIdx, long payload, double timeout, int flags) Potentially blocking operation to send a setting and wait for a report setting message to be received to confirm the operation.fetchMissingSettings
(double timeout, int attempts) Attempt to fill out the known settings with the set of settings it is missing.fetchSetting
(int settingIdx, double timeout) Fetches a setting from the device and returns the received result.Return a CanandSettings of known settings.getSettings
(double timeout, double missingTimeout, int missingAttempts) Fetches the device's current configuration in a blocking manner.void
Setting handler to put inCanandDevice.handleMessage(com.reduxrobotics.canand.CanandMessage)
.sendReceiveSettingCommand
(int cmd, double timeout, boolean clearKnown) Runs a setting command that may mutate all settings and trigger a response.void
sendSettingCommand
(int settingCmdIdx) Sends a setting command with no arguments.void
setSettingById
(int settingId, byte[] value, int flags) Directly sends a CAN message to the associatedCanandDevice
to set a setting by index.void
setSettingById
(int settingId, long value, int flags) Directly sends a CAN message to the associatedCanandDevice
to set a setting by index.boolean
setSettings
(T settings, double timeout) Applies the settings from aCanandSettings
object to the device.setSettings
(T settings, double timeout, int attempts) Applies the settings from aCanandSettings
object to the device, with fine grained control over failure-handling.void
Tells the device to begin transmitting its settings.
-
Field Details
-
kFlag_Ephemeral
public static final int kFlag_EphemeralBit flag specifying this setting should be set ephemeral.,- See Also:
-
-
Constructor Details
-
CanandSettingsManager
Construct a new CanandSettingsManager.- Parameters:
dev
- the device to be associated with this object.ctor
- the constructor of the CanandSettings subclass.
-
-
Method Details
-
getSettings
Fetches the device's current configuration in a blocking manner. This function will block for at least `timeout` seconds waiting for the device to reply, so it is best to put this in a teleop or autonomous init function, rather than the main loop.- Parameters:
timeout
- maximum number of seconds to wait for the all settings pass before giving upmissingTimeout
- maximum number of seconds to wait for each settings retry before giving upmissingAttempts
- if >0, lists how many times to attempt fetching missing settings.- Returns:
CanandSettings
representing what has been received of the device's configuration.
-
fetchMissingSettings
Attempt to fill out the known settings with the set of settings it is missing.- Parameters:
timeout
- maximum timeout per setting index (seconds)attempts
- number of attempts to fetch a setting index (should be at least 1)- Returns:
- a List of setting indexes that were not able to be received despite attempts/timeout
-
startFetchSettings
public void startFetchSettings()Tells the device to begin transmitting its settings. Once they are all transmitted (typically after ~200-300ms), the values can be retrieved fromgetKnownSettings()
-
setSettings
Applies the settings from aCanandSettings
object to the device, with fine grained control over failure-handling. This overload allows specifiyng the number of retries per setting as well as the confirmation timeout. Additionally, it returns aCanandSettings
object of settings that were not able to be successfully applied.- Parameters:
settings
- theCanandSettings
to update the device withtimeout
- maximum time in seconds to wait for each setting to be confirmed. Set to 0 to not check (and not block).attempts
- the maximum number of attempts to write each individual setting- Returns:
- a CanandSettings object of unsuccessfully set settings.
-
setSettings
Applies the settings from aCanandSettings
object to the device.- Parameters:
settings
- theCanandSettings
to update the device withtimeout
- maximum time in seconds to wait for each setting to be confirmed. Set to 0 to not check (and not block).- Returns:
- true if successful, false if a setting operation failed
-
sendReceiveSettingCommand
Runs a setting command that may mutate all settings and trigger a response. Typically used with "reset factory default" type commands- Parameters:
cmd
- setting indextimeout
- total timeout for all settings to be returnedclearKnown
- whether to clear the set of known settings- Returns:
- the set of known settings.
-
getKnownSettings
Return a CanandSettings of known settings. The object returned is a copy of this object's internal copy.- Returns:
- known settings
-
handleSetting
Setting handler to put inCanandDevice.handleMessage(com.reduxrobotics.canand.CanandMessage)
. Example:// boilerplate definition (in practice it won't be null) CanandSettingsManager settingsMgr = null; // from your handler: CanandMessage msg = new CanandMessage(); // in the handler: switch(msg.getApiIndex()) { case CanandDeviceDetails.Msg.kReportSetting: { if (settingsMgr == null) break; settingsMgr.handleSetting(msg); break; } default: break; }
- Parameters:
msg
- the CanandMessage containing the settings data to process (from handleMessage)
-
setSettingById
public void setSettingById(int settingId, long value, int flags) Directly sends a CAN message to the associatedCanandDevice
to set a setting by index. This function does not block nor check if a report settings message is sent in response.Device subclasses will usually have a more user-friendly settings interface, eliminating the need to call this function directly in the vast majority of cases.
- Parameters:
settingId
- the setting idvalue
- the raw numerical value. Only the lower 48 bits will be used.flags
- optional flags to send to the device specifying how the setting will be set.
-
setSettingById
public void setSettingById(int settingId, byte[] value, int flags) Directly sends a CAN message to the associatedCanandDevice
to set a setting by index. This function does not block nor check if a report settings message is sent in response.Device subclasses will usually have a more user-friendly settings interface, eliminating the need to call this function directly in the vast majority of cases.
- Parameters:
settingId
- the setting idvalue
- a value byte array. Up to the first 6 bytes will be used.flags
- optional flags to send to the device specifying how the setting will be set.
-
confirmSetSetting
public CanandSettingsManager.SettingResult confirmSetSetting(int settingIdx, long payload, double timeout, int flags) Potentially blocking operation to send a setting and wait for a report setting message to be received to confirm the operation.- Parameters:
settingIdx
- Setting index to set and listen forpayload
- the long value to send. Only lower 48 bits are used.timeout
- the timeout to wait before giving up in seconds. Passing in 0 will return instantly (not block)flags
- optional flags to send to the device specifying how the setting will be set.- Returns:
- the value received by the report setting packet if existent or
CanandSettingsManager.SettingResult.TIMEOUT
otherwise. If timeout = 0, return "payload" (assume success)
-
fetchSetting
Fetches a setting from the device and returns the received result.- Parameters:
settingIdx
- Setting index to fetchtimeout
- timeout to wait before giving up in seconds. Passing in 0 will returnCanandSettingsManager.SettingResult.TIMEOUT
.- Returns:
CanandSettingsManager.SettingResult
representing the setting result.
-
sendSettingCommand
public void sendSettingCommand(int settingCmdIdx) Sends a setting command with no arguments.- Parameters:
settingCmdIdx
- the index of the setting command to send.
-