forked from Atlas-Scientific/Ezo_I2c_lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iot_cmd.cpp
95 lines (81 loc) · 4.72 KB
/
iot_cmd.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include "iot_cmd.h"
#include <Ezo_i2c_util.h>
bool receive_command(String &string_buffer){
if (Serial.available()) { //if theres any characters in the UART buffer
string_buffer = Serial.readString(); //get them until its a complete command
Serial.print("> "); //print whatever we received
Serial.println(string_buffer);
string_buffer.toUpperCase(); //turn the command to uppercase for easier comparisions
string_buffer.trim(); //remove all extra spaces and newlines
return true;
}
return false;
}
// determines how long we wait depending on the command
void select_delay(const String &str) {
if (str.indexOf("CAL") != -1 || str.indexOf("R") != -1) {
delay(1200);
} else {
delay(300);
}
}
void process_command(const String &string_buffer, Ezo_board device_list[], uint8_t device_list_len, Ezo_board* &default_board){
if (string_buffer == "LIST") { //if our command is list
list_devices(device_list, device_list_len, default_board);
}
//the all command sends a command to all available boards and shows their responses
else if (string_buffer.startsWith("ALL:")) { //if the command starts with ALL:
String cmd = string_buffer.substring(string_buffer.indexOf(':') + 1); //get the rest of the command after the : character
for (uint8_t i = 0; i < device_list_len; i++) { //then send it to every board
device_list[i].send_cmd(cmd.c_str());
}
select_delay(cmd); //wait for some time depending on the command
for (uint8_t i = 0; i < device_list_len; i++) { //go through our list of boards and get their response
receive_and_print_response(device_list[i]);
}
}
// all other commands are passed through to the default board unless they have an address prepended
else if (string_buffer != "" ) { //if we received any other commands
int16_t index = string_buffer.indexOf(':'); //check if the command contains a colon character, which is used for changing addresses
if (index != -1) { //if it contains a colon character
bool addr_found = false;
String name_to_find = string_buffer.substring(0, index); //get the address out of the command
name_to_find.toUpperCase();
if (name_to_find.length() != 0) { //if its valid
//search through list and make device match the address
for (uint8_t i = 0; i < device_list_len; i++) {
if (name_to_find == device_list[i].get_name()) { //if the address matches one of the boards in the list
default_board = &device_list[i]; //set that board as the default
addr_found = true; //indicate we changed the address
break; //and exit the loop
}
}
if (addr_found) { //then send the rest of the command to that board
default_board->send_cmd(string_buffer.substring(index + 1).c_str());
} else { //otherwise print that we didnt find
Serial.print("No device named ");
Serial.println(name_to_find);
return;
}
} else {
Serial.println("Invalid name");
}
}
else { //if theres no colon just pass the command to the default board
default_board->send_cmd(string_buffer.c_str());
}
select_delay(string_buffer); //wait for some time depending on the command
receive_and_print_response(*default_board);
}
}
void list_devices(Ezo_board device_list[], uint8_t device_list_len, Ezo_board* default_board) {
for (uint8_t i = 0; i < device_list_len; i++) { //go thorugh the list of boards
if (default_board == &device_list[i]) { //if its our default board
Serial.print("--> "); //print the pointer arrow
} else { //otherwise
Serial.print(" - "); //print a normal dash
}
print_device_info(device_list[i]); //then print the boards info
Serial.println("");
}
}