diff --git a/components/ratgdo/ratgdo_state.h b/components/ratgdo/ratgdo_state.h index 082a7af..5ebc701 100644 --- a/components/ratgdo/ratgdo_state.h +++ b/components/ratgdo/ratgdo_state.h @@ -27,27 +27,13 @@ enum class DoorState : uint8_t { }; inline const char* DoorState_to_string(DoorState e) { - switch (e) { - case DoorState::UNKNOWN: return "UNKNOWN"; - case DoorState::OPEN: return "OPEN"; - case DoorState::CLOSED: return "CLOSED"; - case DoorState::STOPPED: return "STOPPED"; - case DoorState::OPENING: return "OPENING"; - case DoorState::CLOSING: return "CLOSING"; - default: return "UNKNOWN"; - } + static const char* const names[] = {"UNKNOWN", "OPEN", "CLOSED", "STOPPED", "OPENING", "CLOSING"}; + auto i = static_cast(e); + return (i <= static_cast(DoorState::CLOSING)) ? names[i] : "UNKNOWN"; } inline DoorState to_DoorState(uint8_t t, DoorState unknown) { - switch (t) { - case static_cast(DoorState::UNKNOWN): return DoorState::UNKNOWN; - case static_cast(DoorState::OPEN): return DoorState::OPEN; - case static_cast(DoorState::CLOSED): return DoorState::CLOSED; - case static_cast(DoorState::STOPPED): return DoorState::STOPPED; - case static_cast(DoorState::OPENING): return DoorState::OPENING; - case static_cast(DoorState::CLOSING): return DoorState::CLOSING; - default: return unknown; - } + return (t <= static_cast(DoorState::CLOSING)) ? static_cast(t) : unknown; } /// Enum for all states a the light can be in. @@ -58,21 +44,13 @@ enum class LightState : uint8_t { }; inline const char* LightState_to_string(LightState e) { - switch (e) { - case LightState::OFF: return "OFF"; - case LightState::ON: return "ON"; - case LightState::UNKNOWN: return "UNKNOWN"; - default: return "UNKNOWN"; - } + static const char* const names[] = {"OFF", "ON", "UNKNOWN"}; + auto i = static_cast(e); + return (i <= static_cast(LightState::UNKNOWN)) ? names[i] : "UNKNOWN"; } inline LightState to_LightState(uint8_t t, LightState unknown) { - switch (t) { - case static_cast(LightState::OFF): return LightState::OFF; - case static_cast(LightState::ON): return LightState::ON; - case static_cast(LightState::UNKNOWN): return LightState::UNKNOWN; - default: return unknown; - } + return (t <= static_cast(LightState::UNKNOWN)) ? static_cast(t) : unknown; } LightState light_state_toggle(LightState state); @@ -85,21 +63,13 @@ enum class LockState : uint8_t { }; inline const char* LockState_to_string(LockState e) { - switch (e) { - case LockState::UNLOCKED: return "UNLOCKED"; - case LockState::LOCKED: return "LOCKED"; - case LockState::UNKNOWN: return "UNKNOWN"; - default: return "UNKNOWN"; - } + static const char* const names[] = {"UNLOCKED", "LOCKED", "UNKNOWN"}; + auto i = static_cast(e); + return (i <= static_cast(LockState::UNKNOWN)) ? names[i] : "UNKNOWN"; } inline LockState to_LockState(uint8_t t, LockState unknown) { - switch (t) { - case static_cast(LockState::UNLOCKED): return LockState::UNLOCKED; - case static_cast(LockState::LOCKED): return LockState::LOCKED; - case static_cast(LockState::UNKNOWN): return LockState::UNKNOWN; - default: return unknown; - } + return (t <= static_cast(LockState::UNKNOWN)) ? static_cast(t) : unknown; } // actions @@ -111,23 +81,13 @@ enum class LightAction : uint8_t { }; inline const char* LightAction_to_string(LightAction e) { - switch (e) { - case LightAction::OFF: return "OFF"; - case LightAction::ON: return "ON"; - case LightAction::TOGGLE: return "TOGGLE"; - case LightAction::UNKNOWN: return "UNKNOWN"; - default: return "UNKNOWN"; - } + static const char* const names[] = {"OFF", "ON", "TOGGLE", "UNKNOWN"}; + auto i = static_cast(e); + return (i <= static_cast(LightAction::UNKNOWN)) ? names[i] : "UNKNOWN"; } inline LightAction to_LightAction(uint8_t t, LightAction unknown) { - switch (t) { - case static_cast(LightAction::OFF): return LightAction::OFF; - case static_cast(LightAction::ON): return LightAction::ON; - case static_cast(LightAction::TOGGLE): return LightAction::TOGGLE; - case static_cast(LightAction::UNKNOWN): return LightAction::UNKNOWN; - default: return unknown; - } + return (t <= static_cast(LightAction::UNKNOWN)) ? static_cast(t) : unknown; } enum class LockAction : uint8_t { @@ -137,21 +97,13 @@ enum class LockAction : uint8_t { }; inline const char* LockAction_to_string(LockAction e) { - switch (e) { - case LockAction::UNLOCK: return "UNLOCK"; - case LockAction::LOCK: return "LOCK"; - case LockAction::UNKNOWN: return "UNKNOWN"; - default: return "UNKNOWN"; - } + static const char* const names[] = {"UNLOCK", "LOCK", "UNKNOWN", "UNKNOWN"}; + auto i = static_cast(e); + return (i <= static_cast(LockAction::UNKNOWN)) ? names[i] : "UNKNOWN"; } inline LockAction to_LockAction(uint8_t t, LockAction unknown) { - switch (t) { - case static_cast(LockAction::UNLOCK): return LockAction::UNLOCK; - case static_cast(LockAction::LOCK): return LockAction::LOCK; - case static_cast(LockAction::UNKNOWN): return LockAction::UNKNOWN; - default: return unknown; - } + return (t == static_cast(LockAction::UNLOCK) || t == static_cast(LockAction::LOCK) || t == static_cast(LockAction::UNKNOWN)) ? static_cast(t) : unknown; } enum class DoorAction : uint8_t { @@ -163,25 +115,13 @@ enum class DoorAction : uint8_t { }; inline const char* DoorAction_to_string(DoorAction e) { - switch (e) { - case DoorAction::CLOSE: return "CLOSE"; - case DoorAction::OPEN: return "OPEN"; - case DoorAction::TOGGLE: return "TOGGLE"; - case DoorAction::STOP: return "STOP"; - case DoorAction::UNKNOWN: return "UNKNOWN"; - default: return "UNKNOWN"; - } + static const char* const names[] = {"CLOSE", "OPEN", "TOGGLE", "STOP", "UNKNOWN"}; + auto i = static_cast(e); + return (i <= static_cast(DoorAction::UNKNOWN)) ? names[i] : "UNKNOWN"; } inline DoorAction to_DoorAction(uint8_t t, DoorAction unknown) { - switch (t) { - case static_cast(DoorAction::CLOSE): return DoorAction::CLOSE; - case static_cast(DoorAction::OPEN): return DoorAction::OPEN; - case static_cast(DoorAction::TOGGLE): return DoorAction::TOGGLE; - case static_cast(DoorAction::STOP): return DoorAction::STOP; - case static_cast(DoorAction::UNKNOWN): return DoorAction::UNKNOWN; - default: return unknown; - } + return (t <= static_cast(DoorAction::UNKNOWN)) ? static_cast(t) : unknown; } struct Openings {