|
| 1 | +////////////////////////////////////////////////////////////////////////////// |
| 2 | +////////////////////////////////////////////////////////////////////////////// |
| 3 | + |
| 4 | +// This file contains functions that help the arduino communitate with the |
| 5 | +// raspberry pi |
| 6 | + |
| 7 | +////////////////////////////////////////////////////////////////////////////// |
| 8 | +////////////////////////////////////////////////////////////////////////////// |
| 9 | + |
| 10 | +// handles the receiving of data over the serial port |
| 11 | +void RecvWithStartEndMarkers() |
| 12 | +{ |
| 13 | + static boolean recvInProgress = false; |
| 14 | + static byte ndx = 0; |
| 15 | + char startMarker = '<'; |
| 16 | + char endMarker = '>'; |
| 17 | + char rc; |
| 18 | + |
| 19 | + while (Serial.available() > 0 && new_data == false) |
| 20 | + { |
| 21 | + rc = Serial.read(); |
| 22 | + |
| 23 | + if (recvInProgress == true) |
| 24 | + { |
| 25 | + if (rc != endMarker) |
| 26 | + { |
| 27 | + received_chars[ndx] = rc; |
| 28 | + ndx++; |
| 29 | + if (ndx >= num_chars) |
| 30 | + { |
| 31 | + ndx = num_chars - 1; |
| 32 | + } |
| 33 | + } |
| 34 | + else |
| 35 | + { |
| 36 | + received_chars[ndx] = '\0'; // terminate the string |
| 37 | + recvInProgress = false; |
| 38 | + ndx = 0; |
| 39 | + new_data = true; |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + else if (rc == startMarker) |
| 44 | + { |
| 45 | + recvInProgress = true; |
| 46 | + } |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +// helps get a particular value from the incoming data string |
| 51 | +String getValue(String data, char separator, int index) |
| 52 | +{ |
| 53 | + int found = 0; |
| 54 | + int strIndex[] = {0, -1}; |
| 55 | + int maxIndex = data.length() - 1; |
| 56 | + |
| 57 | + for (int i = 0; i <= maxIndex && found <= index; i++) |
| 58 | + { |
| 59 | + if (data.charAt(i) == separator || i == maxIndex) |
| 60 | + { |
| 61 | + found++; |
| 62 | + strIndex[0] = strIndex[1] + 1; |
| 63 | + strIndex[1] = (i == maxIndex) ? i + 1 : i; |
| 64 | + } |
| 65 | + } |
| 66 | + return found > index ? data.substring(strIndex[0], strIndex[1]) : ""; |
| 67 | +} |
| 68 | + |
| 69 | +// sends message back to raspberry pi saying the command has been executed |
| 70 | +void Finished() |
| 71 | +{ |
| 72 | + Serial.println("Finished Current Job!"); |
| 73 | + Serial.println(">"); |
| 74 | +} |
| 75 | + |
| 76 | +// sends error message back to raspberry pi |
| 77 | +void Invalid() |
| 78 | +{ |
| 79 | + Serial.println("<"); |
| 80 | + Serial.print("Arduino: "); |
| 81 | + Serial.println("Invalid Input!"); |
| 82 | + Serial.println(">"); |
| 83 | +} |
0 commit comments