From 3cc6cff60760b9a6d0f86a2e245b35e2a58d55d3 Mon Sep 17 00:00:00 2001
From: Kai Junge <kai.junge@epfl.ch>
Date: Wed, 13 Apr 2022 22:54:46 +0200
Subject: [PATCH] Windows testing.

---
 comms_wrapper.py              | 24 ++++++-----
 test_windows.py               | 28 +++++++++++++
 test_windows/pyCommsLib.cpp   | 78 +++++++++++++++++++++++++++++++++++
 test_windows/pyCommsLib.h     | 17 ++++++++
 test_windows/test_windows.ino | 25 +++++++++++
 5 files changed, 162 insertions(+), 10 deletions(-)
 create mode 100644 test_windows.py
 create mode 100644 test_windows/pyCommsLib.cpp
 create mode 100644 test_windows/pyCommsLib.h
 create mode 100644 test_windows/test_windows.ino

diff --git a/comms_wrapper.py b/comms_wrapper.py
index dd5088d..94c51c8 100644
--- a/comms_wrapper.py
+++ b/comms_wrapper.py
@@ -35,9 +35,8 @@ class Arduino:
             try:
                 self._rawReceivedMessage = self.arduino.readline().decode('utf-8')[:-2]
             except:
-                pass
-            time.sleep(0.001)
-
+                #pass
+                time.sleep(0.000001)
 
     def _startReadingThread(self):
         self.__thread = Thread(target=self._serial_readline)
@@ -89,7 +88,6 @@ class Arduino:
                 msgName = singleMsg.split(";")[0]
                 msgPayload = singleMsg.split(";")[1]
 
-
                 if msgName == "echo_python":
                         self._echo_python_msg = msgPayload
                 else:
@@ -100,6 +98,11 @@ class Arduino:
 
             else:
                 return False
+
+        #print(receivedMessageTemp["pot"], self.receivedMessages["pot"])
+        if receivedMessageTemp["pot"] != self.receivedMessages["pot"]: 
+            print("asdfasdfasfdasd")
+            print(receivedMessageTemp["pot"])
         
         if receivedMessageTemp == self.receivedMessages: 
             self.newMsgRecieved = False
@@ -150,11 +153,12 @@ class Arduino:
                 break
 
         if self.handshakeStatus: 
+            # while 1: 
+            #     self.receive_message()
+            #     if self._echo_python_msg == "NO_PYTHON_MESSAGE":
+            #         break
             print("Successfull handshake with " + self.descriptiveDeviceName)
-            while 1: 
-                self.receive_message()
-                if self._echo_python_msg == "NO_PYTHON_MESSAGE":
-                    break
+            time.sleep(1)
         else:
             print("!! Handshake failed with " + self.descriptiveDeviceName + " !!")
 
@@ -199,7 +203,7 @@ class Arduino:
                 else:
                     print("Messege received from the arduino: ", self.receivedMessages)
 
-            time.sleep(0.000001)
+            #time.sleep(0.0001)
             return True
 
 
@@ -231,7 +235,7 @@ class Arduino:
 
         for key, value in self.receivedMessages.items():
             if value is None: 
-                print("Check if message name: ", key, " agrees with the arduino side")
+                print("Check if message names: ", key, " agrees with the arduino side")
 
 
 ########
