void myFaderOne_callback_StateChanged(byte bState) { Serial.println(F("myFaderOne State:"));
/ does not work. error: 'myFaderOne' cannot appear in a constant-expression */
switch (bState) {
case myFaderOne.state_Standby : { Serial.println(F("standby"));
} break;
case myFaderOne.state_Fading : { Serial.println(F("fading."));
} break;
case myFaderOne.state_Finished : { Serial.println(F("fading finished!"));
} break;
default: { Serial.println(F("error. not a know state."));
}
} //end switch
/ */
}
the compiler don't like this because of
test_callBackFunc:426: error: 'myFaderOne' cannot appear in a constant-expression
test_callBackFunc:426: error: `.' cannot appear in a constant-expression
i have defined the states in my class as public :
static const byte state_Standby = 1;
static const byte state_Fading = 2;
static const byte state_Finished = 42;
if i check with an if
if ( (*bState) == myFaderOne.state_Standby ) { Serial.println(F("standby"));
}
it just works.
i see the problem is that the compiler needs a const as switch statement.
and he dont get the linke to the consts in the class.
so is there a solution to do this with a switch statement? / to tell the compiler that 'myFaderOne.state_Fading' is a 'const' ??
i know that i can use if..
its just out of interest..
Can you show the actual declaration and definition of your class, please? Preferably, post the complete sketch demonstrating the problem and any other non-standard files needed to build it.
Hi PaulS,
Hi PeterH,
ok sorry.
i hope this time i have all relevant information posted.
in the the Library header file (FaderLin.h) i declare the constants -
/** FaderLin Library **/
//Check if this class is allready there or if it must be created...
#ifndef FaderLin_h
#define FaderLin_h
/** Includes Core Arduino functionality **/
#if ARDUINO < 100
#include <WProgram.h>
#else
#include <Arduino.h>
#endif
/** Class Definition: **/
class FaderLin
{
public:
// public definitions:
static const byte state_NotValid = 0;
static const byte state_Standby = 1;
static const byte state_Fading = 2;
static const byte state_Finished = 42;
//call back function definition
typedef void (* tCbfuncStateChanged) (byte *bState);
typedef void (* tCbfuncValuesChanged) (word *wValues, byte bCount);
// public methods
//Constructor
//FaderLin(byte cbChannelCount_Temp);
FaderLin(byte cbChannelCount_Temp, tCbfuncValuesChanged cbfuncValuesChanged_Temp, tCbfuncStateChanged cbfuncStateChanged_Temp);
//Destructor
~FaderLin();
// initialize class
void begin();
// check if class is ready to operate.
boolean isReady();
// public methods
/** update **/
/**
* update()
* returns state
* main function of library. call as often as possible ;-)
**/
byte update();
/** getState **/
byte getState();
/** fader things **/
void startFadeTo(unsigned long ulFadeDuration_New, uint16_t *waValues_NewTarget);
//void startFadeTo(unsigned long ulFadeDuration_New, uint16_t wValues_NewTarget);
void stopFade();
uint16_t *getCurrentValues();
//uint16_t getCurrentValues();
const byte getChannelCount();
private:
// per object data
// flag to check if the begin function is already called and the class is ready to work.
boolean bReady;
//call back functions:
const tCbfuncValuesChanged cbfuncValuesChanged;
const tCbfuncStateChanged cbfuncStateChanged;
//internal variables
unsigned long ulTimeStamp_FadeStart;
unsigned long ulFadeDuration;
boolean flagFadingFinished;
boolean Active;
// channels:
const byte cbChannelCount;
//http://forum.arduino.cc/index.php?topic=57433.msg412702#msg412702
uint16_t *waValues_Source;
uint16_t *waValues_Target;
uint16_t *waValues_Current;
uint16_t *waValues_Dif;
boolean *baValues_DifIsNegativ;
// private methods
void printArray(uint16_t *array);
// word multiMap(word val, word* _in, word* _out, uint8_t size);
};
#endif //ifndef FaderLin_h
/** the end **/
in my testsketch (test_callBackFunc.ino) i have included the Library and...
/** libraries **/
#include "FaderLin.h"
//FaderLin myFaderOne(2);
FaderLin myFaderOne(2, myFaderOne_callback_OutputChanged, myFaderOne_callback_StateChanged);
//....
void myFaderOne_callback_StateChanged(byte *bState) {
Serial.println(F("myFaderOne State:"));
/* does not work. error: 'myFaderOne' cannot appear in a constant-expression */
switch (*bState) {
case myFaderOne.state_Standby : {
Serial.println(F("standby"));
} break;
case myFaderOne.state_Fading : {
Serial.println(F("fading."));
} break;
case myFaderOne.state_Finished : {
Serial.println(F("fading finished!"));
} break;
default: {
Serial.println(F("error. not a know state."));
}
} //end switch
/* */
/* this way it would work...
if ( (*bState) == myFaderOne.state_Standby ) {
Serial.println(F("standby"));
}
/* */
}
//....
void setup() {
//...
Serial.println(F("FaderLin:"));
Serial.println(F("\t myFaderOne.begin();"));
myFaderOne.begin();
Serial.println(F("\t finished."));
//...
}
void loop(){
//...
myFaderOne.update();
//...
}
... and 'made' a object out of my class.
(pleas correct the wrong technical terms - at the moment i cant remember the right ones..)
so with myFaderOne.state_Standby i have the value of the constant definition?!
but at this point i dont know how exactly this works..
i think the compiler cant make a fix (constant) thing out of my 'myFaderOne.state_Standby'-
so i have to tell the compiler that this hole thing ('myFaderOne.state_Standby') is a constant?!
but how?
thanks PaulS.
that fixed it.
i think i understand - the difference -
FaderLin::state_Standby
so you just use the class definitions
myFaderOne.state_Standby
so you use the instance to get the member.
--> what is returned the second way?
means this i have to use the first one every time i have defined static members in my classes for a cleaner style?
i have tried to minimize code so its easier to read and understand -
if you want to look at the full code - i have added the test sketch and the raw lib files-
( the lib files go into 'arduino_sketchbook\libraries\FaderLin' )
( the test sketch go into 'arduino_sketchbook\test_callBackFunc' )
The only time it matters (assuming that myFaderOne is in scope) is when you need a constant integer expression, i.e., one that is known at compile time, like the switch/case syntax. The designers of C could have relaxed that requirement and quietly created an if/else chain if the case statements are not constant, but they wanted to be able to create jump tables.