From 656e71ebd014c6e6d09ee57f6d22b075fdf823bd Mon Sep 17 00:00:00 2001 From: Florian Date: Thu, 19 Sep 2024 20:34:08 +0200 Subject: [PATCH] Revert changes --- components/tc_bus/tc_bus.cpp | 101 +++++++++++++++++++---------------- components/tc_bus/tc_bus.h | 1 - 2 files changed, 55 insertions(+), 47 deletions(-) diff --git a/components/tc_bus/tc_bus.cpp b/components/tc_bus/tc_bus.cpp index ecd4277b..13986440 100644 --- a/components/tc_bus/tc_bus.cpp +++ b/components/tc_bus/tc_bus.cpp @@ -168,7 +168,6 @@ namespace esphome volatile uint32_t TCBusComponentStore::s_cmd = 0; volatile uint8_t TCBusComponentStore::s_cmdLength = 0; volatile bool TCBusComponentStore::s_cmdReady = false; - volatile uint8_t TCBusComponentStore::s_address = 0; void bitSetIDF(uint32_t *variable, int bitPosition) { *variable |= (1UL << bitPosition); @@ -180,122 +179,132 @@ namespace esphome void IRAM_ATTR HOT TCBusComponentStore::gpio_intr(TCBusComponentStore *arg) { - // Erweiterung für das Q-Protokoll der TCS Anlage - static uint64_t curCMD; // Längere Befehle möglich + // Made by https://github.com/atc1441/TCSintercomArduino + static uint32_t curCMD; static uint32_t usLast; - - static uint16_t curCRC; // Erweiterung auf 16-Bit CRC - static uint16_t calCRC; // Berechnete CRC für Validierung + + static uint8_t curCRC; + static uint8_t calCRC; static uint8_t curLength; static uint8_t cmdIntReady; static uint8_t curPos; - static uint8_t address; // Geräteadresse für Q-Protokoll - static uint8_t payloadLength; // Länge der Nutzdaten im Protokoll uint32_t usNow = micros(); uint32_t timeInUS = usNow - usLast; usLast = usNow; - uint8_t curBit = 4; // Standardwert für ungültige Bit-Erkennung + uint8_t curBit = 4; - // Zeitbasierte Dekodierung von Bits, angepasst an das Q-Protokoll if (timeInUS >= 1000 && timeInUS <= 2999) { - curBit = 0; // Kurzsignal - interpretiert als 0 + curBit = 0; } else if (timeInUS >= 3000 && timeInUS <= 4999) { - curBit = 1; // Langsignal - interpretiert als 1 + curBit = 1; } else if (timeInUS >= 5000 && timeInUS <= 6999) { - curBit = 2; // Startbit oder Kontrollbit + curBit = 2; } else if (timeInUS >= 7000 && timeInUS <= 24000) { - curBit = 3; // Reset/Sync-Bit, Position wird zurückgesetzt + curBit = 3; curPos = 0; } - // Start der Nachrichtenverarbeitung if (curPos == 0) { - if (curBit == 2) // Start der Nachricht + if (curBit == 2) { curPos++; } - curCMD = 0; // Reset des aktuellen Befehls - curCRC = 0; // Reset des CRC - calCRC = 0xFFFF; // Initialwert für CRC-16 + curCMD = 0; + curCRC = 0; + calCRC = 1; curLength = 0; - payloadLength = 0; // Länge der Nutzdaten initialisieren - address = 0; // Reset der Adresse } else if (curBit == 0 || curBit == 1) { - // Zuerst die Adressbits auslesen - if (curPos >= 1 && curPos <= 8) // 8-Bit Adresse - { - if (curBit) - { - bitSet(address, 8 - curPos); - } - curPos++; - } - // Danach die Nutzdatenlänge extrahieren - else if (curPos == 9) // Nutzdatenlänge (1 Bit) + if (curPos == 1) { curLength = curBit; curPos++; } - // Jetzt die Payload verarbeiten (abhängig von curLength) - else if (curPos >= 10 && curPos <= (curLength ? 33 : 17)) + else if (curPos >= 2 && curPos <= 17) { if (curBit) { + #if defined(USE_ESP_IDF) + bitSetIDF(&curCMD, (curLength ? 33 : 17) - curPos); + #else bitSet(curCMD, (curLength ? 33 : 17) - curPos); + #endif } - calCRC ^= curBit; // CRC-Berechnung + + calCRC ^= curBit; curPos++; } - // Verarbeitung des CRC (für den längeren CRC-16) - else if (curPos > (curLength ? 33 : 17) && curPos <= (curLength ? 49 : 33)) + else if (curPos == 18) + { + if (curLength) + { + if (curBit) + { + #if defined(USE_ESP_IDF) + bitSetIDF(&curCMD, 33 - curPos); + #else + bitSet(curCMD, 33 - curPos); + #endif + } + + calCRC ^= curBit; + curPos++; + } + else + { + curCRC = curBit; + cmdIntReady = 1; + } + } + else if (curPos >= 19 && curPos <= 33) { if (curBit) { - bitSet(curCRC, (curLength ? 49 : 33) - curPos); + #if defined(USE_ESP_IDF) + bitSetIDF(&curCMD, 33 - curPos); + #else + bitSet(curCMD, 33 - curPos); + #endif } - calCRC ^= curBit; // Berechnung der Prüfsumme + + calCRC ^= curBit; curPos++; } - else if (curPos == (curLength ? 50 : 34)) // Endbit, prüfen ob das Kommando vollständig ist + else if (curPos == 34) { + curCRC = curBit; cmdIntReady = 1; } } else { - curPos = 0; // Fehlerhafte Bitposition, zurücksetzen + curPos = 0; } - // Wenn das Kommando bereit ist, es verarbeiten if (cmdIntReady) { cmdIntReady = 0; - // CRC-Prüfung, ob die Nachricht korrekt ist if (curCRC == calCRC) { arg->s_cmdReady = true; arg->s_cmd = curCMD; - arg->s_address = address; // Adresse ebenfalls speichern } - // Reset der Variablen nach der Verarbeitung curCMD = 0; curPos = 0; - address = 0; } } diff --git a/components/tc_bus/tc_bus.h b/components/tc_bus/tc_bus.h index 35d71df5..f1fed749 100644 --- a/components/tc_bus/tc_bus.h +++ b/components/tc_bus/tc_bus.h @@ -53,7 +53,6 @@ namespace esphome static volatile uint32_t s_cmd; static volatile uint8_t s_cmdLength; static volatile bool s_cmdReady; - static volatile uint8_t s_address; // Adressfeld für Q-Protokoll ISRInternalGPIOPin rx_pin; };