13 * |
13 * |
14 * You should have received a copy of the GNU General Public License |
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/>. |
15 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
16 */ |
16 */ |
17 #include <iostream> |
17 #include <iostream> |
|
18 #include <thread> |
|
19 #include <chrono> |
|
20 #include <stdexcept> |
18 |
21 |
19 #include "DJMFix.h" |
22 #include "DJMFix.h" |
20 |
23 |
21 namespace djmfix { |
24 namespace djmfix { |
22 |
25 |
23 class DJMFixImpl : public DJMFix { |
26 class DJMFixImpl : public DJMFix { |
24 private: |
27 private: |
25 MidiSender midiSender; |
28 MidiSender* midiSender; |
|
29 std::thread keepAliveThread; |
|
30 bool running = false; |
|
31 bool stopped = false; |
|
32 |
|
33 void run() { |
|
34 while (!stopped) { |
|
35 std::cerr << "DJMFixImpl::run()" << std::endl; // TODO: do not mess STDIO |
|
36 // TODO: send keep-alive messages |
|
37 std::this_thread::sleep_for(std::chrono::milliseconds(200)); |
|
38 } |
|
39 } |
|
40 |
26 public: |
41 public: |
27 |
|
28 DJMFixImpl(MidiSender midiSender) : midiSender(midiSender) { |
|
29 std::cerr << "DJMFixImpl()" << std::endl; // TODO: do not mess STDIO |
|
30 } |
|
31 |
42 |
32 virtual ~DJMFixImpl() override { |
43 virtual ~DJMFixImpl() override { |
33 std::cerr << "~DJMFixImpl()" << std::endl; // TODO: do not mess STDIO |
44 std::cerr << "~DJMFixImpl()" << std::endl; // TODO: do not mess STDIO |
|
45 if (running) stop(); |
|
46 } |
|
47 |
|
48 void setMidiSender(MidiSender* midiSender) { |
|
49 std::cerr << "DJMFixImpl::setMidiSender()" << std::endl; // TODO: do not mess STDIO |
|
50 this->midiSender = midiSender; |
34 } |
51 } |
35 |
52 |
36 virtual void receive(MidiMessage midiMessage) override { |
53 virtual void receive(MidiMessage midiMessage) override { |
37 std::cerr << "DJMFixImpl::receive()" << std::endl; // TODO: do not mess STDIO |
54 std::cerr << "DJMFixImpl::receive()" << std::endl; // TODO: do not mess STDIO |
38 |
|
39 midiSender({0xf0, 0xf7}); |
|
40 } |
55 } |
41 |
56 |
42 void start() override { |
57 void start() override { |
43 std::cerr << "DJMFixImpl::start()" << std::endl; // TODO: do not mess STDIO |
58 std::cerr << "DJMFixImpl::start()" << std::endl; // TODO: do not mess STDIO |
|
59 if (midiSender == nullptr) throw std::logic_error("need a midiSender when starting"); |
|
60 midiSender->send({0xf0, 0xf7}); |
|
61 |
|
62 keepAliveThread = std::thread(&DJMFixImpl::run, this); |
|
63 running = true; |
|
64 |
44 } |
65 } |
45 |
66 |
46 void stop() override { |
67 void stop() override { |
|
68 stopped = true; |
|
69 keepAliveThread.join(); |
|
70 running = false; |
47 std::cerr << "DJMFixImpl::stop()" << std::endl; // TODO: do not mess STDIO |
71 std::cerr << "DJMFixImpl::stop()" << std::endl; // TODO: do not mess STDIO |
48 } |
72 } |
49 }; |
73 }; |
50 |
74 |
51 DJMFix* create(MidiSender midiSender) { |
75 DJMFix* create() { |
52 return new DJMFixImpl(midiSender); |
76 return new DJMFixImpl(); |
53 } |
77 } |
54 |
78 |
55 } |
79 } |