Skip to content

Commit

Permalink
add non-realtime helper base class for FirmwareDriverListeners
Browse files Browse the repository at this point in the history
  • Loading branch information
vsicurella committed Aug 4, 2024
1 parent 0889474 commit fffb44b
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#include "response_queue.h"

LumatoneResponseQueueReader::LumatoneResponseQueueReader()
{
reset(1000);
}

void LumatoneResponseQueueReader::timerCallback()
{
stopTimer();

juce::MidiBuffer readBuffer;
removeNextBlockOfMessages(readBuffer, bufferReadSize);
for (auto event : readBuffer)
{
bool configMsg = false;
auto midiMessage = event.getMessage();
if (midiMessage.isSysEx())
{
auto sysExData = midiMessage.getSysExData();
auto cmd = sysExData[CMD_ID];
auto description = FirmwareSupport::getCommandDescription(midiMessage);

configMsg = cmd < 0x2;

#if JUCE_DEBUG
if (verbose > 1)
DBG("READ: " + midiMessage.getDescription());
#endif

auto errorCode = FirmwareSupport::parseResponseError(sysExData);
handleResponseError(errorCode, cmd, midiMessage);

if (sysExData[MSG_STATUS] == 1)
{
errorCode = handleResponse(midiMessage);
handleResponseError(errorCode, cmd, midiMessage);
}
}

// Ignore non-sysex messages
}

auto bufferSize = juce::jlimit(0, 999999, readQueueSize.load() - bufferReadSize);
readQueueSize.store(bufferSize);

if (bufferSize != 0)
startTimer(bufferReadTimeoutMs);

bufferReadRequested = false;
}

void LumatoneResponseQueueReader::midiMessageReceived(juce::MidiInput *source, const juce::MidiMessage& midiMessage)
{
if (midiMessage.isSysEx())
{
addMessageToQueue(midiMessage);
readQueueSize.store(readQueueSize.load() + 1);

if (!bufferReadRequested)
{
bufferReadRequested = true;
startTimer(bufferReadTimeoutMs);
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#ifndef LUMATONE_RESPONSE_QUEUE_H
#define LUMATONE_RESPONSE_QUEUE_H

#include "./firmware_driver_listener.h"
#include "./firmware_support.h"

class LumatoneResponseQueueReader : public LumatoneFirmwareDriverListener
, protected juce::MidiMessageCollector
, public juce::Timer
{
public:

LumatoneResponseQueueReader();
virtual ~LumatoneResponseQueueReader() { }

virtual FirmwareSupport::Error handleResponse(const juce::MidiMessage& msg) = 0;
virtual void handleResponseError(FirmwareSupport::Error errorToHandle, int commandReceived, const juce::MidiMessage& msg) { }

// juce::Timer implementation
virtual void timerCallback() override;

protected:

virtual void midiMessageReceived(juce::MidiInput* source, const juce::MidiMessage& message) override;

private:

const int bufferReadTimeoutMs = 30;
const int bufferReadSize = 16;
bool bufferReadRequested = false;
std::atomic<int> readQueueSize;

int verbose = 0;
};

#endif // LUMATONE_RESPONSE_QUEUE_H

0 comments on commit fffb44b

Please sign in to comment.