author | František Kučera <franta-hg@frantovo.cz> |
Fri, 18 Dec 2020 23:19:32 +0100 | |
branch | v_0 |
changeset 2 | f34476ab597f |
parent 1 | 98274757fcf6 |
child 5 | ef8f4023e32e |
permissions | -rw-r--r-- |
0 | 1 |
/** |
2 |
* DJM-Fix |
|
3 |
* Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info) |
|
4 |
* |
|
5 |
* This program is free software: you can redistribute it and/or modify |
|
6 |
* it under the terms of the GNU General Public License as published by |
|
7 |
* the Free Software Foundation, version 3 of the License. |
|
8 |
* |
|
9 |
* This program is distributed in the hope that it will be useful, |
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
* GNU General Public License for more details. |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License |
|
15 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 |
*/ |
|
1 | 17 |
#include <iostream> |
2
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
18 |
#include <thread> |
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
19 |
#include <chrono> |
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
20 |
#include <stdexcept> |
1 | 21 |
|
22 |
#include "DJMFix.h" |
|
23 |
||
24 |
namespace djmfix { |
|
25 |
||
26 |
class DJMFixImpl : public DJMFix { |
|
27 |
private: |
|
2
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
28 |
MidiSender* midiSender; |
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
29 |
std::thread keepAliveThread; |
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
30 |
bool running = false; |
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
31 |
bool stopped = false; |
1 | 32 |
|
2
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
33 |
void run() { |
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
34 |
while (!stopped) { |
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
35 |
std::cerr << "DJMFixImpl::run()" << std::endl; // TODO: do not mess STDIO |
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
36 |
// TODO: send keep-alive messages |
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
37 |
std::this_thread::sleep_for(std::chrono::milliseconds(200)); |
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
38 |
} |
1 | 39 |
} |
40 |
||
2
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
41 |
public: |
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
42 |
|
1 | 43 |
virtual ~DJMFixImpl() override { |
44 |
std::cerr << "~DJMFixImpl()" << std::endl; // TODO: do not mess STDIO |
|
2
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
45 |
if (running) stop(); |
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
46 |
} |
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
47 |
|
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
48 |
void setMidiSender(MidiSender* midiSender) { |
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
49 |
std::cerr << "DJMFixImpl::setMidiSender()" << std::endl; // TODO: do not mess STDIO |
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
50 |
this->midiSender = midiSender; |
1 | 51 |
} |
52 |
||
53 |
virtual void receive(MidiMessage midiMessage) override { |
|
54 |
std::cerr << "DJMFixImpl::receive()" << std::endl; // TODO: do not mess STDIO |
|
55 |
} |
|
56 |
||
57 |
void start() override { |
|
58 |
std::cerr << "DJMFixImpl::start()" << std::endl; // TODO: do not mess STDIO |
|
2
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
59 |
if (midiSender == nullptr) throw std::logic_error("need a midiSender when starting"); |
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
60 |
midiSender->send({0xf0, 0xf7}); |
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
61 |
|
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
62 |
keepAliveThread = std::thread(&DJMFixImpl::run, this); |
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
63 |
running = true; |
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
64 |
|
1 | 65 |
} |
66 |
||
67 |
void stop() override { |
|
2
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
68 |
stopped = true; |
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
69 |
keepAliveThread.join(); |
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
70 |
running = false; |
1 | 71 |
std::cerr << "DJMFixImpl::stop()" << std::endl; // TODO: do not mess STDIO |
72 |
} |
|
73 |
}; |
|
74 |
||
2
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
75 |
DJMFix* create() { |
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
76 |
return new DJMFixImpl(); |
1 | 77 |
} |
78 |
||
79 |
} |