diff --git a/tw7100.cpp b/tw7100.cpp index 7066c0f..cdfca72 100644 --- a/tw7100.cpp +++ b/tw7100.cpp @@ -10,6 +10,7 @@ namespace tw7100 { bool waiting_for_answer = false; std::deque cmd_queue; +std::map sourcelist; std::vector pwr_off_query_cmds{ "PWR?", "LAMP?" }; std::vector pwr_on_query_cmds{ // Projection screen adjustment settings @@ -83,7 +84,7 @@ std::vector pwr_on_query_cmds{ // "BTAUDIO?", // TODO: implement parser // Information "SIGNAL?", - "SOURCELIST?" +// "SOURCELIST?" // Pushed on demand if sensor value is empty (e.g. after boot-up) // "SOURCELISTA?", // same as SOURCELIST // "LOGTO?", // TODO: implement parser // "SNO?" // Pushed on demand if sensor value is empty (e.g. after boot-up) @@ -104,6 +105,9 @@ void tw7100Component::update() { if (this->serial_->state.length() < 1) { cmd_queue.push_back("SNO?"); } + if (sourcelist.empty()) { + cmd_queue.push_back("SOURCELIST?"); + } for (auto &cmd : pwr_on_query_cmds) { cmd_queue.push_back(cmd); } @@ -178,9 +182,28 @@ void tw7100Component::update_sensor(std::pair data) { int value = std::stoi(value_string); luminance_level_->publish_state(value); } else if (cmd == "SOURCELIST") { - //for(char& c : value_string) { - // ESP_LOGV(TAG, "%c (%02x)", c, c); - //} + std::string unparsed_result = value_string; + int first_token_end; + int second_token_end; + + uint8_t parse_rounds = 0; // safety fallback if data contains unexpected content + do { + first_token_end = unparsed_result.find(" "); + second_token_end = unparsed_result.find(" ", first_token_end+1); + ESP_LOGV(TAG, "first_token_end: %i, second_token_end: %i, unparsed_result.length(): %i", first_token_end, second_token_end, unparsed_result.length() ); + + std::string key_string = unparsed_result.substr(0, first_token_end); + std::string value = unparsed_result.substr(first_token_end+1, second_token_end-first_token_end-1); + int key = std::stoi(key_string, nullptr, 16); + sourcelist[key] = value; + ESP_LOGV(TAG, "Added %s with key %i(0x%02x) to sourcelist", value.c_str(), key, key); + + unparsed_result.erase(0, second_token_end+1); + ESP_LOGV(TAG, "rest string: %s", unparsed_result.c_str()); + + parse_rounds+=1; + } while (second_token_end > 0 && parse_rounds <= 50); + // TODO: if we exceed parse_rounds we most likely encountered a critical issue, inform user somehow } else if (cmd == "SNO") { serial_->publish_state(value_string); } else {