From 5c232579eaef9f5d4a28773326218ce597ab7fcb Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 7 Jun 2023 09:20:30 -0500 Subject: [PATCH] get rid of strings --- components/ratgdo/ratgdo.cpp | 92 +++++++++++++++++++++++++++++++----- components/ratgdo/ratgdo.h | 53 +++++++++++++++++---- 2 files changed, 124 insertions(+), 21 deletions(-) diff --git a/components/ratgdo/ratgdo.cpp b/components/ratgdo/ratgdo.cpp index 738dec6..15c982e 100644 --- a/components/ratgdo/ratgdo.cpp +++ b/components/ratgdo/ratgdo.cpp @@ -20,6 +20,76 @@ namespace ratgdo { static const char* const TAG = "ratgdo"; static const int STARTUP_DELAY = 2000; // delay before enabling interrupts + const char* door_state_to_string(DoorState state) + { + switch (state) { + case DOOR_STATE_OPEN: + return "OPEN"; + case DOOR_STATE_CLOSED: + return "CLOSED"; + case DOOR_STATE_STOPPED: + return "STOPPED"; + case DOOR_STATE_OPENING: + return "OPENING"; + case DOOR_STATE_CLOSING: + return "CLOSING"; + case DOOR_STATE_UNKNOWN: + default: + return "UNKNOWN"; + } + } + + const char* light_state_to_string(LightState state) + { + switch (state) { + case LIGHT_STATE_OFF: + return "OFF"; + case LIGHT_STATE_ON: + return "ON"; + case IGHT_STATE_UNKNOWN: + default: + return "UNKNOWN"; + } + } + + const char* lock_state_to_string(LockState state) + { + switch (state) { + case LOCK_STATE_UNLOCKED: + return "UNLOCKED"; + case LOCK_STATE_LOCKED: + return "LOCKED"; + case LOCK_STATE_UNKNOWN: + default: + return "UNKNOWN"; + } + } + + const char* motion_state_to_string(MotionState state) + { + switch (state) { + case MOTION_STATE_CLEAR: + return "CLEAR"; + case MOTION_STATE_DETECTED: + return "DETECTED"; + default: + return "CLEAR"; + } + } + + const char* obstruction_state_to_string(ObstructionState state) + { + switch (state) { + case OBSTRUCTION_STATE_CLEAR: + return "CLEAR"; + case OBSTRUCTION_STATE_DETECTED: + return "DETECTED"; + case OBSTRUCTION_STATE_UNKNOWN: + default: + return "UNKNOWN"; + } + } + /*************************** DRY CONTACT CONTROL OF LIGHT & DOOR * ***************************/ void IRAM_ATTR HOT RATGDOStore::isrDoorOpen(RATGDOStore* arg) @@ -82,10 +152,10 @@ namespace ratgdo { void IRAM_ATTR HOT RATGDOStore::isrObstruction(RATGDOStore* arg) { if (arg->input_obst.digital_read()) { - //ESP_LOGD(TAG, "isrObstruction HIGH"); + // ESP_LOGD(TAG, "isrObstruction HIGH"); arg->lastObstructionHigh = millis(); } else { - //ESP_LOGD(TAG, "isrObstruction LOW"); + // ESP_LOGD(TAG, "isrObstruction LOW"); arg->obstructionLowCount++; } } @@ -482,8 +552,8 @@ namespace ratgdo { void RATGDOComponent::openDoor() { - if (this->doorStates[this->store_.doorState] == "open" || doorStates[this->store_.doorState] == "opening") { - ESP_LOGD(TAG, "The door is already %s", this->doorStates[this->store_.doorState]); + if (this->store_.doorState == DoorState.DOOR_STATE_OPEN || this->store_.doorState == DoorState.DOOR_STATE_OPENING) { + ESP_LOGD(TAG, "The door is already %s", door_state_to_string(this->store_.doorState)); return; } toggleDoor(); @@ -491,8 +561,8 @@ namespace ratgdo { void RATGDOComponent::closeDoor() { - if (this->doorStates[this->store_.doorState] == "closed" || doorStates[this->store_.doorState] == "closing") { - ESP_LOGD(TAG, "The door is already %s", this->doorStates[this->store_.doorState]); + if (this->store_.doorState == DoorState.DOOR_STATE_CLOSED || this->store_.doorState == DoorState.DOOR_STATE_CLOSING) { + ESP_LOGD(TAG, "The door is already %s", door_state_to_string(this->store_.doorState)); return; } toggleDoor(); @@ -500,7 +570,7 @@ namespace ratgdo { void RATGDOComponent::stopDoor() { - if (this->doorStates[this->store_.doorState] == "opening" || doorStates[this->store_.doorState] == "closing") { + if (this->store_.doorState == DoorState.DOOR_STATE_OPENING || this->store_.doorState == DoorState.DOOR_STATE_CLOSING) { toggleDoor(); } else { ESP_LOGD(TAG, "The door is not moving."); @@ -517,7 +587,7 @@ namespace ratgdo { void RATGDOComponent::lightOn() { - if (this->lightStates[this->store_.lightState] == "on") { + if (this->store_.lightState == LightState.LIGHT_STATE_ON) { ESP_LOGD(TAG, "already on"); } else { toggleLight(); @@ -526,7 +596,7 @@ namespace ratgdo { void RATGDOComponent::lightOff() { - if (this->lightStates[this->store_.lightState] == "off") { + if (this->store_.lightState == LightState.LIGHT_STATE_OFF) { ESP_LOGD(TAG, "already off"); } else { toggleLight(); @@ -541,7 +611,7 @@ namespace ratgdo { // Lock functions void RATGDOComponent::lock() { - if (this->lockStates[this->store_.lockState] == "locked") { + if (this->store_.lockState == LockState.LOCK_STATE_LOCKED) { ESP_LOGD(TAG, "already locked"); } else { toggleLock(); @@ -550,7 +620,7 @@ namespace ratgdo { void RATGDOComponent::unlock() { - if (this->lockStates[this->store_.lockState] == "unlocked") { + if (this->store_.lockState == LockState.LOCK_STATE_UNLOCKED) { ESP_LOGD(TAG, "already unlocked"); } else { toggleLock(); diff --git a/components/ratgdo/ratgdo.h b/components/ratgdo/ratgdo.h index ad111d2..78d631f 100644 --- a/components/ratgdo/ratgdo.h +++ b/components/ratgdo/ratgdo.h @@ -27,6 +27,44 @@ extern "C" { namespace esphome { namespace ratgdo { + /// Enum for all states a the door can be in. + enum DoorState : uint8_t { + DOOR_STATE_UNKNOWN = 0, + DOOR_STATE_OPEN = 1, + DOOR_STATE_CLOSED = 2, + DOOR_STATE_STOPPED = 3, + DOOR_STATE_OPENING = 4, + DOOR_STATE_CLOSING = 5 + }; + const char* door_state_to_string(DoorState state); + + /// Enum for all states a the light can be in. + enum LightState : uint8_t { + LIGHT_STATE_OFF = 0, + LIGHT_STATE_ON = 1, + LIGHT_STATE_UNKNOWN = 2, + }; + const char* light_state_to_string(LightState state); + + /// Enum for all states a the lock can be in. + enum LockState : uint8_t { + LOCK_STATE_UNLOCKED = 0, + LOCK_STATE_LOCKED = 1, + LOCK_STATE_UNKNOWN = 2, + }; + + /// Enum for all states a the motion can be in. + enum MotionState : uint8_t { + MOTION_STATE_CLEAR = 0, + MOTION_STATE_DETECTED = 1, + }; + + /// Enum for all states a the obstruction can be in. + enum ObstructionState : uint8_t { + OBSTRUCTION_STATE_OBSTRUCTED = 0, + OBSTRUCTION_STATE_CLEAR = 1, + OBSTRUCTION_STATE_UNKNOWN = 2, + }; struct RATGDOStore { ISRInternalGPIOPin input_obst; @@ -41,11 +79,11 @@ namespace ratgdo { int obstructionLowCount = 0; // count obstruction low pulses long lastObstructionHigh = 0; // count time between high pulses from the obst ISR - uint8_t obstructionState = 2; - uint8_t motionState = 0; - uint8_t lockState = 2; - uint8_t lightState = 2; - uint8_t doorState = 0; + uint8_t obstructionState = ObstructionState.OBSTRUCTION_STATE_UNKNOWN; + uint8_t motionState = MotionState.MOTION_STATE_CLEAR; + uint8_t lockState = LockState.LOCK_STATE_UNKNOWN; + uint8_t lightState = LightState.LIGHT_STATE_UNKNOWN; + uint8_t doorState = DoorState.DOOR_STATE_UNKNOWN; static void IRAM_ATTR isrDoorOpen(RATGDOStore* arg); static void IRAM_ATTR isrDoorClose(RATGDOStore* arg); @@ -63,11 +101,6 @@ namespace ratgdo { uint32_t rollingCodeCounter; uint8_t txRollingCode[CODE_LENGTH]; uint8_t rxRollingCode[CODE_LENGTH]; - String doorStates[6] = { "unknown", "open", "closed", "stopped", "opening", "closing" }; - String lightStates[3] = { "off", "on", "unknown" }; - String lockStates[3] = { "unlocked", "locked", "unknown" }; - String motionStates[2] = { "clear", "detected" }; - String obstructionStates[3] = { "obstructed", "clear", "unknown" }; void set_output_gdo_pin(InternalGPIOPin* pin) { this->output_gdo_pin_ = pin; }; void set_input_gdo_pin(InternalGPIOPin* pin) { this->input_gdo_pin_ = pin; };