1 #include "stdafx.h"
2 #include "CayenneLPP.h"
3 #include <memory>
4 
CayenneLPP(uint8_t size)5 CayenneLPP::CayenneLPP(uint8_t size) : maxsize(size) {
6     buffer = (uint8_t*) malloc(size);
7     cursor = 0;
8 }
9 
~CayenneLPP(void)10 CayenneLPP::~CayenneLPP(void) {
11     free(buffer);
12 }
13 
reset(void)14 void CayenneLPP::reset(void) {
15     cursor = 0;
16 }
17 
getSize(void)18 uint8_t CayenneLPP::getSize(void) {
19     return cursor;
20 }
21 
getBuffer(void)22 uint8_t* CayenneLPP::getBuffer(void) {
23 //    uint8_t[cursor] result;
24 //    memcpy(result, buffer, cursor);
25 //    return result;
26     return buffer;
27 }
28 
copy(uint8_t * dst)29 uint8_t CayenneLPP::copy(uint8_t* dst) {
30     memcpy(dst, buffer, cursor);
31     return cursor;
32 }
33 
addDigitalInput(uint8_t channel,uint8_t value)34 uint8_t CayenneLPP::addDigitalInput(uint8_t channel, uint8_t value) {
35     if ((cursor + LPP_DIGITAL_INPUT_SIZE) > maxsize) {
36         return 0;
37     }
38     buffer[cursor++] = channel;
39     buffer[cursor++] = LPP_DIGITAL_INPUT;
40     buffer[cursor++] = value;
41 
42     return cursor;
43 }
44 
addDigitalOutput(uint8_t channel,uint8_t value)45 uint8_t CayenneLPP::addDigitalOutput(uint8_t channel, uint8_t value) {
46     if ((cursor + LPP_DIGITAL_OUTPUT_SIZE) > maxsize) {
47         return 0;
48     }
49     buffer[cursor++] = channel;
50     buffer[cursor++] = LPP_DIGITAL_OUTPUT;
51     buffer[cursor++] = value;
52 
53     return cursor;
54 }
55 
addAnalogInput(uint8_t channel,float value)56 uint8_t CayenneLPP::addAnalogInput(uint8_t channel, float value) {
57     if ((cursor + LPP_ANALOG_INPUT_SIZE) > maxsize) {
58         return 0;
59     }
60 
61     int16_t val = value * 100;
62     buffer[cursor++] = channel;
63     buffer[cursor++] = LPP_ANALOG_INPUT;
64     buffer[cursor++] = val >> 8;
65     buffer[cursor++] = val;
66 
67     return cursor;
68 }
69 
addAnalogOutput(uint8_t channel,float value)70 uint8_t CayenneLPP::addAnalogOutput(uint8_t channel, float value) {
71     if ((cursor + LPP_ANALOG_OUTPUT_SIZE) > maxsize) {
72         return 0;
73     }
74     int16_t val = value * 100;
75     buffer[cursor++] = channel;
76     buffer[cursor++] = LPP_ANALOG_OUTPUT;
77     buffer[cursor++] = val >> 8;
78     buffer[cursor++] = val;
79 
80     return cursor;
81 }
82 
addLuminosity(uint8_t channel,uint16_t lux)83 uint8_t CayenneLPP::addLuminosity(uint8_t channel, uint16_t lux) {
84     if ((cursor + LPP_LUMINOSITY_SIZE) > maxsize) {
85         return 0;
86     }
87     buffer[cursor++] = channel;
88     buffer[cursor++] = LPP_LUMINOSITY;
89     buffer[cursor++] = lux >> 8;
90     buffer[cursor++] = lux;
91 
92     return cursor;
93 }
94 
addPresence(uint8_t channel,uint8_t value)95 uint8_t CayenneLPP::addPresence(uint8_t channel, uint8_t value) {
96     if ((cursor + LPP_PRESENCE_SIZE) > maxsize) {
97         return 0;
98     }
99     buffer[cursor++] = channel;
100     buffer[cursor++] = LPP_PRESENCE;
101     buffer[cursor++] = value;
102 
103     return cursor;
104 }
105 
addTemperature(uint8_t channel,float celsius)106 uint8_t CayenneLPP::addTemperature(uint8_t channel, float celsius) {
107     if ((cursor + LPP_TEMPERATURE_SIZE) > maxsize) {
108         return 0;
109     }
110     int16_t val = celsius * 10;
111     buffer[cursor++] = channel;
112     buffer[cursor++] = LPP_TEMPERATURE;
113     buffer[cursor++] = val >> 8;
114     buffer[cursor++] = val;
115 
116     return cursor;
117 }
118 
addRelativeHumidity(uint8_t channel,float rh)119 uint8_t CayenneLPP::addRelativeHumidity(uint8_t channel, float rh) {
120     if ((cursor + LPP_RELATIVE_HUMIDITY_SIZE) > maxsize) {
121         return 0;
122     }
123     buffer[cursor++] = channel;
124     buffer[cursor++] = LPP_RELATIVE_HUMIDITY;
125     buffer[cursor++] = rh * 2;
126 
127     return cursor;
128 }
129 
addAccelerometer(uint8_t channel,float x,float y,float z)130 uint8_t CayenneLPP::addAccelerometer(uint8_t channel, float x, float y, float z) {
131     if ((cursor + LPP_ACCELEROMETER_SIZE) > maxsize) {
132         return 0;
133     }
134     int16_t vx = x * 1000;
135     int16_t vy = y * 1000;
136     int16_t vz = z * 1000;
137 
138     buffer[cursor++] = channel;
139     buffer[cursor++] = LPP_ACCELEROMETER;
140     buffer[cursor++] = vx >> 8;
141     buffer[cursor++] = vx;
142     buffer[cursor++] = vy >> 8;
143     buffer[cursor++] = vy;
144     buffer[cursor++] = vz >> 8;
145     buffer[cursor++] = vz;
146 
147     return cursor;
148 }
149 
addBarometricPressure(uint8_t channel,float hpa)150 uint8_t CayenneLPP::addBarometricPressure(uint8_t channel, float hpa) {
151     if ((cursor + LPP_BAROMETRIC_PRESSURE_SIZE) > maxsize) {
152         return 0;
153     }
154     int16_t val = hpa * 10;
155 
156     buffer[cursor++] = channel;
157     buffer[cursor++] = LPP_BAROMETRIC_PRESSURE;
158     buffer[cursor++] = val >> 8;
159     buffer[cursor++] = val;
160 
161     return cursor;
162 }
163 
addGyrometer(uint8_t channel,float x,float y,float z)164 uint8_t CayenneLPP::addGyrometer(uint8_t channel, float x, float y, float z) {
165     if ((cursor + LPP_GYROMETER_SIZE) > maxsize) {
166         return 0;
167     }
168     int16_t vx = x * 100;
169     int16_t vy = y * 100;
170     int16_t vz = z * 100;
171 
172     buffer[cursor++] = channel;
173     buffer[cursor++] = LPP_GYROMETER;
174     buffer[cursor++] = vx >> 8;
175     buffer[cursor++] = vx;
176     buffer[cursor++] = vy >> 8;
177     buffer[cursor++] = vy;
178     buffer[cursor++] = vz >> 8;
179     buffer[cursor++] = vz;
180 
181     return cursor;
182 }
183 
addGPS(uint8_t channel,float latitude,float longitude,float meters)184 uint8_t CayenneLPP::addGPS(uint8_t channel, float latitude, float longitude, float meters) {
185     if ((cursor + LPP_GPS_SIZE) > maxsize) {
186         return 0;
187     }
188     int32_t lat = latitude * 10000;
189     int32_t lon = longitude * 10000;
190     int32_t alt = meters * 100;
191 
192     buffer[cursor++] = channel;
193     buffer[cursor++] = LPP_GPS;
194 
195     buffer[cursor++] = lat >> 16;
196     buffer[cursor++] = lat >> 8;
197     buffer[cursor++] = lat;
198     buffer[cursor++] = lon >> 16;
199     buffer[cursor++] = lon >> 8;
200     buffer[cursor++] = lon;
201     buffer[cursor++] = alt >> 16;
202     buffer[cursor++] = alt >> 8;
203     buffer[cursor++] = alt;
204 
205     return cursor;
206 }
207 
208