Skip to content
Snippets Groups Projects
Commit 1bf18735 authored by Kai Junge's avatar Kai Junge
Browse files

Functionality changes before final testing before initial release.

parent 5987a426
Branches
Tags
No related merge requests found
......@@ -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)
......
......@@ -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
#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);
}
#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(">");
......
......@@ -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();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment