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