-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsimproxy.cc
92 lines (74 loc) · 2.85 KB
/
simproxy.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
* simproxy.cc
*
* Copyright (C) 2016 Mikael Djurfeldt <[email protected]>
*
* music-osc is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* libneurosim is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <music.hh>
#include <cstring>
#include "OurUDPProtocol.hh"
struct OurUDPProtocol::toMusicPackage package;
struct OurUDPProtocol::fromMusicPackage outpackage;
std::array<double, OurUDPProtocol::COMMANDKEYS> commandKeyState;
int main (int argc, char* argv[]) {
MUSIC::Setup* setup = new MUSIC::Setup (argc, argv);
MUSIC::ContInputPort * commands = setup->publishContInput ("commands");
MUSIC::ContInputPort * in = setup->publishContInput ("sensorpool");
MUSIC::ContOutputPort * out = setup->publishContOutput ("motorpool");
MUSIC::ArrayData commandMap (&package.commandKeys,
MPI_DOUBLE,
0,
OurUDPProtocol::COMMANDKEYS);
commands->map (&commandMap, 0.0, 1, false);
MUSIC::ArrayData inMap (&package.keysPressed,
MPI_DOUBLE,
0,
OurUDPProtocol::KEYBOARDSIZE);
in->map (&inMap, 0.0, 1, false);
MUSIC::ArrayData outMap (&outpackage.keysPressed,
MPI_DOUBLE,
0,
OurUDPProtocol::KEYBOARDSIZE);
out->map (&outMap, 1);
double stoptime;
if (!setup->config ("stoptime", &stoptime))
throw std::runtime_error("simproxy: stoptime must be set in the MUSIC configuration file");
MUSIC::Runtime* runtime = new MUSIC::Runtime (setup, OurUDPProtocol::TIMESTEP);
for (; runtime->time () < stoptime; runtime->tick ()) {
// Copy input to output
std::copy(package.keysPressed.begin(), package.keysPressed.end(),
outpackage.keysPressed.begin());
// Press another key, four halfnotes up
std::transform(package.keysPressed.begin(), package.keysPressed.end()-4,
outpackage.keysPressed.begin()+4,
outpackage.keysPressed.begin()+4,
[] (double key, double newkey) { return (key>0.5) ? 1 : newkey; });
// Print out if any command keys have changed
std::transform(package.commandKeys.begin(), package.commandKeys.end(),
commandKeyState.begin(),
commandKeyState.begin(),
[] (double &key, double state) {
if (key != state)
std::cout << "commandkey "
<< &key-package.commandKeys.begin()
<< "=" << key << std::endl;
return key;
});
}
runtime->finalize ();
delete runtime;
return 0;
}