diff --git a/firmware/application/tpms_app.cpp b/firmware/application/tpms_app.cpp index b8076363..10c35861 100644 --- a/firmware/application/tpms_app.cpp +++ b/firmware/application/tpms_app.cpp @@ -46,10 +46,32 @@ Optional Packet::reading() const { const auto length = crc_valid_length(); switch(length) { - case 64: return Reading { Reading::Type::FLM_64, reader_.read(0, 32), reader_.read(32, 8) }; - case 72: return Reading { Reading::Type::FLM_72, reader_.read(0, 32), reader_.read(40, 8), reader_.read(48, 8), reader_.read(56, 8) }; - case 80: return Reading { Reading::Type::FLM_80, reader_.read(8, 32), reader_.read(48, 8), reader_.read(56, 8), reader_.read(64, 8) }; - default: return { }; + case 64: + return Reading { + Reading::Type::FLM_64, + reader_.read(0, 32), + Pressure { static_cast(reader_.read(32, 8)) * 4 / 3 }, + Temperature { static_cast(reader_.read(40, 8) & 0x7f) - 50 } + }; + + case 72: + return Reading { + Reading::Type::FLM_72, + reader_.read(0, 32), + Pressure { static_cast(reader_.read(40, 8)) * 4 / 3 }, + Temperature { static_cast(reader_.read(48, 8)) - 50 } + }; + + case 80: + return Reading { + Reading::Type::FLM_80, + reader_.read(8, 32), + Pressure { static_cast(reader_.read(48, 8)) * 4 / 3 }, + Temperature { static_cast(reader_.read(56, 8)) - 50 } + }; + + default: + return { }; } } @@ -149,13 +171,12 @@ void TPMSAppView::draw(const tpms::Packet& packet) { const auto reading_opt = packet.reading(); if( reading_opt.is_valid() ) { const auto reading = reading_opt.value(); - auto s = to_string_dec_uint(toUType(reading.type()), 2) + " " + to_string_hex(reading.id(), 8); - s += " " + to_string_hex(reading.value_1(), 2); - if( reading.value_2().is_valid() ) { - s += " " + to_string_hex(reading.value_2().value(), 2); + auto s = to_string_dec_uint(toUType(reading.type()), 2) + " " + to_string_hex(reading.id().value(), 8); + if( reading.pressure().is_valid() ) { + s += " " + to_string_dec_int(reading.pressure().value().kilopascal(), 3); } - if( reading.value_3().is_valid() ) { - s += " " + to_string_hex(reading.value_3().value(), 2); + if( reading.temperature().is_valid() ) { + s += " " + to_string_dec_int(reading.temperature().value().celsius(), 3); } console.writeln(s); } diff --git a/firmware/application/tpms_app.hpp b/firmware/application/tpms_app.hpp index e8c39da1..3158b701 100644 --- a/firmware/application/tpms_app.hpp +++ b/firmware/application/tpms_app.hpp @@ -46,6 +46,77 @@ private: const bool valid_; }; +class TransponderID { +public: + constexpr TransponderID( + ) : id_ { 0 } + { + } + + constexpr TransponderID( + const uint32_t id + ) : id_ { id } + { + } + + uint32_t value() const { + return id_; + } + +private: + uint32_t id_; +}; + +class Pressure { +public: + constexpr Pressure( + ) : kpa_ { 0 } + { + } + + constexpr Pressure( + const int32_t kilopascal + ) : kpa_ { static_cast(kilopascal) } + { + } + + int16_t kilopascal() const { + return kpa_; + } + + int16_t psi() const { + return static_cast(kpa_) * 1000 / 6895; + } + +private: + int16_t kpa_; +}; + +class Temperature { +public: + constexpr Temperature( + ) : c_ { 0 } + { + } + + constexpr Temperature( + const int32_t celsius + ) : c_ { static_cast(celsius) } + { + } + + int16_t celsius() const { + return c_; + } + + int16_t fahrenheit() const { + return (c_ * 9 / 5) + 32; + } + +private: + int16_t c_; +}; + class Reading { public: enum Type { @@ -56,37 +127,27 @@ public: }; constexpr Reading( - ) : type_ { Type::None }, - id_ { 0 }, - value_1_ { 0 }, - value_2_ { 0 }, - value_3_ { 0 } + ) : type_ { Type::None } { } constexpr Reading( Type type, - uint32_t id, - uint16_t value_1 + TransponderID id ) : type_ { type }, - id_ { id }, - value_1_ { value_1 }, - value_2_ { }, - value_3_ { } + id_ { id } { } - + constexpr Reading( Type type, - uint32_t id, - uint16_t value_1, - uint16_t value_2, - uint16_t value_3 + TransponderID id, + Optional pressure = { }, + Optional temperature = { } ) : type_ { type }, id_ { id }, - value_1_ { value_1 }, - value_2_ { value_2 }, - value_3_ { value_3 } + pressure_ { pressure }, + temperature_ { temperature } { } @@ -94,28 +155,23 @@ public: return type_; } - uint32_t id() const { + TransponderID id() const { return id_; } - uint16_t value_1() const { - return value_1_; + Optional pressure() const { + return pressure_; } - Optional value_2() const { - return value_2_; - } - - Optional value_3() const { - return value_3_; + Optional temperature() const { + return temperature_; } private: - Type type_; - uint32_t id_; - uint16_t value_1_; - Optional value_2_; - Optional value_3_; + Type type_ { Type::None }; + TransponderID id_ { 0 }; + Optional pressure_ { }; + Optional temperature_ { }; }; class Packet {