Enum array help?

Hello!

I could use some help on a fairly trivial C++ problem. I’ve never really dealt with enum arrays before, but I want to learn. I’m working on my groovebox module, which has a virtual LCD screen. I want to track which state the LCD screen is in. It might display track name, or perhaps other stuff.

My current code is horrible. It looked like:

  bool show_sample_visualizer = false;
  bool show_ratchet_visualizer = false;
  bool show_updates_visualizer = false;

I would use these variables to decide what to display.

I would prefer to have a single variable called something like “lcd_mode”.

That might look like:

if(lcd_mode == 0) then {{ show default }}
if(lcd_mode == 1) then {{ show sample visualizer }}
if(lcd_mode == 2) then {{ show ratchet visualizer }}

I could use defines to change the above to…

#define LCD_MODE_DEFAULT 0
#define LCD_MODE_SAMPLE_VISUALIZER 1
#define LCD_MODE_RATCHET_VISUALIZER 2

if(lcd_mode == LCD_MODE_DEFAULT) then {{ show default }}
if(lcd_mode == LCD_MODE_SAMPLE_VISUALIZER) then {{ show sample visualizer }}
if(lcd_mode == LCD_MODE_RATCHET_VISUALIZER) then {{ show ratchet visualizer }}

This is a step in the right direction.

Would it be better if I did something like…

enum lcd_modes {
    DEFAULT,
    SAMPLE,
    RATCHET,
};

if(lcd_mode == lcd_modes[DEFAULT]) then {{ show default }}
if(lcd_mode == lcd_modes[SAMPLE]) then {{ show sample visualizer }}
if(lcd_mode == lcd_modes[RATCHET]) then {{ show ratchet visualizer }}

?? If so, I could use some help with the syntax, as I’m getting errors like:

src/GrooveBox/GrooveBox.hpp:197:32: error: expected primary-expression before ‘[’ token 197 | lcd_screen_mode = lcd_modes [DEFAULT];

Thanks!

I thinks something more like:

switch (lcd_mode) {
   case DEFAULT:
       show_default();
       break;
  case SAMPLE:
(etc)

You don’t need the array syntax. It’s not lcd_modes[DEFAULT], it’s just plain DEFAULT.

there is a more complicated synax “enum class”. but even with that it would be lcd_modes.DEFAULT.

Thanks a ton. I took your advice and it worked. I’m still a bit surprised that it worked! Defining an enum array seems a lot different than other things I’ve run across in C++. I might try “enum class”. It feels a bit more “safe” to me.

Anyhow, thanks again. :slight_smile:

enum class can be safer, that’s presumably why they invented it (much later than regular enum).

One place you might not even realize that your are using enum is in the port and param definitions. I’ll bet you got something like this in you module:

    enum ParamIds {
        NUM_PARAMS
	};
	enum InputIds {
        INPUT,
        NUM_INPUTS
	};
	enum OutputIds {
        OUTPUT,
        NUM_OUTPUTS
	};
	enum LightIds {
        NUM_LIGHTS
    };

and of course when you use one of these it’s typically Module.outputs[OUTPUT]. Not because enums like arrays, just because in this case VCV has decided that outputs is an array, and since enums are (to an extent) interchangeable with integers it “just works”

You could read the documentation, sometimes this can help :thinking:

:wink:

https://en.cppreference.com/w/cpp/language/enum

@qno Can you actually read that? Because if so, I give you the gift of toast: :bread:! Perhaps I’m just getting old, but that documentation is brutal!

==================================

==================================

Ha ha ha. When I first learned C++, I learned it from C++ Primer Plus, by Stephen Prata. I was fortunate to take his c++ course where he taught at my junior college. Anyhow, I did attempt to google “enum arrays” to figure out what I was missing, but ended up asking the community (thank you @Squinky ) because I knew I’d get a more “human” explanation.

1 Like

As for C++ reading and bread-and-butter programming, I like Stroustrups “A Tour of C++”. I’ve only read editions one and two, but the third edition just came out, covering also C++20. Reads somewhat like a “C++, the Good Parts”, and is similar in length to K&R.

1 Like

So often, I find programming language tutorials to seem like trying to learn a foreign language from a book written in that foreign language, or some other foreign language that I do not know :rofl:

4 Likes

If I read it very, very slowly :rofl: