Go Down

Topic: enum and switch cases (Read 2016 times) previous topic - next topic

Cue2

Perhaps I'm not very good with google search but I cannot figure out how to use enum with switch-case. The examples I've seen (including on this forum) look like this:

Code: [Select]
enum Color { UNDEF, RED, ORANGE, YELLOW, GREEN,  BLUE, PURPLE };

void setup()
{
  Color clr = RED;
 
  switch (clr)
  {
    case UNDEF: break;
    case RED: clr = BLUE; break;
    case BLUE: clr = YELLOW; break;   
    // etc
    default: break;
  }
}

void loop()
{
}


However these examples do not seem practical since the enum instance is always hardcoded in them.
Is there something like Enum.Parse in the arduino IDE? since code like this will throw a compiler error

at Color clr = arg:

Code: [Select]
char *arg;
arg = //*Some output from some function*;
enum Color { UNDEF, RED, ORANGE, YELLOW, GREEN,  BLUE, PURPLE };
Color clr = arg;
...

which obviously throws the error
Code: [Select]
cannot convert "char*" to "clr" in initialization

Is there a special trick I can use similar to Enum.Parse in C#? or am I better off just using "if, else if"?

pYro_65

Quote
cannot convert "char*" to "clr" in initialization


Is that really what it said?
Seems more appropriate to say the actual type mismatch:
Quote
cannot convert "char*" to "Color" in initialization


If arg was an int or char, I would suggest casting the value, but char* is pretty incompatible.

Code: [Select]
Color clr = ( Color )( unsigned int ) arg;

PaulS

If arg is of the correct type (Color), then
Code: [Select]
Color clr = arg;
is quite possible.

Something other than snippets would be good. Attempts to assign something other than a Color to clr make no sense.

Cue2

@pYro_65, Yes ofcourse you are right, my mistake there.

@PaulS Well this isn't actually part of any code, it's just me thinking out loud with examples of enum switch cases from the internet. I'm not sure how complete code will help but nonetheless here is a complete sketch I wrote based on them as an example where enum instances are not hardcoded:

Code: [Select]

String content = "";
char character;
enum Color { UNDEF, RED, ORANGE, YELLOW, GREEN,  BLUE, PURPLE };
Color clr;

void setup(){
}

void loop()
{

  while(Serial.available()) {
      character = Serial.read();
      content.concat(character);
  }

  if (content != "") {
    clr=content;
    switch (clr)
    {
      case UNDEF: break;
      case RED: clr = BLUE; break;
      case BLUE: clr = YELLOW; break;   
      // etc
      default: break;
    }
  }
}


In this example if it reads "RED" on the serial it changes "clr" to BLUE. That is if it compiled.

is the use of an enum switch-case pointless in anything but hardcoded use?

tobyb121

Your enum Color is not an array of strings, but will be a list of ints, so once it's gone through the compiler { UNDEF, RED, ORANGE, YELLOW, GREEN,  BLUE, PURPLE } becomes {0,1,2,3,4,5,6}. This obviously can't be compared with a string you are receiving via the serial port.
What you need to do is use strcmp in a series of if statements (http://www.cplusplus.com/reference/cstring/strcmp/).

Go Up