Simple LEDButton used as a toggle instead of a momentary switch?

Hello! I’m surprised that I haven’t implemented this previously and I could use some direction. I’m trying to create a button/light that toggles on or off when clicked.

image

Here’s some of the code that I have in place so far:

Front panel

    addParam(createParamCentered<LEDButton>(mm2px(Vec(x, y)), module, SamplerX8::MUTE_BUTTON_1));
    addChild(createLightCentered<MediumLight<GreenLight>>(mm2px(Vec(x,y)), module, SamplerX8::MUTE_BUTTON_1_LIGHT));

Parameters:

enum ParamIds {
    MUTE_BUTTON_1,
    MUTE_BUTTON_2,
    // etc...
    NUM_PARAMS
};

In my process loop, I have some really simple code:

      lights[MUTE_LIGHT_1].setBrightness(params[MUTE_BUTTON_1].getValue());

This doesn’t quite do the job. It lights up when I click on the button, but goes dim as soon as the mouse button is released. I’m reasonably good at coding, so I could implement the logic for toggling the light’s brightness using an additional array of booleans for tracking the state.

Saving and loading the patch will save and load the parameters used for the LEDButtons. I’d like to take advantage of that automatic behavior instead of manual saving/loading an array of booleans if possible.

In a perfect world, there would be a “LEDSwitch”. LEDButton already inherits from SVGSwitch, so I’m surprised it’s acting more like a momentary switch than a toggle switch. Any suggestions?

Thanks!

In my repo I have something called ToggleButton. At this point it’s just a thin wrapper around rack::app::SvgSwitch. Oh, but that’s not a light. Never mind.

If I’m to follow the code for Fundamental/Mutes.cpp at v1 · VCVRack/Fundamental · GitHub, which is a very similar use case, it looks like I’ll need to track the state and handle the saving and loading myself. No big deal.

I also used a wrapper for SvgSwitch for my standard lit pushbuttons.

@CountModula Could you point me to some code? I’d love to see it.

So you don’t want to look at mine, but the same stuff from the count is good? What have I done to deserve this?

I’ll contact you directly.

Sure, here you go:

https://github.com/countmodula/VCVRackPlugins/blob/v2.0.0/src/components/CountModulaPushButtons.hpp

Note this uses a customised light as my buttons are rounded squares not circles so you may be able to simplify it a bit if you are using round push buttons.

I also use custom helper specifically for lit buttons to allow m to specify the correct light ID when adding the button in the widget.

1 Like

Good idea, I’ll make a LEDSwitch and LEDLightSwitch type that is not momentary. It will use up/down frames but behave like a SvgSwitch, such as BefacoSwitch.

3 Likes

ouch ! too late for me…

I have already about 2000~3000 LedLight switches …

:sunglasses:

4 Likes

Looks like you probably have this sorted but we do something similar on our ShapeMaster Run button if it’s of any help

Screenshot 2021-11-02 at 14.57.56

1 Like

Thanks, I appreciate the input! :slight_smile:

I need a name for a boolean setting in SvgSwitch that makes it draw frames 0 and 1 for the mouse up and down states, rather than draw the frames corresponding to the switch value. “Latch” is the best idea I have, but it’s not a very obvious name.

Switch::momentary makes the switch behave like a momentary push button. When pressed the param value is set to max, and when released it is set to min. This causes frames 0 and 1 (assuming your switch has 2 frames) to be used when the mouse is pressed and released.

The SvgSwitch::latch setting will make the SvgSwitch directly draw frame 0 when the button is pressed and frame 1 when released, while using the normal (non-momentary) switch behavior of cycling param values upon each mouse press. Frame indexes will not be based on the param value, only the mouse press state.

What is a better name for latch? New component classes with names like BefacoLatch will be created that set this boolean to true.

Sorry, that’s a tough one. I’m not sure. Maybe “toggle”?

“Toggle” is too general because all two-state switches toggle between two values.

1 Like

Maybe “SoftLatch” to distinguish from something that latches physically rather than electronically?

I’m not sure I understand, but OpenStageControl has these kinds of buttons:

push might work. Class names would be BefacoPushButton. The name “push” wouldn’t really mean anything, other than being a unique recognizable word to describe the switched button.

On second thought, “push” doesn’t really work because it doesn’t suggest that the button cycles through 2 or more states any more than “button” does. “Toggle” works better, although it’s too general.

My view is that the latch is actually physical. When clicking this type of component, the button physically “latches” into an ON state, and on a second click, unlatches. The key idea of a “latch” button is that it looks the same when latched/unlatched, but has a down/up state while clicking. So I think I’ll go with SvgSwitch::latch and BefacoLatch, etc. unless there’s another suggestion that works better.

1 Like

For inspiration, here’s the Android “togglebutton”