diff --git a/firmware/application/tpms_app.cpp b/firmware/application/tpms_app.cpp index e13b7dcb..d264cba0 100644 --- a/firmware/application/tpms_app.cpp +++ b/firmware/application/tpms_app.cpp @@ -40,6 +40,17 @@ ManchesterFormatted Packet::symbols_formatted() const { return format_manchester(decoder_); } +Optional Packet::reading() const { + const auto length = crc_valid_length(); + + switch(length) { + case 64: return Reading { reader_.read(0, 32), reader_.read(32, 8) }; + case 72: return Reading { reader_.read(0, 32), reader_.read(40, 8), reader_.read(48, 8), reader_.read(56, 8) }; + case 80: return Reading { reader_.read(8, 32), reader_.read(48, 8), reader_.read(56, 8), reader_.read(64, 8) }; + default: return { }; + } +} + size_t Packet::crc_valid_length() const { constexpr uint32_t checksum_bytes = 0b1111111; constexpr uint32_t crc_72_bytes = 0b111111111; @@ -133,11 +144,17 @@ void TPMSAppView::on_packet(const tpms::Packet& packet) { } void TPMSAppView::draw(const tpms::Packet& packet) { - const auto packet_length = packet.crc_valid_length(); - if( packet_length > 0 ) { - const size_t length_chars = packet_length * 2 / 8; - const auto hex_formatted = packet.symbols_formatted(); - console.writeln(hex_formatted.data.substr(0, length_chars)); + const auto reading_opt = packet.reading(); + if( reading_opt.is_valid() ) { + const auto reading = reading_opt.value(); + auto s = to_string_hex(reading.id(), 8) + " " + to_string_hex(reading.value_1(), 2); + if( reading.value_2().is_valid() ) { + s += " " + to_string_hex(reading.value_2().value(), 2); + } + if( reading.value_3().is_valid() ) { + s += " " + to_string_hex(reading.value_3().value(), 2); + } + console.writeln(s); } } diff --git a/firmware/application/tpms_app.hpp b/firmware/application/tpms_app.hpp index 48104f4f..40931b04 100644 --- a/firmware/application/tpms_app.hpp +++ b/firmware/application/tpms_app.hpp @@ -31,6 +31,76 @@ namespace tpms { +template +class Optional { +public: + constexpr Optional() : value_ { }, valid_ { false } { }; + constexpr Optional(const T& value) : value_ { value }, valid_ { true } { }; + constexpr Optional(T&& value) : value_ { std::move(value) }, valid_ { true } { }; + + bool is_valid() const { return valid_; }; + T value() const { return value_; }; + +private: + const T value_; + const bool valid_; +}; + +class Reading { +public: + constexpr Reading( + ) : id_ { 0 }, + value_1_ { 0 }, + value_2_ { 0 }, + value_3_ { 0 } + { + } + + constexpr Reading( + uint32_t id, + uint16_t value_1 + ) : id_ { id }, + value_1_ { value_1 }, + value_2_ { }, + value_3_ { } + { + } + + constexpr Reading( + uint32_t id, + uint16_t value_1, + uint16_t value_2, + uint16_t value_3 + ) : id_ { id }, + value_1_ { value_1 }, + value_2_ { value_2 }, + value_3_ { value_3 } + { + } + + uint32_t id() const { + return id_; + } + + uint16_t value_1() const { + return value_1_; + } + + Optional value_2() const { + return value_2_; + } + + Optional value_3() const { + return value_3_; + } + +private: + uint32_t id_; + uint16_t value_1_; + Optional value_2_; + Optional value_3_; +}; + class Packet { public: constexpr Packet( @@ -45,7 +115,7 @@ public: ManchesterFormatted symbols_formatted() const; - size_t crc_valid_length() const; + Optional reading() const; private: using Reader = FieldReader; @@ -54,6 +124,8 @@ private: const ManchesterDecoder decoder_; const Reader reader_; + + size_t crc_valid_length() const; }; } /* namespace tpms */