diff --git a/README.md b/README.md index 7ffada3e33fc17bb47872528ae74b4beff2f6a23..e4833c8b956d3042da7caf8e05f419cd8fca68d4 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,11 @@ This project is very much in its early stages, so some bugs are expected. - Send mulitple messages with specified messages names from the Arduino side to the Python side (e.g.: can be used to transfer multiple sensor values, status messages, timestamps, etc) - Simplified keyboard keypress reading functionality to test your mechatronic setup + +### Points to bare in mind +- For now, you can only send a single string to the Arduino side. +- The Arduino stores the **most recent message** received from the Python side. For now, there is no buffer or queue of messages (in order to simplify and increase the robustness the communication channel from edge cases). + --- ## How to use this tool diff --git a/pyComms/pyComms.ino b/pyComms/pyComms.ino index d6b3d9491a5581a401f16c80ae4e82f5c481f295..8aea6066851e0df47d3775fb34e86a86d644dcda 100644 --- a/pyComms/pyComms.ino +++ b/pyComms/pyComms.ino @@ -1,28 +1,61 @@ #include "pyCommsLib.h" - +/* +Define the message names you want send from the Arduino side +to the Python side. +The names must exactly match what is defined on the Python side. +*/ String msgName[] = {"msgA", "msgB"}; + + +/* +Define the C arry which will carry the data content. +The number (here it's 2) must match the number of messages you have defined. +*/ String dataCarrier[2]; void setup() { - // Start the serial communication + // Start the serial communication. The baudrate is arbiturary. Serial.begin(115200); - // Connect with the python script. This will perform a handshake + // Connect with the Python side init_python_communication(); } void loop() { - + + // This is how you access the latest received message from the Python side String received_message = latest_received_msg(); + /* + Populate the dataCarrier array with your data + Here we have some temporary data/messages. + You must ALWAYS send strings and no other variable type. + You can convert anything to a string by calling String(); + */ dataCarrier[0] = "Some message"; dataCarrier[1] = String(3); + + /* + * This command "loads" the message. It doesn't send the message yet. + */ load_msg_to_python(msgName, dataCarrier, size_of_array(msgName)); - + + + /* + * The sync() function must be declared at every loop of your arduino code + * sync() will + * a) Receive messages from the Python side + * b) Send loaded messages to the Python side + */ sync(); + /* + * This delay is not critial. + * It is here to show that having a process that blocks the loop can be used. + * e.g.: a sensor with a very slow sampling speed. + */ delay(100); }