passing enums

I can make an enum just fine:

enum PortBDirection
{
  Pin8Output = B00000001,
  Pin9Output = B00000010,
  Pin10Output = B00000100,
  Pin11Output = B00001000,
  Pin12Output = B00010000,
  Pin13Output = B00100000
};

But I can't seem to use it as a type.

inline void SetPortBDirection( const PortBDirection foo )
{
  // bits 6 and 7 are mapped to the crystal pins and are not usable
  char temp = DDRB & B11000000;
  char temp2 = foo & B00111111;
  DDRB = temp | temp2;
}

I seem to recall that in VC I could do this and it gave me the benefit of strict typing. IE I could call SetPortBDirection( Pin8Output ); but I couldn't call SetPortBDirection( 100 );. Maybe I'm mistaken. It's been a while.

But I'm getting all sorts of different errors if I specify const enum PortBDirection foo or whatever.

I CAN however void SetPortBDirection( const char foo ){}

So is that just a difference between VC and GCC? Is there a way to use strong typing on enums?

Sketch:

#include "enums.h"

void setup(){
  
  SetPortBDirection(Pin8Output);
}

void loop(){}

Additional enums.h:

#include "WProgram.h"

typedef enum _PortBDirection
{
  Pin8Output = B00000001,
  Pin9Output = B00000010,
  Pin10Output = B00000100,
  Pin11Output = B00001000,
  Pin12Output = B00010000,
  Pin13Output = B00100000
} PortBDirection;
 
void SetPortBDirection( const PortBDirection foo )
{
  // bits 6 and 7 are mapped to the crystal pins and are not usable
  char temp = DDRB & B11000000;
  char temp2 = foo & B00111111;
  DDRB = temp | temp2;
}

Arduino IDE is not very found of custom types.

You are my hero. I have no idea what folder the .h goes in. I tried the sketch folder and it wasn't found. I tried the applets folder but then remembered that gets deleted. :smiley:

Vaevictus in the irc channel told me arduino/hardware/tools/avr/avr/include, which works. Although it probably doesn't quite belong next to the alloc stuff.

Also of interest: We learned (thanks to help from follower in the irc channel) that it's the pre-preprocessor messing it up. Is there a better name for that? The Arduino IDE mangling to make the .cpp file?

Ohhhhh okay I can open a new tab in the IDE and put the file contents there. :smiley: I get it.

So does the IDE only mangle certain file types? .h is left alone?

Ohhhhh okay I can open a new tab in the IDE and put the file contents there. I get it.
So does the IDE only mangle certain file types? .h is left alone?

I think so, yes :slight_smile: