From c61a23e921f4024418a2569e061073da036ca636 Mon Sep 17 00:00:00 2001
From: Sebastian Huber <sebastian.huber@phys.ethz.ch>
Date: Thu, 20 Mar 2025 09:19:16 +0100
Subject: [PATCH] Changed flushing and send size of different vector for
 logging

---
 .../drivers/communicator/communicator.cpp     | 29 +++++++------------
 1 file changed, 11 insertions(+), 18 deletions(-)

diff --git a/firmware/drivers/communicator/communicator.cpp b/firmware/drivers/communicator/communicator.cpp
index bc4f33b..1fdde41 100644
--- a/firmware/drivers/communicator/communicator.cpp
+++ b/firmware/drivers/communicator/communicator.cpp
@@ -22,6 +22,13 @@ std::vector<char> Communicator::arg_ = std::vector<char>(256, '\0'); //arguments
 unsigned long Communicator::last_transaction_ms_ = 0u; //time of last transaction
 unsigned long Communicator::interval_assume_eow_ms_ = 1800000u; //no transaction for 30 minutes means we shut down
 
+extern "C" char *sbrk(int i);
+double checkFreeMemory() {
+  char stack_dummy = 0;
+  return static_cast<double>(&stack_dummy - sbrk(0));
+}
+
+
 bool Communicator::communicate(){
     if(!serialCharAvailable_()){ //no communication
         if(msElapsedSinceLastTransaction_() > interval_assume_eow_ms_){
@@ -55,18 +62,15 @@ bool Communicator::communicate(){
             }
             if(PTimer::elapsed() >= timeout_us_){
                 handleTimeout_(); //timeout
-                flushSerialBuffer_();
                 return false;
             }
             else if(i >= arg_.size()-10){
                 handleInputOverflow_(); //input overflow
-                flushSerialBuffer_();
                 return false;
             }
             //apply the setting
             else{
                 handleSetCommand_();
-                flushSerialBuffer_();
                 return true;
             }
         }
@@ -74,15 +78,15 @@ bool Communicator::communicate(){
             if(cmd_ == 't'){ //getTemperature
                 snprintf(   outbuf_,
                             255,
-                            "%f,%f,%f,%f,%f;",
+                            "%f,%f,%f,%f,%f,%f;",
                             Temperatures::temperatures[0],
                             Temperatures::temperatures[1],
                             Temperatures::temperatures[2],
                             Temperatures::temperatures[3],
-                            Temperatures::temperatures[4]
+                            Temperatures::temperatures[4],
+                            Communicator::arg_.capacity()
                             );
                 sendBuf_();
-                flushSerialBuffer_();
                 return true;
             }
             else if(cmd_ == 'p'){ //getPidCoeff
@@ -95,7 +99,6 @@ bool Communicator::communicate(){
                             kpids[2]
                             );
                 sendBuf_();
-                flushSerialBuffer_();
                 return true;
             }
             else if(cmd_ == 'v'){ //getPropIntDer
@@ -108,35 +111,29 @@ bool Communicator::communicate(){
                             pids[2]
                             );
                 sendBuf_();
-                flushSerialBuffer_();
                 return true;
             }
             else if(cmd_ == 'o'){ //getOutput
                 snprintf(outbuf_, 255, "%f;", Pid_controller::get_output());
                 sendBuf_();
-                flushSerialBuffer_();
                 return true;
             }
             else if(cmd_ == 's'){ //getSetpoint
                 snprintf(outbuf_, 255, "%f;", Pid_controller::get_setpoint());
                 sendBuf_();
-                flushSerialBuffer_();
                 return true;
             }
             else{ //invalid command
                 handleInvalidCommand_();
-                flushSerialBuffer_();
                 return false;
             }
         }
         else if(isNopCommand_()){
             handleNopCommand_();
-            flushSerialBuffer_();
             return true;
         }
         else{ //invalid command
             handleInvalidCommand_();
-            flushSerialBuffer_();
             return false;
         }
     }
@@ -179,6 +176,7 @@ char Communicator::getSerialChar_(){
 
 void Communicator::sendBuf_(){
     SERCHAN.write(outbuf_);
+    SERCHAN.flush();
 }
 
 void Communicator::handleTimeout_(){
@@ -257,8 +255,3 @@ unsigned long Communicator::msElapsedSinceLastTransaction_(){
 }
 
 
-void Communicator::flushSerialBuffer_(){
-    while (SERCHAN.available()) {
-        SERCHAN.read(); // Dump any remaining data
-    }
-}
\ No newline at end of file
-- 
GitLab