PX4 Firmware
PX4 Autopilot Software http://px4.io
BlockParam.hpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (C) 2012-2017 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 
34 /**
35  * @file BlockParam.hpp
36  *
37  * Controller library code
38  */
39 
40 #pragma once
41 
42 #include "Block.hpp"
43 
44 #include <containers/List.hpp>
45 #include <px4_platform_common/defines.h>
46 #include <parameters/param.h>
47 
48 namespace control
49 {
50 
51 class Block;
52 
53 // A base class for block params that enables traversing linked list.
54 class BlockParamBase : public ListNode<BlockParamBase *>
55 {
56 public:
57  /**
58  * Instantiate a block param base.
59  *
60  * @param parent_prefix Set to true to include the parent name in the parameter name
61  */
62  BlockParamBase(Block *parent, const char *name, bool parent_prefix = true);
63  virtual ~BlockParamBase() = default;
64 
65  virtual bool update() = 0;
66  const char *getName() const { return param_name(_handle); }
67 
68 protected:
70 };
71 
72 // Parameters that are tied to blocks for updating and naming.
73 template <class T>
74 class __EXPORT BlockParam final : public BlockParamBase
75 {
76 public:
77  BlockParam(Block *block, const char *name, bool parent_prefix = true);
78  BlockParam(Block *block, const char *name, bool parent_prefix, T &extern_val);
79 
80  ~BlockParam() override = default;
81 
82  // no copy, assignment, move, move assignment
83  BlockParam(const BlockParam &) = delete;
84  BlockParam &operator=(const BlockParam &) = delete;
85  BlockParam(BlockParam &&) = delete;
86  BlockParam &operator=(BlockParam &&) = delete;
87 
88  T get() const { return _val; }
89 
90  // Store the parameter value to the parameter storage (@see param_set())
91  bool commit() { return (param_set(_handle, &_val) == PX4_OK); }
92 
93  // Store the parameter value to the parameter storage, w/o notifying the system (@see param_set_no_notification())
94  bool commit_no_notification() { return (param_set_no_notification(_handle, &_val) == PX4_OK); }
95 
96  void set(T val) { _val = val; }
97 
98  bool update() override { return (param_get(_handle, &_val) == PX4_OK); }
99 
100 protected:
101  T _val;
102 };
103 
104 template <>
106 
112 
113 } // namespace control
#define PARAM_INVALID
Handle returned when a parameter cannot be found.
Definition: param.h:103
bool update() override
Definition: BlockParam.hpp:98
__EXPORT int param_get(param_t param, void *val)
Copy the value of a parameter.
Definition: parameters.cpp:589
BlockParam< int32_t & > BlockParamExtInt
Definition: BlockParam.hpp:111
__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:820
__EXPORT int param_set(param_t param, const void *val)
Set the value of a parameter.
Definition: parameters.cpp:814
Definition: I2C.hpp:51
An intrusive linked list.
Global flash based parameter store.
Controller library code.
__EXPORT const char * param_name(param_t param)
Obtain the name of a parameter.
Definition: parameters.cpp:486
const char * getName() const
Definition: BlockParam.hpp:66
BlockParamBase(Block *parent, const char *name, bool parent_prefix=true)
Instantiate a block param base.
Definition: BlockParam.cpp:49
BlockParam< int32_t > BlockParamInt
Definition: BlockParam.hpp:108
Definition: reflect.c:56
BlockParam< float & > BlockParamExtFloat
Definition: BlockParam.hpp:110
const char * name
Definition: tests_main.c:58
virtual ~BlockParamBase()=default
BlockParam< bool > BlockParamBool
Definition: BlockParam.hpp:109
BlockParam< float > BlockParamFloat
Definition: BlockParam.hpp:107
bool commit_no_notification()
Definition: BlockParam.hpp:94
virtual bool update()=0
uint32_t param_t
Parameter handle.
Definition: param.h:98