From 30deee981b7d736c5c89e057aeab842ef419515c Mon Sep 17 00:00:00 2001 From: Ollo Date: Sat, 7 Dec 2024 11:01:46 +0100 Subject: [PATCH] Add Serial library --- include/SerialCmd.h | 16 +++++++ src/SerialCmd.cpp | 114 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 130 insertions(+) create mode 100644 include/SerialCmd.h create mode 100644 src/SerialCmd.cpp diff --git a/include/SerialCmd.h b/include/SerialCmd.h new file mode 100644 index 0000000..013fa4f --- /dev/null +++ b/include/SerialCmd.h @@ -0,0 +1,16 @@ +#ifndef SERIALCMD_H +#define SERIALCMD_H + +#include + +int SerialCmd_readFromSerial (void); + +void SerialCmd_clearCmdArray (void); + +int SerialCmd_checkCmdArrayForPrefix (void); + +uint32_t SerialCmd_parseColor(int startOffset); + +char SerialCmd_type(void); + +#endif /* SERIALCMD_H */ \ No newline at end of file diff --git a/src/SerialCmd.cpp b/src/SerialCmd.cpp new file mode 100644 index 0000000..1000860 --- /dev/null +++ b/src/SerialCmd.cpp @@ -0,0 +1,114 @@ + +#include "SerialCmd.h" +#include "ColorUtil.h" +#include + + +// Serial fields +#define CMD_MAX 128 + +static char myCmd[CMD_MAX]; + +/** + * @brief + * number of read bytes + * @return int + */ +int SerialCmd_readFromSerial (void) +{ + //read from the serial buffer and flush + int inputSize = Serial.available(); + + //give serial a chance to receive all bytes + if(inputSize > 0){ + delay(100); + inputSize = Serial.available(); + } + + if(inputSize > 0 && inputSize < CMD_MAX) + { + Serial.print("inputSize: "); + Serial.print(inputSize); + for (int i = 0; i < inputSize; i++){ + myCmd[i] = Serial.read(); + } + } + else if(inputSize >= CMD_MAX) + { + Serial.flush(); + Serial.print("too much data, flush"); + } + return inputSize; +} + +/** + * @brief + * clear the cmd array + */ +void SerialCmd_clearCmdArray(void) +{ + for (int i = 0; i < CMD_MAX; i++) + { + myCmd[i] = '\0'; + } +} + +/** + * @brief + * check if command has the required prefix + * @return int + */ +int SerialCmd_checkCmdArrayForPrefix (void) +{ + if (myCmd[0] == 'o' + && myCmd[1] == 'l' + && myCmd[2] == 'l' + && myCmd[3] == 'p' + && myCmd[4] == 'e') + { + return 1; + } + else + { + return 0; + } +} + +char SerialCmd_type(void) +{ + if (SerialCmd_checkCmdArrayForPrefix()) + { + return myCmd[5]; + } + else + { + return '\0'; + } +} + +uint32_t SerialCmd_parseColor(int startOffset) +{ + unsigned int colorParts[3]; /* 0: red, 1: green, 2: blue */ + unsigned int colorSum = 0; + + if (startOffset > (CMD_MAX - sizeof(colorParts))) + { + return 0; + } + + for (int i=0; i < (2*3); i+= 2) + { + colorParts[i/2] = (myCmd[i+startOffset] <= '9') ? ((myCmd[i+startOffset]-'0') << 4) : (((myCmd[i+startOffset]-'A')+10) << 4); + if (colorParts[i/2] < 0) + { + Serial.print("calcErr:"); + Serial.println(colorParts[i/2]); + } + colorParts[i/2] += ( (myCmd[i+startOffset+1] <= '9') ? (myCmd[i+startOffset+1]-'0') : (((myCmd[i+startOffset+1]-'A')+10)) ); + } + int r = colorParts[0]; /* fill red */ + int g = colorParts[1]; /* fill green */ + int b = colorParts[2]; /* fill blue */ + + return Color(r, g, b); +}