From 67e91ea7229993a007d9fa5777deb0f190fc78af Mon Sep 17 00:00:00 2001 From: Quinn Date: Mon, 10 Jun 2024 18:30:37 -0400 Subject: [PATCH] Pulled the most up to date serial driver --- .gitmodules | 3 + lib/SerialMessage | 1 + lib/SerialMessage/BluetoothSerialMessage.cpp | 58 ---------- lib/SerialMessage/BluetoothSerialMessage.h | 31 ------ lib/SerialMessage/SerialMessage.cpp | 111 ------------------- lib/SerialMessage/SerialMessage.h | 87 --------------- src/main.cpp | 12 +- 7 files changed, 10 insertions(+), 293 deletions(-) create mode 100644 .gitmodules create mode 160000 lib/SerialMessage delete mode 100644 lib/SerialMessage/BluetoothSerialMessage.cpp delete mode 100644 lib/SerialMessage/BluetoothSerialMessage.h delete mode 100644 lib/SerialMessage/SerialMessage.cpp delete mode 100644 lib/SerialMessage/SerialMessage.h diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..88c3b5a --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "lib/SerialMessage"] + path = lib/SerialMessage + url = https://github.com/Cynopolis/SerialMessage.git diff --git a/lib/SerialMessage b/lib/SerialMessage new file mode 160000 index 0000000..7cd4374 --- /dev/null +++ b/lib/SerialMessage @@ -0,0 +1 @@ +Subproject commit 7cd43742b650840c3e1c6f7472f7cb2720bdf479 diff --git a/lib/SerialMessage/BluetoothSerialMessage.cpp b/lib/SerialMessage/BluetoothSerialMessage.cpp deleted file mode 100644 index b765243..0000000 --- a/lib/SerialMessage/BluetoothSerialMessage.cpp +++ /dev/null @@ -1,58 +0,0 @@ -#include "BluetoothSerialMessage.h" - -BluetoothSerialMessage::BluetoothSerialMessage(BluetoothSerial *serial){ - this->serial = serial; -} - -void BluetoothSerialMessage::Init(unsigned int baud_rate){ - // Don't need to do anything here, just let the user init bluetooth serial -} - -void BluetoothSerialMessage::PrintArgs(){ - serial->print("Current number of args: "); - serial->println(populated_args); - for (int i = 0; i < populated_args; i++) { - serial->print(args[i]); - serial->print(" "); - } - serial->println(); -} - -void BluetoothSerialMessage::readSerial(){ - boolean recvInProgress = false; - byte ndx = 0; - char c; - - // read the incoming serial data: - while (serial->available() > 0 && data_recieved == false) { - // get the neext character in the serial buffer - c = serial->read(); - Serial.print(c); - - // only execute this if the startMarker has been received - if (recvInProgress == true) { - // if the incoming character is not the endMarker... - if (c != endMarker) { - // add it to the data array - data[ndx] = c; - ndx++; // increment the data array index - // if the index is greater than the maximum data array size, - // keep overwriting the last element until the endMarker is received. - if (ndx >= num_chars) { - ndx = num_chars - 1; - } - } - // if the incoming character is the endMarker clean up and set the flags - else { - data[ndx] = '\0'; // terminate the string - recvInProgress = false; - ndx = 0; - data_recieved = true; - } - } - // if the incoming character is the startMarker, set the recvInProgress flag - else if (c == startMarker) { - recvInProgress = true; - } - } -} \ No newline at end of file diff --git a/lib/SerialMessage/BluetoothSerialMessage.h b/lib/SerialMessage/BluetoothSerialMessage.h deleted file mode 100644 index 4414068..0000000 --- a/lib/SerialMessage/BluetoothSerialMessage.h +++ /dev/null @@ -1,31 +0,0 @@ -#pragma once -#include "SerialMessage.h" -#include - -class BluetoothSerialMessage : public SerialMessage{ - public: - /** - * @brief Construct a new Bluetooth Serial Message object - */ - BluetoothSerialMessage(BluetoothSerial *serial); - - /** - * @brief Initialize the BluetoothSerialMessage object - */ - void Init(unsigned int baud_rate = 115200) override; - - /** - * @brief prints the args array to the serial monitor - */ - void PrintArgs() override; - - private: - /** - * @brief reads the serial data and stores it in the data array - */ - void readSerial() override; - - BluetoothSerial *serial; - - -}; \ No newline at end of file diff --git a/lib/SerialMessage/SerialMessage.cpp b/lib/SerialMessage/SerialMessage.cpp deleted file mode 100644 index a0853d9..0000000 --- a/lib/SerialMessage/SerialMessage.cpp +++ /dev/null @@ -1,111 +0,0 @@ -/** - * @file SerialMessage.cpp - * @brief This file contains the SerialMessage class - * @details This file contains the SerialMessage class which is used to parse serial messages - * @version 1.0.0 - * @author Quinn Henthorne. Contact: quinn.henthorne@gmail.com -*/ - -#include "SerialMessage.h" - -SerialMessage::SerialMessage(HardwareSerial *serial) : - serial(serial){} - -void SerialMessage::Init(unsigned int baud_rate){ - serial->begin(baud_rate); -} - -void SerialMessage::readSerial(){ - char c; - - // read the incoming serial data: - while (serial->available() > 0 && data_recieved == false) { - // get the neext character in the serial buffer - c = serial->read(); - // only execute this if the startMarker has been received - // if the incoming character is the endMarker clean up and set the flags - if (recvInProgress == true) { - if (c == endMarker) { - data[ndx] = '\0'; // terminate the string - recvInProgress = false; - ndx = 0; - data_recieved = true; - } - // if the incoming character is not the endMarker - else { - // add it to the data array - data[ndx] = c; - ndx++; // increment the data array index - // if the index is greater than the maximum data array size, - // keep overwriting the last element until the endMarker is received. - if (ndx >= num_chars) { - ndx = num_chars - 1; - } - } - } - // if the incoming character is the startMarker, set the recvInProgress flag - else if (c == startMarker) { - recvInProgress = true; - } - } -} - -void SerialMessage::parseData() { // split the data into its parts - this->populated_args = 0; // reset the populated args counter - char * indx; // this is used by strtok() as an index - int i = 0; - indx = strtok(temp_data, ","); // get the first part - the string - while(indx != NULL){ - this->args[i] = atoi(indx); - populated_args++; - i++; - indx = strtok(NULL, ","); // this continues where the previous call left off - } -} - -void SerialMessage::Update(){ - readSerial(); - if (data_recieved == true) { - // for debug only: - // Serial.print("Received:"); - // Serial.print(data); - // Serial.println(":End"); - strcpy(temp_data, data); - // this temporary copy is necessary to protect the original data - // because strtok() used in parseData() replaces the commas with \0 - parseData(); - //PrintArgs(); - data_recieved = false; - new_data = true; - } -} - -bool SerialMessage::IsNewData(){ - return new_data; -} - -void SerialMessage::ClearNewData(){ - new_data = false; -} - -int * SerialMessage::GetArgs(){ - return args; -} - -int SerialMessage::GetArgsLength(){ - return args_length; -} - -int SerialMessage::GetPopulatedArgs(){ - return populated_args; -} - -void SerialMessage::PrintArgs(){ - serial->print("Current number of args: "); - serial->println(populated_args); - for (int i = 0; i < populated_args; i++) { - serial->print(args[i]); - serial->print(" "); - } - serial->println(); -} \ No newline at end of file diff --git a/lib/SerialMessage/SerialMessage.h b/lib/SerialMessage/SerialMessage.h deleted file mode 100644 index bd9b2f3..0000000 --- a/lib/SerialMessage/SerialMessage.h +++ /dev/null @@ -1,87 +0,0 @@ -/** - * @file SerialMessage.h - * @brief This file contains the SerialMessage class - * @details This file contains the SerialMessage class which is used to parse serial messages - * @version 1.0.0 - * @author Quinn Henthorne. Contact: quinn.henthorne@gmail.com -*/ - -#ifndef SERIALMESSAGE_H -#define SERIALMESSAGE_H - -#include "Arduino.h" - -#define num_chars 500 - -class SerialMessage{ - public: - /** - * @brief Construct a new Serial Message object - */ - SerialMessage(HardwareSerial *serial = &Serial); - - /** - * @brief Initialize the SerialMessage object - */ - virtual void Init(unsigned int baud_rate = 115200); - - /** - * @brief Update the SerialMessage object and parse any data that's available - */ - void Update(); - - /** - * @brief Returns true if there is new data available - * @return true if there is new data available - */ - bool IsNewData(); - - /** - * @brief Clears the new data flag - */ - virtual void ClearNewData(); - - /** - * @brief Return a pointer to the args array - * @return a pointer to the args array - */ - int * GetArgs(); - - /** - * @brief Returns the number of args that have been populated for the current message - * @return the number of args that have been populated for the current message - */ - int GetArgsLength(); - - /** - * @brief Returns the number of args that have been populated for the current message - * @return the number of args that have been populated for the current message - */ - int GetPopulatedArgs(); - - /** - * @brief Prints the args array to the serial monitor - */ - virtual void PrintArgs(); - - protected: - virtual void readSerial(); - virtual void parseData(); - - bool new_data = false; - bool data_recieved = false; - bool recvInProgress = false; - char data[num_chars]; // an array to store the received data - char temp_data[num_chars]; // an array that will be used with strtok() - uint16_t ndx = 0; - const static int args_length = 30; - int populated_args = 0; // the number of args that have been populated for the current message - int args[args_length]; - const char startMarker = '!'; - const char endMarker = ';'; - - private: - HardwareSerial *serial; -}; - -#endif \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 978093b..6ad114c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -25,7 +25,7 @@ enum Commands : uint8_t{ // -------------------------------------------------- // BluetoothSerial SerialBT; // BluetoothSerialMessage serialMessageBT(&SerialBT); -SerialMessage serialMessage(&Serial); +SerialMessage<500> serialMessage(&Serial); BoardLayout board(BOARD_WIDTH, BOARD_LENGTH, BOARD_HEIGHT, stacks); // Temporary thing until we can get bluetooth color management working on the quest @@ -61,9 +61,9 @@ void printBoardState(){ // SerialBT.println(";"); } -void setStackColor(int * args, int argsLength){ - int stackNum = args[1]; - int numColors = (argsLength - 2) / 3; +void setStackColor(uint32_t * args, int argsLength){ + uint32_t stackNum = args[1]; + uint32_t numColors = (argsLength - 2) / 3; Color colors[numColors]; for(int i = 0; i < numColors; i++){ @@ -78,8 +78,8 @@ void setStackColor(int * args, int argsLength){ } -void parseData(int * args, int argsLength){ - int command = args[0]; +void parseData(uint32_t * args, int argsLength){ + uint32_t command = args[0]; switch(command){ case Commands::BoardState: printBoardState();