From 2617c87fe79f0449b01523dedc5b203d21b8b077 Mon Sep 17 00:00:00 2001 From: Keisha Zapra Date: Thu, 25 Feb 2021 15:27:57 +0800 Subject: [PATCH 1/3] Wrap individual types in pre-processor directives to allow disabling unneeded types for space --- src/CayenneLPP.cpp | 377 +++++++++++++++++++++++++++++++++++++++++---- src/CayenneLPP.h | 245 +++++++++++++++++------------ 2 files changed, 496 insertions(+), 126 deletions(-) diff --git a/src/CayenneLPP.cpp b/src/CayenneLPP.cpp index f9b8f0b..2be162a 100644 --- a/src/CayenneLPP.cpp +++ b/src/CayenneLPP.cpp @@ -43,34 +43,86 @@ uint8_t CayenneLPP::getError() { bool CayenneLPP::isType(uint8_t type) { - switch (type) { + switch (type) { +#ifndef CAYENNE_DISABLE_DIGITAL_INPUT case LPP_DIGITAL_INPUT: +#endif +#ifndef CAYENNE_DISABLE_DIGITAL_OUTPUT case LPP_DIGITAL_OUTPUT: +#endif +#ifndef CAYENNE_DISABLE_ANALOG_INPUT case LPP_ANALOG_INPUT: +#endif +#ifndef CAYENNE_DISABLE_ANALOG_OUTPUT case LPP_ANALOG_OUTPUT: +#endif +#ifndef CAYENNE_DISABLE_GENERIC_SENSOR case LPP_GENERIC_SENSOR: +#endif +#ifndef CAYENNE_DISABLE_LUMINOSITY case LPP_LUMINOSITY: +#endif +#ifndef CAYENNE_DISABLE_PRESENCE case LPP_PRESENCE: +#endif +#ifndef CAYENNE_DISABLE_TEMPERATURE case LPP_TEMPERATURE: +#endif +#ifndef CAYENNE_DISABLE_RELATIVE_HUMIDITY case LPP_RELATIVE_HUMIDITY: +#endif +#ifndef CAYENNE_DISABLE_ACCELEROMETER case LPP_ACCELEROMETER: +#endif +#ifndef CAYENNE_DISABLE_BAROMETRIC_PRESSUE case LPP_BAROMETRIC_PRESSURE: +#endif +#ifndef CAYENNE_DISABLE_VOLTAGE case LPP_VOLTAGE: +#endif +#ifndef CAYENNE_DISABLE_CURRENT case LPP_CURRENT: +#endif +#ifndef CAYENNE_DISABLE_FREQUENCY case LPP_FREQUENCY: +#endif +#ifndef CAYENNE_DISABLE_PERCENTAGE case LPP_PERCENTAGE: +#endif +#ifndef CAYENNE_DISABLE_ALTITUDE case LPP_ALTITUDE: +#endif +#ifndef CAYENNE_DISABLE_POWER case LPP_POWER: +#endif +#ifndef CAYENNE_DISABLE_DISTANCE case LPP_DISTANCE: +#endif +#ifndef CAYENNE_DISABLE_ENERGY case LPP_ENERGY: +#endif +#ifndef CAYENNE_DISABLE_DIRECTION case LPP_DIRECTION: +#endif +#ifndef CAYENNE_DISABLE_UNIX_TIME case LPP_UNIXTIME: +#endif +#ifndef CAYENNE_DISABLE_GYROMETER case LPP_GYROMETER: +#endif +#ifndef CAYENNE_DISABLE_GPS case LPP_GPS: +#endif +#ifndef CAYENNE_DISABLE_SWITCH case LPP_SWITCH: +#endif +#ifndef CAYENNE_DISABLE_CONCENTRATION case LPP_CONCENTRATION: +#endif +#ifndef CAYENNE_DISABLE_COLOUR case LPP_COLOUR: return true; +#endif } return false; @@ -80,83 +132,135 @@ const char * CayenneLPP::getTypeName(uint8_t type) { switch (type) { +#ifndef CAYENNE_DISABLE_DIGITAL_INPUT case LPP_DIGITAL_INPUT: return "digital_in"; +#endif +#ifndef CAYENNE_DISABLE_DIGITAL_OUTPUT case LPP_DIGITAL_OUTPUT: return "digital_out"; +#endif +#ifndef CAYENNE_DISABLE_ANALOG_INPUT case LPP_ANALOG_INPUT: return "analog_in"; +#endif +#ifndef CAYENNE_DISABLE_ANALOG_OUTPUT case LPP_ANALOG_OUTPUT: return "analog_out"; +#endif +#ifndef CAYENNE_DISABLE_GENERIC_SENSOR case LPP_GENERIC_SENSOR: return "generic"; +#endif +#ifndef CAYENNE_DISABLE_LUMINOSITY case LPP_LUMINOSITY: return "luminosity"; +#endif +#ifndef CAYENNE_DISABLE_PRESENCE case LPP_PRESENCE: return "presence"; +#endif +#ifndef CAYENNE_DISABLE_TEMPERATURE case LPP_TEMPERATURE: return "temperature"; +#endif +#ifndef CAYENNE_DISABLE_RELATIVE_HUMIDITY case LPP_RELATIVE_HUMIDITY: return "humidity"; +#endif +#ifndef CAYENNE_DISABLE_ACCELEROMETER case LPP_ACCELEROMETER: return "accelerometer"; +#endif +#ifndef CAYENNE_DISABLE_BAROMETRIC_PRESSUE case LPP_BAROMETRIC_PRESSURE: return "pressure"; +#endif +#ifndef CAYENNE_DISABLE_VOLTAGE case LPP_VOLTAGE: return "voltage"; +#endif +#ifndef CAYENNE_DISABLE_CURRENT case LPP_CURRENT: return "current"; +#endif +#ifndef CAYENNE_DISABLE_FREQUENCY case LPP_FREQUENCY: return "frequency"; +#endif +#ifndef CAYENNE_DISABLE_PERCENTAGE case LPP_PERCENTAGE: return "percentage"; +#endif +#ifndef CAYENNE_DISABLE_ALTITUDE case LPP_ALTITUDE: return "altitude"; +#endif +#ifndef CAYENNE_DISABLE_POWER case LPP_POWER: return "power"; +#endif +#ifndef CAYENNE_DISABLE_DISTANCE case LPP_DISTANCE: return "distance"; +#endif +#ifndef CAYENNE_DISABLE_ENERGY case LPP_ENERGY: return "energy"; +#endif +#ifndef CAYENNE_DISABLE_DIRECTION case LPP_DIRECTION: return "direction"; +#endif +#ifndef CAYENNE_DISABLE_UNIX_TIME case LPP_UNIXTIME: return "time"; +#endif +#ifndef CAYENNE_DISABLE_GYROMETER case LPP_GYROMETER: return "gyrometer"; +#endif +#ifndef CAYENNE_DISABLE_GPS case LPP_GPS: return "gps"; +#endif +#ifndef CAYENNE_DISABLE_SWITCH case LPP_SWITCH: return "switch"; +#endif +#ifndef CAYENNE_DISABLE_CONCENTRATION case LPP_CONCENTRATION: return "concentration"; +#endif +#ifndef CAYENNE_DISABLE_COLOUR case LPP_COLOUR: return "colour"; +#endif default: return nullptr; @@ -168,83 +272,135 @@ uint8_t CayenneLPP::getTypeSize(uint8_t type) { switch (type) { +#ifndef CAYENNE_DISABLE_DIGITAL_INPUT case LPP_DIGITAL_INPUT: return LPP_DIGITAL_INPUT_SIZE; +#endif +#ifndef CAYENNE_DISABLE_DIGITAL_OUTPUT case LPP_DIGITAL_OUTPUT: return LPP_DIGITAL_OUTPUT_SIZE; +#endif +#ifndef CAYENNE_DISABLE_ANALOG_INPUT case LPP_ANALOG_INPUT: return LPP_ANALOG_INPUT_SIZE; +#endif +#ifndef CAYENNE_DISABLE_ANALOG_OUTPUT case LPP_ANALOG_OUTPUT: return LPP_ANALOG_OUTPUT_SIZE; +#endif +#ifndef CAYENNE_DISABLE_GENERIC_SENSOR case LPP_GENERIC_SENSOR: return LPP_GENERIC_SENSOR_SIZE; +#endif +#ifndef CAYENNE_DISABLE_LUMINOSITY case LPP_LUMINOSITY: return LPP_LUMINOSITY_SIZE; +#endif +#ifndef CAYENNE_DISABLE_PRESENCE case LPP_PRESENCE: return LPP_PRESENCE_SIZE; +#endif +#ifndef CAYENNE_DISABLE_TEMPERATURE case LPP_TEMPERATURE: return LPP_TEMPERATURE_SIZE; +#endif +#ifndef CAYENNE_DISABLE_RELATIVE_HUMIDITY case LPP_RELATIVE_HUMIDITY: return LPP_RELATIVE_HUMIDITY_SIZE; +#endif +#ifndef CAYENNE_DISABLE_ACCELEROMETER case LPP_ACCELEROMETER: return LPP_ACCELEROMETER_SIZE; +#endif +#ifndef CAYENNE_DISABLE_BAROMETRIC_PRESSUE case LPP_BAROMETRIC_PRESSURE: return LPP_BAROMETRIC_PRESSURE_SIZE; +#endif +#ifndef CAYENNE_DISABLE_VOLTAGE case LPP_VOLTAGE: return LPP_VOLTAGE_SIZE; +#endif +#ifndef CAYENNE_DISABLE_CURRENT case LPP_CURRENT: return LPP_CURRENT_SIZE; +#endif +#ifndef CAYENNE_DISABLE_FREQUENCY case LPP_FREQUENCY: return LPP_FREQUENCY_SIZE; +#endif +#ifndef CAYENNE_DISABLE_PERCENTAGE case LPP_PERCENTAGE: return LPP_PERCENTAGE_SIZE; +#endif +#ifndef CAYENNE_DISABLE_ALTITUDE case LPP_ALTITUDE: return LPP_ALTITUDE_SIZE; +#endif +#ifndef CAYENNE_DISABLE_POWER case LPP_POWER: return LPP_POWER_SIZE; +#endif +#ifndef CAYENNE_DISABLE_DISTANCE case LPP_DISTANCE: return LPP_DISTANCE_SIZE; +#endif +#ifndef CAYENNE_DISABLE_ENERGY case LPP_ENERGY: return LPP_ENERGY_SIZE; +#endif +#ifndef CAYENNE_DISABLE_DIRECTION case LPP_DIRECTION: return LPP_DIRECTION_SIZE; +#endif +#ifndef CAYENNE_DISABLE_UNIX_TIME case LPP_UNIXTIME: return LPP_UNIXTIME_SIZE; +#endif +#ifndef CAYENNE_DISABLE_GYROMETER case LPP_GYROMETER: return LPP_GYROMETER_SIZE; +#endif +#ifndef CAYENNE_DISABLE_GPS case LPP_GPS: return LPP_GPS_SIZE; +#endif +#ifndef CAYENNE_DISABLE_SWITCH case LPP_SWITCH: return LPP_SWITCH_SIZE; +#endif +#ifndef CAYENNE_DISABLE_CONCENTRATION case LPP_CONCENTRATION: return LPP_CONCENTRATION_SIZE; +#endif +#ifndef CAYENNE_DISABLE_COLOUR case LPP_COLOUR: return LPP_COLOUR_SIZE; +#endif default: return 0; @@ -255,80 +411,130 @@ uint32_t CayenneLPP::getTypeMultiplier(uint8_t type) { switch (type) { +#ifndef CAYENNE_DISABLE_DIGITAL_INPUT case LPP_DIGITAL_INPUT: return LPP_DIGITAL_INPUT_MULT; +#endif +#ifndef CAYENNE_DISABLE_DIGITAL_OUTPUT case LPP_DIGITAL_OUTPUT: return LPP_DIGITAL_OUTPUT_MULT; +#endif +#ifndef CAYENNE_DISABLE_ANALOG_INPUT case LPP_ANALOG_INPUT: return LPP_ANALOG_INPUT_MULT; +#endif +#ifndef CAYENNE_DISABLE_ANALOG_OUTPUT case LPP_ANALOG_OUTPUT: return LPP_ANALOG_OUTPUT_MULT; +#endif +#ifndef CAYENNE_DISABLE_GENERIC_SENSOR case LPP_GENERIC_SENSOR: return LPP_GENERIC_SENSOR_MULT; +#endif +#ifndef CAYENNE_DISABLE_LUMINOSITY case LPP_LUMINOSITY: return LPP_LUMINOSITY_MULT; +#endif +#ifndef CAYENNE_DISABLE_PRESENCE case LPP_PRESENCE: return LPP_PRESENCE_MULT; +#endif +#ifndef CAYENNE_DISABLE_TEMPERATURE case LPP_TEMPERATURE: return LPP_TEMPERATURE_MULT; +#endif +#ifndef CAYENNE_DISABLE_ACCELEROMETER case LPP_RELATIVE_HUMIDITY: return LPP_RELATIVE_HUMIDITY_MULT; +#endif +#ifndef CAYENNE_DISABLE_ACCELEROMETER case LPP_ACCELEROMETER: return LPP_ACCELEROMETER_MULT; +#endif +#ifndef CAYENNE_DISABLE_BAROMETRIC_PRESSUE case LPP_BAROMETRIC_PRESSURE: return LPP_BAROMETRIC_PRESSURE_MULT; +#endif +#ifndef CAYENNE_DISABLE_VOLTAGE case LPP_VOLTAGE: return LPP_VOLTAGE_MULT; +#endif +#ifndef CAYENNE_DISABLE_CURRENT case LPP_CURRENT: return LPP_CURRENT_MULT; +#endif +#ifndef CAYENNE_DISABLE_FREQUENCY case LPP_FREQUENCY: return LPP_FREQUENCY_MULT; +#endif +#ifndef CAYENNE_DISABLE_PERCENTAGE case LPP_PERCENTAGE: return LPP_PERCENTAGE_MULT; +#endif +#ifndef CAYENNE_DISABLE_ALTITUDE case LPP_ALTITUDE: return LPP_ALTITUDE_MULT; +#endif +#ifndef CAYENNE_DISABLE_POWER case LPP_POWER: return LPP_POWER_MULT; +#endif +#ifndef CAYENNE_DISABLE_DISTANCE case LPP_DISTANCE: return LPP_DISTANCE_MULT; +#endif +#ifndef CAYENNE_DISABLE_ENERGY case LPP_ENERGY: return LPP_ENERGY_MULT; +#endif +#ifndef CAYENNE_DISABLE_DIRECTION case LPP_DIRECTION: return LPP_DIRECTION_MULT; +#endif +#ifndef CAYENNE_DISABLE_UNIX_TIME case LPP_UNIXTIME: return LPP_UNIXTIME_MULT; +#endif +#ifndef CAYENNE_DISABLE_GYROMETER case LPP_GYROMETER: return LPP_GYROMETER_MULT; +#endif +#ifndef CAYENNE_DISABLE_SWITCH case LPP_SWITCH: return LPP_SWITCH_MULT; +#endif +#ifndef CAYENNE_DISABLE_CONCENTRATION case LPP_CONCENTRATION: return LPP_CONCENTRATION_MULT; +#endif +#ifndef CAYENNE_DISABLE_COLOUR case LPP_COLOUR: return LPP_COLOUR_MULT; +#endif default: return 0; @@ -336,16 +542,30 @@ uint32_t CayenneLPP::getTypeMultiplier(uint8_t type) { } bool CayenneLPP::getTypeSigned(uint8_t type) { - + switch (type) { - + +#ifndef CAYENNE_DISABLE_ANALOG_INPUT case LPP_ANALOG_INPUT: +#endif +#ifndef CAYENNE_DISABLE_ANALOG_OUTPUT case LPP_ANALOG_OUTPUT: +#endif +#ifndef CAYENNE_DISABLE_TEMPERATURE case LPP_TEMPERATURE: +#endif +#ifndef CAYENNE_DISABLE_ACCELEROMETER case LPP_ACCELEROMETER: +#endif +#ifndef CAYENNE_DISABLE_ALTITUDE case LPP_ALTITUDE: +#endif +#ifndef CAYENNE_DISABLE_GYROMETER case LPP_GYROMETER: +#endif +#ifndef CAYENNE_DISABLE_GPS case LPP_GPS: +#endif return true; } return false; @@ -372,7 +592,7 @@ template uint8_t CayenneLPP::addField(uint8_t type, uint8_t channel return 0; } - // check sign + // check sign bool sign = value < 0; if (sign) value = -value; @@ -402,94 +622,139 @@ template uint8_t CayenneLPP::addField(uint8_t type, uint8_t channel } +#ifndef CAYENNE_DISABLE_DIGITAL_INPUT uint8_t CayenneLPP::addDigitalInput(uint8_t channel, uint32_t value) { return addField(LPP_DIGITAL_INPUT, channel, value); } +#endif +#ifndef CAYENNE_DISABLE_DIGITAL_OUTPUT uint8_t CayenneLPP::addDigitalOutput(uint8_t channel, uint32_t value) { return addField(LPP_DIGITAL_OUTPUT, channel, value); } +#endif +#ifndef CAYENNE_DISABLE_ANALOG_INPUT uint8_t CayenneLPP::addAnalogInput(uint8_t channel, float value) { return addField(LPP_ANALOG_INPUT, channel, value); } +#endif +#ifndef CAYENNE_DISABLE_ANALOG_OUTPUT uint8_t CayenneLPP::addAnalogOutput(uint8_t channel, float value) { return addField(LPP_ANALOG_OUTPUT, channel, value); } +#endif +#ifndef CAYENNE_DISABLE_GENERIC_SENSOR uint8_t CayenneLPP::addGenericSensor(uint8_t channel, float value) { return addField(LPP_GENERIC_SENSOR, channel, value); } +#endif +#ifndef CAYENNE_DISABLE_LUMINOSITY uint8_t CayenneLPP::addLuminosity(uint8_t channel, uint32_t value) { return addField(LPP_LUMINOSITY, channel, value); } +#endif +#ifndef CAYENNE_DISABLE_PRESENCE uint8_t CayenneLPP::addPresence(uint8_t channel, uint32_t value) { return addField(LPP_PRESENCE, channel, value); } +#endif +#ifndef CAYENNE_DISABLE_TEMPERATURE uint8_t CayenneLPP::addTemperature(uint8_t channel, float value) { return addField(LPP_TEMPERATURE, channel, value); } +#endif +#ifndef CAYENNE_DISABLE_RELATIVE_HUMIDITY uint8_t CayenneLPP::addRelativeHumidity(uint8_t channel, float value) { return addField(LPP_RELATIVE_HUMIDITY, channel, value); } +#endif +#ifndef CAYENNE_DISABLE_VOLTAGE uint8_t CayenneLPP::addVoltage(uint8_t channel, float value) { return addField(LPP_VOLTAGE, channel, value); } +#endif +#ifndef CAYENNE_DISABLE_CURRENT uint8_t CayenneLPP::addCurrent(uint8_t channel, float value) { return addField(LPP_CURRENT, channel, value); } +#endif +#ifndef CAYENNE_DISABLE_FREQUENCY uint8_t CayenneLPP::addFrequency(uint8_t channel, uint32_t value) { return addField(LPP_FREQUENCY, channel, value); } +#endif +#ifndef CAYENNE_DISABLE_PERCENTAGE uint8_t CayenneLPP::addPercentage(uint8_t channel, uint32_t value) { return addField(LPP_PERCENTAGE, channel, value); } +#endif +#ifndef CAYENNE_DISABLE_ALTITUDE uint8_t CayenneLPP::addAltitude(uint8_t channel, float value) { return addField(LPP_ALTITUDE, channel, value); } +#endif +#ifndef CAYENNE_DISABLE_POWER uint8_t CayenneLPP::addPower(uint8_t channel, uint32_t value) { return addField(LPP_POWER, channel, value); } +#endif +#ifndef CAYENNE_DISABLE_DISTANCE uint8_t CayenneLPP::addDistance(uint8_t channel, float value) { return addField(LPP_DISTANCE, channel, value); } +#endif +#ifndef CAYENNE_DISABLE_ENERGY uint8_t CayenneLPP::addEnergy(uint8_t channel, float value) { return addField(LPP_ENERGY, channel, value); } +#endif +#ifndef CAYENNE_DISABLE_BAROMETRIC_PRESSUE uint8_t CayenneLPP::addBarometricPressure(uint8_t channel, float value) { return addField(LPP_BAROMETRIC_PRESSURE, channel, value); } +#endif +#ifndef CAYENNE_DISABLE_UNIX_TIME uint8_t CayenneLPP::addUnixTime(uint8_t channel, uint32_t value) { return addField(LPP_UNIXTIME, channel, value); } +#endif +#ifndef CAYENNE_DISABLE_DIRECTION uint8_t CayenneLPP::addDirection(uint8_t channel, float value) { return addField(LPP_DIRECTION, channel, value); } +#endif +#ifndef CAYENNE_DISABLE_SWITCH uint8_t CayenneLPP::addSwitch(uint8_t channel, uint32_t value) { return addField(LPP_SWITCH, channel, value); } +#endif +#ifndef CAYENNE_DISABLE_CONCENTRATION uint8_t CayenneLPP::addConcentration(uint8_t channel, uint32_t value) { return addField(LPP_CONCENTRATION, channel, value); } +#endif +#ifndef CAYENNE_DISABLE_COLOUR uint8_t CayenneLPP::addColour(uint8_t channel, uint8_t r, uint8_t g, uint8_t b) { // check buffer overflow @@ -502,13 +767,15 @@ uint8_t CayenneLPP::addColour(uint8_t channel, uint8_t r, uint8_t g, uint8_t b) _buffer[_cursor++] = r; _buffer[_cursor++] = g; _buffer[_cursor++] = b; - + return _cursor; } +#endif +#ifndef CAYENNE_DISABLE_ACCELEROMETER uint8_t CayenneLPP::addAccelerometer(uint8_t channel, float x, float y, float z) { - + // check buffer overflow if ((_cursor + LPP_ACCELEROMETER_SIZE + 2) > _maxsize) { _error = LPP_ERROR_OVERFLOW; @@ -531,7 +798,9 @@ uint8_t CayenneLPP::addAccelerometer(uint8_t channel, float x, float y, float z) return _cursor; } +#endif +#ifndef CAYENNE_DISABLE_GYROMETER uint8_t CayenneLPP::addGyrometer(uint8_t channel, float x, float y, float z) { // check buffer overflow @@ -554,11 +823,12 @@ uint8_t CayenneLPP::addGyrometer(uint8_t channel, float x, float y, float z) { _buffer[_cursor++] = vz; return _cursor; - } +#endif +#ifndef CAYENNE_DISABLE_GPS uint8_t CayenneLPP::addGPS(uint8_t channel, float latitude, float longitude, float altitude) { - + // check buffer overflow if ((_cursor + LPP_GPS_SIZE + 2) > _maxsize) { _error = LPP_ERROR_OVERFLOW; @@ -584,6 +854,7 @@ uint8_t CayenneLPP::addGPS(uint8_t channel, float latitude, float longitude, flo return _cursor; } +#endif // ---------------------------------------------------------------------------- @@ -629,7 +900,7 @@ uint8_t CayenneLPP::decode(uint8_t *buffer, uint8_t len, JsonArray& root) { // Get channel # uint8_t channel = buffer[index++]; - + // Get data type uint8_t type = buffer[index++]; if (!isType(type)) { @@ -655,35 +926,54 @@ uint8_t CayenneLPP::decode(uint8_t *buffer, uint8_t len, JsonArray& root) { data["name"] = String(getTypeName(type)); // Parse types - if (LPP_COLOUR == type) { + if (false) { + } +#ifndef CAYENNE_DISABLE_COLOUR + else if (LPP_COLOUR == type) { JsonObject object = data.createNestedObject("value"); object["r"] = getValue(&buffer[index], 1, multiplier, is_signed); object["g"] = getValue(&buffer[index+1], 1, multiplier, is_signed); object["b"] = getValue(&buffer[index+2], 1, multiplier, is_signed); - } else if (LPP_ACCELEROMETER == type || LPP_GYROMETER == type) { - + } +#endif +#ifndef CAYENNE_DISABLE_ACCELEROMETER + else if (LPP_ACCELEROMETER == type) { JsonObject object = data.createNestedObject("value"); object["x"] = getValue(&buffer[index], 2, multiplier, is_signed); object["y"] = getValue(&buffer[index+2], 2, multiplier, is_signed); object["z"] = getValue(&buffer[index+4], 2, multiplier, is_signed); - - } else if (LPP_GPS == type) { - + } +#endif +#ifndef CAYENNE_DISABLE_GYROMETER + else if (LPP_GYROMETER == type) { + JsonObject object = data.createNestedObject("value"); + object["x"] = getValue(&buffer[index], 2, multiplier, is_signed); + object["y"] = getValue(&buffer[index+2], 2, multiplier, is_signed); + object["z"] = getValue(&buffer[index+4], 2, multiplier, is_signed); + } +#endif +#ifndef CAYENNE_DISABLE_GPS + else if (LPP_GPS == type) { JsonObject object = data.createNestedObject("value"); object["latitude"] = getValue(&buffer[index], 3, 10000, is_signed); object["longitude"] = getValue(&buffer[index+3], 3, 10000, is_signed); object["altitude"] = getValue(&buffer[index+6], 3, 100, is_signed); - - } else if (LPP_GENERIC_SENSOR == type || LPP_UNIXTIME == type) { - + } +#endif +#ifndef CAYENNE_DISABLE_GENERIC_SENSOR + else if (LPP_GENERIC_SENSOR == type) { data["value"] = getValue32(&buffer[index], size); - - } else { - + } +#endif +#ifndef CAYENNE_DISABLE_UNIX_TIME + else if (LPP_UNIXTIME == type) { + data["value"] = getValue32(&buffer[index], size); + } +#endif + else { data["value"] = getValue(&buffer[index], size, multiplier, is_signed); - } index += size; @@ -705,7 +995,7 @@ uint8_t CayenneLPP::decodeTTN(uint8_t *buffer, uint8_t len, JsonObject& root) { // Get channel # uint8_t channel = buffer[index++]; - + // Get data type uint8_t type = buffer[index++]; if (!isType(type)) { @@ -728,31 +1018,58 @@ uint8_t CayenneLPP::decodeTTN(uint8_t *buffer, uint8_t len, JsonObject& root) { String name = String(getTypeName(type)) + "_" + channel; // Parse types - if (LPP_COLOUR == type) { + if (false) { + } +#ifndef CAYENNE_DISABLE_COLOUR + else if (LPP_COLOUR == type) { JsonObject object = root.createNestedObject(name); object["r"] = getValue(&buffer[index], 1, multiplier, is_signed); object["g"] = getValue(&buffer[index+1], 1, multiplier, is_signed); object["b"] = getValue(&buffer[index+2], 1, multiplier, is_signed); - } else if (LPP_ACCELEROMETER == type || LPP_GYROMETER == type) { + } +#endif +#ifndef CAYENNE_DISABLE_ACCELEROMETER + else if (LPP_ACCELEROMETER == type) { JsonObject object = root.createNestedObject(name); object["x"] = getValue(&buffer[index], 2, multiplier, is_signed); object["y"] = getValue(&buffer[index+2], 2, multiplier, is_signed); object["z"] = getValue(&buffer[index+4], 2, multiplier, is_signed); - } else if (LPP_GPS == type) { + } +#endif +#ifndef CAYENNE_DISABLE_GYROMETER + else if (LPP_GYROMETER == type) { + + JsonObject object = root.createNestedObject(name); + object["x"] = getValue(&buffer[index], 2, multiplier, is_signed); + object["y"] = getValue(&buffer[index+2], 2, multiplier, is_signed); + object["z"] = getValue(&buffer[index+4], 2, multiplier, is_signed); + + } +#endif +#ifndef CAYENNE_DISABLE_GPS + else if (LPP_GPS == type) { JsonObject object = root.createNestedObject(name); object["latitude"] = getValue(&buffer[index], 3, 10000, is_signed); object["longitude"] = getValue(&buffer[index+3], 3, 10000, is_signed); object["altitude"] = getValue(&buffer[index+6], 3, 100, is_signed); - } else if (LPP_GENERIC_SENSOR == type || LPP_UNIXTIME == type) { - + } +#endif +#ifndef CAYENNE_DISABLE_GENERIC_SENSOR + else if (LPP_GENERIC_SENSOR == type) { root[name] = getValue32(&buffer[index], size); - - } else { + } +#endif +#ifndef CAYENNE_DISABLE_UNIX_TIME + else if (LPP_UNIXTIME == type) { + root[name] = getValue32(&buffer[index], size); + } +#endif + else { root[name] = getValue(&buffer[index], size, multiplier, is_signed); diff --git a/src/CayenneLPP.h b/src/CayenneLPP.h index 33e750a..693b9fc 100644 --- a/src/CayenneLPP.h +++ b/src/CayenneLPP.h @@ -1,7 +1,8 @@ // 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. +// Use of this source code is governed by the MIT license that can be found in +// the LICENSE file. #ifndef CAYENNE_LPP_H #define CAYENNE_LPP_H @@ -9,98 +10,97 @@ #include #include -#define LPP_DIGITAL_INPUT 0 // 1 byte -#define LPP_DIGITAL_OUTPUT 1 // 1 byte -#define LPP_ANALOG_INPUT 2 // 2 bytes, 0.01 signed -#define LPP_ANALOG_OUTPUT 3 // 2 bytes, 0.01 signed -#define LPP_GENERIC_SENSOR 100 // 4 bytes, unsigned -#define LPP_LUMINOSITY 101 // 2 bytes, 1 lux unsigned -#define LPP_PRESENCE 102 // 1 byte, bool -#define LPP_TEMPERATURE 103 // 2 bytes, 0.1°C signed -#define LPP_RELATIVE_HUMIDITY 104 // 1 byte, 0.5% unsigned -#define LPP_ACCELEROMETER 113 // 2 bytes per axis, 0.001G -#define LPP_BAROMETRIC_PRESSURE 115 // 2 bytes 0.1hPa unsigned -#define LPP_VOLTAGE 116 // 2 bytes 0.01V unsigned -#define LPP_CURRENT 117 // 2 bytes 0.001A unsigned -#define LPP_FREQUENCY 118 // 4 bytes 1Hz unsigned -#define LPP_PERCENTAGE 120 // 1 byte 1-100% unsigned -#define LPP_ALTITUDE 121 // 2 byte 1m signed -#define LPP_CONCENTRATION 125 // 2 bytes, 1 ppm unsigned -#define LPP_POWER 128 // 2 byte, 1W, unsigned -#define LPP_DISTANCE 130 // 4 byte, 0.001m, unsigned -#define LPP_ENERGY 131 // 4 byte, 0.001kWh, unsigned -#define LPP_DIRECTION 132 // 2 bytes, 1deg, unsigned -#define LPP_UNIXTIME 133 // 4 bytes, unsigned -#define LPP_GYROMETER 134 // 2 bytes per axis, 0.01 °/s -#define LPP_COLOUR 135 // 1 byte per RGB Color -#define LPP_GPS 136 // 3 byte lon/lat 0.0001 °, 3 bytes alt 0.01 meter -#define LPP_SWITCH 142 // 1 byte, 0/1 +#define LPP_DIGITAL_INPUT 0 // 1 byte +#define LPP_DIGITAL_OUTPUT 1 // 1 byte +#define LPP_ANALOG_INPUT 2 // 2 bytes, 0.01 signed +#define LPP_ANALOG_OUTPUT 3 // 2 bytes, 0.01 signed +#define LPP_GENERIC_SENSOR 100 // 4 bytes, unsigned +#define LPP_LUMINOSITY 101 // 2 bytes, 1 lux unsigned +#define LPP_PRESENCE 102 // 1 byte, bool +#define LPP_TEMPERATURE 103 // 2 bytes, 0.1°C signed +#define LPP_RELATIVE_HUMIDITY 104 // 1 byte, 0.5% unsigned +#define LPP_ACCELEROMETER 113 // 2 bytes per axis, 0.001G +#define LPP_BAROMETRIC_PRESSURE 115 // 2 bytes 0.1hPa unsigned +#define LPP_VOLTAGE 116 // 2 bytes 0.01V unsigned +#define LPP_CURRENT 117 // 2 bytes 0.001A unsigned +#define LPP_FREQUENCY 118 // 4 bytes 1Hz unsigned +#define LPP_PERCENTAGE 120 // 1 byte 1-100% unsigned +#define LPP_ALTITUDE 121 // 2 byte 1m signed +#define LPP_CONCENTRATION 125 // 2 bytes, 1 ppm unsigned +#define LPP_POWER 128 // 2 byte, 1W, unsigned +#define LPP_DISTANCE 130 // 4 byte, 0.001m, unsigned +#define LPP_ENERGY 131 // 4 byte, 0.001kWh, unsigned +#define LPP_DIRECTION 132 // 2 bytes, 1deg, unsigned +#define LPP_UNIXTIME 133 // 4 bytes, unsigned +#define LPP_GYROMETER 134 // 2 bytes per axis, 0.01 °/s +#define LPP_COLOUR 135 // 1 byte per RGB Color +#define LPP_GPS 136 // 3 byte lon/lat 0.0001 °, 3 bytes alt 0.01 meter +#define LPP_SWITCH 142 // 1 byte, 0/1 // Only Data Size -#define LPP_DIGITAL_INPUT_SIZE 1 -#define LPP_DIGITAL_OUTPUT_SIZE 1 -#define LPP_ANALOG_INPUT_SIZE 2 -#define LPP_ANALOG_OUTPUT_SIZE 2 -#define LPP_GENERIC_SENSOR_SIZE 4 -#define LPP_LUMINOSITY_SIZE 2 -#define LPP_PRESENCE_SIZE 1 -#define LPP_TEMPERATURE_SIZE 2 -#define LPP_RELATIVE_HUMIDITY_SIZE 1 -#define LPP_ACCELEROMETER_SIZE 6 -#define LPP_BAROMETRIC_PRESSURE_SIZE 2 -#define LPP_VOLTAGE_SIZE 2 -#define LPP_CURRENT_SIZE 2 -#define LPP_FREQUENCY_SIZE 4 -#define LPP_PERCENTAGE_SIZE 1 -#define LPP_ALTITUDE_SIZE 2 -#define LPP_POWER_SIZE 2 -#define LPP_DISTANCE_SIZE 4 -#define LPP_ENERGY_SIZE 4 -#define LPP_DIRECTION_SIZE 2 -#define LPP_UNIXTIME_SIZE 4 -#define LPP_GYROMETER_SIZE 6 -#define LPP_GPS_SIZE 9 -#define LPP_SWITCH_SIZE 1 -#define LPP_CONCENTRATION_SIZE 2 -#define LPP_COLOUR_SIZE 3 +#define LPP_DIGITAL_INPUT_SIZE 1 +#define LPP_DIGITAL_OUTPUT_SIZE 1 +#define LPP_ANALOG_INPUT_SIZE 2 +#define LPP_ANALOG_OUTPUT_SIZE 2 +#define LPP_GENERIC_SENSOR_SIZE 4 +#define LPP_LUMINOSITY_SIZE 2 +#define LPP_PRESENCE_SIZE 1 +#define LPP_TEMPERATURE_SIZE 2 +#define LPP_RELATIVE_HUMIDITY_SIZE 1 +#define LPP_ACCELEROMETER_SIZE 6 +#define LPP_BAROMETRIC_PRESSURE_SIZE 2 +#define LPP_VOLTAGE_SIZE 2 +#define LPP_CURRENT_SIZE 2 +#define LPP_FREQUENCY_SIZE 4 +#define LPP_PERCENTAGE_SIZE 1 +#define LPP_ALTITUDE_SIZE 2 +#define LPP_POWER_SIZE 2 +#define LPP_DISTANCE_SIZE 4 +#define LPP_ENERGY_SIZE 4 +#define LPP_DIRECTION_SIZE 2 +#define LPP_UNIXTIME_SIZE 4 +#define LPP_GYROMETER_SIZE 6 +#define LPP_GPS_SIZE 9 +#define LPP_SWITCH_SIZE 1 +#define LPP_CONCENTRATION_SIZE 2 +#define LPP_COLOUR_SIZE 3 // Multipliers -#define LPP_DIGITAL_INPUT_MULT 1 -#define LPP_DIGITAL_OUTPUT_MULT 1 -#define LPP_ANALOG_INPUT_MULT 100 -#define LPP_ANALOG_OUTPUT_MULT 100 -#define LPP_GENERIC_SENSOR_MULT 1 -#define LPP_LUMINOSITY_MULT 1 -#define LPP_PRESENCE_MULT 1 -#define LPP_TEMPERATURE_MULT 10 -#define LPP_RELATIVE_HUMIDITY_MULT 2 -#define LPP_ACCELEROMETER_MULT 1000 -#define LPP_BAROMETRIC_PRESSURE_MULT 10 -#define LPP_VOLTAGE_MULT 100 -#define LPP_CURRENT_MULT 1000 -#define LPP_FREQUENCY_MULT 1 -#define LPP_PERCENTAGE_MULT 1 -#define LPP_ALTITUDE_MULT 1 -#define LPP_POWER_MULT 1 -#define LPP_DISTANCE_MULT 1000 -#define LPP_ENERGY_MULT 1000 -#define LPP_DIRECTION_MULT 1 -#define LPP_UNIXTIME_MULT 1 -#define LPP_GYROMETER_MULT 100 -#define LPP_GPS_LAT_LON_MULT 10000 -#define LPP_GPS_ALT_MULT 100 -#define LPP_SWITCH_MULT 1 -#define LPP_CONCENTRATION_MULT 1 -#define LPP_COLOUR_MULT 1 - -#define LPP_ERROR_OK 0 -#define LPP_ERROR_OVERFLOW 1 -#define LPP_ERROR_UNKOWN_TYPE 2 +#define LPP_DIGITAL_INPUT_MULT 1 +#define LPP_DIGITAL_OUTPUT_MULT 1 +#define LPP_ANALOG_INPUT_MULT 100 +#define LPP_ANALOG_OUTPUT_MULT 100 +#define LPP_GENERIC_SENSOR_MULT 1 +#define LPP_LUMINOSITY_MULT 1 +#define LPP_PRESENCE_MULT 1 +#define LPP_TEMPERATURE_MULT 10 +#define LPP_RELATIVE_HUMIDITY_MULT 2 +#define LPP_ACCELEROMETER_MULT 1000 +#define LPP_BAROMETRIC_PRESSURE_MULT 10 +#define LPP_VOLTAGE_MULT 100 +#define LPP_CURRENT_MULT 1000 +#define LPP_FREQUENCY_MULT 1 +#define LPP_PERCENTAGE_MULT 1 +#define LPP_ALTITUDE_MULT 1 +#define LPP_POWER_MULT 1 +#define LPP_DISTANCE_MULT 1000 +#define LPP_ENERGY_MULT 1000 +#define LPP_DIRECTION_MULT 1 +#define LPP_UNIXTIME_MULT 1 +#define LPP_GYROMETER_MULT 100 +#define LPP_GPS_LAT_LON_MULT 10000 +#define LPP_GPS_ALT_MULT 100 +#define LPP_SWITCH_MULT 1 +#define LPP_CONCENTRATION_MULT 1 +#define LPP_COLOUR_MULT 1 + +#define LPP_ERROR_OK 0 +#define LPP_ERROR_OVERFLOW 1 +#define LPP_ERROR_UNKOWN_TYPE 2 class CayenneLPP { public: - CayenneLPP(uint8_t size); ~CayenneLPP(); @@ -111,56 +111,109 @@ class CayenneLPP { uint8_t getError(); // Decoder methods - const char * getTypeName(uint8_t type); - uint8_t decode(uint8_t *buffer, uint8_t size, JsonArray& root); - uint8_t decodeTTN(uint8_t *buffer, uint8_t size, JsonObject& root); + const char *getTypeName(uint8_t type); + uint8_t decode(uint8_t *buffer, uint8_t size, JsonArray &root); + uint8_t decodeTTN(uint8_t *buffer, uint8_t size, JsonObject &root); // Original LPPv1 data types +#ifndef CAYENNE_DISABLE_DIGITAL_INPUT uint8_t addDigitalInput(uint8_t channel, uint32_t value); +#endif +#ifndef CAYENNE_DISABLE_DIGITAL_OUTPUT uint8_t addDigitalOutput(uint8_t channel, uint32_t value); +#endif +#ifndef CAYENNE_DISABLE_ANALOG_INPUT uint8_t addAnalogInput(uint8_t channel, float value); +#endif +#ifndef CAYENNE_DISABLE_ANALOG_OUTPUT uint8_t addAnalogOutput(uint8_t channel, float value); +#endif +#ifndef CAYENNE_DISABLE_LUMINOSITY uint8_t addLuminosity(uint8_t channel, uint32_t value); +#endif +#ifndef CAYENNE_DISABLE_PRESENCE uint8_t addPresence(uint8_t channel, uint32_t value); +#endif +#ifndef CAYENNE_DISABLE_TEMPERATURE uint8_t addTemperature(uint8_t channel, float value); +#endif +#ifndef CAYENNE_DISABLE_RELATIVE_HUMIDITY uint8_t addRelativeHumidity(uint8_t channel, float value); +#endif +#ifndef CAYENNE_DISABLE_ACCELEROMETER uint8_t addAccelerometer(uint8_t channel, float x, float y, float z); +#endif +#ifndef CAYENNE_DISABLE_BAROMETRIC_PRESSUE uint8_t addBarometricPressure(uint8_t channel, float value); +#endif +#ifndef CAYENNE_DISABLE_GYROMETER uint8_t addGyrometer(uint8_t channel, float x, float y, float z); - uint8_t addGPS(uint8_t channel, float latitude, float longitude, float altitude); +#endif +#ifndef CAYENNE_DISABLE_GPS + uint8_t addGPS(uint8_t channel, float latitude, float longitude, + float altitude); +#endif // Additional data types +#ifndef CAYENNE_DISABLE_UNIX_TIME uint8_t addUnixTime(uint8_t channel, uint32_t value); +#endif +#ifndef CAYENNE_DISABLE_GENERIC_SENSOR uint8_t addGenericSensor(uint8_t channel, float value); +#endif +#ifndef CAYENNE_DISABLE_VOLTAGE uint8_t addVoltage(uint8_t channel, float value); +#endif +#ifndef CAYENNE_DISABLE_CURRENT uint8_t addCurrent(uint8_t channel, float value); +#endif +#ifndef CAYENNE_DISABLE_FREQUENCY uint8_t addFrequency(uint8_t channel, uint32_t value); +#endif +#ifndef CAYENNE_DISABLE_PERCENTAGE uint8_t addPercentage(uint8_t channel, uint32_t value); +#endif +#ifndef CAYENNE_DISABLE_ALTITUDE uint8_t addAltitude(uint8_t channel, float value); +#endif +#ifndef CAYENNE_DISABLE_POWER uint8_t addPower(uint8_t channel, uint32_t value); +#endif +#ifndef CAYENNE_DISABLE_DISTANCE uint8_t addDistance(uint8_t channel, float value); +#endif +#ifndef CAYENNE_DISABLE_ENERGY uint8_t addEnergy(uint8_t channel, float value); +#endif +#ifndef CAYENNE_DISABLE_DIRECTION uint8_t addDirection(uint8_t channel, float value); +#endif +#ifndef CAYENNE_DISABLE_SWITCH uint8_t addSwitch(uint8_t channel, uint32_t value); +#endif +#ifndef CAYENNE_DISABLE_CONCENTRATION uint8_t addConcentration(uint8_t channel, uint32_t value); +#endif +#ifndef CAYENNE_DISABLE_COLOUR uint8_t addColour(uint8_t channel, uint8_t r, uint8_t g, uint8_t b); +#endif protected: - bool isType(uint8_t type); uint8_t getTypeSize(uint8_t type); uint32_t getTypeMultiplier(uint8_t type); bool getTypeSigned(uint8_t type); - float getValue(uint8_t * buffer, uint8_t size, uint32_t multiplier, bool is_signed); - uint32_t getValue32(uint8_t * buffer, uint8_t size); - template uint8_t addField(uint8_t type, uint8_t channel, T value); + float getValue(uint8_t *buffer, uint8_t size, uint32_t multiplier, + bool is_signed); + uint32_t getValue32(uint8_t *buffer, uint8_t size); + template + uint8_t addField(uint8_t type, uint8_t channel, T value); - uint8_t * _buffer; + uint8_t *_buffer; uint8_t _maxsize; uint8_t _cursor; uint8_t _error = LPP_ERROR_OK; - }; -#endif \ No newline at end of file +#endif From 74cca04fa0081689874489c1772e28d8182bd365 Mon Sep 17 00:00:00 2001 From: Keisha Zapra Date: Fri, 26 Feb 2021 10:06:03 +0800 Subject: [PATCH 2/3] Remove return true from ifdef --- src/CayenneLPP.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/CayenneLPP.cpp b/src/CayenneLPP.cpp index 2be162a..30cb037 100644 --- a/src/CayenneLPP.cpp +++ b/src/CayenneLPP.cpp @@ -121,8 +121,8 @@ bool CayenneLPP::isType(uint8_t type) { #endif #ifndef CAYENNE_DISABLE_COLOUR case LPP_COLOUR: - return true; #endif + return true; } return false; From 0f2495fe8f9accac10c7ac06469a6565b4d2dd79 Mon Sep 17 00:00:00 2001 From: Keisha Zapra Date: Thu, 4 Mar 2021 08:57:23 +0800 Subject: [PATCH 3/3] Fix bug where accelerometer was required to be enabled for humidity to work --- src/CayenneLPP.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/CayenneLPP.cpp b/src/CayenneLPP.cpp index 30cb037..f59d3be 100644 --- a/src/CayenneLPP.cpp +++ b/src/CayenneLPP.cpp @@ -451,7 +451,7 @@ uint32_t CayenneLPP::getTypeMultiplier(uint8_t type) { return LPP_TEMPERATURE_MULT; #endif -#ifndef CAYENNE_DISABLE_ACCELEROMETER +#ifndef CAYENNE_DISABLE_RELATIVE_HUMIDITY case LPP_RELATIVE_HUMIDITY: return LPP_RELATIVE_HUMIDITY_MULT; #endif