From ab270ac08b98651879f7801811a7fa4b0dbd6873 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 7 Jun 2023 10:06:51 -0500 Subject: [PATCH] get rid of strings --- components/ratgdo/ratgdo.cpp | 70 -------------------------- components/ratgdo/ratgdo_child.h | 3 +- components/ratgdo/ratgdo_state.cpp | 79 ++++++++++++++++++++++++++++++ components/ratgdo/ratgdo_state.h | 71 +++++++++++++++++++++++++++ 4 files changed, 152 insertions(+), 71 deletions(-) create mode 100644 components/ratgdo/ratgdo_state.cpp create mode 100644 components/ratgdo/ratgdo_state.h diff --git a/components/ratgdo/ratgdo.cpp b/components/ratgdo/ratgdo.cpp index 033ed96..018b8db 100644 --- a/components/ratgdo/ratgdo.cpp +++ b/components/ratgdo/ratgdo.cpp @@ -22,76 +22,6 @@ 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 LIGHT_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_OBSTRUCTED: - return "OBSTRUCTED"; - case OBSTRUCTION_STATE_UNKNOWN: - default: - return "UNKNOWN"; - } - } - /*************************** DRY CONTACT CONTROL OF LIGHT & DOOR * ***************************/ void IRAM_ATTR HOT RATGDOStore::isrDoorOpen(RATGDOStore* arg) diff --git a/components/ratgdo/ratgdo_child.h b/components/ratgdo/ratgdo_child.h index cc13d7a..b58121c 100644 --- a/components/ratgdo/ratgdo_child.h +++ b/components/ratgdo/ratgdo_child.h @@ -3,13 +3,14 @@ #include "esphome/core/helpers.h" #include "ratgdo.h" +#include "ratgdo_state.h" + namespace esphome { namespace ratgdo { // Forward declare RATGDOComponent class RATGDOComponent; - class RATGDOClient : public Parented { public: virtual void on_door_state(esphome::ratgdo::DoorState state) = 0; diff --git a/components/ratgdo/ratgdo_state.cpp b/components/ratgdo/ratgdo_state.cpp new file mode 100644 index 0000000..500d5b0 --- /dev/null +++ b/components/ratgdo/ratgdo_state.cpp @@ -0,0 +1,79 @@ +#include "ratgdo_state.h" + +#include "esphome/core/log.h" + +namespace esphome { +namespace ratgdo { + + 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 LIGHT_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_OBSTRUCTED: + return "OBSTRUCTED"; + case OBSTRUCTION_STATE_UNKNOWN: + default: + return "UNKNOWN"; + } + } + +} // namespace ratgdo +} // namespace esphome diff --git a/components/ratgdo/ratgdo_state.h b/components/ratgdo/ratgdo_state.h new file mode 100644 index 0000000..5f68a7a --- /dev/null +++ b/components/ratgdo/ratgdo_state.h @@ -0,0 +1,71 @@ +/************************************ + * Rage + * Against + * The + * Garage + * Door + * Opener + * + * Copyright (C) 2022 Paul Wieland + * + * GNU GENERAL PUBLIC LICENSE + ************************************/ + +#pragma once +#include "esphome/components/uart/uart.h" +#include "esphome/core/component.h" +#include "esphome/core/gpio.h" +#include "esphome/core/log.h" +#include "esphome/core/preferences.h" + +extern "C" { +#include "secplus.h" +} + +#include "ratgdo_child.h" +#define CODE_LENGTH 19 + +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, + }; + +} // namespace ratgdo +} // namespace esphome \ No newline at end of file