From 4ca0fda8d7faf45e86ff05eca2c7a3b3a2d5807f Mon Sep 17 00:00:00 2001
From: Pascal <engelerp@phys.ethz.ch>
Date: Tue, 27 Feb 2024 11:16:07 +0100
Subject: [PATCH] Added class for pure temperature observation

---
 .../temperature_communicator.cpp              | 65 +++++++++++++++++++
 .../temperature_communicator.hpp              | 26 ++++++++
 2 files changed, 91 insertions(+)
 create mode 100644 firmware/drivers/temperature_communicator/temperature_communicator.cpp
 create mode 100644 firmware/drivers/temperature_communicator/temperature_communicator.hpp

diff --git a/firmware/drivers/temperature_communicator/temperature_communicator.cpp b/firmware/drivers/temperature_communicator/temperature_communicator.cpp
new file mode 100644
index 0000000..465bb41
--- /dev/null
+++ b/firmware/drivers/temperature_communicator/temperature_communicator.cpp
@@ -0,0 +1,65 @@
+#include <temperature_communicator.hpp>
+#include <cstdio> //snprintf
+#include <utility.hpp>
+#include <cstdlib> //std::strtod
+#include <algorithm> //This is necessary to fix a bug that cost me 4h.
+#include <Arduino.h>
+
+char TemperatureCommunicator::outbuf_[256]; //out buffer
+char TemperatureCommunicator::cmd_ = 'X'; //command received
+
+bool TemperatureCommunicator::communicate(){
+    if(!serialCharAvailable_()){ //no communication
+    //    if(msElapsedSinceLastTransaction_() > interval_assume_eow_ms_){
+            //no sign of supervision for 30 minutes
+    //    }
+    //    else{
+            //have supervision
+    //    }
+        return false;
+    }
+    else{
+        cmd_ = getSerialChar_();
+        if(cmd_ == 't'){ //getTemperature
+            snprintf(   outbuf_,
+                        255,
+                        "%f,%f,%f,%f,%f;",
+                        Temperatures::temperatures[0],
+                        Temperatures::temperatures[1],
+                        Temperatures::temperatures[2],
+                        Temperatures::temperatures[3],
+                        Temperatures::temperatures[4]
+                        );
+            sendBuf_();
+            return true;
+        }
+        else{ //invalid command
+            handleInvalidCommand_();
+            return false;
+        }
+    }
+}
+
+bool TemperatureCommunicator::serialCharAvailable_(){
+    return SERCHAN.available() > 0;
+}
+
+char TemperatureCommunicator::getSerialChar_(){
+    int newChar = SERCHAN.read();
+    if(newChar < 0 || newChar >= 128){//invalid char
+        return 'X';
+    }
+    else{
+        return char(newChar);
+    }
+}
+
+void TemperatureCommunicator::sendBuf_(){
+    SERCHAN.write(outbuf_);
+}
+
+void TemperatureCommunicator::handleInvalidCommand_(){
+    snprintf(outbuf_, 255, "!");
+    sendBuf_();
+    return;
+}
\ No newline at end of file
diff --git a/firmware/drivers/temperature_communicator/temperature_communicator.hpp b/firmware/drivers/temperature_communicator/temperature_communicator.hpp
new file mode 100644
index 0000000..0b29e9b
--- /dev/null
+++ b/firmware/drivers/temperature_communicator/temperature_communicator.hpp
@@ -0,0 +1,26 @@
+#ifndef TEMPERATURECOMMUNICATOR_HPP_INCLUDED
+#define TEMPERATURECOMMUNICATOR_HPP_INCLUDED
+#include <algorithm>
+#include <vector>
+
+#define SERCHAN SerialUSB
+
+class TemperatureCommunicator{
+public:
+    static bool communicate();
+
+private:
+    /*Function Members*/
+    static bool serialCharAvailable_();
+
+    static char getSerialChar_();
+    static void sendBuf_();
+    
+    static void handleInvalidCommand_();
+
+    /*Data Members*/
+    static char outbuf_[256];
+    static char cmd_;
+};
+
+#endif
\ No newline at end of file
-- 
GitLab