From 02b6d374efc5da1a84e9f477952add545c780429 Mon Sep 17 00:00:00 2001 From: sqozz Date: Wed, 18 Sep 2024 23:14:13 +0200 Subject: [PATCH] Add all documented switches --- switch/__init__.py | 59 +++++++++++++++++------------------ switch/tw7100_switch.cpp | 42 ++++++++++++++++++------- tw7100.cpp | 67 ++++++++++++++++++++++++++++++++-------- tw7100.h | 24 +++++++++++++- 4 files changed, 136 insertions(+), 56 deletions(-) diff --git a/switch/__init__.py b/switch/__init__.py index dbaea05..7fdf846 100644 --- a/switch/__init__.py +++ b/switch/__init__.py @@ -10,39 +10,36 @@ CODEOWNERS = ["@sqozz"] tw7100Switch = tw7100_ns.class_("tw7100Switch", switch.Switch, cg.Component) -CONFIG_SCHEMA = cv.Schema( - { - cv.GenerateID(): cv.use_id(tw7100), - cv.Optional("PWR"): switch.switch_schema( - tw7100Switch - ), - cv.Optional("BTAUDIO"): switch.switch_schema( - tw7100Switch - ), - cv.Optional("MUTE"): switch.switch_schema( - tw7100Switch - ), - } -) +switches = [ + ("PWR","mdi:power"), + ("BTAUDIO","mdi:speaker-bluetooth"), + ("MUTE","mdi:video-box-off"), + ("WLPWR","mdi:access-point"), + ("AUTOHOME","mdi:cog-clockwise"), +# ("PRODUCT",""), # present in datasheet but tw7100 only responds with "ERR" (no details) + ("STANDBYCONF","mdi:frequently-asked-questions"), + ("ILLUM","mdi:sunglasses"), + ("VREVERSE","mdi:arrow-expand-vertical"), + ("HREVERSE","mdi:arrow-expand-horizontal"), + ("AUDIOOUT","mdi:audio-input-stereo-minijack"), + ("IMGPROC",""), + ("4KENHANCE","mdi:video-4k-box"), + ("LOGTO",""), +] + +schema={cv.GenerateID(): cv.use_id(tw7100)} +for name,icon in switches: + schema.update({cv.Optional(name): switch.switch_schema(tw7100Switch, icon=icon)}) + +CONFIG_SCHEMA = cv.Schema(schema) async def to_code(config): parent = await cg.get_variable(config[CONF_ID]) - if "PWR" in config: - switch_ = await switch.new_switch(config["PWR"]) - cg.add(switch_.set_cmd("PWR")) - cg.add(switch_.set_tw7100_parent(parent)) - cg.add(parent.set_set_power(switch_)) - - if "BTAUDIO" in config: - switch_ = await switch.new_switch(config["BTAUDIO"]) - cg.add(switch_.set_cmd("BTAUDIO")) - cg.add(switch_.set_tw7100_parent(parent)) - cg.add(parent.set_set_btaudio(switch_)) - - if "MUTE" in config: - switch_ = await switch.new_switch(config["MUTE"]) - cg.add(switch_.set_cmd("MUTE")) - cg.add(switch_.set_tw7100_parent(parent)) - cg.add(parent.set_set_mute(switch_)) + for name,icon in switches: + if name in config: + switch_ = await switch.new_switch(config[name]) + cg.add(switch_.set_cmd(name)) + cg.add(switch_.set_tw7100_parent(parent)) + cg.add(getattr(parent, "set_set_{}".format(name.lower()))(switch_)) diff --git a/switch/tw7100_switch.cpp b/switch/tw7100_switch.cpp index 9fdd23e..187b7c7 100644 --- a/switch/tw7100_switch.cpp +++ b/switch/tw7100_switch.cpp @@ -17,19 +17,39 @@ void tw7100Switch::dump_config() { void tw7100Switch::write_state(bool state) { static const char *const TAG = "write_state()"; ESP_LOGV(TAG, "write switch state for cmd: %s, new state: %i", this->cmd_.c_str(), state); - if (cmd_ == "PWR") { - std::string param = state ? "ON" : "OFF"; - ESP_LOGV(TAG, "pushing back cmd %s with param %s", cmd_.c_str(), param.c_str()); - parent_->push_cmd(cmd_, param); + std::string param = ""; + char param_char[3]; + param_char[0] = 0; + + if ((cmd_ == "PWR")||(cmd_ == "MUTE")||(cmd_ == "HREVERSE")||(cmd_ == "VREVERSE")) { + param = state ? "ON" : "OFF"; + } else if (cmd_ == "IMGPROC") { + sprintf(param_char, "%02d", ((uint8_t)state) + 1); + } else if (cmd_ == "AUDIOOUT") { + sprintf(param_char, "%02d", 10 + state); + } else if (cmd_ == "4KENHANCE") { + sprintf(param_char, "%02d", state); } else if (cmd_ == "BTAUDIO") { - char param [3]; - sprintf(param, "%02d", state); - ESP_LOGV(TAG, "pushing back cmd %s with param %s", cmd_.c_str(), param); - parent_->push_cmd(cmd_, param); - } else if (cmd_ == "MUTE") { - std::string param = state ? "ON" : "OFF"; - ESP_LOGV(TAG, "pushing back cmd %s with param %s", cmd_.c_str(), param.c_str()); + sprintf(param_char, "%02d", state); + } else if (cmd_ == "ILLUM") { + sprintf(param_char, "%02d", state); + } else if (cmd_ == "STANDBYCONF") { + sprintf(param_char, "%02d", state); + } else if (cmd_ == "AUTOHOME") { + sprintf(param_char, "%02d", state); + } else if (cmd_ == "WLPWR") { + sprintf(param_char, "%02d", state); + } else if (cmd_ == "LOGTO") { + sprintf(param_char, "%02d", state); + } + + if(strlen(param_char) > 0) { + param = param_char; + } + + if (param.length() > 0) { parent_->push_cmd(cmd_, param); + ESP_LOGV("", "CMD: %s, PARAM: %s", cmd_.c_str(), param.c_str()); } } diff --git a/tw7100.cpp b/tw7100.cpp index 8abfd59..b91d33e 100644 --- a/tw7100.cpp +++ b/tw7100.cpp @@ -52,7 +52,9 @@ std::vector pwr_on_query_cmds{ /* "GAMMA?", "CSEL?", +*/ "4KENHANCE?", +/* "IMGPRESET?", */ "SHRF?", @@ -65,38 +67,39 @@ std::vector pwr_on_query_cmds{ "DYNRANGE?", "HDRPQ?", "HDRHLG?", - "IMGPROC?", */ -// + "IMGPROC?", + // Sound settings "VOL?", -/* "AUDIOOUT?", -*/ -// "MUTE?", + // Environment settings -/* "HREVERSE?", "VREVERSE?", +/* "MSEL?", "SPEED?", +*/ "ILLUM?", "STANDBYCONF?", -*/ -// "PRODUCT?", -// +// "PRODUCT?", // produces ERR + // Home Screen settings -// "AUTOHOME?", // TODO: implement parser + "AUTOHOME?", + // Network settings -// "WLPWR?", // TODO: implement parser + "WLPWR?", + // Bluetooth - "BTAUDIO?", // TODO: implement parser + "BTAUDIO?", + // Information "SIGNAL?", // "SOURCELIST?" // Pushed on demand if sensor value is empty (e.g. after boot-up) // "SOURCELISTA?", // same as SOURCELIST -// "LOGTO?", // TODO: implement parser + "LOGTO?", // produces ERR // "SNO?" // Pushed on demand if sensor value is empty (e.g. after boot-up) }; @@ -248,6 +251,44 @@ void tw7100Component::update_sensor(std::pair data) { ESP_LOGV(TAG, "updating btaudio switch"); int value = std::stoi(value_string); btaudio_switch_->publish_state(value > 0); + } else if (cmd == "4KENHANCE") { + ESP_LOGV(TAG, "updating 4kenhance switch"); + int value = std::stoi(value_string); + _4kenhance_switch_->publish_state(value > 0); + } else if (cmd == "IMGPROC") { + ESP_LOGV(TAG, "updating imgproc switch"); + int value = std::stoi(value_string); + img_processing_switch_->publish_state(value > 1); + } else if (cmd == "AUDIOOUT") { + ESP_LOGV(TAG, "updating audioout switch"); + int value = std::stoi(value_string); + audioout_switch_->publish_state(value == 11); + } else if (cmd == "HREVERSE") { + ESP_LOGV(TAG, "updating hflip switch"); + hflip_switch_->publish_state(value_string == "ON"); + } else if (cmd == "VREVERSE") { + ESP_LOGV(TAG, "updating vflip switch"); + vflip_switch_->publish_state(value_string == "ON"); + } else if (cmd == "ILLUM") { + ESP_LOGV(TAG, "updating illumination switch"); + int value = std::stoi(value_string); + illumination_switch_->publish_state(value == 1); + } else if (cmd == "STANDBYCONF") { + ESP_LOGV(TAG, "updating standbyconf switch"); + int value = std::stoi(value_string); + standbyconf_switch_->publish_state(value == 1); + } else if (cmd == "AUTOHOME") { + ESP_LOGV(TAG, "updating autohome switch"); + int value = std::stoi(value_string); + autohome_switch_->publish_state(value == 1); + } else if (cmd == "WLPWR") { + ESP_LOGV(TAG, "updating wlan power switch"); + int value = std::stoi(value_string); + wlan_power_switch_->publish_state(value == 1); + } else if (cmd == "LOGTO") { + ESP_LOGV(TAG, "updating logto switch"); + int value = std::stoi(value_string); + logto_switch_->publish_state(value == 1); } else if (cmd == "SOURCE") { ESP_LOGV(TAG, "updating source select"); auto call = source_select_->make_call(); diff --git a/tw7100.h b/tw7100.h index 9c5c201..e4eaae3 100644 --- a/tw7100.h +++ b/tw7100.h @@ -41,9 +41,20 @@ class tw7100Component : public PollingComponent, public uart::UARTDevice { void set_signal_status(sensor::Sensor *signal_status) { this->signal_status_ = signal_status; } void set_luminance_level(sensor::Sensor *luminance_level) { this->luminance_level_ = luminance_level; } void set_serial(text_sensor::TextSensor *serial) { this->serial_ = serial; } - void set_set_power(switch_::Switch *switch_) { this->power_switch_ = switch_; } + void set_set_pwr(switch_::Switch *switch_) { this->power_switch_ = switch_; } void set_set_btaudio(switch_::Switch *switch_) { this->btaudio_switch_ = switch_; } void set_set_mute(switch_::Switch *switch_) { this->mute_switch_ = switch_; } + void set_set_wlpwr(switch_::Switch *switch_) { this->wlan_power_switch_ = switch_; } + void set_set_autohome(switch_::Switch *switch_) { this->autohome_switch_ = switch_; } + void set_set_product(switch_::Switch *switch_) { this->product_switch_ = switch_; } + void set_set_standbyconf(switch_::Switch *switch_) { this->standbyconf_switch_ = switch_; } + void set_set_illum(switch_::Switch *switch_) { this->illumination_switch_ = switch_; } + void set_set_vreverse(switch_::Switch *switch_) { this->vflip_switch_ = switch_; } + void set_set_hreverse(switch_::Switch *switch_) { this->hflip_switch_ = switch_; } + void set_set_audioout(switch_::Switch *switch_) { this->audioout_switch_ = switch_; } + void set_set_imgproc(switch_::Switch *switch_) { this->img_processing_switch_ = switch_; } + void set_set_4kenhance(switch_::Switch *switch_) { this->_4kenhance_switch_ = switch_; } + void set_set_logto(switch_::Switch *switch_) { this->logto_switch_ = switch_; } void set_set_source(tw7100::tw7100Select *source_) { this->source_select_ = source_; } void set_set_luminance(tw7100::tw7100Select *luminance_) { this->luminance_select_ = luminance_; } void set_set_vol(tw7100::tw7100Number *number_) { this->volume_number_ = number_; } @@ -80,6 +91,17 @@ class tw7100Component : public PollingComponent, public uart::UARTDevice { switch_::Switch *power_switch_{nullptr}; switch_::Switch *btaudio_switch_{nullptr}; switch_::Switch *mute_switch_{nullptr}; + switch_::Switch *wlan_power_switch_{nullptr}; + switch_::Switch *autohome_switch_{nullptr}; + switch_::Switch *product_switch_{nullptr}; + switch_::Switch *standbyconf_switch_{nullptr}; + switch_::Switch *illumination_switch_{nullptr}; + switch_::Switch *vflip_switch_{nullptr}; + switch_::Switch *hflip_switch_{nullptr}; + switch_::Switch *audioout_switch_{nullptr}; + switch_::Switch *img_processing_switch_{nullptr}; + switch_::Switch *_4kenhance_switch_{nullptr}; + switch_::Switch *logto_switch_{nullptr}; tw7100::tw7100Select *source_select_{nullptr}; tw7100::tw7100Select *luminance_select_{nullptr}; tw7100::tw7100Number *volume_number_{nullptr};