Skip to content

Commit

Permalink
Add command to autodetect 16n channel/cc config with sysex
Browse files Browse the repository at this point in the history
  • Loading branch information
Dewb committed Sep 21, 2023
1 parent 40b0042 commit 3a9d791
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 14 deletions.
66 changes: 56 additions & 10 deletions src/faderbank/FaderbankModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ struct FBFaderParam : rack::engine::ParamQuantity
};

FaderbankModule::FaderbankModule()
: startCC(32)
, _iiDevice(this)
{
config(NUM_FADERS, 0, NUM_FADERS, 0);

Expand All @@ -21,7 +19,11 @@ FaderbankModule::FaderbankModule()
configParam<FBFaderParam>(i, 0.0, 10.0, 0.0);
}

_iiDevice.setAddress(0x34);
for (int i = 0; i < 16; i++)
{
// by default, assign CC faders starting with 32, all on channel 1
inputMap[32 + i] = i;
}
}

FaderbankModule::~FaderbankModule()
Expand All @@ -40,8 +42,8 @@ void FaderbankModule::process(const ProcessArgs& args)
{
outputs[i].setVoltage(params[i].getValue());

float iiValue = params[i].getValue() / 10.0 * FADERBANK_II_MAX_VALUE;
_iiDevice.updateFollowerData(i, static_cast<uint16_t>(iiValue));
// float iiValue = params[i].getValue() / 10.0 * FADERBANK_II_MAX_VALUE;
// _iiDevice.updateFollowerData(i, static_cast<uint16_t>(iiValue));
}
}

Expand All @@ -51,13 +53,36 @@ void FaderbankModule::processMIDIMessage(const rack::midi::Message& msg)

switch (msg.getStatus())
{
case 0xb:
case 0xb: // Continuous Controller
{
// Combine channel and CC number into a lookup key
uint16_t key = (msg.getChannel() << 8) | msg.getNote();
auto iter = inputMap.find(key);
if (iter != inputMap.end())
{
uint8_t index = iter->second;
if (index >= 0 && index < NUM_FADERS)
{
params[index].setValue((msg.getValue() * 10.0) / 127.0);
}
}
}
break;
case 0xF: // System Exclusive
{
uint8_t cc = msg.getNote();
int index = cc - startCC;
if (index >= 0 && index < NUM_FADERS)
if (msg.bytes[1] == 0x7d && // 16n manufacturer ID
msg.bytes[2] == 0x00 &&
msg.bytes[3] == 0x00 &&
msg.bytes[4] == 0x0F && // sysex config response ID
msg.bytes.size() > (9 + 48 + NUM_FADERS))
{
params[index].setValue((msg.getValue() * 10.0) / 127.0);
inputMap.clear();
for (int i = 0; i < NUM_FADERS; i++)
{
uint8_t channel = msg.bytes[9 + 16 + i] - 1;
uint8_t ccNum = msg.bytes[9 + 48 + i];
inputMap[(channel << 8) | ccNum] = i;
}
}
}
break;
Expand All @@ -71,6 +96,14 @@ json_t* FaderbankModule::dataToJson()
json_t* rootJ = json_object();
json_object_set_new(rootJ, "midi", midiInput.toJson());
json_object_set_new(rootJ, "faderSize", json_integer(faderSize));

json_t* configJ = json_object();
for (auto& entry : inputMap)
{
json_object_set_new(configJ, std::to_string(entry.first).c_str(), json_integer(entry.second));
}
json_object_set_new(rootJ, "16n_config", configJ);

return rootJ;
}

Expand All @@ -83,4 +116,17 @@ void FaderbankModule::dataFromJson(json_t* rootJ)
json_t* faderSizeJ = json_object_get(rootJ, "faderSize");
if (faderSizeJ)
faderSize = static_cast<FaderSize>(json_integer_value(faderSizeJ));

json_t* configJ = json_object_get(rootJ, "16n_config");
if (configJ)
{
inputMap.clear();
json_t* dataJ;
const char* key;
json_object_foreach(configJ, key, dataJ)
{
int16_t val = std::stoi(key);
inputMap[val] = json_integer_value(dataJ);
}
}
}
7 changes: 3 additions & 4 deletions src/faderbank/FaderbankModule.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#include "rack.hpp"
#include "iiBus.h"

#include <map>

#define NUM_FADERS 16

struct FaderbankModule : rack::Module
Expand All @@ -17,7 +19,7 @@ struct FaderbankModule : rack::Module
void dataFromJson(json_t* rootJ) override;

rack::midi::InputQueue midiInput;
uint8_t startCC;
std::map<uint16_t, uint8_t> inputMap;

typedef enum
{
Expand All @@ -26,7 +28,4 @@ struct FaderbankModule : rack::Module
} FaderSize;

FaderSize faderSize = Fader90mm;

protected:
iiDevice _iiDevice;
};
13 changes: 13 additions & 0 deletions src/faderbank/FaderbankWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,17 @@ void FaderbankWidget::appendContextMenu(Menu* menu)
menu->addChild(createSubmenuItem("MIDI connection", "", [=](Menu *childMenu) {
appendMidiMenu(childMenu, &fb->midiInput);
}));

menu->addChild(createMenuItem("Autodetect 16n configuration", "",
[=]() {
// Send a sysex message to request device channel/CC config.
midi::Message msg;
msg.setSize(6);
msg.bytes = { 0xF0, 0x7d, 0x00, 0x00, 0x1F, 0xF7 };
midi::Output output;
output.setDriverId(fb->midiInput.getDriverId());
output.setDeviceId(fb->midiInput.getDeviceId());
output.sendMessage(msg);
}
));
}

0 comments on commit 3a9d791

Please sign in to comment.