From b59f276ae4cba194f482ecb3d7ccf255100f6476 Mon Sep 17 00:00:00 2001
From: SirWalross <27106448+SirWalross@users.noreply.github.com>
Date: Thu, 26 May 2022 00:34:30 +0200
Subject: [PATCH] Add write.bash for automating arduino uploads

---
 pull.bash => scripts/pull.bash                |  4 +-
 push.bash => scripts/push.bash                |  0
 scripts/write.bash                            | 87 +++++++++++++++++++
 .../DmsMessung/DmsMessung.ino                 |  0
 .../Temperaturmessung/Temperaturmessung.ino   |  1 -
 5 files changed, 89 insertions(+), 3 deletions(-)
 rename pull.bash => scripts/pull.bash (69%)
 rename push.bash => scripts/push.bash (100%)
 create mode 100755 scripts/write.bash
 rename LabPrak_Test/LabPrak_Test.ino => sketches/DmsMessung/DmsMessung.ino (100%)
 rename DHT11_Mehrere/DHT11_Mehrere.ino => sketches/Temperaturmessung/Temperaturmessung.ino (99%)

diff --git a/pull.bash b/scripts/pull.bash
similarity index 69%
rename from pull.bash
rename to scripts/pull.bash
index afe5e82..66b149f 100755
--- a/pull.bash
+++ b/scripts/pull.bash
@@ -1,8 +1,8 @@
 #!/bin/bash
 
-cd `dirname "$0"`
+cd `dirname "$0"`/..
 echo $( date '+%F %H:%M:%S' ) | tee -a bash.log
 git pull 2>&1 | tee -a bash.log
 echo "" | tee -a bash.log
-./push.bash 2>&1 | tee -a bash.log
+scripts/./push.bash 2>&1 | tee -a bash.log
 echo "" | tee -a bash.log
\ No newline at end of file
diff --git a/push.bash b/scripts/push.bash
similarity index 100%
rename from push.bash
rename to scripts/push.bash
diff --git a/scripts/write.bash b/scripts/write.bash
new file mode 100755
index 0000000..cd04b47
--- /dev/null
+++ b/scripts/write.bash
@@ -0,0 +1,87 @@
+#!/bin/bash
+
+cd `dirname "$0"`/../sketches
+
+fqbn=arduino:avr:nano
+core=arduino:avr
+serial_dms=/dev/ttyACM0
+serial_temp=/dev/ttyACM1
+
+
+echo "Checking connected devices..."
+
+device_list=$(arduino-cli board list)
+connected_devices=$(echo -e $device_list | grep "^\(\($serial_dms\)\|\($serial_temp\)\).*$fqbn.*$core$" | wc -l)
+
+if [[ $connected_devices != "2" ]] 
+then
+    echo -e "\x1b[31mError: not all arduino devices are connected\x1b[0m"
+    echo -e "Connected devices are:\n$device_list"
+    echo -e "Exiting."
+    exit 1
+fi
+
+echo "Checking installed cores..."
+
+core_list=$(arduino-cli core list)
+core_list_installed=$(echo -e $core_list | grep "^$core")
+
+if [[ -z $core_list_installed ]]
+then
+    echo -e "\x1b[33mWarning: Core $core for board $fqbn is not installed.\x1b[0m\nInstalling..."
+
+    arduino-cli core install $core
+
+    core_list=$(arduino-cli core list)
+    core_list_installed=$(echo -e $core_list | grep "^$core")
+
+    if [[ -z $core_list_installed ]]
+    then
+        echo -e "\x1b[31mError: failed to install core $core for board $fqbn.\x1b[0m\n"
+        echo -e "Installed cores are:\n$core_list"
+        echo -e "Exiting."
+        exit 1
+    fi
+fi
+
+echo "Compiling sketches..."
+
+compile=$(arduino-cli compile --fqbn $fqbn DmsMessung)
+if [[ $? -gt 0 ]]
+then
+    echo -e "\x1b[31mError: compilation of DmsMessung failed:\x1b[0m"
+    echo -e "$compile"
+    echo -e "Exiting."
+    exit 1
+fi
+
+compile=$(arduino-cli compile --fqbn $fqbn Temperaturmessung)
+if [[ $? -gt 0 ]]
+then
+    echo -e "\x1b[31mError: compilation of Temperaturmessung failed:\x1b[0m"
+    echo -e "$compile"
+    echo -e "Exiting."
+    exit 1
+fi
+
+echo "Uploading sketches..."
+
+upload=$(arduino-cli upload -p $serial_dms --fqbn $fqbn DmsMessung)
+if [[ $? -gt 0 ]]
+then
+    echo -e "\x1b[31mError: upload of DmsMessung failed:\x1b[0m"
+    echo -e "$upload"
+    echo -e "Exiting."
+    exit 1
+fi
+
+upload=$(arduino-cli upload -p $serial_temp --fqbn $fqbn Temperaturmessung)
+if [[ $? -gt 0 ]]
+then
+    echo -e "\x1b[31mError: upload of Temperaturmessung failed:\x1b[0m"
+    echo -e "$upload"
+    echo -e "Exiting."
+    exit 1
+fi
+
+echo "Finished"
\ No newline at end of file
diff --git a/LabPrak_Test/LabPrak_Test.ino b/sketches/DmsMessung/DmsMessung.ino
similarity index 100%
rename from LabPrak_Test/LabPrak_Test.ino
rename to sketches/DmsMessung/DmsMessung.ino
diff --git a/DHT11_Mehrere/DHT11_Mehrere.ino b/sketches/Temperaturmessung/Temperaturmessung.ino
similarity index 99%
rename from DHT11_Mehrere/DHT11_Mehrere.ino
rename to sketches/Temperaturmessung/Temperaturmessung.ino
index 0bcc5b8..8f2d297 100644
--- a/DHT11_Mehrere/DHT11_Mehrere.ino
+++ b/sketches/Temperaturmessung/Temperaturmessung.ino
@@ -1,4 +1,3 @@
-
 #include <DHT.h>
 
 #define DHT1_Pin 6
-- 
GitLab