Return a String containing the key values of a decode_results structure in a C/C++ code style format. @param[in] results A ptr to a decode_results structure. @return A String containing the code-ified result.
| 250 | /// @param[in] results A ptr to a decode_results structure. |
| 251 | /// @return A String containing the code-ified result. |
| 252 | String resultToSourceCode(const decode_results * const results) { |
| 253 | String output = ""; |
| 254 | const uint16_t length = getCorrectedRawLength(results); |
| 255 | const bool hasState = hasACState(results->decode_type); |
| 256 | // Reserve some space for the string to reduce heap fragmentation. |
| 257 | // "uint16_t rawData[9999] = {}; // LONGEST_PROTOCOL\n" = ~55 chars. |
| 258 | // "NNNN, " = ~7 chars on average per raw entry |
| 259 | // Protocols with a `state`: |
| 260 | // "uint8_t state[NN] = {};\n" = ~25 chars |
| 261 | // "0xNN, " = 6 chars per byte. |
| 262 | // Protocols without a `state`: |
| 263 | // " DEADBEEFDEADBEEF\n" |
| 264 | // "uint32_t address = 0xDEADBEEF;\n" |
| 265 | // "uint32_t command = 0xDEADBEEF;\n" |
| 266 | // "uint64_t data = 0xDEADBEEFDEADBEEF;" = ~116 chars max. |
| 267 | output.reserve(55 + (length * 7) + hasState ? 25 + (results->bits / 8) * 6 |
| 268 | : 116); |
| 269 | // Start declaration |
| 270 | output += F("uint16_t "); // variable type |
| 271 | output += F("rawData["); // array name |
| 272 | output += uint64ToString(length, 10); |
| 273 | // array size |
| 274 | output += F("] = {"); // Start declaration |
| 275 | |
| 276 | // Dump data |
| 277 | for (uint16_t i = 1; i < results->rawlen; i++) { |
| 278 | uint32_t usecs; |
| 279 | for (usecs = results->rawbuf[i] * kRawTick; usecs > UINT16_MAX; |
| 280 | usecs -= UINT16_MAX) { |
| 281 | output += uint64ToString(UINT16_MAX); |
| 282 | if (i % 2) |
| 283 | output += F(", 0, "); |
| 284 | else |
| 285 | output += F(", 0, "); |
| 286 | } |
| 287 | output += uint64ToString(usecs, 10); |
| 288 | if (i < results->rawlen - 1) |
| 289 | output += kCommaSpaceStr; // ',' not needed on the last one |
| 290 | if (i % 2 == 0) output += ' '; // Extra if it was even. |
| 291 | } |
| 292 | |
| 293 | // End declaration |
| 294 | output += F("};"); |
| 295 | |
| 296 | // Comment |
| 297 | output += F(" // "); |
| 298 | output += typeToString(results->decode_type, results->repeat); |
| 299 | // Only display the value if the decode type doesn't have an A/C state. |
| 300 | if (!hasState) |
| 301 | output += ' ' + uint64ToString(results->value, 16); |
| 302 | output += F("\n"); |
| 303 | |
| 304 | // Now dump "known" codes |
| 305 | if (results->decode_type != UNKNOWN) { |
| 306 | if (hasState) { |
| 307 | #if DECODE_AC |
| 308 | uint16_t nbytes = ceil(static_cast<float>(results->bits) / 8.0); |
| 309 | output += F("uint8_t state["); |