206 lines
3.8 KiB
C++
206 lines
3.8 KiB
C++
// Adapted from https://developer.mbed.org/teams/myDevicesIoT/code/Cayenne-LPP/
|
|
|
|
// Copyright © 2017 The Things Network
|
|
// Use of this source code is governed by the MIT license that can be found in the LICENSE file.
|
|
|
|
#include "ETCPLPP.h"
|
|
|
|
ETCPLPP::ETCPLPP(uint8_t size) : maxsize(size)
|
|
{
|
|
|
|
buffer = (uint8_t *)malloc(size);
|
|
cursor = 0;
|
|
}
|
|
|
|
ETCPLPP::~ETCPLPP(void)
|
|
{
|
|
|
|
if (buffer)
|
|
free(buffer);
|
|
}
|
|
|
|
void ETCPLPP::reset(void)
|
|
{
|
|
cursor = 0;
|
|
}
|
|
|
|
uint8_t ETCPLPP::getSize(void)
|
|
{
|
|
return cursor;
|
|
}
|
|
|
|
uint8_t *ETCPLPP::getBuffer(void)
|
|
{
|
|
|
|
return buffer;
|
|
}
|
|
|
|
uint8_t ETCPLPP::copy(uint8_t *dst)
|
|
{
|
|
memcpy(dst, buffer, cursor);
|
|
return cursor;
|
|
}
|
|
|
|
uint8_t ETCPLPP::addDigitalInput(uint8_t channel, uint8_t value)
|
|
{
|
|
if ((cursor + LPP_DIGITAL_INPUT_SIZE) > maxsize)
|
|
{
|
|
return 0;
|
|
}
|
|
buffer[cursor++] = channel;
|
|
buffer[cursor++] = LPP_DIGITAL_INPUT;
|
|
buffer[cursor++] = value;
|
|
|
|
return cursor;
|
|
}
|
|
|
|
uint8_t ETCPLPP::addDigitalOutput(uint8_t channel, uint8_t value)
|
|
{
|
|
if ((cursor + LPP_DIGITAL_OUTPUT_SIZE) > maxsize)
|
|
{
|
|
return 0;
|
|
}
|
|
buffer[cursor++] = channel;
|
|
buffer[cursor++] = LPP_DIGITAL_OUTPUT;
|
|
buffer[cursor++] = value;
|
|
|
|
return cursor;
|
|
}
|
|
|
|
uint8_t ETCPLPP::addAnalogInput(uint8_t channel, float value)
|
|
{
|
|
if ((cursor + LPP_ANALOG_INPUT_SIZE) > maxsize)
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
int16_t val = value * 100;
|
|
buffer[cursor++] = channel;
|
|
buffer[cursor++] = LPP_ANALOG_INPUT;
|
|
buffer[cursor++] = val >> 8;
|
|
buffer[cursor++] = val;
|
|
|
|
return cursor;
|
|
}
|
|
|
|
uint8_t ETCPLPP::addAnalogOutput(uint8_t channel, float value)
|
|
{
|
|
if ((cursor + LPP_ANALOG_OUTPUT_SIZE) > maxsize)
|
|
{
|
|
return 0;
|
|
}
|
|
int16_t val = value * 100;
|
|
buffer[cursor++] = channel;
|
|
buffer[cursor++] = LPP_ANALOG_OUTPUT;
|
|
buffer[cursor++] = val >> 8;
|
|
buffer[cursor++] = val;
|
|
|
|
return cursor;
|
|
}
|
|
|
|
uint8_t ETCPLPP::addLuminosity(uint8_t channel, uint16_t lux)
|
|
{
|
|
if ((cursor + LPP_LUMINOSITY_SIZE) > maxsize)
|
|
{
|
|
return 0;
|
|
}
|
|
buffer[cursor++] = channel;
|
|
buffer[cursor++] = LPP_LUMINOSITY;
|
|
buffer[cursor++] = lux >> 8;
|
|
buffer[cursor++] = lux;
|
|
|
|
return cursor;
|
|
}
|
|
|
|
uint8_t ETCPLPP::addTemperature(uint8_t channel, float celsius)
|
|
{
|
|
if ((cursor + LPP_TEMPERATURE_SIZE) > maxsize)
|
|
{
|
|
return 0;
|
|
}
|
|
int16_t val = celsius * 10;
|
|
buffer[cursor++] = channel;
|
|
buffer[cursor++] = LPP_TEMPERATURE;
|
|
buffer[cursor++] = val >> 8;
|
|
buffer[cursor++] = val;
|
|
|
|
return cursor;
|
|
}
|
|
|
|
uint8_t ETCPLPP::addRelativeHumidity(uint8_t channel, float rh)
|
|
{
|
|
if ((cursor + LPP_RELATIVE_HUMIDITY_SIZE) > maxsize)
|
|
{
|
|
return 0;
|
|
}
|
|
buffer[cursor++] = channel;
|
|
buffer[cursor++] = LPP_RELATIVE_HUMIDITY;
|
|
buffer[cursor++] = rh * 2;
|
|
|
|
return cursor;
|
|
}
|
|
|
|
uint8_t ETCPLPP::addBarometricPressure(uint8_t channel, float hpa)
|
|
{
|
|
if ((cursor + LPP_BAROMETRIC_PRESSURE_SIZE) > maxsize)
|
|
{
|
|
return 0;
|
|
}
|
|
int16_t val = hpa * 10;
|
|
|
|
buffer[cursor++] = channel;
|
|
buffer[cursor++] = LPP_BAROMETRIC_PRESSURE;
|
|
buffer[cursor++] = val >> 8;
|
|
buffer[cursor++] = val;
|
|
|
|
return cursor;
|
|
}
|
|
|
|
uint8_t ETCPLPP::addBatteryVoltage(uint8_t channel, float voltage)
|
|
{
|
|
|
|
if ((cursor + LPP_BATTERY_VOLTAGE_SIZE) > maxsize)
|
|
{
|
|
return 0;
|
|
}
|
|
int16_t val = voltage * 1000;
|
|
|
|
buffer[cursor++] = channel;
|
|
buffer[cursor++] = LPP_BATTERY_VOLTAGE;
|
|
buffer[cursor++] = val >> 8;
|
|
buffer[cursor++] = val;
|
|
|
|
return cursor;
|
|
}
|
|
|
|
uint8_t ETCPLPP::addBatteryStatus(uint8_t channel, uint8_t percent)
|
|
{
|
|
|
|
if ((cursor + LPP_BATTERY_STATUS_SIZE) > maxsize)
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
buffer[cursor++] = channel;
|
|
buffer[cursor++] = LPP_BATTERY_STATUS;
|
|
buffer[cursor++] = percent;
|
|
|
|
return cursor;
|
|
}
|
|
|
|
uint8_t ETCPLPP::addStatus(uint8_t channel, uint16_t status)
|
|
{
|
|
|
|
if ((cursor + LPP_STATUS_SIZE) > maxsize)
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
buffer[cursor++] = channel;
|
|
buffer[cursor++] = LPP_STATUS;
|
|
buffer[cursor++] = status >> 8;
|
|
buffer[cursor++] = status;
|
|
|
|
return cursor;
|
|
}
|