forked from hsstraub/TerpstraSysEx.2014
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add non-realtime helper base class for FirmwareDriverListeners
- Loading branch information
1 parent
0889474
commit fffb44b
Showing
2 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
Source/lumatone_editor_library/lumatone_midi_driver/response_queue.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
|
36 changes: 36 additions & 0 deletions
36
Source/lumatone_editor_library/lumatone_midi_driver/response_queue.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |