From 89aa4cffff5c81369f06f4a762c25368aacc5810 Mon Sep 17 00:00:00 2001
From: Zoe Pfister <zoe.pfister@uibk.ac.at>
Date: Thu, 12 Jan 2023 15:36:59 +0100
Subject: [PATCH] WIP: Move more functions to Utilities.cpp, add Definitions.h
 and Exceptions

---
 .../lib/Utilities/Definitions.h               | 28 +++++++++++++++++
 .../lib/Utilities/SDCardException.h           | 22 +++++++++++++
 .../lib/Utilities/SDSetupException.h          | 21 +++++++++++++
 .../lib/Utilities/Utilities.cpp               | 31 +++++++++++++++++++
 .../lib/Utilities/Utilities.h                 |  6 +++-
 5 files changed, 107 insertions(+), 1 deletion(-)
 create mode 100644 host/host_central_mast/lib/Utilities/Definitions.h
 create mode 100644 host/host_central_mast/lib/Utilities/SDCardException.h
 create mode 100644 host/host_central_mast/lib/Utilities/SDSetupException.h

diff --git a/host/host_central_mast/lib/Utilities/Definitions.h b/host/host_central_mast/lib/Utilities/Definitions.h
new file mode 100644
index 0000000..ed28c03
--- /dev/null
+++ b/host/host_central_mast/lib/Utilities/Definitions.h
@@ -0,0 +1,28 @@
+//
+// Created by zoe on 1/12/23.
+//
+
+#ifndef HOST_CENTRAL_MAST_DEFINITIONS_H
+#define HOST_CENTRAL_MAST_DEFINITIONS_H
+
+#include "Arduino.h"
+
+#define uS_TO_S_FACTOR 1000000ULL // Conversion factor for micro seconds to seconds
+#define TIME_TO_SLEEP 5           // Time ESP32 will go to sleep (in seconds)
+
+#define UART_BAUD 115200
+#define PIN_DTR 25
+#define PIN_TX 27
+#define PIN_RX 26
+#define PWR_PIN 4
+
+#define SD_MISO 2
+#define SD_MOSI 15
+#define SD_SCLK 14
+#define SD_CS 13
+#define LED_PIN 12
+
+const String INFLUXDB_TOKEN =
+    "dUh2gbVLv7e3egqocxriDsJQNUacA9qZ5YXsYtdnVAglnHgy4nx-jDVO7nGlSF34BosfnuwnUDaviC7dQeC5RQ==";
+
+#endif // HOST_CENTRAL_MAST_DEFINITIONS_H
diff --git a/host/host_central_mast/lib/Utilities/SDCardException.h b/host/host_central_mast/lib/Utilities/SDCardException.h
new file mode 100644
index 0000000..039d703
--- /dev/null
+++ b/host/host_central_mast/lib/Utilities/SDCardException.h
@@ -0,0 +1,22 @@
+//
+// Created by zoe on 1/12/23.
+//
+
+#ifndef HOST_CENTRAL_MAST_SDCARDEXCEPTION_H
+#define HOST_CENTRAL_MAST_SDCARDEXCEPTION_H
+
+#include <Arduino.h>
+#include <exception>
+
+// create a custom exception type called SD Card Exception
+
+class SDCardException : public std::exception {
+  public:
+    SDCardException(const String &message) : message(message) {}
+    const char *what() const noexcept override { return message.c_str(); }
+
+  private:
+    String message;
+};
+
+#endif // HOST_CENTRAL_MAST_SDCARDEXCEPTION_H
diff --git a/host/host_central_mast/lib/Utilities/SDSetupException.h b/host/host_central_mast/lib/Utilities/SDSetupException.h
new file mode 100644
index 0000000..09c1ed3
--- /dev/null
+++ b/host/host_central_mast/lib/Utilities/SDSetupException.h
@@ -0,0 +1,21 @@
+//
+// Created by zoe on 1/12/23.
+//
+
+#ifndef HOST_CENTRAL_MAST_SDSETUPEXCEPTION_H
+#define HOST_CENTRAL_MAST_SDSETUPEXCEPTION_H
+
+#include <Arduino.h>
+#include <exception>
+// create a custom exception type called SD Setup Exception
+
+class SDSetupException : public std::exception {
+  public:
+    SDSetupException(const String &message) : message(message) {}
+    const char *what() const noexcept override { return message.c_str(); }
+
+  private:
+    String message;
+};
+
+#endif // HOST_CENTRAL_MAST_SDSETUPEXCEPTION_H
diff --git a/host/host_central_mast/lib/Utilities/Utilities.cpp b/host/host_central_mast/lib/Utilities/Utilities.cpp
index 061d6d0..21f3aca 100644
--- a/host/host_central_mast/lib/Utilities/Utilities.cpp
+++ b/host/host_central_mast/lib/Utilities/Utilities.cpp
@@ -4,6 +4,8 @@
 
 #include "Utilities.h"
 
+void setupSDCard();
+void saveStringToSDCard(const std::string &dataString);
 std::list<String> getFilesInDirectory(const String &dirname) {
     std::list<String> files;
     File dir = openDirectory(dirname);
@@ -117,4 +119,33 @@ void createDirectory(const String &dirname) {
     } else {
         esp_log_write(ESP_LOG_WARN, "createDirectory", "Directory already exists\n");
     }
+}
+
+void setupSDCard(int MISO, int MOSI, int SCLK, int CS) {
+    SPI.begin(SCLK, MISO, MOSI, CS);
+    if (!SD.begin(CS)) {
+        esp_log_write(ESP_LOG_ERROR, "Utilities", "Card MOUNT FAIL\n");
+        throw SDSetupException("Card MOUNT FAIL");
+    } else {
+        uint32_t cardSize = SD.cardSize() / (1024 * 1024);
+        String sdcardSizeString = "SDCard Size: " + String(cardSize) + "MB";
+        esp_log_write(ESP_LOG_DEBUG, "Utilities", "%s\n", sdcardSizeString.c_str());
+    }
+}
+
+void saveStringToSDCard(const std::string &dataString) {
+    File dataFile = SD.open("/datalog.txt", FILE_APPEND);
+
+    // if the file is available, write to it:
+    if (dataFile) {
+        if (dataString.length() > 0) {
+            dataFile.println(dataString.c_str());
+        }
+        dataFile.close();
+    }
+    // if the file isn't open, pop up an error:
+    else {
+        esp_log_write(ESP_LOG_ERROR, "Utilities", "error opening datalog.txt\n");
+        throw SDCardException("error opening datalog.txt");
+    }
 }
\ No newline at end of file
diff --git a/host/host_central_mast/lib/Utilities/Utilities.h b/host/host_central_mast/lib/Utilities/Utilities.h
index a0c07e4..1156c02 100644
--- a/host/host_central_mast/lib/Utilities/Utilities.h
+++ b/host/host_central_mast/lib/Utilities/Utilities.h
@@ -6,7 +6,10 @@
 #define HOST_CENTRAL_MAST_UTILITIES_H
 
 #include "SD.h"
+#include "SDCardException.h"
+#include "SDSetupException.h"
 #include <Arduino.h>
+#include <Definitions.h>
 #include <WString.h>
 #include <list>
 
@@ -17,5 +20,6 @@ std::optional<String> getLastFileInDirectory(const String &dirname);
 void writeFile(const String &messageToBeSend, const String &filePath);
 String readFile(const String &filePath);
 void createDirectory(const String &dirname);
-
+void setupSDCard(int MISO, int MOSI, int SCLK, int CS);
+void saveStringToSDCard(const std::string &dataString);
 #endif // HOST_CENTRAL_MAST_UTILITIES_H
-- 
GitLab