Cayenne-LPP avec NFC
src/CayenneLPP.cpp@8:2b94df050b00, 2022-02-08 (annotated)
- Committer:
- superphil06
- Date:
- Tue Feb 08 09:03:59 2022 +0000
- Revision:
- 8:2b94df050b00
- Parent:
- 7:8509fb453a2e
add addVEHICLE function
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 | |
anthonyv | 7:8509fb453a2e | 3 | CayenneLPP::CayenneLPP(uint8_t size) : maxsize(size) |
anthonyv | 7:8509fb453a2e | 4 | { |
eptak | 0:15c6e1cab70a | 5 | buffer = (uint8_t*) malloc(size); |
eptak | 0:15c6e1cab70a | 6 | cursor = 0; |
eptak | 0:15c6e1cab70a | 7 | } |
eptak | 0:15c6e1cab70a | 8 | |
anthonyv | 7:8509fb453a2e | 9 | CayenneLPP::~CayenneLPP(void) |
anthonyv | 7:8509fb453a2e | 10 | { |
eptak | 0:15c6e1cab70a | 11 | free(buffer); |
eptak | 0:15c6e1cab70a | 12 | } |
eptak | 0:15c6e1cab70a | 13 | |
anthonyv | 7:8509fb453a2e | 14 | void CayenneLPP::reset(void) |
anthonyv | 7:8509fb453a2e | 15 | { |
eptak | 0:15c6e1cab70a | 16 | cursor = 0; |
eptak | 0:15c6e1cab70a | 17 | } |
eptak | 0:15c6e1cab70a | 18 | |
anthonyv | 7:8509fb453a2e | 19 | uint8_t CayenneLPP::getSize(void) |
anthonyv | 7:8509fb453a2e | 20 | { |
eptak | 0:15c6e1cab70a | 21 | return cursor; |
eptak | 0:15c6e1cab70a | 22 | } |
eptak | 0:15c6e1cab70a | 23 | |
anthonyv | 7:8509fb453a2e | 24 | uint8_t* CayenneLPP::getBuffer(void) |
anthonyv | 7:8509fb453a2e | 25 | { |
eptak | 0:15c6e1cab70a | 26 | // uint8_t[cursor] result; |
eptak | 0:15c6e1cab70a | 27 | // memcpy(result, buffer, cursor); |
eptak | 0:15c6e1cab70a | 28 | // return result; |
eptak | 0:15c6e1cab70a | 29 | return buffer; |
eptak | 0:15c6e1cab70a | 30 | } |
eptak | 0:15c6e1cab70a | 31 | |
anthonyv | 7:8509fb453a2e | 32 | uint8_t CayenneLPP::copy(uint8_t* dst) |
anthonyv | 7:8509fb453a2e | 33 | { |
eptak | 0:15c6e1cab70a | 34 | memcpy(dst, buffer, cursor); |
eptak | 0:15c6e1cab70a | 35 | return cursor; |
eptak | 0:15c6e1cab70a | 36 | } |
eptak | 0:15c6e1cab70a | 37 | |
anthonyv | 7:8509fb453a2e | 38 | uint8_t CayenneLPP::addDigitalInput(uint8_t channel, uint8_t value) |
anthonyv | 7:8509fb453a2e | 39 | { |
eptak | 0:15c6e1cab70a | 40 | if ((cursor + LPP_DIGITAL_INPUT_SIZE) > maxsize) { |
eptak | 0:15c6e1cab70a | 41 | return 0; |
eptak | 0:15c6e1cab70a | 42 | } |
anthonyv | 7:8509fb453a2e | 43 | buffer[cursor++] = channel; |
anthonyv | 7:8509fb453a2e | 44 | buffer[cursor++] = LPP_DIGITAL_INPUT; |
anthonyv | 7:8509fb453a2e | 45 | buffer[cursor++] = value; |
eptak | 0:15c6e1cab70a | 46 | |
eptak | 0:15c6e1cab70a | 47 | return cursor; |
eptak | 0:15c6e1cab70a | 48 | } |
eptak | 0:15c6e1cab70a | 49 | |
anthonyv | 7:8509fb453a2e | 50 | uint8_t CayenneLPP::addDigitalOutput(uint8_t channel, uint8_t value) |
anthonyv | 7:8509fb453a2e | 51 | { |
eptak | 0:15c6e1cab70a | 52 | if ((cursor + LPP_DIGITAL_OUTPUT_SIZE) > maxsize) { |
eptak | 0:15c6e1cab70a | 53 | return 0; |
eptak | 0:15c6e1cab70a | 54 | } |
anthonyv | 7:8509fb453a2e | 55 | buffer[cursor++] = channel; |
anthonyv | 7:8509fb453a2e | 56 | buffer[cursor++] = LPP_DIGITAL_OUTPUT; |
anthonyv | 7:8509fb453a2e | 57 | buffer[cursor++] = value; |
eptak | 0:15c6e1cab70a | 58 | |
eptak | 0:15c6e1cab70a | 59 | return cursor; |
eptak | 0:15c6e1cab70a | 60 | } |
eptak | 0:15c6e1cab70a | 61 | |
anthonyv | 7:8509fb453a2e | 62 | uint8_t CayenneLPP::addAnalogInput(uint8_t channel, float value) |
anthonyv | 7:8509fb453a2e | 63 | { |
eptak | 0:15c6e1cab70a | 64 | if ((cursor + LPP_ANALOG_INPUT_SIZE) > maxsize) { |
eptak | 0:15c6e1cab70a | 65 | return 0; |
eptak | 0:15c6e1cab70a | 66 | } |
anthonyv | 7:8509fb453a2e | 67 | |
eptak | 0:15c6e1cab70a | 68 | int16_t val = value * 100; |
anthonyv | 7:8509fb453a2e | 69 | buffer[cursor++] = channel; |
anthonyv | 7:8509fb453a2e | 70 | buffer[cursor++] = LPP_ANALOG_INPUT; |
anthonyv | 7:8509fb453a2e | 71 | buffer[cursor++] = val >> 8; |
anthonyv | 7:8509fb453a2e | 72 | buffer[cursor++] = val; |
eptak | 0:15c6e1cab70a | 73 | |
eptak | 0:15c6e1cab70a | 74 | return cursor; |
eptak | 0:15c6e1cab70a | 75 | } |
eptak | 0:15c6e1cab70a | 76 | |
anthonyv | 7:8509fb453a2e | 77 | uint8_t CayenneLPP::addAnalogOutput(uint8_t channel, float value) |
anthonyv | 7:8509fb453a2e | 78 | { |
eptak | 0:15c6e1cab70a | 79 | if ((cursor + LPP_ANALOG_OUTPUT_SIZE) > maxsize) { |
eptak | 0:15c6e1cab70a | 80 | return 0; |
eptak | 0:15c6e1cab70a | 81 | } |
eptak | 0:15c6e1cab70a | 82 | int16_t val = value * 100; |
anthonyv | 7:8509fb453a2e | 83 | buffer[cursor++] = channel; |
eptak | 0:15c6e1cab70a | 84 | buffer[cursor++] = LPP_ANALOG_OUTPUT; |
anthonyv | 7:8509fb453a2e | 85 | buffer[cursor++] = val >> 8; |
anthonyv | 7:8509fb453a2e | 86 | buffer[cursor++] = val; |
anthonyv | 7:8509fb453a2e | 87 | |
eptak | 0:15c6e1cab70a | 88 | return cursor; |
eptak | 0:15c6e1cab70a | 89 | } |
eptak | 0:15c6e1cab70a | 90 | |
anthonyv | 7:8509fb453a2e | 91 | uint8_t CayenneLPP::addLuminosity(uint8_t channel, uint16_t lux) |
anthonyv | 7:8509fb453a2e | 92 | { |
eptak | 0:15c6e1cab70a | 93 | if ((cursor + LPP_LUMINOSITY_SIZE) > maxsize) { |
eptak | 0:15c6e1cab70a | 94 | return 0; |
eptak | 0:15c6e1cab70a | 95 | } |
anthonyv | 7:8509fb453a2e | 96 | buffer[cursor++] = channel; |
anthonyv | 7:8509fb453a2e | 97 | buffer[cursor++] = LPP_LUMINOSITY; |
anthonyv | 7:8509fb453a2e | 98 | buffer[cursor++] = lux >> 8; |
anthonyv | 7:8509fb453a2e | 99 | buffer[cursor++] = lux; |
anthonyv | 7:8509fb453a2e | 100 | |
anthonyv | 7:8509fb453a2e | 101 | return cursor; |
anthonyv | 7:8509fb453a2e | 102 | } |
anthonyv | 7:8509fb453a2e | 103 | |
anthonyv | 7:8509fb453a2e | 104 | uint8_t CayenneLPP::addPresence(uint8_t channel, uint8_t value) |
anthonyv | 7:8509fb453a2e | 105 | { |
anthonyv | 7:8509fb453a2e | 106 | if ((cursor + LPP_PRESENCE_SIZE) > maxsize) { |
anthonyv | 7:8509fb453a2e | 107 | return 0; |
anthonyv | 7:8509fb453a2e | 108 | } |
anthonyv | 7:8509fb453a2e | 109 | buffer[cursor++] = channel; |
anthonyv | 7:8509fb453a2e | 110 | buffer[cursor++] = LPP_PRESENCE; |
anthonyv | 7:8509fb453a2e | 111 | buffer[cursor++] = value; |
eptak | 0:15c6e1cab70a | 112 | |
eptak | 0:15c6e1cab70a | 113 | return cursor; |
eptak | 0:15c6e1cab70a | 114 | } |
eptak | 0:15c6e1cab70a | 115 | |
anthonyv | 7:8509fb453a2e | 116 | |
anthonyv | 7:8509fb453a2e | 117 | uint8_t CayenneLPP::addNFC(uint8_t channel, char * NFC) |
anthonyv | 7:8509fb453a2e | 118 | { |
anthonyv | 7:8509fb453a2e | 119 | int index; |
anthonyv | 7:8509fb453a2e | 120 | buffer[cursor++] = channel; |
anthonyv | 7:8509fb453a2e | 121 | for (index=0; index <4; index++) { |
anthonyv | 7:8509fb453a2e | 122 | buffer[cursor++]=NFC[index] ; |
eptak | 0:15c6e1cab70a | 123 | } |
eptak | 0:15c6e1cab70a | 124 | return cursor; |
eptak | 0:15c6e1cab70a | 125 | } |
eptak | 0:15c6e1cab70a | 126 | |
superphil06 | 8:2b94df050b00 | 127 | uint8_t CayenneLPP::addVEHICLE(uint8_t channel, char * VEH) |
superphil06 | 8:2b94df050b00 | 128 | { |
superphil06 | 8:2b94df050b00 | 129 | int index; |
superphil06 | 8:2b94df050b00 | 130 | buffer[cursor++] = channel; |
superphil06 | 8:2b94df050b00 | 131 | for (index=0; index <4; index++) { |
superphil06 | 8:2b94df050b00 | 132 | buffer[cursor++]=VEH[index] ; |
superphil06 | 8:2b94df050b00 | 133 | } |
superphil06 | 8:2b94df050b00 | 134 | return cursor; |
superphil06 | 8:2b94df050b00 | 135 | } |
superphil06 | 8:2b94df050b00 | 136 | |
superphil06 | 8:2b94df050b00 | 137 | |
anthonyv | 7:8509fb453a2e | 138 | |
anthonyv | 7:8509fb453a2e | 139 | uint8_t CayenneLPP::addTemperature(uint8_t channel, float celsius) |
anthonyv | 7:8509fb453a2e | 140 | { |
eptak | 0:15c6e1cab70a | 141 | if ((cursor + LPP_TEMPERATURE_SIZE) > maxsize) { |
eptak | 0:15c6e1cab70a | 142 | return 0; |
eptak | 0:15c6e1cab70a | 143 | } |
eptak | 0:15c6e1cab70a | 144 | int16_t val = celsius * 10; |
anthonyv | 7:8509fb453a2e | 145 | buffer[cursor++] = channel; |
anthonyv | 7:8509fb453a2e | 146 | buffer[cursor++] = LPP_TEMPERATURE; |
anthonyv | 7:8509fb453a2e | 147 | buffer[cursor++] = val >> 8; |
anthonyv | 7:8509fb453a2e | 148 | buffer[cursor++] = val; |
eptak | 0:15c6e1cab70a | 149 | |
eptak | 0:15c6e1cab70a | 150 | return cursor; |
eptak | 0:15c6e1cab70a | 151 | } |
eptak | 0:15c6e1cab70a | 152 | |
anthonyv | 7:8509fb453a2e | 153 | uint8_t CayenneLPP::addRelativeHumidity(uint8_t channel, float rh) |
anthonyv | 7:8509fb453a2e | 154 | { |
eptak | 0:15c6e1cab70a | 155 | if ((cursor + LPP_RELATIVE_HUMIDITY_SIZE) > maxsize) { |
eptak | 0:15c6e1cab70a | 156 | return 0; |
eptak | 0:15c6e1cab70a | 157 | } |
anthonyv | 7:8509fb453a2e | 158 | buffer[cursor++] = channel; |
anthonyv | 7:8509fb453a2e | 159 | buffer[cursor++] = LPP_RELATIVE_HUMIDITY; |
anthonyv | 7:8509fb453a2e | 160 | buffer[cursor++] = rh * 2; |
eptak | 0:15c6e1cab70a | 161 | |
eptak | 0:15c6e1cab70a | 162 | return cursor; |
eptak | 0:15c6e1cab70a | 163 | } |
eptak | 0:15c6e1cab70a | 164 | |
anthonyv | 7:8509fb453a2e | 165 | uint8_t CayenneLPP::addAccelerometer(uint8_t channel, float x, float y, float z) |
anthonyv | 7:8509fb453a2e | 166 | { |
eptak | 0:15c6e1cab70a | 167 | if ((cursor + LPP_ACCELEROMETER_SIZE) > maxsize) { |
eptak | 0:15c6e1cab70a | 168 | return 0; |
eptak | 0:15c6e1cab70a | 169 | } |
eptak | 0:15c6e1cab70a | 170 | int16_t vx = x * 1000; |
eptak | 0:15c6e1cab70a | 171 | int16_t vy = y * 1000; |
eptak | 0:15c6e1cab70a | 172 | int16_t vz = z * 1000; |
anthonyv | 7:8509fb453a2e | 173 | |
anthonyv | 7:8509fb453a2e | 174 | buffer[cursor++] = channel; |
anthonyv | 7:8509fb453a2e | 175 | buffer[cursor++] = LPP_ACCELEROMETER; |
anthonyv | 7:8509fb453a2e | 176 | buffer[cursor++] = vx >> 8; |
anthonyv | 7:8509fb453a2e | 177 | buffer[cursor++] = vx; |
anthonyv | 7:8509fb453a2e | 178 | buffer[cursor++] = vy >> 8; |
anthonyv | 7:8509fb453a2e | 179 | buffer[cursor++] = vy; |
anthonyv | 7:8509fb453a2e | 180 | buffer[cursor++] = vz >> 8; |
anthonyv | 7:8509fb453a2e | 181 | buffer[cursor++] = vz; |
eptak | 0:15c6e1cab70a | 182 | |
eptak | 0:15c6e1cab70a | 183 | return cursor; |
eptak | 0:15c6e1cab70a | 184 | } |
eptak | 0:15c6e1cab70a | 185 | |
anthonyv | 7:8509fb453a2e | 186 | uint8_t CayenneLPP::addBarometricPressure(uint8_t channel, float hpa) |
anthonyv | 7:8509fb453a2e | 187 | { |
eptak | 0:15c6e1cab70a | 188 | if ((cursor + LPP_BAROMETRIC_PRESSURE_SIZE) > maxsize) { |
eptak | 0:15c6e1cab70a | 189 | return 0; |
eptak | 0:15c6e1cab70a | 190 | } |
eptak | 0:15c6e1cab70a | 191 | int16_t val = hpa * 10; |
anthonyv | 7:8509fb453a2e | 192 | |
anthonyv | 7:8509fb453a2e | 193 | buffer[cursor++] = channel; |
anthonyv | 7:8509fb453a2e | 194 | buffer[cursor++] = LPP_BAROMETRIC_PRESSURE; |
anthonyv | 7:8509fb453a2e | 195 | buffer[cursor++] = val >> 8; |
anthonyv | 7:8509fb453a2e | 196 | buffer[cursor++] = val; |
eptak | 0:15c6e1cab70a | 197 | |
eptak | 0:15c6e1cab70a | 198 | return cursor; |
eptak | 0:15c6e1cab70a | 199 | } |
eptak | 0:15c6e1cab70a | 200 | |
anthonyv | 7:8509fb453a2e | 201 | uint8_t CayenneLPP::addGyrometer(uint8_t channel, float x, float y, float z) |
anthonyv | 7:8509fb453a2e | 202 | { |
eptak | 0:15c6e1cab70a | 203 | if ((cursor + LPP_GYROMETER_SIZE) > maxsize) { |
eptak | 0:15c6e1cab70a | 204 | return 0; |
eptak | 0:15c6e1cab70a | 205 | } |
eptak | 0:15c6e1cab70a | 206 | int16_t vx = x * 100; |
eptak | 0:15c6e1cab70a | 207 | int16_t vy = y * 100; |
eptak | 0:15c6e1cab70a | 208 | int16_t vz = z * 100; |
anthonyv | 7:8509fb453a2e | 209 | |
anthonyv | 7:8509fb453a2e | 210 | buffer[cursor++] = channel; |
anthonyv | 7:8509fb453a2e | 211 | buffer[cursor++] = LPP_GYROMETER; |
anthonyv | 7:8509fb453a2e | 212 | buffer[cursor++] = vx >> 8; |
anthonyv | 7:8509fb453a2e | 213 | buffer[cursor++] = vx; |
anthonyv | 7:8509fb453a2e | 214 | buffer[cursor++] = vy >> 8; |
anthonyv | 7:8509fb453a2e | 215 | buffer[cursor++] = vy; |
anthonyv | 7:8509fb453a2e | 216 | buffer[cursor++] = vz >> 8; |
anthonyv | 7:8509fb453a2e | 217 | buffer[cursor++] = vz; |
eptak | 0:15c6e1cab70a | 218 | |
eptak | 0:15c6e1cab70a | 219 | return cursor; |
eptak | 0:15c6e1cab70a | 220 | } |
eptak | 0:15c6e1cab70a | 221 | |
anthonyv | 7:8509fb453a2e | 222 | uint8_t CayenneLPP::addGPS(uint8_t channel, float latitude, float longitude, float meters) |
anthonyv | 7:8509fb453a2e | 223 | { |
eptak | 0:15c6e1cab70a | 224 | if ((cursor + LPP_GPS_SIZE) > maxsize) { |
eptak | 0:15c6e1cab70a | 225 | return 0; |
eptak | 0:15c6e1cab70a | 226 | } |
eptak | 0:15c6e1cab70a | 227 | int32_t lat = latitude * 10000; |
eptak | 0:15c6e1cab70a | 228 | int32_t lon = longitude * 10000; |
eptak | 0:15c6e1cab70a | 229 | int32_t alt = meters * 100; |
anthonyv | 7:8509fb453a2e | 230 | |
anthonyv | 7:8509fb453a2e | 231 | buffer[cursor++] = channel; |
anthonyv | 7:8509fb453a2e | 232 | buffer[cursor++] = LPP_GPS; |
eptak | 0:15c6e1cab70a | 233 | |
anthonyv | 7:8509fb453a2e | 234 | buffer[cursor++] = lat >> 16; |
anthonyv | 7:8509fb453a2e | 235 | buffer[cursor++] = lat >> 8; |
anthonyv | 7:8509fb453a2e | 236 | buffer[cursor++] = lat; |
anthonyv | 7:8509fb453a2e | 237 | buffer[cursor++] = lon >> 16; |
anthonyv | 7:8509fb453a2e | 238 | buffer[cursor++] = lon >> 8; |
anthonyv | 7:8509fb453a2e | 239 | buffer[cursor++] = lon; |
anthonyv | 7:8509fb453a2e | 240 | buffer[cursor++] = alt >> 16; |
eptak | 0:15c6e1cab70a | 241 | buffer[cursor++] = alt >> 8; |
eptak | 0:15c6e1cab70a | 242 | buffer[cursor++] = alt; |
eptak | 0:15c6e1cab70a | 243 | |
eptak | 0:15c6e1cab70a | 244 | return cursor; |
eptak | 0:15c6e1cab70a | 245 | } |
eptak | 0:15c6e1cab70a | 246 |