From 635b1fd4c3f6a18fcb86f6603db90240aab8f49f Mon Sep 17 00:00:00 2001 From: Jared Boone Date: Wed, 6 Apr 2016 17:01:54 -0700 Subject: [PATCH] Extract TPMS packet stuff to separate files. --- firmware/application/Makefile | 1 + firmware/application/tpms_app.cpp | 83 ---------------- firmware/application/tpms_app.hpp | 118 +---------------------- firmware/common/tpms_packet.cpp | 129 +++++++++++++++++++++++++ firmware/common/tpms_packet.hpp | 155 ++++++++++++++++++++++++++++++ 5 files changed, 286 insertions(+), 200 deletions(-) create mode 100644 firmware/common/tpms_packet.cpp create mode 100644 firmware/common/tpms_packet.hpp diff --git a/firmware/application/Makefile b/firmware/application/Makefile index 6969eb19..a6bb3cb9 100755 --- a/firmware/application/Makefile +++ b/firmware/application/Makefile @@ -175,6 +175,7 @@ CPPSRC = main.cpp \ ../commom/ais_packet.cpp \ ais_app.cpp \ tpms_app.cpp \ + ../common/tpms_packet.cpp \ ert_app.cpp \ ../common/ert_packet.cpp \ sd_card.cpp \ diff --git a/firmware/application/tpms_app.cpp b/firmware/application/tpms_app.cpp index fb43e7d8..d83372f1 100644 --- a/firmware/application/tpms_app.cpp +++ b/firmware/application/tpms_app.cpp @@ -27,8 +27,6 @@ #include "string_format.hpp" -#include "crc.hpp" - #include "utility.hpp" namespace tpms { @@ -53,87 +51,6 @@ std::string temperature(Temperature temperature) { } /* namespace format */ -Timestamp Packet::received_at() const { - return packet_.timestamp(); -} - -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 { - 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 { }; - } -} - -size_t Packet::crc_valid_length() const { - constexpr uint32_t checksum_bytes = 0b1111111; - constexpr uint32_t crc_72_bytes = 0b111111111; - constexpr uint32_t crc_80_bytes = 0b1111111110; - - std::array bytes; - for(size_t i=0; i crc_72 { 0x01, 0x00 }; - CRC<8> crc_80 { 0x01, 0x00 }; - - for(size_t i=0; i pressure = { }, - Optional temperature = { } - ) : type_ { type }, - id_ { id }, - pressure_ { pressure }, - temperature_ { temperature } - { - } - - Type type() const { - return type_; - } - - TransponderID id() const { - return id_; - } - - Optional pressure() const { - return pressure_; - } - - Optional temperature() const { - return temperature_; - } - -private: - Type type_ { Type::None }; - TransponderID id_ { 0 }; - Optional pressure_ { }; - Optional temperature_ { }; -}; - -class Packet { -public: - constexpr Packet( - const baseband::Packet& packet - ) : packet_ { packet }, - decoder_ { packet_, 0 }, - reader_ { decoder_ } - { - } - - Timestamp received_at() const; - - ManchesterFormatted symbols_formatted() const; - - Optional reading() const; - -private: - using Reader = FieldReader; - - const baseband::Packet packet_; - const ManchesterDecoder decoder_; - - const Reader reader_; - - size_t crc_valid_length() const; -}; - -} /* namespace tpms */ +#include "tpms_packet.hpp" namespace std { diff --git a/firmware/common/tpms_packet.cpp b/firmware/common/tpms_packet.cpp new file mode 100644 index 00000000..7b18622b --- /dev/null +++ b/firmware/common/tpms_packet.cpp @@ -0,0 +1,129 @@ +/* + * Copyright (C) 2015 Jared Boone, ShareBrained Technology, Inc. + * + * This file is part of PortaPack. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#include "tpms_packet.hpp" + +#include "crc.hpp" + +namespace tpms { + +Timestamp Packet::received_at() const { + return packet_.timestamp(); +} + +ManchesterFormatted Packet::symbols_formatted() const { + return format_manchester(decoder_); +} + +Optional Packet::reading(const SignalType signal_type) const { + if( signal_type == SignalType::FLM ) { + const auto length = crc_valid_length(); + + switch(length) { + 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 { }; + } + } + + if( signal_type == SignalType::Subaru ) { + return Reading { + Reading::Type::SUB_35, + reader_.read(3, 25), + Pressure { static_cast(reader_.read(28, 8)) } + }; + } + + if( signal_type == SignalType::GMC ) { + return Reading { + Reading::Type::GMC_96, + reader_.read(20, 32), + Pressure { static_cast(reader_.read(52, 8)) } + }; + } + + return { }; +} + +size_t Packet::crc_valid_length() const { + constexpr uint32_t checksum_bytes = 0b1111111; + constexpr uint32_t crc_72_bytes = 0b111111111; + constexpr uint32_t crc_80_bytes = 0b1111111110; + + std::array bytes; + for(size_t i=0; i crc_72 { 0x01, 0x00 }; + CRC<8> crc_80 { 0x01, 0x00 }; + + for(size_t i=0; i +#include + +#include "optional.hpp" + +#include "units.hpp" +using units::Temperature; +using units::Pressure; + +#include "baseband_packet.hpp" +#include "manchester.hpp" +#include "field_reader.hpp" + +namespace tpms { + +enum SignalType : uint32_t { + FLM = 1, + Subaru = 2, + GMC = 3, +}; + +class TransponderID { +public: + constexpr TransponderID( + ) : id_ { 0 } + { + } + + constexpr TransponderID( + const uint32_t id + ) : id_ { id } + { + } + + constexpr uint32_t value() const { + return id_; + } + +private: + uint32_t id_; +}; + +class Reading { +public: + enum Type { + None = 0, + FLM_64 = 1, + FLM_72 = 2, + FLM_80 = 3, + SUB_35 = 4, + GMC_96 = 5, + }; + + constexpr Reading( + ) : type_ { Type::None } + { + } + + constexpr Reading( + Type type, + TransponderID id + ) : type_ { type }, + id_ { id } + { + } + + constexpr Reading( + Type type, + TransponderID id, + Optional pressure = { }, + Optional temperature = { } + ) : type_ { type }, + id_ { id }, + pressure_ { pressure }, + temperature_ { temperature } + { + } + + Type type() const { + return type_; + } + + TransponderID id() const { + return id_; + } + + Optional pressure() const { + return pressure_; + } + + Optional temperature() const { + return temperature_; + } + +private: + Type type_ { Type::None }; + TransponderID id_ { 0 }; + Optional pressure_ { }; + Optional temperature_ { }; +}; + +class Packet { +public: + constexpr Packet( + const baseband::Packet& packet + ) : packet_ { packet }, + decoder_ { packet_, 0 }, + reader_ { decoder_ } + { + } + + Timestamp received_at() const; + + ManchesterFormatted symbols_formatted() const; + + Optional reading(const SignalType signal_type) const; + +private: + using Reader = FieldReader; + + const baseband::Packet packet_; + const ManchesterDecoder decoder_; + + const Reader reader_; + + size_t crc_valid_length() const; +}; + +} /* namespace tpms */ + +#endif/*__TPMS_PACKET_H__*/