Hello,
Thanks for your help. I’ve got it working, though I ended up using VCVLightButton instead of VCVLightLatch. That was after looking at how the VCV Pulses module works.
I can’t get the saving of settings working for some reason but that’s a separate problem to solve.
Here’s the (slightly truncated) code:
#include "plugin.hpp"
struct Tug : Module {
enum ParamId {
ENUMS(BUTTON_PARAMS, 20),
PARAMS_LEN
};
enum InputId {
INPUT_INPUT,
INPUTS_LEN
};
enum OutputId {
OUTPUT_OUTPUT,
OUTPUTS_LEN
};
enum LightId {
ENUMS(BUTTON_LIGHTS, 20),
LIGHTS_LEN
};
int fromRange = -1;
int toRange = -1;
dsp::BooleanTrigger tapTriggers[20];
Tug() {
config(PARAMS_LEN, INPUTS_LEN, OUTPUTS_LEN, LIGHTS_LEN);
for (int i = 0; i < 20; i++) {
configButton(BUTTON_PARAMS + i, string::f("Button %d", i + 1));
}
configInput(INPUT_INPUT, "Input");
configOutput(OUTPUT_OUTPUT, "Output");
}
void process(const ProcessArgs& args) override {
// find out which column light button has been pressed (if any) and save its index
// index values 0 - 9 are from the "FROM column of light buttons
// index values 10 - 19 are from the "TO" column of light buttons
for (int i = 0; i < 20; i++) {
bool tap = params[BUTTON_PARAMS + i].getValue() > 0.f;
if (tapTriggers[i].process(tap)) {
if (i < 10) {
fromRange = i;
} else {
toRange = i;
}
break;
}
}
// set all lights
for (int i = 0; i < 10; i++) {
lights[BUTTON_LIGHTS + i].setBrightness((i != fromRange ? 0.f : 1.f));
lights[BUTTON_LIGHTS + i + 10].setBrightness((i + 10 != toRange ? 0.f : 1.f));
}
// actual work of the plugin will then go here…
}
// save settings
json_t* settingsToJson() {
json_t* rootJ = json_object();
json_object_set_new(rootJ, "fromRange", json_integer(fromRange));
json_object_set_new(rootJ, "toRange", json_integer(toRange));
return rootJ;
}
// load settings
void settingsFromJson(json_t* rootJ) {
json_t* fromRangeJ = json_object_get(rootJ, "fromRange");
json_t* toRangeJ = json_object_get(rootJ, "toRange");
if (fromRangeJ) {
fromRange = json_integer_value(fromRangeJ);
}
if (toRangeJ) {
toRange = json_integer_value(toRangeJ);
}
}
};
struct TugWidget : ModuleWidget {
TugWidget(Tug* module) {
setModule(module);
setPanel(createPanel(asset::plugin(pluginInstance, "res/Tug.svg")));
// blah blah
// from buttons
addParam(createLightParamCentered<VCVLightButton<MediumSimpleLight<WhiteLight>>>(mm2px(Vec(10.16, 32.15)), module, Tug::BUTTON_PARAMS + 0, Tug::BUTTON_LIGHTS + 0));
// then 9 more…
// to buttons
addParam(createLightParamCentered<VCVLightButton<MediumSimpleLight<WhiteLight>>>(mm2px(Vec(30.48, 32.15)), module, Tug::BUTTON_PARAMS + 10, Tug::BUTTON_LIGHTS + 10));
// then 9 more…
addInput(createInputCentered<PJ301MPort>(mm2px(Vec(10.16, 109.225)), module, Tug::INPUT_INPUT));
addOutput(createOutputCentered<PJ301MPort>(mm2px(Vec(30.48, 109.225)), module, Tug::OUTPUT_OUTPUT));
}
};
Model* modelTug = createModel<Tug, TugWidget>("Tug");
Thanks again!