-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelpers.ino
75 lines (64 loc) · 1.47 KB
/
helpers.ino
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
#include <CRCx.h>
void _yield() {
httpServer.handleClient();
yield();
mqtt.loop();
}
uint16_t calcCRC(uint8_t * arr, uint8_t size) {
uint16_t CRC = crcx::crc16(arr, size);
return CRC;
}
bool valid_msg() {
if (msg.size() < 8) return false;
// Copy buffer
uint8_t sz = msg.size() - 2;
uint8_t tmp[sz];
for (uint8_t i = 0; i < sz; i++) {
tmp[i] = msg[i];
}
#if false
uint16_t CRC = crcx::crc16(tmp, sizeof(tmp));
if (CRC == 0x00) return true;
else false;
#else
// Calculate and extract CRCs
uint16_t CRC = crcx::crc16(tmp, sizeof(tmp));
uint16_t msgCRC = word(msg[msg.size() - 1], msg[msg.size() - 2]);
// Compare and return
if (CRC == msgCRC) return true;
else return false;
#endif
}
float getFloat(byte x) {
float tmp;
tmp = word(msg[x], msg[x + 1]);
tmp -= 300;
tmp /= 10;
return tmp;
}
void sendmsg(uint8_t * message, uint8_t len) {
String s;
uint8_t x;
uint16_t CRC = calcCRC(message, len);
digitalWrite(POOLSTAR_RTS, HIGH);
while (millis() < (lastCharTime + 19)) _yield();
//delay(1);
PS.flush();
yield();
for (uint8_t i = 0; i < len; i++) {
x = message[i];
if (x < 0x10) s += "0";
s += String(x, HEX);
s += " ";
PS.write(x);
PS.flush();
yield();
}
PS.write(lowByte(CRC));
PS.write(highByte(CRC));
yield();
PS.flush();
digitalWrite(POOLSTAR_RTS, LOW);
PUB("POOLSTAR/msg", s.c_str());
_yield();
}