diff --git a/test_windows.py b/test_windows.py
new file mode 100644
index 0000000..22e4b4f
--- /dev/null
+++ b/test_windows.py
@@ -0,0 +1,28 @@
+from comms_wrapper import *
+from time import sleep, time
+
+def main():
+    key = Key()
+
+    arduino1 = Arduino(descriptiveDeviceName="myArduino", portName="COM4", baudrate=115200)
+   
+    arduino1.define_message_names(["pot"])
+    
+    arduino1.connect_and_handshake()
+    
+    timer = time()
+    while 1: 
+        curr_time = round(time() - timer, 5)
+        curr_time = str(curr_time)[:5]
+
+        arduino1.receive_message(printOutput = False)
+
+        #print(arduino1._rawReceivedMessage)
+        
+        # if arduino1.newMsgRecieved:
+        #     print(curr_time, arduino1.receivedMessages)
+
+
+
+if __name__ == '__main__':
+    main()
diff --git a/test_windows/pyCommsLib.cpp b/test_windows/pyCommsLib.cpp
new file mode 100644
index 0000000..456e46c
--- /dev/null
+++ b/test_windows/pyCommsLib.cpp
@@ -0,0 +1,78 @@
+#include "pyCommsLib.h"
+#include "Arduino.h"
+
+
+#define size_of_array(arr) sizeof(arr) / sizeof(*arr)
+
+
+String rawMsgFromPython = "NO_PYTHON_MESSAGE";
+String payload = "";
+
+
+void load_msg_to_python(String* msgName, String* msg, int numOfMsg) {
+  // If we have the same number of data compared to the message
+  payload = "";
+  
+  for (int i = 0; i < numOfMsg; i++) {
+    payload.concat(msgName[i]);
+    payload.concat(";");
+    payload.concat(msg[i]);
+    payload.concat(":");
+  }
+}
+
+
+void receive_msg_from_python() {
+
+  String msg = "";
+
+  while (Serial.available()) {
+    if (Serial.available()) {
+      msg = Serial.readStringUntil('\n');
+    }
+  }
+
+  if (msg != "") {
+    rawMsgFromPython = msg;
+  }
+
+}
+
+
+String latest_received_msg() { 
+  return rawMsgFromPython;
+}
+
+void init_python_communication() {
+
+  while (true) {
+    // if the python side sent a message
+    if (Serial.available() > 0) {
+      String rawMsgFromPython = Serial.readStringUntil('\n');
+      if (rawMsgFromPython == "handshake1") {
+        break;
+      }
+    }
+  }
+
+  long timer = millis();
+  while (millis() - timer < 500) {
+    Serial.println("handshake2");
+  }
+
+
+  while (Serial.available()) {
+    Serial.read();
+  }
+}
+
+
+void sync() { 
+  receive_msg_from_python();
+  String final_payload = "<echo_python;";
+  final_payload.concat(latest_received_msg());
+  final_payload.concat(":");
+  final_payload.concat(payload); 
+  final_payload.concat(">");
+  Serial.println(final_payload);
+}
diff --git a/test_windows/pyCommsLib.h b/test_windows/pyCommsLib.h
new file mode 100644
index 0000000..75ee2f4
--- /dev/null
+++ b/test_windows/pyCommsLib.h
@@ -0,0 +1,17 @@
+#ifndef PYCOMMSLIB_H
+#define PYCOMMSLIB_H
+
+#define size_of_array(arr) sizeof(arr) / sizeof(*arr)
+
+
+#include <Arduino.h>
+
+void load_msg_to_python(String* msgName, String* msg, int numOfMsg);
+
+String latest_received_msg();
+
+void init_python_communication();
+
+void sync();
+
+#endif
diff --git a/test_windows/test_windows.ino b/test_windows/test_windows.ino
new file mode 100644
index 0000000..abcb2d4
--- /dev/null
+++ b/test_windows/test_windows.ino
@@ -0,0 +1,25 @@
+#include "pyCommsLib.h"
+
+String msgName[] = {"pot"};
+String dataCarrier[1];
+
+void setup() {
+  // Start the serial communication. The baudrate is arbiturary.
+  Serial.begin(115200);
+
+  // Connect with the Python side
+  init_python_communication();
+}
+
+long timer = millis();
+void loop() {
+  
+  int pot = analogRead(A0);
+  //Serial.println(pot);
+  
+  dataCarrier[0] = String(pot);
+
+  load_msg_to_python(msgName, dataCarrier, size_of_array(msgName));
+  sync();
+  
+}
-- 
GitLab