updated naming

This commit is contained in:
2021-03-30 00:05:34 +02:00
parent 4ba8332404
commit a89791f251
5 changed files with 33 additions and 7 deletions

26
src/Packet.h Normal file
View File

@@ -0,0 +1,26 @@
#pragma once
/*
* Simple Serial Protocol
*
* This product includes software developed by Nick Leeman.
* Simple Serial Protocol Project (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.
*
*/
struct Packet
{
char * id;
char * command;
char * data;
};

125
src/SerialConnection.h Normal file
View 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;
};

105
src/SimpleSerialProtocol.h Normal file
View File

@@ -0,0 +1,105 @@
#pragma once
/*
* Simple Serial Protocol
*
* This product includes software developed by Nick Leeman.
* Simple Serial Protocol Project (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>
#include "Packet.h"
#include "SerialConnection.h"
class SimpleSerialProtocol
{
private:
SerialConnection serialConnection;
public:
bool init(int deviceIndex, unsigned long baud);
bool receivePackets(void (*callback)(Packet));
bool sendPacket(char* ID, char* command, char* data);
};
bool SimpleSerialProtocol::init(int deviceIndex, unsigned long baud) {
// Check if device index is in range
if (deviceIndex < 0 || deviceIndex > 4) {
return false;
}
// Init serial connection
serialConnection.init(deviceIndex, baud);
// Serial Connection Delay
delay(5);
return true;
};
bool SimpleSerialProtocol::receivePackets(void (*callback)(Packet)) {
// Try to get buffer from serial device
char* receivedBuffer = serialConnection.handle();
// Check if received a complete buffer
if (receivedBuffer != NULL) {
int bufferSize = serialConnection.getBufferSize();
// Buffer pointer
char* strtokIndex;
char id[bufferSize / 2];
char command[bufferSize / 2];
char data[bufferSize / 2];
// Get ID
strtokIndex = strtok(receivedBuffer, ":");
strcpy(id, strtokIndex);
// Get Command
strtokIndex = strtok(NULL, ":");
strcpy(command, strtokIndex);
// Get Data
strtokIndex = strtok(NULL, ":");
strcpy(data, strtokIndex);
// Pack received data into packet
Packet packet;
packet.id = id;
packet.command = command;
packet.data = data;
// Call pointer callback function
callback(packet);
// Received complete buffer
return true;
}
// Didn't receive complete buffer
return false;
};
bool SimpleSerialProtocol::sendPacket(char* ID, char* command, char* data) {
// Create new packet buffer
char serialPacketBuffer[serialConnection.getBufferSize()];
// Pack data in buffer
sprintf(serialPacketBuffer, "%s%s:%s:%s%s", serialConnection.getPacketMarker(0), ID, command, data, serialConnection.getPacketMarker(1));
// Send buffer to serial device
serialConnection.send(serialPacketBuffer);
return true;
};