ford.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /* Copyright (C) 2022-2023 Salvatore Sanfilippo -- All Rights Reserved
  2. * See the LICENSE file for information about the license.
  3. *
  4. * Ford tires TPMS. Usually 443.92 Mhz FSK (in Europe).
  5. *
  6. * 52 us short pules
  7. * Preamble: 0101010101010101010101010101
  8. * Sync: 0110 (that is 52 us gap + 104 us pulse + 52 us gap)
  9. * Data: 8 bytes Manchester encoded
  10. * 01 = zero
  11. * 10 = one
  12. */
  13. #include "../../app.h"
  14. static bool decode(uint8_t* bits, uint32_t numbytes, uint32_t numbits, ProtoViewMsgInfo* info) {
  15. const char* sync_pattern = "010101010101"
  16. "0110";
  17. uint8_t sync_len = 12 + 4; /* We just use 12 preamble symbols + sync. */
  18. if(numbits - sync_len < 8 * 8) return false;
  19. uint64_t off = bitmap_seek_bits(bits, numbytes, 0, numbits, sync_pattern);
  20. if(off == BITMAP_SEEK_NOT_FOUND) return false;
  21. FURI_LOG_E(TAG, "Fort TPMS preamble+sync found");
  22. info->start_off = off;
  23. off += sync_len; /* Skip preamble and sync. */
  24. uint8_t raw[8];
  25. uint32_t decoded = convert_from_line_code(
  26. raw, sizeof(raw), bits, numbytes, off, "01", "10"); /* Manchester. */
  27. FURI_LOG_E(TAG, "Ford TPMS decoded bits: %lu", decoded);
  28. if(decoded < 8 * 8) return false; /* Require the full 8 bytes. */
  29. /* CRC is just the sum of the first 7 bytes MOD 256. */
  30. uint8_t crc = 0;
  31. for(int j = 0; j < 7; j++) crc += raw[j];
  32. if(crc != raw[7]) return false; /* Require sane CRC. */
  33. info->pulses_count = (off + 8 * 8 * 2) - info->start_off;
  34. float psi = 0.25 * (((raw[6] & 0x20) << 3) | raw[4]);
  35. /* Temperature apperas to be valid only if the most significant
  36. * bit of the value is not set. Otherwise its meaning is unknown.
  37. * Likely useful to alternatively send temperature or other info. */
  38. int temp = raw[5] & 0x80 ? 0 : raw[5] - 56;
  39. int flags = raw[5] & 0x7f;
  40. int car_moving = (raw[6] & 0x44) == 0x44;
  41. fieldset_add_bytes(info->fieldset, "Tire ID", raw, 4 * 2);
  42. fieldset_add_float(info->fieldset, "Pressure psi", psi, 2);
  43. fieldset_add_int(info->fieldset, "Temperature C", temp, 8);
  44. fieldset_add_hex(info->fieldset, "Flags", flags, 7);
  45. fieldset_add_uint(info->fieldset, "Moving", car_moving, 1);
  46. return true;
  47. }
  48. ProtoViewDecoder FordTPMSDecoder =
  49. {.name = "Ford TPMS", .decode = decode, .get_fields = NULL, .build_message = NULL};