Cayenne Low Power Payload
Dependents: LORAWAN-TTN-CAYENNE-LM35 ER4Lora gps_accelerometer sgam_mdw_test ... more
src/CayenneLPP.cpp@6:5a9d65b33e85, 2017-03-09 (annotated)
- Committer:
- eptak
- Date:
- Thu Mar 09 16:32:30 2017 +0000
- Revision:
- 6:5a9d65b33e85
- Parent:
- 1:3747637941bf
fix typo
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++] = val >> 8; |
eptak | 0:15c6e1cab70a | 76 | buffer[cursor++] = val; |
eptak | 0:15c6e1cab70a | 77 | |
eptak | 0:15c6e1cab70a | 78 | return cursor; |
eptak | 0:15c6e1cab70a | 79 | } |
eptak | 0:15c6e1cab70a | 80 | |
eptak | 0:15c6e1cab70a | 81 | uint8_t CayenneLPP::addLuminosity(uint8_t channel, uint16_t lux) { |
eptak | 0:15c6e1cab70a | 82 | if ((cursor + LPP_LUMINOSITY_SIZE) > maxsize) { |
eptak | 0:15c6e1cab70a | 83 | return 0; |
eptak | 0:15c6e1cab70a | 84 | } |
eptak | 0:15c6e1cab70a | 85 | buffer[cursor++] = channel; |
eptak | 0:15c6e1cab70a | 86 | buffer[cursor++] = LPP_LUMINOSITY; |
eptak | 0:15c6e1cab70a | 87 | buffer[cursor++] = lux >> 8; |
eptak | 0:15c6e1cab70a | 88 | buffer[cursor++] = lux; |
eptak | 0:15c6e1cab70a | 89 | |
eptak | 0:15c6e1cab70a | 90 | return cursor; |
eptak | 0:15c6e1cab70a | 91 | } |
eptak | 0:15c6e1cab70a | 92 | |
eptak | 0:15c6e1cab70a | 93 | uint8_t CayenneLPP::addPresence(uint8_t channel, uint8_t value) { |
eptak | 0:15c6e1cab70a | 94 | if ((cursor + LPP_PRESENCE_SIZE) > maxsize) { |
eptak | 0:15c6e1cab70a | 95 | return 0; |
eptak | 0:15c6e1cab70a | 96 | } |
eptak | 0:15c6e1cab70a | 97 | buffer[cursor++] = channel; |
eptak | 0:15c6e1cab70a | 98 | buffer[cursor++] = LPP_PRESENCE; |
eptak | 0:15c6e1cab70a | 99 | buffer[cursor++] = value; |
eptak | 0:15c6e1cab70a | 100 | |
eptak | 0:15c6e1cab70a | 101 | return cursor; |
eptak | 0:15c6e1cab70a | 102 | } |
eptak | 0:15c6e1cab70a | 103 | |
eptak | 0:15c6e1cab70a | 104 | uint8_t CayenneLPP::addTemperature(uint8_t channel, float celsius) { |
eptak | 0:15c6e1cab70a | 105 | if ((cursor + LPP_TEMPERATURE_SIZE) > maxsize) { |
eptak | 0:15c6e1cab70a | 106 | return 0; |
eptak | 0:15c6e1cab70a | 107 | } |
eptak | 0:15c6e1cab70a | 108 | int16_t val = celsius * 10; |
eptak | 0:15c6e1cab70a | 109 | buffer[cursor++] = channel; |
eptak | 0:15c6e1cab70a | 110 | buffer[cursor++] = LPP_TEMPERATURE; |
eptak | 0:15c6e1cab70a | 111 | buffer[cursor++] = val >> 8; |
eptak | 0:15c6e1cab70a | 112 | buffer[cursor++] = val; |
eptak | 0:15c6e1cab70a | 113 | |
eptak | 0:15c6e1cab70a | 114 | return cursor; |
eptak | 0:15c6e1cab70a | 115 | } |
eptak | 0:15c6e1cab70a | 116 | |
eptak | 0:15c6e1cab70a | 117 | uint8_t CayenneLPP::addRelativeHumidity(uint8_t channel, float rh) { |
eptak | 0:15c6e1cab70a | 118 | if ((cursor + LPP_RELATIVE_HUMIDITY_SIZE) > maxsize) { |
eptak | 0:15c6e1cab70a | 119 | return 0; |
eptak | 0:15c6e1cab70a | 120 | } |
eptak | 0:15c6e1cab70a | 121 | buffer[cursor++] = channel; |
eptak | 0:15c6e1cab70a | 122 | buffer[cursor++] = LPP_RELATIVE_HUMIDITY; |
eptak | 0:15c6e1cab70a | 123 | buffer[cursor++] = rh * 2; |
eptak | 0:15c6e1cab70a | 124 | |
eptak | 0:15c6e1cab70a | 125 | return cursor; |
eptak | 0:15c6e1cab70a | 126 | } |
eptak | 0:15c6e1cab70a | 127 | |
eptak | 0:15c6e1cab70a | 128 | uint8_t CayenneLPP::addAccelerometer(uint8_t channel, float x, float y, float z) { |
eptak | 0:15c6e1cab70a | 129 | if ((cursor + LPP_ACCELEROMETER_SIZE) > maxsize) { |
eptak | 0:15c6e1cab70a | 130 | return 0; |
eptak | 0:15c6e1cab70a | 131 | } |
eptak | 0:15c6e1cab70a | 132 | int16_t vx = x * 1000; |
eptak | 0:15c6e1cab70a | 133 | int16_t vy = y * 1000; |
eptak | 0:15c6e1cab70a | 134 | int16_t vz = z * 1000; |
eptak | 0:15c6e1cab70a | 135 | |
eptak | 0:15c6e1cab70a | 136 | buffer[cursor++] = channel; |
eptak | 0:15c6e1cab70a | 137 | buffer[cursor++] = LPP_ACCELEROMETER; |
eptak | 0:15c6e1cab70a | 138 | buffer[cursor++] = vx >> 8; |
eptak | 0:15c6e1cab70a | 139 | buffer[cursor++] = vx; |
eptak | 0:15c6e1cab70a | 140 | buffer[cursor++] = vy >> 8; |
eptak | 0:15c6e1cab70a | 141 | buffer[cursor++] = vy; |
eptak | 0:15c6e1cab70a | 142 | buffer[cursor++] = vz >> 8; |
eptak | 0:15c6e1cab70a | 143 | buffer[cursor++] = vz; |
eptak | 0:15c6e1cab70a | 144 | |
eptak | 0:15c6e1cab70a | 145 | return cursor; |
eptak | 0:15c6e1cab70a | 146 | } |
eptak | 0:15c6e1cab70a | 147 | |
eptak | 0:15c6e1cab70a | 148 | uint8_t CayenneLPP::addBarometricPressure(uint8_t channel, float hpa) { |
eptak | 0:15c6e1cab70a | 149 | if ((cursor + LPP_BAROMETRIC_PRESSURE_SIZE) > maxsize) { |
eptak | 0:15c6e1cab70a | 150 | return 0; |
eptak | 0:15c6e1cab70a | 151 | } |
eptak | 0:15c6e1cab70a | 152 | int16_t val = hpa * 10; |
eptak | 0:15c6e1cab70a | 153 | |
eptak | 0:15c6e1cab70a | 154 | buffer[cursor++] = channel; |
eptak | 0:15c6e1cab70a | 155 | buffer[cursor++] = LPP_BAROMETRIC_PRESSURE; |
eptak | 0:15c6e1cab70a | 156 | buffer[cursor++] = val >> 8; |
eptak | 0:15c6e1cab70a | 157 | buffer[cursor++] = val; |
eptak | 0:15c6e1cab70a | 158 | |
eptak | 0:15c6e1cab70a | 159 | return cursor; |
eptak | 0:15c6e1cab70a | 160 | } |
eptak | 0:15c6e1cab70a | 161 | |
eptak | 0:15c6e1cab70a | 162 | uint8_t CayenneLPP::addGyrometer(uint8_t channel, float x, float y, float z) { |
eptak | 0:15c6e1cab70a | 163 | if ((cursor + LPP_GYROMETER_SIZE) > maxsize) { |
eptak | 0:15c6e1cab70a | 164 | return 0; |
eptak | 0:15c6e1cab70a | 165 | } |
eptak | 0:15c6e1cab70a | 166 | int16_t vx = x * 100; |
eptak | 0:15c6e1cab70a | 167 | int16_t vy = y * 100; |
eptak | 0:15c6e1cab70a | 168 | int16_t vz = z * 100; |
eptak | 0:15c6e1cab70a | 169 | |
eptak | 0:15c6e1cab70a | 170 | buffer[cursor++] = channel; |
eptak | 0:15c6e1cab70a | 171 | buffer[cursor++] = LPP_GYROMETER; |
eptak | 0:15c6e1cab70a | 172 | buffer[cursor++] = vx >> 8; |
eptak | 0:15c6e1cab70a | 173 | buffer[cursor++] = vx; |
eptak | 0:15c6e1cab70a | 174 | buffer[cursor++] = vy >> 8; |
eptak | 0:15c6e1cab70a | 175 | buffer[cursor++] = vy; |
eptak | 0:15c6e1cab70a | 176 | buffer[cursor++] = vz >> 8; |
eptak | 0:15c6e1cab70a | 177 | buffer[cursor++] = vz; |
eptak | 0:15c6e1cab70a | 178 | |
eptak | 0:15c6e1cab70a | 179 | return cursor; |
eptak | 0:15c6e1cab70a | 180 | } |
eptak | 0:15c6e1cab70a | 181 | |
eptak | 0:15c6e1cab70a | 182 | uint8_t CayenneLPP::addGPS(uint8_t channel, float latitude, float longitude, float meters) { |
eptak | 0:15c6e1cab70a | 183 | if ((cursor + LPP_GPS_SIZE) > maxsize) { |
eptak | 0:15c6e1cab70a | 184 | return 0; |
eptak | 0:15c6e1cab70a | 185 | } |
eptak | 0:15c6e1cab70a | 186 | int32_t lat = latitude * 10000; |
eptak | 0:15c6e1cab70a | 187 | int32_t lon = longitude * 10000; |
eptak | 0:15c6e1cab70a | 188 | int32_t alt = meters * 100; |
eptak | 0:15c6e1cab70a | 189 | |
eptak | 0:15c6e1cab70a | 190 | buffer[cursor++] = channel; |
eptak | 0:15c6e1cab70a | 191 | buffer[cursor++] = LPP_GPS; |
eptak | 0:15c6e1cab70a | 192 | |
eptak | 0:15c6e1cab70a | 193 | buffer[cursor++] = lat >> 16; |
eptak | 0:15c6e1cab70a | 194 | buffer[cursor++] = lat >> 8; |
eptak | 0:15c6e1cab70a | 195 | buffer[cursor++] = lat; |
eptak | 0:15c6e1cab70a | 196 | buffer[cursor++] = lon >> 16; |
eptak | 0:15c6e1cab70a | 197 | buffer[cursor++] = lon >> 8; |
eptak | 0:15c6e1cab70a | 198 | buffer[cursor++] = lon; |
eptak | 0:15c6e1cab70a | 199 | buffer[cursor++] = alt >> 16; |
eptak | 0:15c6e1cab70a | 200 | buffer[cursor++] = alt >> 8; |
eptak | 0:15c6e1cab70a | 201 | buffer[cursor++] = alt; |
eptak | 0:15c6e1cab70a | 202 | |
eptak | 0:15c6e1cab70a | 203 | return cursor; |
eptak | 0:15c6e1cab70a | 204 | } |
eptak | 0:15c6e1cab70a | 205 |