diff --git a/LICENSE b/LICENSE deleted file mode 100644 index 204b93d..0000000 --- a/LICENSE +++ /dev/null @@ -1,19 +0,0 @@ -MIT License Copyright (c) - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is furnished -to do so, subject to the following conditions: - -The above copyright notice and this permission notice (including the next -paragraph) shall be included in all copies or substantial portions of the -Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS -FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS -OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, -WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF -OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/examples/ssp/ssp.ino b/examples/ssp/ssp.ino new file mode 100644 index 0000000..748d746 --- /dev/null +++ b/examples/ssp/ssp.ino @@ -0,0 +1,32 @@ +#include +#include + +// Define new device +SSPDevice deviceOne; + +// Gets called when a complete packet has been received. +void onPacket(Packet packet) { + Serial.println("Received Packet!"); + Serial.println(packet.id); + Serial.println(packet.command); + Serial.println(packet.data); + Serial.println("----------------"); + + // To send a packet + deviceOne.sendPacket("IDPacket", "Commandhere", "testtest"); +} + +void setup() { + // Initialize (deviceOne) serial device on index 0 with a baud rate of 115200. + deviceOne.init(0, 115200); +} + +void loop() { + // Call receive packets function (looping) to handle incoming serial packets. + // If a complete packet has been received it will call the callback function (onPacket). + deviceOne.receivePackets(onPacket); + + // Small delay to not over overwork CPU. + delay(1); +} + diff --git a/include/Packet.h b/include/Packet.h new file mode 100644 index 0000000..a8e8a06 --- /dev/null +++ b/include/Packet.h @@ -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; +}; diff --git a/include/SSPDevice.h b/include/SSPDevice.h new file mode 100644 index 0000000..3d432ec --- /dev/null +++ b/include/SSPDevice.h @@ -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 + +#include "Packet.h" +#include "SerialConnection.h" + +class SSPDevice +{ + 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 SSPDevice::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 SSPDevice::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 SSPDevice::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; +}; \ No newline at end of file diff --git a/include/SerialConnection.h b/include/SerialConnection.h new file mode 100644 index 0000000..e7aab8f --- /dev/null +++ b/include/SerialConnection.h @@ -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 + +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; +}; \ No newline at end of file diff --git a/library.json b/library.json new file mode 100644 index 0000000..31aeb71 --- /dev/null +++ b/library.json @@ -0,0 +1,23 @@ +{ + "name": "SimpleSerialProtocol", + "version": "1.0.0", + "description": "Easy-to-use serial protocol system for arduino type devices.", + "keywords": "Serial, Connectivity, USB, Simple", + "repository": + { + "type": "git", + "url": "https://git.aterve.com/Frozenverse/SimpleSerialProtocol.git" + }, + "authors": + [ + { + "name": "Nick Leeman", + "email": "nick@aterve.nl", + "url": "https://www.froz.io" + } + ], + "license": "GNU", + "homepage": "https://git.aterve.com/Frozenverse/SimpleSerialProtocol", + "frameworks": "*", + "platforms": "*" +} \ No newline at end of file