Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
E
ECSS Services
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
AcubeSat-OBC
ECSS Services
Commits
2b51a94e
Commit
2b51a94e
authored
2 years ago
by
kongr45gpen
Committed by
athatheo
2 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Add notifying parameter callbacks
parent
21283376
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
inc/Helpers/NotifyParameter.hpp
+93
-0
93 additions, 0 deletions
inc/Helpers/NotifyParameter.hpp
inc/Helpers/Parameter.hpp
+1
-1
1 addition, 1 deletion
inc/Helpers/Parameter.hpp
test/Parameters/NotifyParameterTests.cpp
+63
-0
63 additions, 0 deletions
test/Parameters/NotifyParameterTests.cpp
with
157 additions
and
1 deletion
inc/Helpers/NotifyParameter.hpp
0 → 100644
+
93
−
0
View file @
2b51a94e
#ifndef ECSS_SERVICES_NOTIFYPARAMETER_HPP
#define ECSS_SERVICES_NOTIFYPARAMETER_HPP
#include
<etl/optional.h>
#include
<functional>
#include
"Parameter.hpp"
/**
* A Notifying parameter will call a function whenever its value is written to.
*
* This is useful for updating the state of things when a parameter is changed,
* for example to disable/enable peripherals, to make configuration changes etc.
*
* @warning Calling NotifyParameter::setValue will *not* call the notifier
* function. You should use setValueLoudly for this purpose instead.
*
* @tparam DataType The data type of the parameter's value
*/
template
<
typename
DataType
>
class
NotifyParameter
:
public
Parameter
<
DataType
>
{
public:
using
Notifier
=
std
::
function
<
void
(
const
DataType
&
)
>
;
using
Parent
=
Parameter
<
DataType
>
;
/**
* Constructor without a notifier function. Nothing will then happen when the parameter is updated.
*/
explicit
NotifyParameter
(
DataType
initialValue
)
:
Parent
(
initialValue
)
{}
/**
* Constructor with a default notifier function.
*/
NotifyParameter
(
DataType
initialValue
,
const
Notifier
&
notifier
)
:
Parent
(
initialValue
),
notifier
(
notifier
)
{}
/**
* Same as Parameter::setValue(), but also calls the NotifyParameter::notifier function, if it
* exists.
*/
inline
void
setValueLoudly
(
DataType
value
)
{
Parent
::
setValue
(
value
);
if
(
notifier
)
{
(
*
notifier
)(
Parent
::
currentValue
);
}
}
/**
* Call the notifier if it exists, without updating the value
*/
inline
void
notify
()
{
if
(
notifier
)
{
(
*
notifier
)(
Parent
::
currentValue
);
}
}
inline
void
setValueFromMessage
(
Message
&
message
)
override
{
Parent
::
setValueFromMessage
(
message
);
if
(
notifier
)
{
(
*
notifier
)(
Parent
::
currentValue
);
}
}
/**
* Set the notifier function, to be called whenever the value of this parameter is updated.
*
* @note This function will be called even when a _parameter update_ command is received, but the
* new value is the same as the previous one. This is done so that there is an option to repair
* systems with a weird or unknown state.
* @param call Whether to also call the notifier function immediately, to ensure that a change is
* made.
*/
void
setNotifier
(
const
Notifier
&
_notifier
,
bool
call
=
true
)
{
notifier
=
_notifier
;
if
(
call
)
{
_notifier
(
Parent
::
currentValue
);
}
}
/**
* Unset the notifier function, so that nothing is called when the value of this function is updated.
*/
void
unsetNotifier
()
{
notifier
.
reset
();
}
private
:
etl
::
optional
<
Notifier
>
notifier
;
};
#endif //ECSS_SERVICES_NOTIFYPARAMETER_HPP
This diff is collapsed.
Click to expand it.
inc/Helpers/Parameter.hpp
+
1
−
1
View file @
2b51a94e
...
...
@@ -58,7 +58,7 @@ public:
*/
template
<
typename
DataType
>
class
Parameter
:
public
ParameterBase
{
pr
ivate
:
pr
otected
:
DataType
currentValue
;
public:
...
...
This diff is collapsed.
Click to expand it.
test/Parameters/NotifyParameterTests.cpp
0 → 100644
+
63
−
0
View file @
2b51a94e
#include
"Helpers/NotifyParameter.hpp"
#include
"Message.hpp"
#include
"catch2/catch_all.hpp"
TEST_CASE
(
"Notify Parameter: Notifier"
)
{
int
counter
=
0
;
NotifyParameter
<
uint32_t
>
parameter
(
0
);
SECTION
(
"Notifier not set"
)
{
parameter
.
setValueLoudly
(
1
);
CHECK
(
counter
==
0
);
}
parameter
.
setNotifier
([
&
counter
](
auto
)
->
auto
{
counter
++
;
});
CHECK
(
counter
==
1
);
parameter
.
setValueLoudly
(
2
);
CHECK
(
counter
==
2
);
parameter
.
setValueLoudly
(
2
);
CHECK
(
counter
==
3
);
parameter
.
unsetNotifier
();
parameter
.
setValueLoudly
(
3
);
CHECK
(
counter
==
3
);
}
TEST_CASE
(
"Notify Parameter: Messages"
)
{
int16_t
storage
;
NotifyParameter
<
uint32_t
>
parameter
(
0
,
[
&
storage
](
auto
v
)
->
auto
{
storage
=
v
;
});
Message
message
(
0
,
0
,
Message
::
TC
);
message
.
appendUint32
(
184
);
parameter
.
setValueFromMessage
(
message
);
CHECK
(
storage
==
184
);
}
TEST_CASE
(
"Notify Parameter: Extra functionality"
)
{
int
counter
=
0
;
NotifyParameter
<
uint32_t
>
parameter
(
0
);
auto
notifier
=
[
&
counter
](
auto
)
->
auto
{
counter
++
;
};
parameter
.
setNotifier
(
notifier
);
CHECK
(
counter
==
1
);
parameter
.
setNotifier
(
notifier
,
false
);
CHECK
(
counter
==
1
);
parameter
.
notify
();
CHECK
(
counter
==
2
);
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment