ReduxLib C++ 2026.1.2
Loading...
Searching...
No Matches
CanandSettings.h
1// Copyright (c) Bagholders of Redux Robotics and other contributors.
2// This is open source and can be modified and shared under the Mozilla Public License v2.0.
3
4#pragma once
5#include <cstdint>
6#include <unordered_map>
7#include <vector>
8#include <fmt/format.h>
9#include "CanandDevice.h"
10
11namespace redux::canand {
12
13/**
14 * Base (simple) settings class for Redux devices.
15 *
16 * Inheriting classes with more complex firmware interfaces may or may not use this structure.
17 * It's typically used in conjunction with CanandSettingsManager.
18 *
19 * In general, however, it's a pretty useful structure.
20 */
22 public:
23 /** Default constructor. */
24 CanandSettings() = default;
25 /**
26 * Copy constructor -- only copies over a filtered copy of values.
27 * @param stg reference to another CanandSettings.
28 */
30
31 /** Destructor. */
32 ~CanandSettings() = default;
33
34 /**
35 * Return a direct filtered view of settings values as a new unordered_map, limited to only valid
36 * settings.
37 * @return map
38 */
39 inline std::unordered_map<uint8_t, uint64_t> FilteredMap() {
40 std::unordered_map<uint8_t, uint64_t> ret;
41 for (auto addr : SettingAddresses()) {
42 if (values.contains(addr)) {
43 ret[addr] = values[addr];
44 }
45 }
46 return ret;
47 }
48
49 /**
50 * Returns whether or not all settings fields have been written into the object.
51 *
52 * <p>May return false if the a getSettings call did not succeed in fetching every setting.
53 *
54 * @return whether the settings object has been filled
55 */
56 inline bool AllSettingsReceived() const {
57 for (auto addr : SettingAddresses()) {
58 if (!values.contains(addr)) return false;
59 }
60 return true;
61 }
62
63 /**
64 * Gets the array of settings addresses this settings class records.
65 * @return DetailsKey array
66 */
67 virtual const std::vector<uint8_t>& SettingAddresses() const {
68 return details::VDEP_SETTINGS;
69 };
70
71 /**
72 * Gets the backing store.
73 * @return the underlying map.
74 */
75 inline std::unordered_map<uint8_t, uint64_t>& GetMap() {
76 return values;
77 }
78
79 /**
80 * Returns if this CanandSettings has any set settings or not.
81 * Useful when a CanandSettings is returned as a result of setSettings to check if all settings
82 * succeeded.
83 * @return true if empty
84 */
85 inline bool IsEmpty() const {
86 return values.empty();
87 }
88
89 /**
90 * Returns if this CanandSettings is set to be ephemeral.
91 *
92 * Ephemeral settings do not persist on device reboot, but do not impose any flash wear.
93 * @return true if ephemeral
94 */
95 inline bool IsEphemeral() const {
96 return ephemeral;
97 }
98
99 /**
100 * Sets whether or not the settings will be set as ephemeral -- that is, does not persist on
101 * device power cycle.
102 *
103 * Pre-v2024 firmwares will not support this!
104 *
105 * @param value true if ephemeral
106 */
107 inline void SetEphemeral(bool value) {
108 ephemeral = value;
109 }
110
111 /**
112 * Dump the CanandSettings map as a string.
113 * @return string
114 */
115 inline std::string ToString() {
116 std::string s = "CanandSettings {\n";
117 for (auto& it: values) {
118 s.append(fmt::format(" 0x{:x}: {:x},\n", it.first, it.second));
119 }
120 s.append("}");
121 return s;
122 }
123
124 protected:
125 /**
126 * The backing store.
127 */
128 std::unordered_map<uint8_t, uint64_t> values;
129
130 /**
131 * Whether the settings in this class should be set ephemerally.
132 */
133 bool ephemeral = false;
134};
135
136}
Definition CanandSettings.h:21
CanandSettings(CanandSettings &stg)
Definition CanandSettings.h:29
bool IsEphemeral() const
Definition CanandSettings.h:95
std::unordered_map< uint8_t, uint64_t > FilteredMap()
Definition CanandSettings.h:39
virtual const std::vector< uint8_t > & SettingAddresses() const
Definition CanandSettings.h:67
void SetEphemeral(bool value)
Definition CanandSettings.h:107
std::unordered_map< uint8_t, uint64_t > values
Definition CanandSettings.h:128
bool ephemeral
Definition CanandSettings.h:133
std::string ToString()
Definition CanandSettings.h:115
bool IsEmpty() const
Definition CanandSettings.h:85
std::unordered_map< uint8_t, uint64_t > & GetMap()
Definition CanandSettings.h:75
bool AllSettingsReceived() const
Definition CanandSettings.h:56
Definition Namespaces.h:17