From 2866b79ff4967684ef1d845b199e57736d64c6ce Mon Sep 17 00:00:00 2001 From: T Date: Thu, 20 Mar 2025 22:20:45 +0100 Subject: [PATCH] Ignore Lamp time when power off --- optoma_rs232.cpp | 51 +++++++++++++++++++++++++++++------------------- optoma_rs232.h | 20 +++++++++---------- 2 files changed, 41 insertions(+), 30 deletions(-) diff --git a/optoma_rs232.cpp b/optoma_rs232.cpp index 8aa2f81..3d6255c 100644 --- a/optoma_rs232.cpp +++ b/optoma_rs232.cpp @@ -7,11 +7,18 @@ namespace optoma_rs232 { [[maybe_unused]] static const char *const TAG = "optoma_rs232"; -static const char *QUERIES[] = { +enum QUERIES { + INFO = 0, + TEMP, + LAMP_TIME, + FAN_1, +}; + +constexpr const char *QUERY_DATA[] = { + "~00150 1\r", // Info "~00150 18\r", // Temp "~00108 1\r", // Lamp time "~00351 1\r", // Fan 1 - "~00150 1\r", // Info }; template static void publish(C *c, const M &m) { @@ -62,8 +69,8 @@ void OptomaRS232Component::loop() { } void OptomaRS232Component::update() { - last_query_ = (last_query_ + 1) % std::size(QUERIES); - write_array(reinterpret_cast(QUERIES[last_query_]), strlen(QUERIES[last_query_])); + last_query_ = (last_query_ + 1) % std::size(QUERY_DATA); + write_array(reinterpret_cast(QUERY_DATA[last_query_]), strlen(QUERY_DATA[last_query_])); } void OptomaRS232Component::beamer_input_select_changed(const std::string &state, size_t) { @@ -118,7 +125,7 @@ void OptomaRS232Component::process_line_(const std::string &str) { } if (str_startswith(str, "OK")) { - // ESP_LOGD(TAG, "OK-message: %s", str.c_str()); + ESP_LOGD(TAG, "OK-message: %s", str.c_str()); process_query_response_(str); return; } @@ -129,27 +136,36 @@ void OptomaRS232Component::process_line_(const std::string &str) { void OptomaRS232Component::process_query_response_(const std::string &str) { if (str.length() >= 3) { switch (last_query_) { - case 0: + case QUERIES::TEMP: publish(beamer_temp_sensor_, strtol(str.c_str() + 2, 0, 10)); break; - case 1: - publish(beamer_lamp_time_sensor_, strtol(str.c_str() + 2, 0, 10)); + case QUERIES::LAMP_TIME: + if (str.length() == 7) + publish(beamer_lamp_time_sensor_, strtol(str.c_str() + 2, 0, 10)); break; - case 2: + case QUERIES::FAN_1: publish(beamer_fan1_sensor_, strtol(str.c_str() + 2, 0, 10)); break; - case 3: + case QUERIES::INFO: if (str.length() >= 13) { char buf[17]{}; strncpy(buf, str.c_str(), sizeof(buf)); - publish(beamer_color_mode_sensor_, strtol(buf + 14, 0, 10)); + const auto color_mode = strtol(buf + 14, 0, 10); buf[14] = 0; - - // publish(beamer_firmware_,strtol(buf + 10, 0, 10)); + const auto firmware = strtol(buf + 10, 0, 10); buf[10] = 0; - - int input = strtol(buf + 8, 0, 10); // OK10041700C00900 + const auto input = strtol(buf + 8, 0, 10); buf[8] = 0; + const auto lamp_time = strtol(buf + 3, 0, 10); + buf[3] = 0; + const auto power = strtol(buf + 2, 0, 10); + + publish_power_(power); + // publish(beamer_firmware_, firmware); + if (power) + publish(beamer_lamp_time_sensor_, lamp_time); + publish(beamer_color_mode_sensor_, color_mode); + switch (input) { case Inputs::HDMI_1: current_input_ = Inputs::HDMI_1; @@ -169,11 +185,6 @@ void OptomaRS232Component::process_query_response_(const std::string &str) { publish_input_("Unknown"); break; } - - publish(beamer_lamp_time_sensor_, strtol(buf + 3, 0, 10)); - buf[3] = 0; - - publish_power_(strtol(buf + 2, 0, 10)); break; } default:; diff --git a/optoma_rs232.h b/optoma_rs232.h index 57a2cdc..7cac20b 100644 --- a/optoma_rs232.h +++ b/optoma_rs232.h @@ -3,11 +3,11 @@ #include "esphome/core/component.h" #include "esphome/components/uart/uart.h" -#undef USE_SENSOR -#undef USE_BINARY_SENSOR -#undef USE_TEXT_SENSOR -#undef USE_SELECT -#undef USE_SWITCH +// #undef USE_SENSOR +// #undef USE_BINARY_SENSOR +// #undef USE_TEXT_SENSOR +// #undef USE_SELECT +// #undef USE_SWITCH #ifdef USE_SENSOR #include "esphome/components/sensor/sensor.h" @@ -52,19 +52,19 @@ class OptomaRS232Component : public uart::UARTDevice, public PollingComponent { // clang-format off #ifndef USE_SENSOR -#define SUB_SENSOR(name) protected: DummySensor *name## _sensor_{nullptr}; public: +#define SUB_SENSOR(name) protected: DummySensor *name##_sensor_{nullptr}; public: #endif #ifndef USE_BINARY_SENSOR -#define SUB_BINARY_SENSOR(name) protected: DummySensor *name## _binary_sensor_{nullptr}; public: +#define SUB_BINARY_SENSOR(name) protected: DummySensor *name##_binary_sensor_{nullptr}; public: #endif #ifndef USE_TEXT_SENSOR -#define SUB_TEXT_SENSOR(name) protected: DummySensor *name## _text_sensor_{nullptr}; public: +#define SUB_TEXT_SENSOR(name) protected: DummySensor *name##_text_sensor_{nullptr}; public: #endif #ifndef USE_SELECT -#define SUB_SELECT(name) protected: DummySelect *name## _select_{nullptr}; public: +#define SUB_SELECT(name) protected: DummySelect *name##_select_{nullptr}; public: #endif #ifndef USE_SWITCH -#define SUB_SWITCH(name) protected: DummySwitch *name## _switch_{nullptr}; public: +#define SUB_SWITCH(name) protected: DummySwitch *name##_switch_{nullptr}; public: #endif // clang-format on