updated naming
This commit is contained in:
125
src/SerialConnection.h
Normal file
125
src/SerialConnection.h
Normal file
@@ -0,0 +1,125 @@
|
||||
#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.
|
||||
*
|
||||
*/
|
||||
|
||||
// Base Arduino Library
|
||||
#include <Arduino.h>
|
||||
|
||||
class SerialConnection
|
||||
{
|
||||
private:
|
||||
byte serialBufferSize = 300;
|
||||
char serialBuffer[300];
|
||||
char serialData[300] = {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;
|
||||
}
|
||||
|
||||
else if (serialIndex == 1) {
|
||||
Serial1.begin(serialBaud);
|
||||
serialRefrence = &Serial1;
|
||||
}
|
||||
|
||||
else if (serialIndex == 2) {
|
||||
Serial2.begin(serialBaud);
|
||||
serialRefrence = &Serial2;
|
||||
}
|
||||
|
||||
else if (serialIndex == 3) {
|
||||
Serial3.begin(serialBaud);
|
||||
serialRefrence = &Serial3;
|
||||
}
|
||||
};
|
||||
|
||||
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;
|
||||
};
|
Reference in New Issue
Block a user