Firmware
px4_param.h
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2018 PX4 Development Team. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  * 3. Neither the name PX4 nor the names of its contributors may be
16  * used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  ****************************************************************************/
33 
40 #pragma once
41 
42 #include "px4_param_macros.h"
43 
44 #include <parameters/px4_parameters_public.h>
45 
49 inline static param_t param_handle(px4::params p)
50 {
51  return (param_t)p;
52 }
53 
54 
55 
56 
57 #define _DEFINE_SINGLE_PARAMETER(x) \
58  do_not_explicitly_use_this_namespace::PAIR(x);
59 
60 #define _CALL_UPDATE(x) \
61  STRIP(x).update();
62 
63 // define the parameter update method, which will update all parameters.
64 // It is marked as 'final', so that wrong usages lead to a compile error (see below)
65 #define _DEFINE_PARAMETER_UPDATE_METHOD(...) \
66  protected: \
67  void updateParamsImpl() final { \
68  APPLY_ALL(_CALL_UPDATE, __VA_ARGS__) \
69  } \
70  private:
71 
72 // Define a list of parameters. This macro also creates code to update parameters.
73 // If you get a compile error like:
74 // error: virtual function ‘virtual void <class>::updateParamsImpl()’
75 // It means you have a custom inheritance tree (at least one class with params that inherits from another
76 // class with params) and you need to use DEFINE_PARAMETERS_CUSTOM_PARENT() for **all** classes in
77 // that tree.
78 #define DEFINE_PARAMETERS(...) \
79  APPLY_ALL(_DEFINE_SINGLE_PARAMETER, __VA_ARGS__) \
80  _DEFINE_PARAMETER_UPDATE_METHOD(__VA_ARGS__)
81 
82 
83 #define _DEFINE_PARAMETER_UPDATE_METHOD_CUSTOM_PARENT(parent_class, ...) \
84  protected: \
85  void updateParamsImpl() override { \
86  parent_class::updateParamsImpl(); \
87  APPLY_ALL(_CALL_UPDATE, __VA_ARGS__) \
88  } \
89  private:
90 
91 #define DEFINE_PARAMETERS_CUSTOM_PARENT(parent_class, ...) \
92  APPLY_ALL(_DEFINE_SINGLE_PARAMETER, __VA_ARGS__) \
93  _DEFINE_PARAMETER_UPDATE_METHOD_CUSTOM_PARENT(parent_class, __VA_ARGS__)
94 
95 
96 
97 // This namespace never needs to be used directly. Use the DEFINE_PARAMETERS_CUSTOM_PARENT and
98 // DEFINE_PARAMETERS macros instead (the Param classes don't depend on using the macro, the macro just
99 // makes sure that update() is automatically called).
101 {
102 
103 template<typename T, px4::params p>
104 class Param
105 {
106 };
107 
108 // We use partial template specialization for each param type. This is only supported for classes, not individual methods,
109 // which is why we have to repeat the whole class
110 template<px4::params p>
111 class Param<float, p>
112 {
113 public:
114  // static type-check
115  static_assert(px4::param_types_array[(int)p] == PARAM_TYPE_FLOAT, "parameter type must be float");
116 
117  Param()
118  {
119  param_set_used(handle());
120  update();
121  }
122 
123  float get() const { return _val; }
124 
125  const float &reference() const { return _val; }
126 
128  bool commit() const { return param_set(handle(), &_val) == 0; }
129 
131  bool commit_no_notification() const { return param_set_no_notification(handle(), &_val) == 0; }
132 
133  void set(float val) { _val = val; }
134 
135  bool update() { return param_get(handle(), &_val) == 0; }
136 
137  param_t handle() const { return param_handle(p); }
138 private:
139  float _val;
140 };
141 
142 // external version
143 template<px4::params p>
144 class Param<float &, p>
145 {
146 public:
147  // static type-check
148  static_assert(px4::param_types_array[(int)p] == PARAM_TYPE_FLOAT, "parameter type must be float");
149 
150  Param(float &external_val)
151  : _val(external_val)
152  {
153  param_set_used(handle());
154  update();
155  }
156 
157  float get() const { return _val; }
158 
159  const float &reference() const { return _val; }
160 
162  bool commit() const { return param_set(handle(), &_val) == 0; }
163 
165  bool commit_no_notification() const { return param_set_no_notification(handle(), &_val) == 0; }
166 
167  void set(float val) { _val = val; }
168 
169  bool update() { return param_get(handle(), &_val) == 0; }
170 
171  param_t handle() const { return param_handle(p); }
172 private:
173  float &_val;
174 };
175 
176 template<px4::params p>
177 class Param<int32_t, p>
178 {
179 public:
180  // static type-check
181  static_assert(px4::param_types_array[(int)p] == PARAM_TYPE_INT32, "parameter type must be int32_t");
182 
183  Param()
184  {
185  param_set_used(handle());
186  update();
187  }
188 
189  int32_t get() const { return _val; }
190 
191  const int32_t &reference() const { return _val; }
192 
194  bool commit() const { return param_set(handle(), &_val) == 0; }
195 
197  bool commit_no_notification() const { return param_set_no_notification(handle(), &_val) == 0; }
198 
199  void set(int32_t val) { _val = val; }
200 
201  bool update() { return param_get(handle(), &_val) == 0; }
202 
203  param_t handle() const { return param_handle(p); }
204 private:
205  int32_t _val;
206 };
207 
208 //external version
209 template<px4::params p>
210 class Param<int32_t &, p>
211 {
212 public:
213  // static type-check
214  static_assert(px4::param_types_array[(int)p] == PARAM_TYPE_INT32, "parameter type must be int32_t");
215 
216  Param(int32_t &external_val)
217  : _val(external_val)
218  {
219  param_set_used(handle());
220  update();
221  }
222 
223  int32_t get() const { return _val; }
224 
225  const int32_t &reference() const { return _val; }
226 
228  bool commit() const { return param_set(handle(), &_val) == 0; }
229 
231  bool commit_no_notification() const { return param_set_no_notification(handle(), &_val) == 0; }
232 
233  void set(int32_t val) { _val = val; }
234 
235  bool update() { return param_get(handle(), &_val) == 0; }
236 
237  param_t handle() const { return param_handle(p); }
238 private:
239  int32_t &_val;
240 };
241 
242 template<px4::params p>
243 class Param<bool, p>
244 {
245 public:
246  // static type-check
247  static_assert(px4::param_types_array[(int)p] == PARAM_TYPE_INT32, "parameter type must be int32_t");
248 
249  Param()
250  {
251  param_set_used(handle());
252  update();
253  }
254 
255  bool get() const { return _val; }
256 
257  const bool &reference() const { return _val; }
258 
260  bool commit() const
261  {
262  int32_t value_int = (int32_t)_val;
263  return param_set(handle(), &value_int) == 0;
264  }
265 
268  {
269  int32_t value_int = (int32_t)_val;
270  return param_set_no_notification(handle(), &value_int) == 0;
271  }
272 
273  void set(bool val) { _val = val; }
274 
275  bool update()
276  {
277  int32_t value_int;
278  int ret = param_get(handle(), &value_int);
279 
280  if (ret == 0) {
281  _val = value_int != 0;
282  return true;
283  }
284 
285  return false;
286  }
287 
288  param_t handle() const { return param_handle(p); }
289 private:
290  bool _val;
291 };
292 
293 template <px4::params p>
295 
296 template <px4::params p>
298 
299 template <px4::params p>
301 
302 template <px4::params p>
304 
305 template <px4::params p>
306 using ParamBool = Param<bool, p>;
307 
308 } /* namespace do_not_explicitly_use_this_namespace */
309 
310 
311 // Raise an appropriate compile error if a Param class is used directly (just to simplify debugging)
312 template<px4::params p>
313 class ParamInt
314 {
315  static_assert((int)p &&false, "Do not use this class directly, use the DEFINE_PARAMETERS macro instead");
316 };
317 template<px4::params p>
319 {
320  static_assert((int)p &&false, "Do not use this class directly, use the DEFINE_PARAMETERS macro instead");
321 };
bool commit_no_notification() const
Store the parameter value to the parameter storage, w/o notifying the system (.
Definition: px4_param.h:131
Definition: px4_param.h:313
#define PARAM_TYPE_INT32
Parameter types.
Definition: param.h:60
__EXPORT int param_get(param_t param, void *val)
Copy the value of a parameter.
Definition: parameters.cpp:587
__EXPORT int param_set_no_notification(param_t param, const void *val)
Set the value of a parameter, but do not notify the system about the change.
Definition: parameters.cpp:803
__EXPORT int param_set(param_t param, const void *val)
Set the value of a parameter.
Definition: parameters.cpp:797
bool commit_no_notification() const
Store the parameter value to the parameter storage, w/o notifying the system (.
Definition: px4_param.h:165
bool commit_no_notification() const
Store the parameter value to the parameter storage, w/o notifying the system (.
Definition: px4_param.h:231
__EXPORT void param_set_used(param_t param)
Mark a parameter as used.
Definition: parameters.cpp:821
bool commit_no_notification() const
Store the parameter value to the parameter storage, w/o notifying the system (.
Definition: px4_param.h:197
bool commit() const
Store the parameter value to the parameter storage (.
Definition: px4_param.h:260
Definition: px4_param.h:318
bool commit() const
Store the parameter value to the parameter storage (.
Definition: px4_param.h:128
Definition: px4_param.h:100
Helper macros used by px4_param.h.
bool commit() const
Store the parameter value to the parameter storage (.
Definition: px4_param.h:228
bool commit() const
Store the parameter value to the parameter storage (.
Definition: px4_param.h:194
bool commit_no_notification() const
Store the parameter value to the parameter storage, w/o notifying the system (.
Definition: px4_param.h:267
bool commit() const
Store the parameter value to the parameter storage (.
Definition: px4_param.h:162
uint32_t param_t
Parameter handle.
Definition: param.h:98