From ad1cc457686064bb2619b6a2fd0a8a1d7a7294ed Mon Sep 17 00:00:00 2001 From: sqozz Date: Fri, 20 Sep 2024 14:49:38 +0200 Subject: [PATCH] Make all sensors optional --- tw7100.cpp | 285 +++++++++++++++++++++++++++++++++++------------------ tw7100.h | 4 +- 2 files changed, 191 insertions(+), 98 deletions(-) diff --git a/tw7100.cpp b/tw7100.cpp index 7dc418f..c5f92c4 100644 --- a/tw7100.cpp +++ b/tw7100.cpp @@ -11,13 +11,19 @@ namespace esphome { namespace tw7100 { +bool powered = false; bool waiting_for_answer = false; bool waiting_for_error_report = false; unsigned long waiting_for_answer_since = 0; std::deque cmd_queue; unsigned long last_set_cmd_timestamp; -std::vector pwr_off_query_cmds{ "PWR?", "LAMP?" }; +std::vector pwr_off_query_cmds{ + "PWR?", +#ifdef USE_SENSOR + "LAMP?" +#endif +}; std::vector pwr_on_query_cmds{ #ifdef USE_SENSOR "SIGNAL?", @@ -92,18 +98,18 @@ void tw7100Component::setup() { static const char *const TAG = "setup()"; ESP_LOGV(TAG, "SETUP"); #ifdef USE_SELECT - if (source_select_ != nullptr) source_select_->setup(); - if (luminance_select_ != nullptr) luminance_select_->setup(); - if (aspect_select_ != nullptr) aspect_select_->setup(); - if (cmode_select_ != nullptr) cmode_select_->setup(); - if (gamma_select_ != nullptr) gamma_select_->setup(); - if (imgpreset_select_ != nullptr) imgpreset_select_->setup(); - if (mcfi_select_ != nullptr) mcfi_select_->setup(); - if (ovscan_select_ != nullptr) ovscan_select_->setup(); - if (clrspace_select_ != nullptr) clrspace_select_->setup(); - if (dynrange_select_ != nullptr) dynrange_select_->setup(); - if (msel_select_ != nullptr) msel_select_->setup(); - if (speed_select_ != nullptr) speed_select_->setup(); + if (source_select_) source_select_->setup(); + if (luminance_select_) luminance_select_->setup(); + if (aspect_select_) aspect_select_->setup(); + if (cmode_select_) cmode_select_->setup(); + if (gamma_select_) gamma_select_->setup(); + if (imgpreset_select_) imgpreset_select_->setup(); + if (mcfi_select_) mcfi_select_->setup(); + if (ovscan_select_) ovscan_select_->setup(); + if (clrspace_select_) clrspace_select_->setup(); + if (dynrange_select_) dynrange_select_->setup(); + if (msel_select_) msel_select_->setup(); + if (speed_select_) speed_select_->setup(); #endif }; @@ -113,7 +119,7 @@ void tw7100Component::update() { for (auto &cmd : pwr_off_query_cmds) { cmd_queue.push_back(cmd); } - if ((powered_->state)) { + if (powered) { #ifdef USE_TEXT_SENSOR if (this->serial_->state.length() < 1) { cmd_queue.push_back("SNO?"); @@ -157,7 +163,7 @@ void tw7100Component::loop() { ESP_LOGV(TAG, "buffer content: %s", response.c_str()); std::pair parsed_response = parse_response(response); if (parsed_response.first == "ERR" && parsed_response.second == "ERR") { - if (powered_->state && !waiting_for_error_report) { + if (powered && !waiting_for_error_report) { ESP_LOGW(TAG, "found ERR response, pushing request to fetch full error"); waiting_for_error_report = true; cmd_queue.push_front("ERR?"); @@ -186,7 +192,7 @@ void tw7100Component::dump_config() { void tw7100Component::push_cmd(std::string cmd, std::string param) { static const char *const TAG = "push_cmd()"; - if (powered_->state) { + if (powered) { if (cmd == "PWR" && param == "OFF") { _specialTimeoutMillis = 5000; // time to wait after PWR OFF until projector responds again cmd_queue.clear(); // clear eventual existing query commands to avoid errors @@ -207,51 +213,77 @@ void tw7100Component::push_cmd(std::string cmd, std::string param) { void tw7100Component::update_sensor(std::pair data) { static const char *const TAG = "update_sensor()"; - std::string cmd = data.first; - std::string value_string = data.second; + const std::string cmd = data.first; + const std::string value_string = data.second; if (cmd == "PWR") { ESP_LOGV(TAG, "updating power sensors"); int value = std::stoi(value_string); + powered = (value > 0); #ifdef USE_BINARY_SENSOR - powered_->publish_state(value > 0); + //if (powered_) powered_->publish_state(value > 0); #endif #ifdef USE_SWITCH - power_switch_->publish_state(value > 0); // ack previous cmd or update switch + if (power_switch_) power_switch_->publish_state(value > 0); // ack previous cmd or update switch #endif #ifdef USE_SENSOR - power_status_->publish_state(value); + if (power_status_) power_status_->publish_state(value); +#endif } else if (cmd == "LAMP") { ESP_LOGV(TAG, "updating lamp sensors"); +#ifdef USE_SENSOR int value = std::stoi(value_string); - lamp_hours_->publish_state(value); + if (lamp_hours_) lamp_hours_->publish_state(value); +#endif } else if (cmd == "SIGNAL") { ESP_LOGV(TAG, "updating signal sensors"); +#ifdef USE_BINARY_SENSOR int value = std::stoi(value_string); - has_signal_->publish_state(value > 0); - signal_status_->publish_state(value); + if (has_signal_) has_signal_->publish_state(value > 0); #endif -#ifdef USE_SELECT + if (signal_status_) signal_status_->publish_state(value); } else if (cmd == "LUMINANCE") { ESP_LOGV(TAG, "updating luminance sensors"); +#ifdef USE_SENSOR int value = std::stoi(value_string); - luminance_level_->publish_state(value); - auto call = luminance_select_->make_call(); - call.set_option(luminance_select_->luminance[value_string]); - call.perform(); + if (luminance_level_) luminance_level_->publish_state(value); +#endif +#ifdef USE_SELECT + if (luminance_select_) { + if(tw7100Select::luminance.find(value_string) != tw7100Select::luminance.end()) { + auto call = luminance_select_->make_call(); + call.set_option(tw7100Select::luminance[value_string]); + call.perform(); + } else { + ESP_LOGE(TAG, "Result %s not present in options", value_string); + } + } +#endif } else if (cmd == "SOURCE") { ESP_LOGV(TAG, "updating source select"); - auto call = source_select_->make_call(); - call.set_option(source_select_->sources[value_string]); - call.perform(); +#ifdef USE_SELECT + if (source_select_) { + if(tw7100Select::sources.find(value_string) != tw7100Select::sources.end()) { + auto call = source_select_->make_call(); + call.set_option(tw7100Select::sources[value_string]); + call.perform(); + } else { + ESP_LOGE(TAG, "Result %s not present in options", value_string); + } + } +#endif } else if (cmd == "ASPECT") { ESP_LOGV(TAG, "updating aspect select"); - if(aspect_select_->aspect.find(value_string) != aspect_select_->aspect.end()) { - auto call = aspect_select_->make_call(); - call.set_option(aspect_select_->aspect[value_string]); - call.perform(); - } else { - ESP_LOGE(TAG, "Result %s not present in options", value_string); +#ifdef USE_SELECT + if (aspect_select_) { + if(tw7100Select::aspect.find(value_string) != tw7100Select::aspect.end()) { + auto call = aspect_select_->make_call(); + call.set_option(tw7100Select::aspect[value_string]); + call.perform(); + } else { + ESP_LOGE(TAG, "Result %s not present in options", value_string); + } } +#endif } else if (cmd == "OVSCAN") { ESP_LOGV(TAG, "updating ovscan select"); } else if (cmd == "CMODE") { @@ -270,143 +302,204 @@ void tw7100Component::update_sensor(std::pair data) { ESP_LOGV(TAG, "updating msel select"); } else if (cmd == "SPEED") { ESP_LOGV(TAG, "updating speed select"); -#endif #ifdef USE_TEXT_SENSOR } else if (cmd == "SNO") { - serial_->publish_state(value_string); + if (serial_ != nullptr) serial_->publish_state(value_string); #endif -#ifdef USE_SWITCH } else if (cmd == "BTAUDIO") { ESP_LOGV(TAG, "updating btaudio switch"); +#ifdef USE_SWITCH int value = std::stoi(value_string); - btaudio_switch_->publish_state(value > 0); + if (btaudio_switch_) btaudio_switch_->publish_state(value > 0); +#endif } else if (cmd == "4KENHANCE") { ESP_LOGV(TAG, "updating 4kenhance switch"); +#ifdef USE_SWITCH int value = std::stoi(value_string); - _4kenhance_switch_->publish_state(value > 0); + if (_4kenhance_switch_) _4kenhance_switch_->publish_state(value > 0); +#endif } else if (cmd == "IMGPROC") { ESP_LOGV(TAG, "updating imgproc switch"); +#ifdef USE_SWITCH int value = std::stoi(value_string); - img_processing_switch_->publish_state(value > 1); + if (img_processing_switch_) img_processing_switch_->publish_state(value > 1); +#endif } else if (cmd == "AUDIOOUT") { ESP_LOGV(TAG, "updating audioout switch"); +#ifdef USE_SWITCH int value = std::stoi(value_string); - audioout_switch_->publish_state(value == 11); + if (audioout_switch_) audioout_switch_->publish_state(value == 11); +#endif } else if (cmd == "HREVERSE") { ESP_LOGV(TAG, "updating hflip switch"); - hflip_switch_->publish_state(value_string == "ON"); +#ifdef USE_SWITCH + if (hflip_switch_) hflip_switch_->publish_state(value_string == "ON"); +#endif } else if (cmd == "VREVERSE") { ESP_LOGV(TAG, "updating vflip switch"); - vflip_switch_->publish_state(value_string == "ON"); +#ifdef USE_SWITCH + if (vflip_switch_) vflip_switch_->publish_state(value_string == "ON"); +#endif } else if (cmd == "ILLUM") { ESP_LOGV(TAG, "updating illumination switch"); +#ifdef USE_SWITCH int value = std::stoi(value_string); - illumination_switch_->publish_state(value == 1); + if (illumination_switch_) illumination_switch_->publish_state(value == 1); +#endif } else if (cmd == "STANDBYCONF") { ESP_LOGV(TAG, "updating standbyconf switch"); +#ifdef USE_SWITCH int value = std::stoi(value_string); - standbyconf_switch_->publish_state(value == 1); + if (standbyconf_switch_) standbyconf_switch_->publish_state(value == 1); +#endif } else if (cmd == "AUTOHOME") { ESP_LOGV(TAG, "updating autohome switch"); +#ifdef USE_SWITCH int value = std::stoi(value_string); - autohome_switch_->publish_state(value == 1); + if (autohome_switch_) autohome_switch_->publish_state(value == 1); +#endif } else if (cmd == "WLPWR") { ESP_LOGV(TAG, "updating wlan power switch"); +#ifdef USE_SWITCH int value = std::stoi(value_string); - wlan_power_switch_->publish_state(value == 1); + if (wlan_power_switch_) wlan_power_switch_->publish_state(value == 1); +#endif } else if (cmd == "LOGTO") { ESP_LOGV(TAG, "updating logto switch"); +#ifdef USE_SWITCH int value = std::stoi(value_string); - logto_switch_->publish_state(value == 1); + if (logto_switch_) logto_switch_->publish_state(value == 1); +#endif } else if (cmd == "MUTE") { ESP_LOGV(TAG, "updating mute switch with value %s", value_string.c_str()); - mute_switch_->publish_state(value_string == "ON"); +#ifdef USE_SWITCH + if (mute_switch_) mute_switch_->publish_state(value_string == "ON"); #endif -#ifdef USE_NUMBER } else if (cmd == "VOL") { - int value = std::stoi(value_string); ESP_LOGV(TAG, "updating volume number with value %s", value_string.c_str()); - volume_number_->publish_state(value); +#ifdef USE_NUMBER + int value = std::stoi(value_string); + if (volume_number_) volume_number_->publish_state(value); +#endif } else if (cmd == "VKEYSTONE") { - int value = std::stoi(value_string); ESP_LOGV(TAG, "updating vkeystone number with value %s", value_string.c_str()); - vkeystone_number_->publish_state(value); +#ifdef USE_NUMBER + int value = std::stoi(value_string); + if (vkeystone_number_) vkeystone_number_->publish_state(value); +#endif } else if (cmd == "HKEYSTONE") { - int value = std::stoi(value_string); ESP_LOGV(TAG, "updating hkeystone number with value %s", value_string.c_str()); - hkeystone_number_->publish_state(value); +#ifdef USE_NUMBER + int value = std::stoi(value_string); + if (hkeystone_number_) hkeystone_number_->publish_state(value); +#endif } else if (cmd == "BRIGHT") { - int value = std::stoi(value_string); ESP_LOGV(TAG, "updating brightness number with value %s", value_string.c_str()); - if (brightness_number_ != nullptr) brightness_number_->publish_state(value); +#ifdef USE_NUMBER + int value = std::stoi(value_string); + if (brightness_number_) brightness_number_->publish_state(value); +#endif } else if (cmd == "CONTRAST") { - int value = std::stoi(value_string); ESP_LOGV(TAG, "updating contrast number with value %s", value_string.c_str()); - contrast_number_->publish_state(value); +#ifdef USE_NUMBER + int value = std::stoi(value_string); + if (contrast_number_) contrast_number_->publish_state(value); +#endif } else if (cmd == "DENSITY") { - int value = std::stoi(value_string); ESP_LOGV(TAG, "updating density number with value %s", value_string.c_str()); - density_number_->publish_state(value); +#ifdef USE_NUMBER + int value = std::stoi(value_string); + if (density_number_) density_number_->publish_state(value); +#endif } else if (cmd == "TINT") { - int value = std::stoi(value_string); ESP_LOGV(TAG, "updating tint number with value %s", value_string.c_str()); - tint_number_->publish_state(value); +#ifdef USE_NUMBER + int value = std::stoi(value_string); + if (tint_number_) tint_number_->publish_state(value); +#endif } else if (cmd == "CTEMP") { - int value = std::stoi(value_string); ESP_LOGV(TAG, "updating ctemp number with value %s", value_string.c_str()); - ctemp_number_->publish_state(value); +#ifdef USE_NUMBER + int value = std::stoi(value_string); + if (ctemp_number_) ctemp_number_->publish_state(value); +#endif } else if (cmd == "FCOLOR") { - int value = std::stoi(value_string); ESP_LOGV(TAG, "updating fcolor number with value %s", value_string.c_str()); - fcolor_number_->publish_state(value); +#ifdef USE_NUMBER + int value = std::stoi(value_string); + if (fcolor_number_) fcolor_number_->publish_state(value); +#endif } else if (cmd == "NRS") { - int value = std::stoi(value_string); ESP_LOGV(TAG, "updating nrs number with value %s", value_string.c_str()); - nrs_number_->publish_state(value); +#ifdef USE_NUMBER + int value = std::stoi(value_string); + if (nrs_number_) nrs_number_->publish_state(value); +#endif } else if (cmd == "MPEGNRS") { - int value = std::stoi(value_string); ESP_LOGV(TAG, "updating mpegngs number with value %s", value_string.c_str()); - mpegnrs_number_->publish_state(value); +#ifdef USE_NUMBER + int value = std::stoi(value_string); + if (mpegnrs_number_) mpegnrs_number_->publish_state(value); +#endif } else if (cmd == "OFFSETR") { - int value = std::stoi(value_string); ESP_LOGV(TAG, "updating offsetr number with value %s", value_string.c_str()); - offsetr_number_->publish_state(value); +#ifdef USE_NUMBER + int value = std::stoi(value_string); + if (offsetr_number_) offsetr_number_->publish_state(value); +#endif } else if (cmd == "OFFSETG") { - int value = std::stoi(value_string); ESP_LOGV(TAG, "updating offsetg number with value %s", value_string.c_str()); - offsetg_number_->publish_state(value); +#ifdef USE_NUMBER + int value = std::stoi(value_string); + if (offsetg_number_) offsetg_number_->publish_state(value); +#endif } else if (cmd == "OFFSETB") { - int value = std::stoi(value_string); ESP_LOGV(TAG, "updating offsetb number with value %s", value_string.c_str()); - oggsetb_number_->publish_state(value); +#ifdef USE_NUMBER + int value = std::stoi(value_string); + if (offsetb_number_) offsetb_number_->publish_state(value); +#endif } else if (cmd == "GAINR") { - int value = std::stoi(value_string); ESP_LOGV(TAG, "updating gainr number with value %s", value_string.c_str()); - gainr_number_->publish_state(value); +#ifdef USE_NUMBER + int value = std::stoi(value_string); + if (gainr_number_) gainr_number_->publish_state(value); +#endif } else if (cmd == "GAING") { - int value = std::stoi(value_string); ESP_LOGV(TAG, "updating gaing number with value %s", value_string.c_str()); - gaing_number_->publish_state(value); +#ifdef USE_NUMBER + int value = std::stoi(value_string); + if (gaing_number_) gaing_number_->publish_state(value); +#endif } else if (cmd == "GAINB") { - int value = std::stoi(value_string); ESP_LOGV(TAG, "updating gainb number with value %s", value_string.c_str()); - gainb_number_->publish_state(value); +#ifdef USE_NUMBER + int value = std::stoi(value_string); + if (gainb_number_) gainb_number_->publish_state(value); +#endif } else if (cmd == "SHRF") { - int value = std::stoi(value_string); ESP_LOGV(TAG, "updating shrf number with value %s", value_string.c_str()); - shrf_number_->publish_state(value); +#ifdef USE_NUMBER + int value = std::stoi(value_string); + if (shrf_number_) shrf_number_->publish_state(value); +#endif } else if (cmd == "SHRS") { - int value = std::stoi(value_string); ESP_LOGV(TAG, "updating shrs number with value %s", value_string.c_str()); - shrs_number_->publish_state(value); +#ifdef USE_NUMBER + int value = std::stoi(value_string); + if (shrs_number_) shrs_number_->publish_state(value); +#endif } else if (cmd == "DERANGE") { - int value = std::stoi(value_string); ESP_LOGV(TAG, "updating derange number with value %s", value_string.c_str()); - derange_number_->publish_state(value); - } else if (cmd == "DESTRENGTH") { +#ifdef USE_NUMBER int value = std::stoi(value_string); + if (derange_number_) derange_number_->publish_state(value); +#endif + } else if (cmd == "DESTRENGTH") { ESP_LOGV(TAG, "updating destrength number with value %s", value_string.c_str()); - destrength_number_->publish_state(value); +#ifdef USE_NUMBER + int value = std::stoi(value_string); + if (destrength_number_) destrength_number_->publish_state(value); #endif } else { ESP_LOGW(TAG, "Command %s unknown, skipping updating sensor with value", cmd.c_str()); diff --git a/tw7100.h b/tw7100.h index 75ad373..1b11ff3 100644 --- a/tw7100.h +++ b/tw7100.h @@ -109,7 +109,7 @@ class tw7100Component : public PollingComponent, public uart::UARTDevice { void set_set_mpegnrs(tw7100::tw7100Number *number_) { this->mpegnrs_number_ = number_; } void set_set_offsetr(tw7100::tw7100Number *number_) { this->offsetr_number_ = number_; } void set_set_offsetg(tw7100::tw7100Number *number_) { this->offsetg_number_ = number_; } - void set_set_offsetb(tw7100::tw7100Number *number_) { this->oggsetb_number_ = number_; } + void set_set_offsetb(tw7100::tw7100Number *number_) { this->offsetb_number_ = number_; } void set_set_gainr(tw7100::tw7100Number *number_) { this->gainr_number_ = number_; } void set_set_gaing(tw7100::tw7100Number *number_) { this->gaing_number_ = number_; } void set_set_gainb(tw7100::tw7100Number *number_) { this->gainb_number_ = number_; } @@ -178,7 +178,7 @@ class tw7100Component : public PollingComponent, public uart::UARTDevice { tw7100::tw7100Number *mpegnrs_number_{nullptr}; tw7100::tw7100Number *offsetr_number_{nullptr}; tw7100::tw7100Number *offsetg_number_{nullptr}; - tw7100::tw7100Number *oggsetb_number_{nullptr}; + tw7100::tw7100Number *offsetb_number_{nullptr}; tw7100::tw7100Number *gainr_number_{nullptr}; tw7100::tw7100Number *gaing_number_{nullptr}; tw7100::tw7100Number *gainb_number_{nullptr};