Skip to content

Commit

Permalink
removed serial console from library; added getter methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Marius Bäsler committed Oct 4, 2024
1 parent ca7c357 commit fb5e1bf
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 66 deletions.
53 changes: 49 additions & 4 deletions examples/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@
void setup()
{
Serial.begin(115200);
setupEnocean(16, 17); // for ESP32
Serial.println("Setup starting");
Serial2.begin(57600, SERIAL_8N1, 16, 17);

}

void loop()
{

while (Serial2.available())
{
byte const b{Serial2.read()};
int const b{Serial2.read()};

if (b == 0x55)
{
Expand All @@ -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
Expand Down
69 changes: 18 additions & 51 deletions src/enocean.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
}
return header;
}
String Packet::getState()
{
return state;
}
byte* Packet::getSenderAddress()
{
return senderAddress;
}
24 changes: 13 additions & 11 deletions src/enocean.h
Original file line number Diff line number Diff line change
Expand Up @@ -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{};
Expand All @@ -33,6 +36,5 @@ class Packet {

};

void setupEnocean(int rxPin, int txPin);

#endif // ENOCEAN_H

0 comments on commit fb5e1bf

Please sign in to comment.