Hello Community!
Question about Enum - I decoding my HomeAlarm using a NodeMCU and want to sent the status to my Homey for home automation
It's required to send HomeAlarmState defined as Enum with the structure below, which I have defined globally before void setup()
enum HomeAlarmState {
armed ,
disarmed,
partially_armed
};
But now I need your help
- How to set the values on each individual parameter. I have tried with e.g. armed = 1; but I got a compile error
DSCPanel_NodeMCU_Homey:327: error: lvalue required as left operand of assignment
disarmed = 1;
-
How do I print the Enum?
have tried with Serial.println(HomeAlarmState) without success.
-
How to set the Enum HomeAlarmState to the Homey.setCapabilityValue("homealarm_state", HomeAlarmState); function. I get a compile errror as
DSCPanel_NodeMCU_Homey:366: error: expected primary-expression before ')' token
Homey.setCapabilityValue("homealarm_state", HomeAlarmState);
An example
enum
{
armed ,
disarmed,
partially_armed
} currentState;
void setup()
{
Serial.begin(115200);
currentState = armed;
Serial.println(currentState);
currentState = disarmed;
Serial.println(currentState);
currentState = partially_armed;
Serial.println(currentState);
}
void loop()
{
}
How do I print the complete Enum?
You can't. By definition an enum only has a singe value at any time.
How to set the Enum HomeAlarmState to
See the example above which sets the current state of the enum.
Magnus_P:
enum HomeAlarmState {
armed ,
disarmed,
partially_armed
};
- How do I print the Enum?
have tried with Serial.println(HomeAlarmState) without success.
If you have some (PROG) memory to spare a function can print the values in human readable form
void printHomeAlarmState(HomeAlarmState state) // or printHAS(..)
{
switch(state)
{
case armed:
Serial.print(F("fully armed"));
break;
case disarmed:
Serial.printF(("not armed"));
break;
case partially_armed:
Serial.print(F("partial armed"));
break;
default:
Serial.print(F("unknown state"));
break;
}
}
The latter is needed if you extend your enum and forget to update the printHAS() code....
An enum is not a variable but a type. So you need to create a variable of that type.
UKHeliBob did it like below
enum
{
armed ,
disarmed,
partially_armed
} currentState;
I use
enum ALARMSTATE
{
armed ,
disarmed,
partially_armed
};
ALARMSTATE currentState = disarmed; // just as an example