Introduce TPMS measurement types, change formatting.

This commit is contained in:
Jared Boone 2016-01-22 10:59:41 -08:00
parent bd0ec913f5
commit b70138ad58
2 changed files with 120 additions and 43 deletions

View File

@ -46,10 +46,32 @@ Optional<Reading> Packet::reading() const {
const auto length = crc_valid_length(); const auto length = crc_valid_length();
switch(length) { switch(length) {
case 64: return Reading { Reading::Type::FLM_64, reader_.read(0, 32), reader_.read(32, 8) }; case 64:
case 72: return Reading { Reading::Type::FLM_72, reader_.read(0, 32), reader_.read(40, 8), reader_.read(48, 8), reader_.read(56, 8) }; return Reading {
case 80: return Reading { Reading::Type::FLM_80, reader_.read(8, 32), reader_.read(48, 8), reader_.read(56, 8), reader_.read(64, 8) }; Reading::Type::FLM_64,
default: return { }; reader_.read(0, 32),
Pressure { static_cast<int32_t>(reader_.read(32, 8)) * 4 / 3 },
Temperature { static_cast<int32_t>(reader_.read(40, 8) & 0x7f) - 50 }
};
case 72:
return Reading {
Reading::Type::FLM_72,
reader_.read(0, 32),
Pressure { static_cast<int32_t>(reader_.read(40, 8)) * 4 / 3 },
Temperature { static_cast<int32_t>(reader_.read(48, 8)) - 50 }
};
case 80:
return Reading {
Reading::Type::FLM_80,
reader_.read(8, 32),
Pressure { static_cast<int32_t>(reader_.read(48, 8)) * 4 / 3 },
Temperature { static_cast<int32_t>(reader_.read(56, 8)) - 50 }
};
default:
return { };
} }
} }
@ -149,13 +171,12 @@ void TPMSAppView::draw(const tpms::Packet& packet) {
const auto reading_opt = packet.reading(); const auto reading_opt = packet.reading();
if( reading_opt.is_valid() ) { if( reading_opt.is_valid() ) {
const auto reading = reading_opt.value(); const auto reading = reading_opt.value();
auto s = to_string_dec_uint(toUType(reading.type()), 2) + " " + to_string_hex(reading.id(), 8); auto s = to_string_dec_uint(toUType(reading.type()), 2) + " " + to_string_hex(reading.id().value(), 8);
s += " " + to_string_hex(reading.value_1(), 2); if( reading.pressure().is_valid() ) {
if( reading.value_2().is_valid() ) { s += " " + to_string_dec_int(reading.pressure().value().kilopascal(), 3);
s += " " + to_string_hex(reading.value_2().value(), 2);
} }
if( reading.value_3().is_valid() ) { if( reading.temperature().is_valid() ) {
s += " " + to_string_hex(reading.value_3().value(), 2); s += " " + to_string_dec_int(reading.temperature().value().celsius(), 3);
} }
console.writeln(s); console.writeln(s);
} }

View File

@ -46,6 +46,77 @@ private:
const bool valid_; 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<int16_t>(kilopascal) }
{
}
int16_t kilopascal() const {
return kpa_;
}
int16_t psi() const {
return static_cast<int32_t>(kpa_) * 1000 / 6895;
}
private:
int16_t kpa_;
};
class Temperature {
public:
constexpr Temperature(
) : c_ { 0 }
{
}
constexpr Temperature(
const int32_t celsius
) : c_ { static_cast<int16_t>(celsius) }
{
}
int16_t celsius() const {
return c_;
}
int16_t fahrenheit() const {
return (c_ * 9 / 5) + 32;
}
private:
int16_t c_;
};
class Reading { class Reading {
public: public:
enum Type { enum Type {
@ -56,37 +127,27 @@ public:
}; };
constexpr Reading( constexpr Reading(
) : type_ { Type::None }, ) : type_ { Type::None }
id_ { 0 },
value_1_ { 0 },
value_2_ { 0 },
value_3_ { 0 }
{ {
} }
constexpr Reading( constexpr Reading(
Type type, Type type,
uint32_t id, TransponderID id
uint16_t value_1
) : type_ { type }, ) : type_ { type },
id_ { id }, id_ { id }
value_1_ { value_1 },
value_2_ { },
value_3_ { }
{ {
} }
constexpr Reading( constexpr Reading(
Type type, Type type,
uint32_t id, TransponderID id,
uint16_t value_1, Optional<Pressure> pressure = { },
uint16_t value_2, Optional<Temperature> temperature = { }
uint16_t value_3
) : type_ { type }, ) : type_ { type },
id_ { id }, id_ { id },
value_1_ { value_1 }, pressure_ { pressure },
value_2_ { value_2 }, temperature_ { temperature }
value_3_ { value_3 }
{ {
} }
@ -94,28 +155,23 @@ public:
return type_; return type_;
} }
uint32_t id() const { TransponderID id() const {
return id_; return id_;
} }
uint16_t value_1() const { Optional<Pressure> pressure() const {
return value_1_; return pressure_;
} }
Optional<uint16_t> value_2() const { Optional<Temperature> temperature() const {
return value_2_; return temperature_;
}
Optional<uint16_t> value_3() const {
return value_3_;
} }
private: private:
Type type_; Type type_ { Type::None };
uint32_t id_; TransponderID id_ { 0 };
uint16_t value_1_; Optional<Pressure> pressure_ { };
Optional<uint16_t> value_2_; Optional<Temperature> temperature_ { };
Optional<uint16_t> value_3_;
}; };
class Packet { class Packet {