|
| 1 | +/* |
| 2 | +* Parts of a message: |
| 3 | +* S~ - Pass away |
| 4 | +* S - Start of message |
| 5 | +* . - End of message |
| 6 | +* |
| 7 | +* An example message: |
| 8 | +* "S0|o7|h7|." - For all arduino (0), set 7 to OUPUT (o7), and turn it on (h7) |
| 9 | +* "S1|o1|h1|o2|l2|." - |
| 10 | +* For the arduino numbered with 1 (1), set 1 to OUPUT (o1), and turn it on (h1), |
| 11 | +* then set 2 to OUTPUT (o2), and turn it off (l2) |
| 12 | +*/ |
| 13 | +#define AC_ID 1 |
| 14 | + |
| 15 | + |
| 16 | +void ac_listen(int baudrate=9600) { |
| 17 | + if (Serial.available() <= 0) return; |
| 18 | + String got = Serial.readStringUntil('.'); |
| 19 | + |
| 20 | + if (got[0] == 'S') { |
| 21 | + got = got.substring(1, got.length()); |
| 22 | + } else { |
| 23 | + return; |
| 24 | + } |
| 25 | + |
| 26 | + if (got[0] == '~') { // Pass it away without processing |
| 27 | + Serial.write(('S' + got + '.').c_str()); |
| 28 | + return; |
| 29 | + } |
| 30 | + else if ((int)got[0] == 0) { // Pass it away, but process command |
| 31 | + Serial.write(('S' + got + '.').c_str()); |
| 32 | + } |
| 33 | + else if ((int)got[0] != AC_ID) { // Not relevant for this board, pass it away |
| 34 | + Serial.write(('S' + got + '.').c_str()); |
| 35 | + } |
| 36 | + |
| 37 | + if (got[0] == '0' || got[0] == AC_ID) { |
| 38 | + got.replace('.', ' '); |
| 39 | + got.trim(); |
| 40 | + got.toLowerCase(); |
| 41 | + ac_decode(got.substring(2, got.length())); |
| 42 | + |
| 43 | + Serial.end(); |
| 44 | + Serial.begin(baudrate); |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | + |
| 49 | +// String got = commands separated with '|' |
| 50 | +void ac_decode(String got) { |
| 51 | + String cmd_part = ""; |
| 52 | + |
| 53 | + for (uint16_t i = 0; i < got.length(); i++) { |
| 54 | + if (got[i] == '|') { |
| 55 | + ac_use_cmd(cmd_part); |
| 56 | + cmd_part = ""; |
| 57 | + continue; |
| 58 | + } |
| 59 | + |
| 60 | + cmd_part += got[i]; |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +/* |
| 65 | + Examples: |
| 66 | + "1|o13|h13" -> The 1 ID board | pinMode(13, OUTPUT) | digitalWrite(13, HIGH) |
| 67 | + "0|ao1|ah1" -> For all board | pinMode(A1, OUTPUT) | analogWrite(A1, HIGH) |
| 68 | + "0|ao1|ap2|ah1" -> For all board | pinMode(A1, OUTPUT) | pinMode(A2, INPUT_PULLUP) | analogWrite(A1, HIGH) |
| 69 | +*/ |
| 70 | +void ac_use_cmd(String command) { |
| 71 | + if (command == "" || command == '|' || command == '.') return; |
| 72 | + |
| 73 | + if (command[0] == 'o') { |
| 74 | + pinMode(command.substring(1, command.length()).toInt(), OUTPUT); |
| 75 | + } |
| 76 | + else if (command[0] == 'i') { |
| 77 | + pinMode(command.substring(1, command.length()).toInt(), INPUT); |
| 78 | + } |
| 79 | + else if (command[0] == 'p') { |
| 80 | + pinMode(command.substring(1, command.length()).toInt(), INPUT_PULLUP); |
| 81 | + } |
| 82 | + else if (command[0] == 'h') { |
| 83 | + digitalWrite(command.substring(1, command.length()).toInt(), HIGH); |
| 84 | + } |
| 85 | + else if (command[0] == 'l') { |
| 86 | + digitalWrite(command.substring(1, command.length()).toInt(), LOW); |
| 87 | + } |
| 88 | + else if (command[0] == 'a') { |
| 89 | + if (command[1] == 'o') { |
| 90 | + pinMode(command.substring(2, command.length()).toInt() + 14, OUTPUT); |
| 91 | + } |
| 92 | + else if (command[1] == 'i') { |
| 93 | + pinMode(command.substring(2, command.length()).toInt() + 14, INPUT); |
| 94 | + } |
| 95 | + else if (command[1] == 'p') { |
| 96 | + pinMode(command.substring(2, command.length()).toInt() + 14, INPUT_PULLUP); |
| 97 | + } |
| 98 | + else if (command[1] == 'h') { |
| 99 | + analogWrite(command.substring(2, command.length()).toInt() + 14, HIGH); |
| 100 | + } |
| 101 | + else if (command[1] == 'l') { |
| 102 | + analogWrite(command.substring(2, command.length()).toInt() + 14, LOW); |
| 103 | + } |
| 104 | + } |
| 105 | +} |
| 106 | + |
0 commit comments