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