Can't upload using ENUM as Argument

Hi!

I'm having trouble using ENUM as a function argument, because doing so causes the uploading freezes and give TimeOut error.

Here is my .h file:

enum componentColor {
 defaultFontColor, defaultFillColor, defaultBorderColor,
 activeFontColor, activeFillColor, activeBorderColor
};

and this is my function:

void setColor(componentColor component, word color){
 switch(component){
 case defaultFontColor:
 _defaultFontColor = color;
 break;
 case defaultFillColor:
 _defaultFillColor = color;
 break;
 case defaultBorderColor:
 _defaultBorderColor = color;
 break;
 case activeFontColor:
 _activeFontColor = color;
 break;
 case activeFillColor:
 _activeFillColor = color;
 break;
 case activeBorderColor:
 _activeBorderColor = color;
 break;
 }
}

however, if I change the argument from

componentColor component

to

byte component

my program can be normally uploaded, but it's not totally right.

Am I doing something wrong? Thanks!

What version of the IDE are you using and what's your hardware environment?

The IDE is trying to help by creating your function prototype for you. Unfortunately it puts it at the top of your code before the enum is defined. Hence the error.

If you will provide your own function prototype, then the IDE won't try to make one for you and your code will compile.

econjack:
What version of the IDE are you using and what's your hardware environment?

My Version is 1.6.1

Delta_G:
The IDE is trying to help by creating your function prototype for you. Unfortunately it puts it at the top of your code before the enum is defined. Hence the error.

If you will provide your own function prototype, then the IDE won't try to make one for you and your code will compile.

So, I have to add the prototype to .h file like this?

void setColor(componentColor c, word color);

What Delta_G is telling you is that you need something like:

enum componentColor {
  defaultFontColor, defaultFillColor, defaultBorderColor,
  activeFontColor, activeFillColor, activeBorderColor
};

void setColor(componentColor component, word color);   // Function prototype declaration

at the top of your file because, in the absence of the explicit function prototype declaration above, the compiler places it before your enum, which means the compiler can figure out what the first argument in setColor() is. Also, within your setColor() function, none of the "underscore" variables (e.g., _defaultFillColor) are defined.

I updated my IDE version to 1.6.3, but nothing changed.

econjack:
What Delta_G is telling you is that you need something like:

enum componentColor {

defaultFontColor, defaultFillColor, defaultBorderColor,
 activeFontColor, activeFillColor, activeBorderColor
};

void setColor(componentColor component, word color);   // Function prototype declaration




at the top of your file because, in the absence of the explicit function prototype declaration above, the compiler places it before your *enum*, which means the compiler can figure out what the first argument in *setColor()* is. Also, within your *setColor()* function, none of the "underscore" variables (e.g., *_defaultFillColor*) are defined.

Thanks for your reply!

I added the prototype like you said, but the problem continue.
About the variables definition problem, I posted here only the code part related to the problem (I will be more alert next time), but these variables are declared like this:

word _defaultFontColor, _defaultFillColor, _defaultBorderColor;
word _activeFontColor, _activeFillColor, _activeBorderColor;

Trying some things, I changed the switch from

void setColor(componentColor component, word color){
	switch(component){
		case defaultFontColor:
			_defaultFontColor = color;
			break;
		case defaultFillColor:
			_defaultFillColor = color;
			break;
		case defaultBorderColor:
			_defaultBorderColor = color;
			break;
		case activeFontColor:
			_activeFontColor = color;
			break;
		case activeFillColor:
			_activeFillColor = color;
			break;
		case activeBorderColor:
			_activeBorderColor = color;
			break;
	}
}

to

void setColor(componentColor component, word color){
	byte TestVar;
	switch(TestVar){
		case defaultFontColor:
			_defaultFontColor = color;
			break;
		case defaultFillColor:
			_defaultFillColor = color;
			break;
		case defaultBorderColor:
			_defaultBorderColor = color;
			break;
		case activeFontColor:
			_activeFontColor = color;
			break;
		case activeFillColor:
			_activeFillColor = color;
			break;
		case activeBorderColor:
			_activeBorderColor = color;
			break;
	}
}

and it uploads normally.
Maybe ENUM can't be used in "switch"?

IF you would post ALL of your code, or at LEAST the actual error messages, someone here can pin-point the real problem in a few seconds. Otherwise, we all have one hand tied behind our backs....

Regards,
Ray L.

I'm sorry about not posting the whole code, because I think that it would be less understandable and posting just the problematic part, it would be more objective.

I'm building a library to draw a single button.

My header file:

#ifndef TFT_Button_h
#define TFT_Button_h

#include "Arduino.h"
#include "UTFT.h"
#include "UTouch.h"
#include <string.h>

