| 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/ButtonEvent.h" |
| 24 |
|
|
|
| 25 |
|
|
#include "Base.h" |
| 26 |
|
|
|
| 27 |
|
|
using namespace Reta; |
| 28 |
|
|
using namespace std; |
| 29 |
|
|
|
| 30 |
|
|
/****************************************************************************/ |
| 31 |
|
|
|
| 32 |
|
✗ |
struct RETA_LOCAL ButtonEvent::Impl : public Base |
| 33 |
|
|
{ |
| 34 |
|
|
Impl(shared_ptr<Task>, const string &, unsigned int); |
| 35 |
|
|
|
| 36 |
|
|
shared_ptr<Task> task; |
| 37 |
|
|
const string prefix; |
| 38 |
|
|
|
| 39 |
|
|
vector<uint8_t> event; |
| 40 |
|
|
vector<uint8_t> lastState; |
| 41 |
|
|
}; |
| 42 |
|
|
|
| 43 |
|
|
/****************************************************************************/ |
| 44 |
|
|
|
| 45 |
|
✗ |
ButtonEvent::Impl::Impl( |
| 46 |
|
|
shared_ptr<Task> task, |
| 47 |
|
|
const string &prefix, |
| 48 |
|
✗ |
unsigned int width) : |
| 49 |
|
|
Base {prefix}, |
| 50 |
|
|
task {task}, |
| 51 |
|
|
prefix {prefix}, |
| 52 |
|
|
event(width, false), |
| 53 |
|
✗ |
lastState(width, false) |
| 54 |
|
|
{ |
| 55 |
|
✗ |
checkZeroWidth(width); |
| 56 |
|
|
|
| 57 |
|
✗ |
pdtask *pdtask {task->getPdTask()}; |
| 58 |
|
|
|
| 59 |
|
✗ |
pdserv_signal( |
| 60 |
|
✗ |
pdtask, 1, (prefix + "/Event").c_str(), pd_boolean_T, |
| 61 |
|
✗ |
event.data(), event.size(), NULL); |
| 62 |
|
|
} |
| 63 |
|
|
|
| 64 |
|
|
/****************************************************************************/ |
| 65 |
|
|
|
| 66 |
|
✗ |
ButtonEvent::ButtonEvent( |
| 67 |
|
|
shared_ptr<Task> task, |
| 68 |
|
|
const std::string &prefix, |
| 69 |
|
✗ |
unsigned int width) : |
| 70 |
|
✗ |
impl {make_unique<Impl>(task, prefix, width)} |
| 71 |
|
|
{} |
| 72 |
|
|
|
| 73 |
|
|
/****************************************************************************/ |
| 74 |
|
|
|
| 75 |
|
✗ |
ButtonEvent::~ButtonEvent() |
| 76 |
|
|
{} |
| 77 |
|
|
|
| 78 |
|
|
/****************************************************************************/ |
| 79 |
|
|
|
| 80 |
|
✗ |
void ButtonEvent::update(bool buttonState) |
| 81 |
|
|
{ |
| 82 |
|
✗ |
update(vector<uint8_t> {buttonState}); |
| 83 |
|
|
} |
| 84 |
|
|
|
| 85 |
|
|
/****************************************************************************/ |
| 86 |
|
|
|
| 87 |
|
✗ |
void ButtonEvent::update(function<bool(unsigned int)> buttonState) |
| 88 |
|
|
{ |
| 89 |
|
✗ |
vector<uint8_t> stateVector(impl->event.size()); |
| 90 |
|
✗ |
for (unsigned int i = 0; i < impl->event.size(); i++) { |
| 91 |
|
✗ |
stateVector[i] = buttonState(i); |
| 92 |
|
|
} |
| 93 |
|
|
|
| 94 |
|
✗ |
update(stateVector); |
| 95 |
|
|
} |
| 96 |
|
|
|
| 97 |
|
|
/****************************************************************************/ |
| 98 |
|
|
|
| 99 |
|
✗ |
void ButtonEvent::update(const vector<uint8_t> &buttonState) |
| 100 |
|
|
{ |
| 101 |
|
✗ |
impl->checkSize(buttonState, impl->event); |
| 102 |
|
|
|
| 103 |
|
✗ |
for (unsigned int i = 0; i < impl->event.size(); i++) { |
| 104 |
|
✗ |
impl->event[i] = buttonState[i] && !impl->lastState[i]; |
| 105 |
|
✗ |
impl->lastState[i] = buttonState[i]; |
| 106 |
|
|
} |
| 107 |
|
|
} |
| 108 |
|
|
|
| 109 |
|
|
/****************************************************************************/ |
| 110 |
|
|
|
| 111 |
|
✗ |
bool ButtonEvent::hasEvent(unsigned int i) const |
| 112 |
|
|
{ |
| 113 |
|
✗ |
return impl->event.at(i); |
| 114 |
|
|
} |
| 115 |
|
|
|
| 116 |
|
|
/****************************************************************************/ |
| 117 |
|
|
|
| 118 |
|
✗ |
vector<uint8_t> ButtonEvent::getEventVector() const |
| 119 |
|
|
{ |
| 120 |
|
✗ |
return impl->event; |
| 121 |
|
|
} |
| 122 |
|
|
|
| 123 |
|
|
/****************************************************************************/ |
| 124 |
|
|
|
| 125 |
|
✗ |
bool ButtonEvent::anyEvent() const |
| 126 |
|
|
{ |
| 127 |
|
✗ |
for (auto event : impl->event) { |
| 128 |
|
✗ |
if (event) { |
| 129 |
|
✗ |
return event; |
| 130 |
|
|
} |
| 131 |
|
|
} |
| 132 |
|
|
|
| 133 |
|
✗ |
return false; |
| 134 |
|
|
} |
| 135 |
|
|
|
| 136 |
|
|
/****************************************************************************/ |
| 137 |
|
|
|