DJMFix.cpp
author František Kučera <franta-hg@frantovo.cz>
Sat, 19 Dec 2020 17:33:16 +0100
branchv_0
changeset 5 ef8f4023e32e
parent 2 f34476ab597f
child 6 bddcf2bf29f2
permissions -rw-r--r--
sending and receiving MIDI messages through ALSA (the dirty way)

/**
 * DJM-Fix
 * Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info)
 *
 * This program 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, version 3 of the License.
 *
 * This program 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 <iostream>
#include <iomanip>
#include <thread>
#include <atomic>
#include <chrono>
#include <stdexcept>

#include "DJMFix.h"

namespace djmfix {

class DJMFixImpl : public DJMFix {
private:
	MidiSender* midiSender;
	std::thread keepAliveThread;
	std::atomic<bool> running{false};
	std::atomic<bool> stopped{false};

	void run() {
		while (!stopped) {
			std::cerr << "DJMFixImpl::run()" << std::endl; // TODO: do not mess STDIO
			// TODO: send keep-alive messages
			std::this_thread::sleep_for(std::chrono::milliseconds(200));
		}
	}

	// TODO: remove
	std::string toString(const MidiMessage& midiMessage) {
		std::stringstream result;
		for (uint8_t b : midiMessage) result << std::hex << std::setw(2) << std::setfill('0') << (int) b;
		return result.str();
	}

public:

	virtual ~DJMFixImpl() override {
		std::cerr << "~DJMFixImpl()" << std::endl; // TODO: do not mess STDIO
		if (running) stop();
	}

	void setMidiSender(MidiSender* midiSender) {
		std::cerr << "DJMFixImpl::setMidiSender()" << std::endl; // TODO: do not mess STDIO
		this->midiSender = midiSender;
	}

	virtual void receive(MidiMessage midiMessage) override {
		std::cerr << "DJMFixImpl::receive(): size = " << midiMessage.size() << " data = " << toString(midiMessage) << std::endl; // TODO: do not mess STDIO

		if (midiMessage.size() == 54 && midiMessage[9] == 0x13) {
			std::cerr << "DJMFixImpl::receive(): got message with HashA and SeedE" << std::endl; // TODO: do not mess STDIO
		}

	}

	void start() override {
		std::cerr << "DJMFixImpl::start()" << std::endl; // TODO: do not mess STDIO
		if (midiSender == nullptr) throw std::logic_error("need a midiSender when starting DJMFix");

		// TODO: methods for parsing and constructing messages from parts (TLV)
		midiSender->send({0xf0, 0x00, 0x40, 0x05, 0x00, 0x00, 0x00, 0x17, 0x00, 0x50, 0x01, 0xf7});
		std::this_thread::sleep_for(std::chrono::milliseconds(10));
		midiSender->send({0xf0, 0x00, 0x40, 0x05, 0x00, 0x00, 0x00, 0x17, 0x00, 0x12, 0x2a, 0x01, 0x0b, 0x50, 0x69, 0x6f, 0x6e, 0x65, 0x65, 0x72, 0x44, 0x4a, 0x02, 0x0b, 0x72, 0x65, 0x6b, 0x6f, 0x72, 0x64, 0x62, 0x6f, 0x78, 0x03, 0x12, 0x02, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x03, 0x04, 0x08, 0x00, 0x00, 0x00, 0x00, 0xf7});

		keepAliveThread = std::thread(&DJMFixImpl::run, this);
		running = true;

	}

	void stop() override {
		stopped = true;
		keepAliveThread.join();
		running = false;
		std::cerr << "DJMFixImpl::stop()" << std::endl; // TODO: do not mess STDIO
	}
};

DJMFix* create() {
	return new DJMFixImpl();
}

}