enum componentColor {
	defaultFontColor, defaultFillColor, defaultBorderColor,
	activeFontColor, activeFillColor, activeBorderColor
};

class TFT_Button
{
	public:
		TFT_Button(UTFT *ptrUTFT, UTouch *ptrUTouch);
		void 	setup(int Width, int Height, String Label);
		boolean read();
		void 	drawActive(int x=-1, int y=-1);
		void 	drawDefault(int x=-1, int y=-1);
		
		void 	setColor(componentColor component, word color);
		void 	setColor(componentColor component, byte r, byte g, byte b);
		
		void 	setFont(uint8_t* font);
		uint8_t* getFont();
		
	private:
		int 	width, height;
		int 	drawnX, drawnY;
		String label;
		uint8_t* buttonFont;
		
		boolean active;
		
		unsigned long Timer;
		
		word _defaultFontColor, _defaultFillColor, _defaultBorderColor;
		word _activeFontColor, _activeFillColor, _activeBorderColor;
		
	protected:
		UTFT *_UTFT;
		UTouch *_UTouch;
};

#endif

My cpp file WITH JUST the class object (or another name, I don't know what) and the function in question:

#include "Arduino.h"
#include "TFT_Button.h"

TFT_Button::TFT_Button(UTFT *ptrUTFT, UTouch *ptrUTouch)
{
	_UTFT = ptrUTFT;
	_UTouch = ptrUTouch;
}

void TFT_Button::setColor(componentColor component, byte r, byte g, byte b){
	word color = ((r&248)<<8 | (g&252)<<3 | (b&248)>>3);
	setColor(component, color);
}

void TFT_Button::setColor(componentColor component, word color){
	switch(component){
		case defaultFontColor:
			_defaultFontColor = color;
			break;
		case defaultFillColor:
			_defaultFillColor = color;
			break;
		case defaultBorderColor:
			_defaultBorderColor = color;
			break;
		case activeFontColor:
			_activeFontColor = color;
			break;
		case activeFillColor:
			_activeFillColor = color;
			break;
		case activeBorderColor:
			_activeBorderColor = color;
			break;
	}
}

My ino file:

#include <UTFT.h>
#include <UTouch.h>
#include <TFT_Button.h>

extern uint8_t SmallFont[];
extern uint8_t BigFont[];

UTouch        myTouch( 6, 5, 4, 3, 2);
UTFT          myGLCD(TFT01_32,38,39,40,41);
TFT_Button    myButton(&myGLCD,&myTouch);

void setup() {
    myGLCD.InitLCD();
    myGLCD.clrScr();
    myGLCD.setFont(BigFont);
    myGLCD.fillScr(VGA_BLUE);
  
    myGLCD.setColor(VGA_BLACK);
    myGLCD.setBackColor(VGA_WHITE);
  
    myTouch.InitTouch();
    myTouch.setPrecision(PREC_MEDIUM);
    
    myButton.setup(40,40,"Test");
    myButton.setColor(defaultFontColor,0xFFFF);
    myButton.setColor(defaultFillColor,0x0000);
    myButton.setColor(defaultBorderColor,0xFFFF);
    myButton.setColor(activeFontColor,0x0000);
    myButton.setColor(activeFillColor,0xFFFF);
    myButton.setColor(activeBorderColor,0x0000);
    myButton.drawDefault(20,20);
}

void loop() {
   myButton.read()
}

Actually, if I try to upload the program, the Arduino IDE progress bar freezes while uploading (COMPILE OK) and then gives TimeOut error.

I found the solution:

Using directly the ENUM type in the "switch" freezes the upload.

So I cast the ENUM object into a byte (correct if the technical terms are wrong).

Changed from switch(component) to switch(byte(component))

The final "switch" function:

void TFT_Button::setColor(componentColor component, word color){
	switch(byte(component)){
		case defaultFontColor:
			_defaultFontColor = color;
			break;
		case defaultFillColor:
			_defaultFillColor = color;
			break;
		case defaultBorderColor:
			_defaultBorderColor = color;
			break;
		case activeFontColor:
			_activeFontColor = color;
			break;
		case activeFillColor:
			_activeFillColor = color;
			break;
		case activeBorderColor:
			_activeBorderColor = color;
			break;
	}
}

Now it uploads perfectly.

Sorry for the trouble and thanks for the help!

That's not a good solution.... The enum should work, so you have a problem somewhere in your code...

Regards,
Ray L.

I've started writing all my sketches as .h and .cpp files. If I want to use the IDE to upload then I just make a blank .ino file that includes the sketch files. That way everything is in the headers and source files and the IDE doesn't try to "help" me at all. It makes things work the way I expect them to a lot more often and I don't spend any more time chasing these obscure errors that aren't from the code as I wrote it.