From 6f612de2267fda005e9820412ac80a4d5d6eabc0 Mon Sep 17 00:00:00 2001 From: Vincenzo Sicurella Date: Wed, 14 Aug 2024 23:15:36 -0400 Subject: [PATCH] rework key update buffer --- .../device/key_update_buffer.cpp | 167 +++++++++++------- .../device/key_update_buffer.h | 13 +- 2 files changed, 108 insertions(+), 72 deletions(-) diff --git a/Source/lumatone_editor_library/device/key_update_buffer.cpp b/Source/lumatone_editor_library/device/key_update_buffer.cpp index 01aa1218..dbea9af4 100644 --- a/Source/lumatone_editor_library/device/key_update_buffer.cpp +++ b/Source/lumatone_editor_library/device/key_update_buffer.cpp @@ -5,7 +5,25 @@ LumatoneKeyUpdateBuffer::LumatoneKeyUpdateBuffer(LumatoneFirmwareDriver& driverI : firmwareDriver(driverIn) , LumatoneState("LumatoneKeyUpdateBuffer", state) { - keysToUpdate.remapTable(280); + // updatedLayout.remapTable(280); + + for (int b = 0; b < MAX_LUMATONE_BOARDS; b++) + { + if (b < getNumBoards()) + { + const LumatoneBoard& board = getBoard(b); + for (int k = 0; k < MAX_LUMATONE_BOARD_KEYS; k++) + { + if (k < board.getNumKeys()) + { + const LumatoneKey& key = board.getKey(k); + int index = b * MAX_LUMATONE_BOARD_KEYS + k; + keysToUpdate[index] = nullptr; + lastUpdatedKeys[index] = LumatoneKey(); + } + } + } + } } LumatoneKeyUpdateBuffer::~LumatoneKeyUpdateBuffer() @@ -35,101 +53,114 @@ void LumatoneKeyUpdateBuffer::sendKeyColourConfig(int boardId, int keyIndex, juc void LumatoneKeyUpdateBuffer::updateKeyConfig(int boardIndex, int keyIndex, const LumatoneKey& config) { - juce::ScopedLock l(lock); + // juce::ScopedLock l(lock); // while (!lock.tryEnter()) {} - auto keyNum = getKeyNum(boardIndex, keyIndex); - MappedLumatoneKey currentUpdateData = keysToUpdate[keyNum]; - const LumatoneKey& currentKey = getKey(boardIndex, keyIndex); - preUpdateLayout.setKey(currentKey, boardIndex, keyIndex); - if (currentKey == config) - { - keysToUpdate.set(keyNum, MappedLumatoneKey()); - } - else if (currentUpdateData.boardIndex < 0 || currentUpdateData.keyIndex < 0) - { - auto updateKey = MappedLumatoneKey(config, boardIndex, keyIndex); - updateKey.setColour(currentKey.getColour()); - keysToUpdate.set(keyNum, updateKey); - } - else if (!currentUpdateData.configIsEqual(config)) + int index = boardIndex * MAX_LUMATONE_BOARD_KEYS + keyIndex; + const LumatoneKey& lastUpdatedKey = lastUpdatedKeys[index]; + const LumatoneKey** keyPtr = &keysToUpdate[index]; + + // if (/*currentKey == config && */lastUpdatedKey == config) + // { + // *keyPtr = nullptr; + // } + // else + // { + *keyPtr = ¤tKey; + // } + + if (!isTimerRunning()) { - auto updateKey = MappedLumatoneKey(config, boardIndex, keyIndex); - updateKey.setColour(currentUpdateData.getColour()); - keysToUpdate.set(keyNum, updateKey); + // processUpdates(); + startTimer(updateMs); } - - startTimer(updateMs); } void LumatoneKeyUpdateBuffer::updateKeyColour(int boardIndex, int keyIndex, juce::Colour colour) { - juce::ScopedLock l(lock); + // juce::ScopedLock l(lock); // while (!lock.tryEnter()) {} - auto keyNum = getKeyNum(boardIndex, keyIndex); - MappedLumatoneKey currentUpdateData = keysToUpdate[keyNum]; const LumatoneKey& currentKey = getKey(boardIndex, keyIndex); - preUpdateLayout.setKey(currentKey, boardIndex, keyIndex); - if (currentKey.getColour() == colour && currentKey.configIsEqual(currentUpdateData)) - { - keysToUpdate.set(keyNum, MappedLumatoneKey()); - } - else if (currentUpdateData.boardIndex < 0 || currentUpdateData.keyIndex < 0) - { - auto updateKey = MappedLumatoneKey(currentKey, boardIndex, keyIndex); - updateKey.setColour(colour); - keysToUpdate.set(keyNum, updateKey); - } - else if (currentUpdateData.isColour(colour)) - { - auto updateKey = MappedLumatoneKey(currentUpdateData, boardIndex, keyIndex); - updateKey.setColour(colour); - keysToUpdate.set(keyNum, updateKey); - } + int index = boardIndex * MAX_LUMATONE_BOARD_KEYS + keyIndex; + const LumatoneKey& lastUpdatedKey = lastUpdatedKeys[index]; + const LumatoneKey** keyPtr = &keysToUpdate[boardIndex * MAX_LUMATONE_BOARD_KEYS + keyIndex]; + // if (/*currentKey.isColour(colour) &&*/ lastUpdatedKey.isColour(colour)) + // { + // *keyPtr = nullptr; + // } + // else + // { + *keyPtr = ¤tKey; + // } - startTimer(updateMs); + if (!isTimerRunning()) + { + // processUpdates(); + startTimer(updateMs); + } } -void LumatoneKeyUpdateBuffer::timerCallback() +void LumatoneKeyUpdateBuffer::processUpdates() { - stopTimer(); - - if (! lock.tryEnter()) - return; + // DBG("<<<< process updates >>>>"); - int keyNum = 0; - for (int board = 0; board < getNumBoards(); board++) + for (int board = 0; board < MAX_LUMATONE_BOARDS; board++) { int boardId = board + 1; - for (int keyIndex = 0; keyIndex < getOctaveBoardSize(); keyIndex++) + for (int keyIndex = 0; keyIndex < MAX_LUMATONE_BOARD_KEYS; keyIndex++) { - auto keyUpdate = keysToUpdate[keyNum]; - if (keyUpdate.boardIndex >= 0 && keyUpdate.keyIndex >= 0) - { - // auto currentKey = getKey(board, keyIndex); - auto currentKey = preUpdateLayout.getKey(board, keyIndex); - if (!currentKey.configIsEqual(keyUpdate)) - firmwareDriver.sendKeyFunctionParameters(boardId, keyIndex, keyUpdate.getMidiNumber(), keyUpdate.getMidiChannel(), keyUpdate.getType(), keyUpdate.isCCFaderDefault()); + int index = board * MAX_LUMATONE_BOARD_KEYS + keyIndex; + const LumatoneKey** key = &keysToUpdate[index]; + if (key == nullptr || *key == nullptr) + continue; - if (!currentKey.colourIsEqual(keyUpdate)) - { - auto colour = keyUpdate.getColour(); - if (getLumatoneVersion() >= LumatoneFirmware::ReleaseVersion::VERSION_1_0_11) - firmwareDriver.sendKeyLightParameters(boardId, keyIndex, colour.getRed(), colour.getGreen(), colour.getBlue()); - else - firmwareDriver.sendKeyLightParameters_Version_1_0_0(boardId, keyIndex, colour.getRed() * 0.5f, colour.getGreen() * 0.5f, colour.getBlue() * 0.5f); - } + const LumatoneKey& keyUpdate = **key; + + // const LumatoneKey& currentKey = getKey(board, keyIndex); + const LumatoneKey& lastUpdatedKey = lastUpdatedKeys[index]; + if (!lastUpdatedKey.configIsEqual(keyUpdate)) + firmwareDriver.sendKeyFunctionParameters(boardId, keyIndex, keyUpdate.getMidiNumber(), keyUpdate.getMidiChannel(), keyUpdate.getType(), keyUpdate.isCCFaderDefault()); + + if (!lastUpdatedKey.colourIsEqual(keyUpdate)) + { + auto colour = keyUpdate.getColour(); + if (getLumatoneVersion() >= LumatoneFirmware::ReleaseVersion::VERSION_1_0_11) + firmwareDriver.sendKeyLightParameters(boardId, keyIndex, colour.getRed(), colour.getGreen(), colour.getBlue()); + else + firmwareDriver.sendKeyLightParameters_Version_1_0_0(boardId, keyIndex, colour.getRed() * 0.5f, colour.getGreen() * 0.5f, colour.getBlue() * 0.5f); } - keysToUpdate.set(keyNum, MappedLumatoneKey()); - keyNum++; + *key = nullptr; + lastUpdatedKeys[index] = keyUpdate; } } +} + +void LumatoneKeyUpdateBuffer::handleStatePropertyChange(juce::ValueTree stateIn, const juce::Identifier &property) +{ + LumatoneState::handleStatePropertyChange(stateIn, property); + + // juce::ValueTree lumatoneStateTree = state.getChildWithName(LumatoneConfigProperty::State); + // if (stateIn == lumatoneStateTree || stateIn.isAChildOf(lumatoneStateTree)) + // { + // // layout updated + + // DBG("key update buffer layout update"); + // } +} +void LumatoneKeyUpdateBuffer::timerCallback() +{ + stopTimer(); + + if (! lock.tryEnter()) + return; + // DBG(">>>> buffer timer callback <<<<"); + processUpdates(); } diff --git a/Source/lumatone_editor_library/device/key_update_buffer.h b/Source/lumatone_editor_library/device/key_update_buffer.h index 136e3546..cf207ef0 100644 --- a/Source/lumatone_editor_library/device/key_update_buffer.h +++ b/Source/lumatone_editor_library/device/key_update_buffer.h @@ -25,17 +25,22 @@ class LumatoneKeyUpdateBuffer : public LumatoneState, void updateKeyConfig(int boardIndex, int keyIndex, const LumatoneKey& config); void updateKeyColour(int boardIndex, int keyIndex, juce::Colour colour); + void processUpdates(); + +private: + + void handleStatePropertyChange(juce::ValueTree stateIn, const juce::Identifier& property) override; + private: juce::CriticalSection lock; LumatoneFirmwareDriver& firmwareDriver; - juce::HashMap keysToUpdate; - - LumatoneLayout preUpdateLayout; + const LumatoneKey* keysToUpdate[MAX_LUMATONE_KEYS]; + LumatoneKey lastUpdatedKeys[MAX_LUMATONE_KEYS]; - int updateMs = 300; + int updateMs = 700; }; #endif LUMATONE_UPDATE_BUFFER_H