Add sourcelist parser

This commit is contained in:
sqozz 2024-09-15 22:38:38 +02:00
parent 924bad62ed
commit 16ed3a216e

View file

@ -10,6 +10,7 @@ namespace tw7100 {
bool waiting_for_answer = false;
std::deque<std::string> cmd_queue;
std::map<int, std::string> sourcelist;
std::vector<std::string> pwr_off_query_cmds{ "PWR?", "LAMP?" };
std::vector<std::string> pwr_on_query_cmds{
// Projection screen adjustment settings
@ -83,7 +84,7 @@ std::vector<std::string> 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<std::string, std::string> 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 {