GCC Code Coverage Report


Directory: src/
File: src/ExternalSwitch.cpp
Date: 2026-09-03 17:04:53
Exec Total Coverage
Lines: 0 35 0.0%
Branches: 0 32 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/ExternalSwitch.h"
24
25 #include "Base.h"
26 #include "reta/Delay.h"
27
28 #include <sstream>
29 #include <stdexcept>
30
31 using namespace Reta;
32 using namespace std;
33
34 /****************************************************************************/
35
36 struct RETA_LOCAL ExternalSwitch::Impl : public Base
37 {
38 Impl(shared_ptr<Task>, const string &, unsigned int);
39
40 shared_ptr<Task> task;
41
42 vector<uint8_t> state;
43 Delay downDelay;
44 vector<uint8_t> prevDelayState;
45 vector<uint8_t> rising;
46 };
47
48 /****************************************************************************/
49
50 ExternalSwitch::Impl::Impl(
51 shared_ptr<Task> task,
52 const string &prefix,
53 unsigned int width) :
54 Base {prefix},
55 task {task},
56 state(width, false),
57 downDelay(task, prefix + "/DownDelay", 0.5, width),
58 prevDelayState(width, false),
59 rising(width, false)
60 {
61 checkZeroWidth(width);
62
63 pdtask *pdtask {task->getPdTask()};
64
65 pdserv_signal(
66 pdtask, 1, (prefix + "/State").c_str(), pd_boolean_T,
67 state.data(), state.size(), NULL);
68 pdserv_signal(
69 pdtask, 1, (prefix + "/Rising").c_str(), pd_boolean_T,
70 rising.data(), rising.size(), NULL);
71 }
72
73 /****************************************************************************/
74
75 ExternalSwitch::ExternalSwitch(
76 shared_ptr<Task> task,
77 const std::string &prefix,
78 unsigned int width) :
79 impl {make_unique<Impl>(task, prefix, width)}
80 {}
81
82 /****************************************************************************/
83
84 ExternalSwitch::~ExternalSwitch()
85 {}
86
87 /****************************************************************************/
88
89 void ExternalSwitch::setProcessData(bool state, unsigned int i)
90 {
91 impl->state.at(i) = state;
92 }
93
94 /****************************************************************************/
95
96 void ExternalSwitch::update()
97 {
98 bool resetDelay = false;
99
100 for (unsigned int i = 0; i < impl->state.size(); i++) {
101 impl->downDelay.updateDown(impl->state[i], resetDelay);
102
103 impl->rising[i] =
104 impl->downDelay.getOutput(i) && !impl->prevDelayState[i];
105 impl->prevDelayState[i] = impl->downDelay.getOutput(i);
106 }
107 }
108
109 /****************************************************************************/
110
111 bool ExternalSwitch::isRising(unsigned int i) const
112 {
113 return impl->rising.at(i);
114 }
115
116 /****************************************************************************/
117
118 std::vector<uint8_t> ExternalSwitch::getRisingVector() const
119 {
120 return impl->rising;
121 }
122
123 /****************************************************************************/
124
125 bool ExternalSwitch::anyRising() const
126 {
127 for (auto event : impl->rising) {
128 if (event) {
129 return event;
130 }
131 }
132
133 return false;
134 }
135
136 /****************************************************************************/
137