-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathOutgoing.ino
More file actions
64 lines (54 loc) · 1.57 KB
/
Outgoing.ino
File metadata and controls
64 lines (54 loc) · 1.57 KB
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
char command[20];
void SetCommand(String str) {
memset(command, 0, sizeof(command));
if (str[0] != commandPrefix[0]) //Startswith " ! "
str = commandPrefix + str;
str.toCharArray(command, sizeof(command));
}
uint8_t GetCommandLength() {
uint8_t i = 0;
for (i = 0; i < sizeof(command); i++)
if (command[i] == 0x00 || command[i] == 0x0D) //Empty or CarriageReturn
break;
return i;
}
uint8_t GetCommandLengthWithFooter() {
return GetCommandLength() + sizeof(packetFooterOutgoing);
}
uint8_t GetMessageLength() {
return GetCommandLengthWithFooter() + sizeof(packetHeader);
}
void CreateCommand(uint8_t bts[]){
//Set Header
memcpy(bts, packetHeader, sizeof(packetHeader));
uint8_t commandLenghWithFooter = GetCommandLengthWithFooter();
//Set length
bts[lenghtBytePosHigh] = highByte(commandLenghWithFooter);// / 256;
bts[lenghtBytePosLow] = lowByte(commandLenghWithFooter); // % 256;
//Set command
memcpy(bts + sizeof(packetHeader), command, GetCommandLength());
//Set Footer
memcpy(bts + sizeof(packetHeader) + GetCommandLength(), packetFooterOutgoing, sizeof(packetFooterOutgoing));
}
void SendCommand(String str) {
SetCommand(str);
//Bytes to send:
uint8_t bts[GetMessageLength()];
CreateCommand(bts);
#if DEBUG
Serial.print("Out: ");
// for (int i = 0; i < GetMessageLength(); i++)
// Serial.write(bts[i]);
// Serial.println();
Serial.println(command);
#endif
SendDataToDevice(bts);
}
String getHex(uint8_t data) {
String str;
if (data < 0x10)
str = "0";
str += String(data, HEX);
str.toUpperCase();
return str;
}