From 4a8ebffa82da544ceab1f0a30467f87970dae957 Mon Sep 17 00:00:00 2001 From: ZanzyTHEbar Date: Sun, 24 Jul 2022 21:52:30 +0100 Subject: [PATCH] update - Completed Serial Manager --- ESP/lib/src/data/config/project_config.hpp | 4 +- .../src/io/SerialManager/serialmanager.cpp | 81 +++++++++++++------ .../src/io/SerialManager/serialmanager.hpp | 21 ++++- 3 files changed, 75 insertions(+), 31 deletions(-) diff --git a/ESP/lib/src/data/config/project_config.hpp b/ESP/lib/src/data/config/project_config.hpp index 849acca..9c09207 100644 --- a/ESP/lib/src/data/config/project_config.hpp +++ b/ESP/lib/src/data/config/project_config.hpp @@ -39,8 +39,8 @@ public: struct TrackerConfig_t { - DeviceConfig_t device{}; - CameraConfig_t camera{}; + DeviceConfig_t device; + CameraConfig_t camera; std::vector networks; }; diff --git a/ESP/lib/src/io/SerialManager/serialmanager.cpp b/ESP/lib/src/io/SerialManager/serialmanager.cpp index 9f97f7c..9ef75f8 100644 --- a/ESP/lib/src/io/SerialManager/serialmanager.cpp +++ b/ESP/lib/src/io/SerialManager/serialmanager.cpp @@ -6,24 +6,32 @@ SerialManager::SerialManager() : serialManagerActive(false), serialBuffer{0}, device_config_name{0}, device_config_OTAPassword{0}, - device_config_OTAPort(0) {} + device_config_OTAPort(0), + camera_config_vflip{0}, + camera_config_href{0}, + camera_config_framesize{0}, + camera_config_quality{0}, + wifi_config_name{0}, + wifi_config_ssid{0}, + wifi_config_password{0} {} SerialManager::~SerialManager() {} -void SerialManager::listenToSerial(int timeout) +void SerialManager::listenToSerial(unsigned long timeout) { log_d("Listening to serial"); serialManagerActive = true; Serial.setTimeout(timeout); - static boolean recvInProgress = false; - static byte index = 0; // index - char startDelimiter = '<'; - char endDelimiter = '>'; - char receivedChar; // to test for received data on the line + static bool recvInProgress = false; + static uint8_t index = 0; // index + char startDelimiter = '<'; //! we need to decide on a delimiter for the start of a message + char endDelimiter = '>'; //! we need to decide on a delimiter for the end of a message + char receivedChar; // to test for received data on the line - while (serialManagerActive) + while ((Serial.available() > 0) && !newData) { + serialManagerActive = true; receivedChar = Serial.read(); if (recvInProgress) { @@ -53,11 +61,6 @@ void SerialManager::listenToSerial(int timeout) recvInProgress = true; } } - - if (Serial.available() > 0) - { - Serial.readBytesUntil('\n', this->serialBuffer, sizeof(this->serialBuffer)); - } delay(timeout); serialManagerActive = false; } @@ -68,36 +71,64 @@ void SerialManager::parseData() log_d("Parsing data"); char *strtokIndx; // this is used by strtok() as an index - strtokIndx = strtok(tempBuffer, ","); // get the first part - the string + //! Parse the data + //* Device Config *// + strtokIndx = strtok(tempBuffer, ","); // get the first part strcpy(device_config_name, strtokIndx); // copy it to buffer - strtokIndx = strtok(NULL, ","); // get the second part - the string - strcpy(device_config_OTAPassword, strtokIndx); // copy it to buffer + strtokIndx = strtok(NULL, ","); // get the second part + strcpy(device_config_OTAPassword, strtokIndx); - strtokIndx = strtok(NULL, ","); // get the first part - the string - device_config_OTAPort = atoi(strtokIndx); // convert this part to an integer + strtokIndx = strtok(NULL, ","); + device_config_OTAPort = atoi(strtokIndx); - projectConfig.setDeviceConfig( ); // get the second part - the value - if (newData) + //* Camera Config *// + strtokIndx = strtok(NULL, ","); + camera_config_vflip = atoi(strtokIndx); + + strtokIndx = strtok(NULL, ","); + camera_config_framesize = atoi(strtokIndx); + + strtokIndx = strtok(NULL, ","); + camera_config_href = atoi(strtokIndx); + + strtokIndx = strtok(NULL, ","); + camera_config_quality = atoi(strtokIndx); + + //* Wifi Config *// + strtokIndx = strtok(tempBuffer, ","); + strcpy(wifi_config_name, strtokIndx); + + strtokIndx = strtok(NULL, ","); + strcpy(wifi_config_ssid, strtokIndx); + + strtokIndx = strtok(NULL, ","); + strcpy(wifi_config_password, strtokIndx); + + /* if (newData) { log_d("New data"); newData = false; - char *token = strtok(serialBuffer, ","); + char *token = strtok(tempBuffer, ","); while (token != NULL) { log_d("Token: %s", token); token = strtok(NULL, ","); } - } + } */ } -void SerialManager::moveData() +void SerialManager::handleSerial() { - listenToSerial(30000); // test for serial input for 30 seconds - if (newData) // input received + listenToSerial(30000L); // test for serial input for 30 seconds + if (newData) // input received { strcpy(tempBuffer, serialBuffer); // this temporary copy is necessary to protect the original data because strtok() used in parseData() replaces the commas with \0 parseData(); // split the data into tokens and store them in the data structure newData = false; // reset new data } + + projectConfig.setDeviceConfig(device_config_name, device_config_OTAPassword, &device_config_OTAPort, true); // set the values in the project config + projectConfig.setCameraConfig(&camera_config_vflip, &camera_config_framesize, &camera_config_href, &camera_config_quality, true); // set the values in the project config + projectConfig.setWifiConfig(wifi_config_name, wifi_config_ssid, wifi_config_password, true); // set the values in the project config } diff --git a/ESP/lib/src/io/SerialManager/serialmanager.hpp b/ESP/lib/src/io/SerialManager/serialmanager.hpp index 3a8b11a..7549183 100644 --- a/ESP/lib/src/io/SerialManager/serialmanager.hpp +++ b/ESP/lib/src/io/SerialManager/serialmanager.hpp @@ -9,17 +9,30 @@ public: SerialManager(); virtual ~SerialManager(); - void listenToSerial(int timeout); - void parseData(); - void moveData(); + void handleSerial(); bool serialManagerActive; + /* Device Config Variables */ char device_config_name[32]; char device_config_OTAPassword[100]; int device_config_OTAPort; + /* Camera Config Variables */ + uint8_t camera_config_vflip; + uint8_t camera_config_framesize; + uint8_t camera_config_href; + uint8_t camera_config_quality; + + /* Wifi Config Variables */ + char wifi_config_name[32]; + char wifi_config_ssid[100]; + char wifi_config_password[100]; + private: + + void listenToSerial(unsigned long timeout); + void parseData(); enum DataTypes_e { DataType_Unknown, @@ -30,8 +43,8 @@ private: DataType_Debug }; - char tempBuffer[sizeof(serialBuffer) / sizeof(serialBuffer[0])]; char serialBuffer[100000]; //! Need to find the appropriate size for this - count the maximum possible size of a message + char tempBuffer[sizeof(serialBuffer) / sizeof(serialBuffer[0])]; bool newData; };