Skip to content

Commit 1718c4e

Browse files
committed
CayenneLPP class
1 parent d957c89 commit 1718c4e

File tree

2 files changed

+312
-0
lines changed

2 files changed

+312
-0
lines changed

src/CayenneLPP.cpp

Lines changed: 239 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,239 @@
1+
// Adapted from https://developer.mbed.org/teams/myDevicesIoT/code/Cayenne-LPP/
2+
3+
// Copyright © 2017 The Things Network
4+
// Use of this source code is governed by the MIT license that can be found in the LICENSE file.
5+
6+
#include "CayenneLPP.h"
7+
8+
CayenneLPP::CayenneLPP(uint8_t size) : maxsize(size)
9+
{
10+
buffer = (uint8_t *)malloc(size);
11+
cursor = 0;
12+
}
13+
14+
CayenneLPP::~CayenneLPP(void)
15+
{
16+
free(buffer);
17+
}
18+
19+
void CayenneLPP::reset(void)
20+
{
21+
cursor = 0;
22+
}
23+
24+
uint8_t CayenneLPP::getSize(void)
25+
{
26+
return cursor;
27+
}
28+
29+
uint8_t *CayenneLPP::getBuffer(void)
30+
{
31+
// uint8_t[cursor] result;
32+
// memcpy(result, buffer, cursor);
33+
// return result;
34+
return buffer;
35+
}
36+
37+
uint8_t CayenneLPP::copy(uint8_t *dst)
38+
{
39+
memcpy(dst, buffer, cursor);
40+
return cursor;
41+
}
42+
43+
uint8_t CayenneLPP::addDigitalInput(uint8_t channel, uint8_t value)
44+
{
45+
if ((cursor + LPP_DIGITAL_INPUT_SIZE) > maxsize)
46+
{
47+
return 0;
48+
}
49+
buffer[cursor++] = channel;
50+
buffer[cursor++] = LPP_DIGITAL_INPUT;
51+
buffer[cursor++] = value;
52+
53+
return cursor;
54+
}
55+
56+
uint8_t CayenneLPP::addDigitalOutput(uint8_t channel, uint8_t value)
57+
{
58+
if ((cursor + LPP_DIGITAL_OUTPUT_SIZE) > maxsize)
59+
{
60+
return 0;
61+
}
62+
buffer[cursor++] = channel;
63+
buffer[cursor++] = LPP_DIGITAL_OUTPUT;
64+
buffer[cursor++] = value;
65+
66+
return cursor;
67+
}
68+
69+
uint8_t CayenneLPP::addAnalogInput(uint8_t channel, float value)
70+
{
71+
if ((cursor + LPP_ANALOG_INPUT_SIZE) > maxsize)
72+
{
73+
return 0;
74+
}
75+
76+
int16_t val = value * 100;
77+
buffer[cursor++] = channel;
78+
buffer[cursor++] = LPP_ANALOG_INPUT;
79+
buffer[cursor++] = val >> 8;
80+
buffer[cursor++] = val;
81+
82+
return cursor;
83+
}
84+
85+
uint8_t CayenneLPP::addAnalogOutput(uint8_t channel, float value)
86+
{
87+
if ((cursor + LPP_ANALOG_OUTPUT_SIZE) > maxsize)
88+
{
89+
return 0;
90+
}
91+
int16_t val = value * 100;
92+
buffer[cursor++] = channel;
93+
buffer[cursor++] = LPP_ANALOG_OUTPUT;
94+
buffer[cursor++] = val >> 8;
95+
buffer[cursor++] = val;
96+
97+
return cursor;
98+
}
99+
100+
uint8_t CayenneLPP::addLuminosity(uint8_t channel, uint16_t lux)
101+
{
102+
if ((cursor + LPP_LUMINOSITY_SIZE) > maxsize)
103+
{
104+
return 0;
105+
}
106+
buffer[cursor++] = channel;
107+
buffer[cursor++] = LPP_LUMINOSITY;
108+
buffer[cursor++] = lux >> 8;
109+
buffer[cursor++] = lux;
110+
111+
return cursor;
112+
}
113+
114+
uint8_t CayenneLPP::addPresence(uint8_t channel, uint8_t value)
115+
{
116+
if ((cursor + LPP_PRESENCE_SIZE) > maxsize)
117+
{
118+
return 0;
119+
}
120+
buffer[cursor++] = channel;
121+
buffer[cursor++] = LPP_PRESENCE;
122+
buffer[cursor++] = value;
123+
124+
return cursor;
125+
}
126+
127+
uint8_t CayenneLPP::addTemperature(uint8_t channel, float celsius)
128+
{
129+
if ((cursor + LPP_TEMPERATURE_SIZE) > maxsize)
130+
{
131+
return 0;
132+
}
133+
int16_t val = celsius * 10;
134+
buffer[cursor++] = channel;
135+
buffer[cursor++] = LPP_TEMPERATURE;
136+
buffer[cursor++] = val >> 8;
137+
buffer[cursor++] = val;
138+
139+
return cursor;
140+
}
141+
142+
uint8_t CayenneLPP::addRelativeHumidity(uint8_t channel, float rh)
143+
{
144+
if ((cursor + LPP_RELATIVE_HUMIDITY_SIZE) > maxsize)
145+
{
146+
return 0;
147+
}
148+
buffer[cursor++] = channel;
149+
buffer[cursor++] = LPP_RELATIVE_HUMIDITY;
150+
buffer[cursor++] = rh * 2;
151+
152+
return cursor;
153+
}
154+
155+
uint8_t CayenneLPP::addAccelerometer(uint8_t channel, float x, float y, float z)
156+
{
157+
if ((cursor + LPP_ACCELEROMETER_SIZE) > maxsize)
158+
{
159+
return 0;
160+
}
161+
int16_t vx = x * 1000;
162+
int16_t vy = y * 1000;
163+
int16_t vz = z * 1000;
164+
165+
buffer[cursor++] = channel;
166+
buffer[cursor++] = LPP_ACCELEROMETER;
167+
buffer[cursor++] = vx >> 8;
168+
buffer[cursor++] = vx;
169+
buffer[cursor++] = vy >> 8;
170+
buffer[cursor++] = vy;
171+
buffer[cursor++] = vz >> 8;
172+
buffer[cursor++] = vz;
173+
174+
return cursor;
175+
}
176+
177+
uint8_t CayenneLPP::addBarometricPressure(uint8_t channel, float hpa)
178+
{
179+
if ((cursor + LPP_BAROMETRIC_PRESSURE_SIZE) > maxsize)
180+
{
181+
return 0;
182+
}
183+
int16_t val = hpa * 10;
184+
185+
buffer[cursor++] = channel;
186+
buffer[cursor++] = LPP_BAROMETRIC_PRESSURE;
187+
buffer[cursor++] = val >> 8;
188+
buffer[cursor++] = val;
189+
190+
return cursor;
191+
}
192+
193+
uint8_t CayenneLPP::addGyrometer(uint8_t channel, float x, float y, float z)
194+
{
195+
if ((cursor + LPP_GYROMETER_SIZE) > maxsize)
196+
{
197+
return 0;
198+
}
199+
int16_t vx = x * 100;
200+
int16_t vy = y * 100;
201+
int16_t vz = z * 100;
202+
203+
buffer[cursor++] = channel;
204+
buffer[cursor++] = LPP_GYROMETER;
205+
buffer[cursor++] = vx >> 8;
206+
buffer[cursor++] = vx;
207+
buffer[cursor++] = vy >> 8;
208+
buffer[cursor++] = vy;
209+
buffer[cursor++] = vz >> 8;
210+
buffer[cursor++] = vz;
211+
212+
return cursor;
213+
}
214+
215+
uint8_t CayenneLPP::addGPS(uint8_t channel, float latitude, float longitude, float meters)
216+
{
217+
if ((cursor + LPP_GPS_SIZE) > maxsize)
218+
{
219+
return 0;
220+
}
221+
int32_t lat = latitude * 10000;
222+
int32_t lon = longitude * 10000;
223+
int32_t alt = meters * 100;
224+
225+
buffer[cursor++] = channel;
226+
buffer[cursor++] = LPP_GPS;
227+
228+
buffer[cursor++] = lat >> 16;
229+
buffer[cursor++] = lat >> 8;
230+
buffer[cursor++] = lat;
231+
buffer[cursor++] = lon >> 16;
232+
buffer[cursor++] = lon >> 8;
233+
buffer[cursor++] = lon;
234+
buffer[cursor++] = alt >> 16;
235+
buffer[cursor++] = alt >> 8;
236+
buffer[cursor++] = alt;
237+
238+
return cursor;
239+
}

