From 1bf18735f238b7df657033d58e415e35748302b0 Mon Sep 17 00:00:00 2001 From: kjunge <kai.junge@epfl.ch> Date: Sat, 22 Jan 2022 12:26:02 +0100 Subject: [PATCH] Functionality changes before final testing before initial release. --- comms_wrapper.py | 57 +++++++++++++++++++++--------------- comms_wrapper_test_ground.py | 3 +- pyComms/pyComms.ino | 28 +++--------------- pyComms/pyCommsLib.cpp | 17 ++++------- pyComms/pyCommsLib.h | 2 +- 5 files changed, 46 insertions(+), 61 deletions(-) diff --git a/comms_wrapper.py b/comms_wrapper.py index b8798d2..a717362 100644 --- a/comms_wrapper.py +++ b/comms_wrapper.py @@ -11,16 +11,18 @@ from pynput import keyboard class Arduino: def __init__(self, descriptiveDeviceName, portName, baudrate): + # About the device self.descriptiveDeviceName = descriptiveDeviceName self.portName = portName self.baudrate = baudrate + # Communication self._rawReceivedMessage = None - self._prevMessage = None self.receivedMessages = {} - self.messageNames = list(self.receivedMessages.keys()) + self.messageNames = None self.arduino = None self.handshakeStatus = False + self.connectionStatus = False self._echo_python_msg = None @@ -52,7 +54,7 @@ class Arduino: self.messageNames = list(self.receivedMessages.keys()) - def _just_connect(self): + def _connect_to_arduino(self): # Connect to the arduino device try: self.arduino = serial.Serial(port=self.portName, baudrate=self.baudrate) @@ -60,14 +62,14 @@ class Arduino: # toggle dtr to reset the arduino self.arduino.dtr = True self.arduino.dtr = False + + self.connectionStatus = True print("Successfully connected to " + self.descriptiveDeviceName) + return True except: print("!! Cannot connect to " + self.descriptiveDeviceName + " !!") return False - - self.arduino.dtr = True - self.arduino.dtr = False def _disect_and_save_message(self, msg): @@ -99,13 +101,7 @@ class Arduino: self.receivedMessages = receivedMessageTemp return True - - def _list_connected_serial_devices(): - devices = list(serial.tools.list_ports.comports()) - for device in devices: - print(device) - - + # Public methods def define_message_names(self, msgNames): if type(msgNames) == list: @@ -117,16 +113,10 @@ class Arduino: def connect_and_handshake(self): # Connect to the arduino device - try: - self.arduino = serial.Serial(port=self.portName, baudrate=self.baudrate) - - # toggle dtr to reset the arduino - self.arduino.dtr = True - self.arduino.dtr = False - - print("Successfully connected to " + self.descriptiveDeviceName) - except: - print("!! Cannot connect to " + self.descriptiveDeviceName + " !!") + + if self._connect_to_arduino(): + pass + else: return False # Start the reading thread @@ -203,9 +193,30 @@ class Arduino: time.sleep(0.000001) return True + + def current_status(self): + status = { + "Device name" : self.descriptiveDeviceName, + "Baudrate: ": self.baudrate, + "Portname: ": self.portName, + "Connection: ": self.connectionStatus, + "Handshake: ": self.handshakeStatus, + "Message names: ": self.messageNames + } + + if printOutput: + print(status) + + return status + + def debug(self, verbose = False): if verbose: self.receive_message(printOutput=True, verbose = True) + + print("Current status of this device:") + print(self.current_status()) + else: print("----------------------") self.receive_message(printOutput=True) diff --git a/comms_wrapper_test_ground.py b/comms_wrapper_test_ground.py index 8a2061e..00eb56b 100644 --- a/comms_wrapper_test_ground.py +++ b/comms_wrapper_test_ground.py @@ -32,5 +32,4 @@ def main(): time.sleep(0.2) if __name__ == '__main__': - main() - \ No newline at end of file + main() \ No newline at end of file diff --git a/pyComms/pyComms.ino b/pyComms/pyComms.ino index d56c95f..3dcd88b 100644 --- a/pyComms/pyComms.ino +++ b/pyComms/pyComms.ino @@ -1,12 +1,8 @@ #include "pyCommsLib.h" -/* - * Variables for you need to define for this program to work - */ -String msgName[] = {"msgA", "msgB", "msgC"}; -String dataCarrier[3]; - +String msgName[] = {"msgA", "msgB"}; +String dataCarrier[2]; void setup() { @@ -19,28 +15,12 @@ void setup() { void loop() { - - //////////////////// - // SENDING DATA - //////////////////// - /* - Fill up your data here - - MAKE SURE TO CONVERT ANYTHING TO A String TYPE! - - This can be easilly done by doing String(whatever) - see Data[1] = String(20); - */ - dataCarrier[0] = "Some message"; dataCarrier[1] = String(3); - dataCarrier[2] = received_msg(); - - // The send command. Just copy and paste this. load_msg_to_python(msgName, dataCarrier, size_of_array(msgName)); - // this delay is not necessary to have here - it's only to show that even with a delay this process works. - delay(100); - sync(); + + delay(100); } diff --git a/pyComms/pyCommsLib.cpp b/pyComms/pyCommsLib.cpp index fa396a3..cb2d4b3 100644 --- a/pyComms/pyCommsLib.cpp +++ b/pyComms/pyCommsLib.cpp @@ -1,11 +1,14 @@ #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 = ""; @@ -18,6 +21,7 @@ void load_msg_to_python(String* msgName, String* msg, int numOfMsg) { } } + void receive_msg_from_python() { String msg = ""; @@ -35,7 +39,7 @@ void receive_msg_from_python() { } -String received_msg() { +String latest_received_msg() { return rawMsgFromPython; } @@ -63,19 +67,10 @@ void init_python_communication() { } -void echo_python() { - String echo = "<echo_pytyon;"; - echo.concat(received_msg()); - echo.concat(":>"); - - Serial.println(echo); -} - - void sync() { receive_msg_from_python(); String final_payload = "<echo_python;"; - final_payload.concat(received_msg()); + final_payload.concat(latest_received_msg()); final_payload.concat(":"); final_payload.concat(payload); final_payload.concat(">"); diff --git a/pyComms/pyCommsLib.h b/pyComms/pyCommsLib.h index 1da9eb6..75ee2f4 100644 --- a/pyComms/pyCommsLib.h +++ b/pyComms/pyCommsLib.h @@ -8,7 +8,7 @@ void load_msg_to_python(String* msgName, String* msg, int numOfMsg); -String received_msg(); +String latest_received_msg(); void init_python_communication(); -- GitLab