From 0ee2505d3bd07d816faf50e81de6a2c39a308ab0 Mon Sep 17 00:00:00 2001 From: EverythingSmartHome Date: Tue, 24 Oct 2023 17:06:50 +0100 Subject: [PATCH] Migrate distance sensors from header to yaml --- everything-presence-one-beta.yaml | 83 ++++++++++++++++++++++++++++--- 1 file changed, 76 insertions(+), 7 deletions(-) diff --git a/everything-presence-one-beta.yaml b/everything-presence-one-beta.yaml index a32699d..1d100d9 100644 --- a/everything-presence-one-beta.yaml +++ b/everything-presence-one-beta.yaml @@ -182,13 +182,87 @@ binary_sensor: - platform: template name: "Target 4 Active" # Don't change disabled_by_default: True +globals: + - id: last_target_count + type: int + restore_value: no + initial_value: '0' uart: id: uart_bus tx_pin: GPIO13 rx_pin: GPIO14 baud_rate: 115200 - + debug: + direction: BOTH + dummy_receiver: True + after: + delimiter: "*\r\n" + sequence: + - lambda: |- + UARTDebug::log_string(direction, bytes); + + const int max_targets = 4; + static float t_distance[max_targets] = {0.0}; + static float t_snr[max_targets] = {0.0}; + static bool t_active[max_targets] = {false}; + + std::string line(bytes.begin(), bytes.end()); + + if (line.substr(0, 6) == "$JYRPO") { + size_t pos = 0; + std::vector v; + std::string token; + std::string s = line.substr(7); + + while ((pos = s.find(",")) != std::string::npos) { + token = s.substr(0, pos); + v.push_back(token); + s.erase(0, pos + 1); + } + v.push_back(s); + + int target_count = atoi(v[0].c_str()); + int target_index = atoi(v[1].c_str()) - 1; + float target_distance_val = atof(v[2].c_str()); + float target_snr_val = atof(v[4].c_str()); + + ESP_LOGD("UART", "Target Count: %d, Target Index: %d", target_count, target_index); + + if (id(last_target_count) != target_count) { + // Reset all targets to inactive + for (int i = 0; i < max_targets; i++) { + t_active[i] = false; + t_distance[i] = 0.0; + t_snr[i] = 0.0; + } + id(last_target_count) = target_count; + } + + if (target_index < max_targets) { + t_distance[target_index] = target_distance_val; + t_snr[target_index] = target_snr_val; + t_active[target_index] = true; + } + + if (target_index == target_count - 1) { + id(target_distance_1_m).publish_state(t_distance[0]); + id(target_1_snr).publish_state(t_snr[0]); + id(target_1_active).publish_state(t_active[0]); + + id(target_distance_2_m).publish_state(t_distance[1]); + id(target_2_snr).publish_state(t_snr[1]); + id(target_2_active).publish_state(t_active[1]); + + id(target_distance_3_m).publish_state(t_distance[2]); + id(target_3_snr).publish_state(t_snr[2]); + id(target_3_active).publish_state(t_active[2]); + + id(target_distance_4_m).publish_state(t_distance[3]); + id(target_4_snr).publish_state(t_snr[3]); + id(target_4_active).publish_state(t_active[3]); + } + } switch: - platform: template @@ -573,9 +647,4 @@ button: text_sensor: - platform: template name: "Distance Zones Status" - id: "distance_zone_status" - -custom_component: -- lambda: |- - auto my_custom = new Sen0395Distance(id(uart_bus)); - return {my_custom}; \ No newline at end of file + id: "distance_zone_status" \ No newline at end of file