forked from iot-playground/Arduino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEsp8266EasyIoTMsg.cpp
151 lines (121 loc) · 3.06 KB
/
Esp8266EasyIoTMsg.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
V1.4 - upgraded to ESP8266 0.952 firmware version
V1.3 - additional data types
V1.1 - additional data types
V1.0 - first version
Created by Igor Jarc
See http://iot-playground.com for details
Please use community fourum on website do not contact author directly
Code based on MySensors datatypes and API. http://www.mysensors.org
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
*/
#include "Esp8266EasyIoTMsg.h"
Esp8266EasyIoTMsg::Esp8266EasyIoTMsg() {
start = START_MSG;
version = PROTOCOL_VERSION;
}
Esp8266EasyIoTMsg::Esp8266EasyIoTMsg(uint8_t _sensor, uint8_t _type) {
start = START_MSG;
version = PROTOCOL_VERSION;
sensor = _sensor;
type = _type;
}
Esp8266EasyIoTMsg& Esp8266EasyIoTMsg::set(const char* value) {
uint8_t len = strlen(value);
length = len;
miSetPayloadType(P_STRING);
strncpy(data, value, min(length, MAX_PAYLOAD));
return *this;
}
Esp8266EasyIoTMsg& Esp8266EasyIoTMsg::set(uint8_t value) {
length = 1;
miSetPayloadType(P_BYTE);
data[0] = value;
return *this;
}
Esp8266EasyIoTMsg& Esp8266EasyIoTMsg::set(float value, uint8_t decimals) {
length = 5; // 32 bit float + persi
miSetPayloadType(P_FLOAT32);
fValue=value;
fPrecision = decimals;
return *this;
}
uint16_t Esp8266EasyIoTMsg::getUInt() const {
if (miGetPayloadType() == P_UINT16) {
return uiValue;
} else if (miGetPayloadType() == P_STRING) {
return atoi(data);
} else {
return 0;
}
}
int Esp8266EasyIoTMsg::getInt() const {
if (miGetPayloadType() == P_INT16) {
return iValue;
} else if (miGetPayloadType() == P_STRING) {
return atoi(data);
} else {
return 0;
}
}
bool Esp8266EasyIoTMsg::getBool() const {
return getInt();
}
float Esp8266EasyIoTMsg::getFloat() const {
if (miGetPayloadType() == P_FLOAT32) {
return fValue;
} else if (miGetPayloadType() == P_STRING) {
return atof(data);
} else {
return 0;
}
}
unsigned long Esp8266EasyIoTMsg::getULong() const {
if (miGetPayloadType() == P_ULONG32) {
return ulValue;
} else if (miGetPayloadType() == P_STRING) {
return atol(data);
} else {
return 0;
}
}
/*
* calculate CRC8
*/
void Esp8266EasyIoTMsg::crc8() {
crc = calculateCrc8();
}
uint8_t Esp8266EasyIoTMsg::calculateCrc8() {
uint8_t loop_count;
uint8_t bit_counter;
uint8_t dt;
uint8_t feedback_bit;
uint8_t tmpcrc;
uint8_t number_of_bytes_to_read = HEADER_SIZE + length;
// Must set crc to a constant value.
tmpcrc = crc;
crc = 0;
uint8_t extcrc = 0;
uint8_t* buff = reinterpret_cast<uint8_t*>(&start);
for (loop_count = 0; loop_count != number_of_bytes_to_read; loop_count++)
{
dt = (uint8_t)buff[loop_count];
bit_counter = 8;
do {
feedback_bit = (extcrc ^ dt) & 0x01;
if ( feedback_bit == 0x01 ) {
extcrc = extcrc ^ 0x18; //0X18 = X^8+X^5+X^4+X^0
}
extcrc = (extcrc >> 1) & 0x7F;
if ( feedback_bit == 0x01 ) {
extcrc = extcrc | 0x80;
}
dt = dt >> 1;
bit_counter--;
} while (bit_counter > 0);
}
crc = tmpcrc;
return extcrc;
}