GCC Code Coverage Report


Directory: src/
File: src/Counter.cpp
Date: 2026-09-03 17:04:53
Exec Total Coverage
Lines: 0 40 0.0%
Branches: 0 46 0.0%

Line Branch Exec Source
1 /*****************************************************************************
2 *
3 * Copyright (C) 2021 Jonathan Grahl <jonathan.grahl@igh.de>
4 * 2021 Florian Pose <florian.pose@igh.de>
5 *
6 * This file is part of the reta library (realtime-automation).
7 *
8 * The reta library is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as published
10 * by the Free Software Foundation, either version 3 of the License, or (at
11 * your option) any later version.
12 *
13 * The reta library is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with the reta library. If not, see <http://www.gnu.org/licenses/>.
20 *
21 ****************************************************************************/
22
23 #include "reta/Counter.h"
24
25 #include "Base.h"
26 #include "reta/UserParameter.h"
27
28 using std::function;
29 using std::make_unique;
30 using std::shared_ptr;
31 using std::string;
32 using std::vector;
33
34 using namespace Reta;
35
36 /****************************************************************************/
37
38 struct RETA_LOCAL Counter::Impl : public Base
39 {
40 Impl(shared_ptr<Task>, const string &, unsigned int);
41
42 shared_ptr<Task> task;
43
44 vector<unsigned int> output;
45 vector<unsigned int> offset;
46
47 UserParameter addOffset;
48 UserParameter reset;
49 };
50
51 /****************************************************************************/
52
53 Counter::Impl::Impl(
54 shared_ptr<Task> task,
55 const string &prefix,
56 unsigned int width) :
57 Base {prefix},
58 task {task},
59 output(width, 0U),
60 offset(width, 0U),
61 addOffset(task, prefix + "/AddOffset", width),
62 reset(task, prefix + "/Reset", width)
63 {
64 checkZeroWidth(width);
65
66 pdserv *pdserv {task->getPdServ()};
67 pdtask *pdtask {task->getPdTask()};
68
69 pdserv_signal(
70 pdtask, 1, (prefix + "/Output").c_str(), pd_uint32_T,
71 output.data(), output.size(), NULL);
72 pdserv_parameter(
73 pdserv, (prefix + "/Offset").c_str(), 0666, pd_uint32_T,
74 offset.data(), offset.size(), NULL, NULL, NULL);
75 }
76
77 /****************************************************************************/
78
79 Counter::Counter(
80 shared_ptr<Task> task,
81 const string &prefix,
82 unsigned int width) :
83 impl {make_unique<Impl>(task, prefix, width)}
84 {}
85
86 /****************************************************************************/
87
88 Counter::~Counter()
89 {}
90
91 /****************************************************************************/
92
93 void Counter::update(bool count)
94 {
95 update(vector<uint8_t> {count});
96 }
97
98 /****************************************************************************/
99
100 void Counter::update(function<bool(unsigned int)> count)
101 {
102 vector<uint8_t> countVector(impl->output.size());
103 for (unsigned int i = 0; i < impl->output.size(); i++) {
104 countVector[i] = count(i);
105 }
106
107 update(countVector);
108 }
109
110 /****************************************************************************/
111 void Counter::update(const vector<uint8_t> &count)
112 {
113 impl->checkSize(count, impl->output);
114
115 impl->addOffset.update();
116 impl->reset.update();
117
118 for (unsigned int i = 0; i < impl->output.size(); i++) {
119 if (count[i]) {
120 impl->output[i]++;
121 }
122
123 if (impl->addOffset.getChanged(i)) {
124 impl->output[i] += impl->offset[i];
125 }
126
127 if (impl->reset.getChanged(i)) {
128 impl->output[i] = 0U;
129 }
130 }
131 }
132
133 /****************************************************************************/
134
135 unsigned int Counter::getOutput(unsigned int i) const
136 {
137 return impl->output.at(i);
138 }
139
140 /****************************************************************************/
141
142 vector<unsigned int> Counter::getOutputVector() const
143 {
144 return impl->output;
145 }
146
147 /****************************************************************************/
148