diff --git a/examples/main.cpp b/examples/main.cpp index 49a8556..d796989 100644 --- a/examples/main.cpp +++ b/examples/main.cpp @@ -3,7 +3,9 @@ void setup() { Serial.begin(115200); - setupEnocean(16, 17); // for ESP32 + Serial.println("Setup starting"); + Serial2.begin(57600, SERIAL_8N1, 16, 17); + } void loop() @@ -11,7 +13,7 @@ void loop() while (Serial2.available()) { - byte const b{Serial2.read()}; + int const b{Serial2.read()}; if (b == 0x55) { @@ -21,13 +23,56 @@ void loop() } while (Serial2.available() < ENOCEAN_HEADER + 1); Packet packet; - Serial.println(packet.state); + + Serial2.readBytes(packet.getHeader(), ENOCEAN_HEADER); + byte const CRC8H{Serial2.read()}; + + if (packet.checkHeaderCRC8(CRC8H)) + { + Serial.println("Header CRC8 is valid."); + } + else + { + Serial.println("Header CRC8 is invalid."); + return; + } + byte waitCounter; + byte available; + do + { + yield(); + available = Serial2.available(); + + } while (available < packet.payloadLength + 1 || waitCounter++ > 50); + + if (packet.payloadLength + 1 <= available) + { + Serial.println("read data"); + + Serial2.readBytes(packet.enocean_data, packet.dataLength); + Serial2.readBytes(packet.enocean_optional, packet.optionalLength); + byte const CRC8D = Serial2.read(); + + if (packet.checkDataCRC8(CRC8D)) + { + Serial.println("Data CRC8 is valid."); + } + else + { + Serial.println("Data CRC8 is invalid."); + return; + }; + packet.handleTelegram(); + + } + Serial.println(packet.getState()); + for (int i = 0; i < 4; i++) { Serial.print("senderAddress["); Serial.print(i); Serial.print("]: "); - Serial.println(packet.senderAddress[i], HEX); + Serial.println(packet.getSenderAddress()[i], HEX); } } // magic byte 0x55 diff --git a/src/enocean.cpp b/src/enocean.cpp index e0b3284..fcf2dcb 100644 --- a/src/enocean.cpp +++ b/src/enocean.cpp @@ -22,68 +22,28 @@ Packet::Packet() state = 'Unknown'; // init done - Serial2.readBytes(header, ENOCEAN_HEADER); - type = header[3]; - - getPacketLength(); - if (checkHeaderCRC8()) - { - Serial.println("Header CRC8 is valid."); - } - else - { - Serial.println("Header CRC8 is invalid."); - return; - } - - byte waitCounter; - byte available; - do - { - yield(); - available = Serial2.available(); - - } while (available < payloadLength + 1 || waitCounter++ > 50); - - if (payloadLength + 1 <= available) - { - Serial.println("read data"); - - Serial2.readBytes(enocean_data, dataLength); - Serial2.readBytes(enocean_optional, optionalLength); - - if (checkDataCRC8()) - { - Serial.println("Data CRC8 is valid."); - } - else - { - Serial.println("Data CRC8 is invalid."); - return; - }; - rssi = enocean_optional[5]; - handleTelegram(); - // parsePacketData(data); - } + // Serial2.readBytes(header, ENOCEAN_HEADER); + } -bool Packet::checkHeaderCRC8() +bool Packet::checkHeaderCRC8(byte CRC8H) { - byte const CRC8H{Serial2.read()}; + getPacketLength(); + type = header[3]; crc8.begin(); uint8_t checksum = crc8.get_crc8(header, ENOCEAN_HEADER); return checksum == CRC8H; } -bool Packet::checkDataCRC8() +bool Packet::checkDataCRC8(byte CRC8D) { - byte CRC8D = Serial2.read(); byte enocean_buffer[2 * ENOCEAN_MAX_DATA]; memcpy(enocean_buffer, enocean_data, dataLength); memcpy(enocean_buffer + dataLength, enocean_optional, optionalLength); crc8.begin(); uint8_t checksumData = crc8.get_crc8(enocean_buffer, payloadLength); + rssi = enocean_optional[5]; return checksumData == CRC8D; } @@ -155,8 +115,15 @@ void Packet::handleRPSTelegram() // Implementation of RPS handling } -void setupEnocean(int rxPin, int txPin) +byte* Packet::getHeader() { - Serial.println("Setup starting"); - Serial2.begin(57600, SERIAL_8N1, rxPin, txPin); -} \ No newline at end of file + return header; +} +String Packet::getState() +{ + return state; +} +byte* Packet::getSenderAddress() +{ + return senderAddress; +} diff --git a/src/enocean.h b/src/enocean.h index 104e9ac..3cb7d4e 100644 --- a/src/enocean.h +++ b/src/enocean.h @@ -10,21 +10,24 @@ class Packet { public: Packet(); - byte senderAddress[4]; - String state; -private: + bool checkHeaderCRC8(byte CRC8H); void handleTelegram(); - void handleRPSTelegram(); - bool checkHeaderCRC8(); - bool checkDataCRC8(); - void getPacketLength(); - byte header[ENOCEAN_HEADER]; - byte payload[ENOCEAN_MAX_DATA]; + bool checkDataCRC8(byte CRC8D); + byte* getHeader(); + String getState(); + byte* getSenderAddress(); + uint8_t payloadLength; byte enocean_data[ENOCEAN_MAX_DATA]; byte enocean_optional[ENOCEAN_MAX_DATA]; uint8_t optionalLength; uint8_t dataLength; - uint8_t payloadLength; +private: + byte senderAddress[4]; + String state; + byte header[ENOCEAN_HEADER]; + void handleRPSTelegram(); + void getPacketLength(); + byte payload[ENOCEAN_MAX_DATA]; byte rssi; byte type; CRC8 crc8{}; @@ -33,6 +36,5 @@ class Packet { }; -void setupEnocean(int rxPin, int txPin); #endif // ENOCEAN_H \ No newline at end of file