128 lines
3.2 KiB
C++
128 lines
3.2 KiB
C++
#pragma once
|
|
|
|
/*
|
|
* Simple Serial Protocol
|
|
*
|
|
* This product includes software developed by Nick Leeman.
|
|
* Simple Serial Protocol (https://git.aterve.com/Frozenverse/SimpleSerialProtocol).
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
*/
|
|
|
|
class SerialConnection
|
|
{
|
|
private:
|
|
int serialBufferSize = 512;
|
|
char serialBuffer[512];
|
|
char serialData[512] = {0};
|
|
|
|
const char packetStartMarker = '<';
|
|
const char packetEndMarker = '>';
|
|
|
|
// Receiving a new packet
|
|
boolean newSerialData = false;
|
|
|
|
// Currently receiving a packet
|
|
boolean serialReadInProgress = false;
|
|
|
|
// Total bytes received in current packet
|
|
byte serialBytesReceived = 0;
|
|
|
|
// Serial connection
|
|
Stream *serialRefrence;
|
|
|
|
public:
|
|
void init(int serialIndex, unsigned long serialBaud);
|
|
void send(char * data);
|
|
char* handle();
|
|
char* getPacketMarker(int type);
|
|
int getBufferSize();
|
|
|
|
};
|
|
|
|
void SerialConnection::init(int serialIndex, unsigned long serialBaud) {
|
|
if (serialIndex == 0) {
|
|
Serial.begin(serialBaud);
|
|
serialRefrence = &Serial;
|
|
}
|
|
|
|
#ifdef Serial1
|
|
else if (serialIndex == 1) {
|
|
Serial1.begin(serialBaud);
|
|
serialRefrence = &Serial1;
|
|
}
|
|
#endif
|
|
|
|
#ifdef Serial2
|
|
else if (serialIndex == 2) {
|
|
Serial2.begin(serialBaud);
|
|
serialRefrence = &Serial2;
|
|
}
|
|
#endif
|
|
|
|
#ifdef Serial3
|
|
else if (serialIndex == 3) {
|
|
Serial3.begin(serialBaud);
|
|
serialRefrence = &Serial3;
|
|
}
|
|
#endif
|
|
};
|
|
|
|
char* SerialConnection::handle() {
|
|
if(serialRefrence->available() > 0) {
|
|
char _byte = serialRefrence->read();
|
|
|
|
// Check if we reached end of packet
|
|
if (_byte == packetEndMarker) {
|
|
serialReadInProgress = false;
|
|
newSerialData = true;
|
|
serialBuffer[serialBytesReceived] = 0;
|
|
return serialBuffer;
|
|
}
|
|
|
|
// Read packet data
|
|
if(serialReadInProgress) {
|
|
serialBuffer[serialBytesReceived] = _byte;
|
|
serialBytesReceived++;
|
|
|
|
if (serialBytesReceived == serialBufferSize) {
|
|
serialBytesReceived = serialBufferSize - 1;
|
|
}
|
|
}
|
|
|
|
// Check if received new packet marker
|
|
if (_byte == packetStartMarker) {
|
|
serialBytesReceived = 0;
|
|
serialReadInProgress = true;
|
|
}
|
|
|
|
return NULL;
|
|
}
|
|
|
|
return NULL;
|
|
};
|
|
|
|
void SerialConnection::send(char * data) {
|
|
serialRefrence->println(data);
|
|
};
|
|
|
|
char* SerialConnection::getPacketMarker(int type) {
|
|
if (type == 0) {
|
|
return "<";
|
|
} else {
|
|
return ">";
|
|
}
|
|
};
|
|
|
|
int SerialConnection::getBufferSize() {
|
|
return (int)serialBufferSize;
|
|
}; |