src/CayenneLPP.h

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Adapted from https://developer.mbed.org/teams/myDevicesIoT/code/Cayenne-LPP/
2+
3+
// Copyright © 2017 The Things Network
4+
// Use of this source code is governed by the MIT license that can be found in the LICENSE file.
5+
6+
#ifndef _CAYENNE_LPP_H_
7+
#define _CAYENNE_LPP_H_
8+
9+
#include <Arduino.h>
10+
11+
//LPP_BATTERY = // TODO Unsupported in IPSO Smart Object
12+
//LPP_PROXIMITY = // TODO Unsupported in IPSO Smart Object
13+
14+
#define LPP_DIGITAL_INPUT 0 // 1 byte
15+
#define LPP_DIGITAL_OUTPUT 1 // 1 byte
16+
#define LPP_ANALOG_INPUT 2 // 2 bytes, 0.01 signed
17+
#define LPP_ANALOG_OUTPUT 3 // 2 bytes, 0.01 signed
18+
#define LPP_LUMINOSITY 101 // 2 bytes, 1 lux unsigned
19+
#define LPP_PRESENCE 102 // 1 byte, 1
20+
#define LPP_TEMPERATURE 103 // 2 bytes, 0.1°C signed
21+
#define LPP_RELATIVE_HUMIDITY 104 // 1 byte, 0.5% unsigned
22+
#define LPP_ACCELEROMETER 113 // 2 bytes per axis, 0.001G
23+
#define LPP_BAROMETRIC_PRESSURE 115 // 2 bytes 0.1 hPa Unsigned
24+
#define LPP_GYROMETER 134 // 2 bytes per axis, 0.01 °/s
25+
#define LPP_GPS 136 // 3 byte lon/lat 0.0001 °, 3 bytes alt 0.01 meter
26+
27+
// Data ID + Data Type + Data Size
28+
#define LPP_DIGITAL_INPUT_SIZE 3 // 1 byte
29+
#define LPP_DIGITAL_OUTPUT_SIZE 3 // 1 byte
30+
#define LPP_ANALOG_INPUT_SIZE 4 // 2 bytes, 0.01 signed
31+
#define LPP_ANALOG_OUTPUT_SIZE 4 // 2 bytes, 0.01 signed
32+
#define LPP_LUMINOSITY_SIZE 4 // 2 bytes, 1 lux unsigned
33+
#define LPP_PRESENCE_SIZE 3 // 1 byte, 1
34+
#define LPP_TEMPERATURE_SIZE 4 // 2 bytes, 0.1°C signed
35+
#define LPP_RELATIVE_HUMIDITY_SIZE 3 // 1 byte, 0.5% unsigned
36+
#define LPP_ACCELEROMETER_SIZE 8 // 2 bytes per axis, 0.001G
37+
#define LPP_BAROMETRIC_PRESSURE_SIZE 4 // 2 bytes 0.1 hPa Unsigned
38+
#define LPP_GYROMETER_SIZE 8 // 2 bytes per axis, 0.01 °/s
39+
#define LPP_GPS_SIZE 11 // 3 byte lon/lat 0.0001 °, 3 bytes alt 0.01 meter
40+
41+
class CayenneLPP
42+
{
43+
public:
44+
CayenneLPP(uint8_t size);
45+
~CayenneLPP();
46+
47+
void reset(void);
48+
uint8_t getSize(void);
49+
uint8_t *getBuffer(void);
50+
uint8_t copy(uint8_t *buffer);
51+
52+
uint8_t addDigitalInput(uint8_t channel, uint8_t value);
53+
uint8_t addDigitalOutput(uint8_t channel, uint8_t value);
54+
55+
uint8_t addAnalogInput(uint8_t channel, float value);
56+
uint8_t addAnalogOutput(uint8_t channel, float value);
57+
58+
uint8_t addLuminosity(uint8_t channel, uint16_t lux);
59+
uint8_t addPresence(uint8_t channel, uint8_t value);
60+
uint8_t addTemperature(uint8_t channel, float celsius);
61+
uint8_t addRelativeHumidity(uint8_t channel, float rh);
62+
uint8_t addAccelerometer(uint8_t channel, float x, float y, float z);
63+
uint8_t addBarometricPressure(uint8_t channel, float hpa);
64+
uint8_t addGyrometer(uint8_t channel, float x, float y, float z);
65+
uint8_t addGPS(uint8_t channel, float latitude, float longitude, float meters);
66+
67+
private:
68+
uint8_t *buffer;
69+
uint8_t maxsize;
70+
uint8_t cursor;
71+
};
72+
73+
#endif

0 commit comments

Comments
 (0)