Skip to content

Commit

Permalink
Protocol Test
Browse files Browse the repository at this point in the history
  • Loading branch information
AzonInc committed Sep 19, 2024
1 parent 6f40a8c commit 2bde418
Showing 1 changed file with 46 additions and 56 deletions.
102 changes: 46 additions & 56 deletions components/tc_bus/tc_bus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,134 +177,124 @@ namespace esphome
return (variable >> bitPosition) & 0x01;
}

void IRAM_ATTR HOT TCBusComponentStore::gpio_intr(TCBusComponentStore *arg)
void IRAM_ATTR HOT TCSComponentStore::gpio_intr(TCSComponentStore *arg)
{
// Made by https://github.com/atc1441/TCSintercomArduino
static uint32_t curCMD;
// Erweiterung für das Q-Protokoll der TCS Anlage
static uint64_t curCMD; // Längere Befehle möglich
static uint32_t usLast;

static uint8_t curCRC;
static uint8_t calCRC;
static uint16_t curCRC; // Erweiterung auf 16-Bit CRC
static uint16_t calCRC; // Berechnete CRC für Validierung
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;
uint8_t curBit = 4; // Standardwert für ungültige Bit-Erkennung

// Zeitbasierte Dekodierung von Bits, angepasst an das Q-Protokoll
if (timeInUS >= 1000 && timeInUS <= 2999)
{
curBit = 0;
curBit = 0; // Kurzsignal - interpretiert als 0
}
else if (timeInUS >= 3000 && timeInUS <= 4999)
{
curBit = 1;
curBit = 1; // Langsignal - interpretiert als 1
}
else if (timeInUS >= 5000 && timeInUS <= 6999)
{
curBit = 2;
curBit = 2; // Startbit oder Kontrollbit
}
else if (timeInUS >= 7000 && timeInUS <= 24000)
{
curBit = 3;
curBit = 3; // Reset/Sync-Bit, Position wird zurückgesetzt
curPos = 0;
}

// Start der Nachrichtenverarbeitung
if (curPos == 0)
{
if (curBit == 2)
if (curBit == 2) // Start der Nachricht
{
curPos++;
}

curCMD = 0;
curCRC = 0;
calCRC = 1;
curCMD = 0; // Reset des aktuellen Befehls
curCRC = 0; // Reset des CRC
calCRC = 0xFFFF; // Initialwert für CRC-16
curLength = 0;
payloadLength = 0; // Länge der Nutzdaten initialisieren
address = 0; // Reset der Adresse
}
else if (curBit == 0 || curBit == 1)
{
if (curPos == 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)
{
curLength = curBit;
curPos++;
}
else if (curPos >= 2 && curPos <= 17)
// Jetzt die Payload verarbeiten (abhängig von curLength)
else if (curPos >= 10 && curPos <= (curLength ? 33 : 17))
{
if (curBit)
{
#if defined(USE_ESP_IDF)
bitSetIDF(&curCMD, (curLength ? 33 : 17) - curPos);
#else
bitSet(curCMD, (curLength ? 33 : 17) - curPos);
#endif
}

calCRC ^= curBit;
calCRC ^= curBit; // CRC-Berechnung
curPos++;
}
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)
// Verarbeitung des CRC (für den längeren CRC-16)
else if (curPos > (curLength ? 33 : 17) && curPos <= (curLength ? 49 : 33))
{
if (curBit)
{
#if defined(USE_ESP_IDF)
bitSetIDF(&curCMD, 33 - curPos);
#else
bitSet(curCMD, 33 - curPos);
#endif
bitSet(curCRC, (curLength ? 49 : 33) - curPos);
}

calCRC ^= curBit;
calCRC ^= curBit; // Berechnung der Prüfsumme
curPos++;
}
else if (curPos == 34)
else if (curPos == (curLength ? 50 : 34)) // Endbit, prüfen ob das Kommando vollständig ist
{
curCRC = curBit;
cmdIntReady = 1;
}
}
else
{
curPos = 0;
curPos = 0; // Fehlerhafte Bitposition, zurücksetzen
}

// 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;
}
}

Expand Down

0 comments on commit 2bde418

Please sign in to comment.