Refactor digital mode handlers into Model and View classes.

This commit is contained in:
Jared Boone 2015-12-01 17:29:54 -08:00
parent ec977ccd05
commit f49cdfdd99

View File

@ -499,26 +499,6 @@ ReceiverView::~ReceiverView() {
} }
void ReceiverView::on_show() { void ReceiverView::on_show() {
auto& message_map = context().message_map();
message_map.register_handler(Message::ID::AISPacket,
[this](Message* const p) {
const auto message = static_cast<const AISPacketMessage*>(p);
this->on_packet_ais(*message);
}
);
message_map.register_handler(Message::ID::TPMSPacket,
[this](Message* const p) {
const auto message = static_cast<const TPMSPacketMessage*>(p);
this->on_packet_tpms(*message);
}
);
message_map.register_handler(Message::ID::ERTPacket,
[this](Message* const p) {
const auto message = static_cast<const ERTPacketMessage*>(p);
this->on_packet_ert(*message);
}
);
sd_card_status_signal_token = sd_card::status_signal += [this](const sd_card::Status status) { sd_card_status_signal_token = sd_card::status_signal += [this](const sd_card::Status status) {
this->on_sd_card_status(status); this->on_sd_card_status(status);
}; };
@ -526,20 +506,6 @@ void ReceiverView::on_show() {
void ReceiverView::on_hide() { void ReceiverView::on_hide() {
sd_card::status_signal -= sd_card_status_signal_token; sd_card::status_signal -= sd_card_status_signal_token;
auto& message_map = context().message_map();
message_map.unregister_handler(Message::ID::ERTPacket);
message_map.unregister_handler(Message::ID::TPMSPacket);
message_map.unregister_handler(Message::ID::AISPacket);
}
void ReceiverView::on_packet_ais(const AISPacketMessage& message) {
const auto result = baseband::ais::packet_decode(message.packet.payload, message.packet.bits_received);
auto console = reinterpret_cast<Console*>(widget_content.get());
if( result.first == "OK" ) {
console->writeln(result.second);
}
} }
static FIL fil_tpms; static FIL fil_tpms;
@ -619,13 +585,52 @@ static ManchesterFormatted format_manchester(
return { hex_data, hex_error }; return { hex_data, hex_error };
} }
void ReceiverView::on_packet_tpms(const TPMSPacketMessage& message) { class AISModel {
public:
baseband::ais::decoded_packet on_packet(const AISPacketMessage& message) {
return baseband::ais::packet_decode(message.packet.payload, message.packet.bits_received);
}
private:
};
class AISView : public Console {
public:
void on_show() override {
Console::on_show();
auto& message_map = context().message_map();
message_map.register_handler(Message::ID::AISPacket,
[this](Message* const p) {
const auto message = static_cast<const AISPacketMessage*>(p);
this->log(this->model.on_packet(*message));
}
);
}
void on_hide() override {
auto& message_map = context().message_map();
message_map.unregister_handler(Message::ID::AISPacket);
Console::on_hide();
}
private:
AISModel model;
void log(const baseband::ais::decoded_packet decoded) {
if( decoded.first == "OK" ) {
writeln(decoded.second);
}
}
};
class TPMSModel {
public:
ManchesterFormatted on_packet(const TPMSPacketMessage& message) {
const ManchesterDecoder decoder(message.packet.payload, message.packet.bits_received, 1); const ManchesterDecoder decoder(message.packet.payload, message.packet.bits_received, 1);
const auto hex_formatted = format_manchester(decoder); const auto hex_formatted = format_manchester(decoder);
auto console = reinterpret_cast<Console*>(widget_content.get());
console->writeln(hex_formatted.data.substr(0, 240 / 8));
if( !f_error(&fil_tpms) ) { if( !f_error(&fil_tpms) ) {
rtc::RTC datetime; rtc::RTC datetime;
rtcGetTime(&RTCD1, &datetime); rtcGetTime(&RTCD1, &datetime);
@ -645,25 +650,97 @@ void ReceiverView::on_packet_tpms(const TPMSPacketMessage& message) {
f_puts(log.c_str(), &fil_tpms); f_puts(log.c_str(), &fil_tpms);
f_sync(&fil_tpms); f_sync(&fil_tpms);
} }
return hex_formatted;
} }
void ReceiverView::on_packet_ert(const ERTPacketMessage& message) { private:
auto console = reinterpret_cast<Console*>(widget_content.get()); };
class TPMSView : public Console {
public:
void on_show() override {
Console::on_show();
auto& message_map = context().message_map();
message_map.register_handler(Message::ID::TPMSPacket,
[this](Message* const p) {
const auto message = static_cast<const TPMSPacketMessage*>(p);
this->log(this->model.on_packet(*message));
}
);
}
void on_hide() override {
auto& message_map = context().message_map();
message_map.unregister_handler(Message::ID::TPMSPacket);
Console::on_hide();
}
private:
TPMSModel model;
void log(const ManchesterFormatted& formatted) {
writeln(formatted.data.substr(0, 240 / 8));
}
};
class ERTModel {
public:
std::string on_packet(const ERTPacketMessage& message) {
std::string s;
if( message.packet.preamble == 0x555516a3 ) { if( message.packet.preamble == 0x555516a3 ) {
console->writeln("IDM"); s += "IDM\n";
} }
if( message.packet.preamble == 0x1f2a60 ) { if( message.packet.preamble == 0x1f2a60 ) {
console->writeln("SCM"); s += "SCM\n";
} }
const ManchesterDecoder decoder(message.packet.payload, message.packet.bits_received); const ManchesterDecoder decoder(message.packet.payload, message.packet.bits_received);
const auto hex_formatted = format_manchester(decoder); const auto hex_formatted = format_manchester(decoder);
console->writeln(hex_formatted.data); s += hex_formatted.data;
console->writeln(hex_formatted.errors); s += "\n";
s += hex_formatted.errors;
s += "\n";
return s;
} }
private:
};
class ERTView : public Console {
public:
void on_show() override {
Console::on_show();
auto& message_map = context().message_map();
message_map.register_handler(Message::ID::ERTPacket,
[this](Message* const p) {
const auto message = static_cast<const ERTPacketMessage*>(p);
this->log(this->model.on_packet(*message));
}
);
}
void on_hide() override {
auto& message_map = context().message_map();
message_map.unregister_handler(Message::ID::ERTPacket);
Console::on_hide();
}
private:
ERTModel model;
void log(const std::string& s) {
write(s);
}
};
void ReceiverView::on_sd_card_status(const sd_card::Status status) { void ReceiverView::on_sd_card_status(const sd_card::Status status) {
if( status == sd_card::Status::Mounted ) { if( status == sd_card::Status::Mounted ) {
const auto open_result = f_open(&fil_tpms, "tpms.txt", FA_WRITE | FA_OPEN_ALWAYS); const auto open_result = f_open(&fil_tpms, "tpms.txt", FA_WRITE | FA_OPEN_ALWAYS);
@ -754,9 +831,17 @@ void ReceiverView::on_modulation_changed(int32_t modulation) {
switch(modulation) { switch(modulation) {
case 3: case 3:
widget_content = std::make_unique<AISView>();
add_child(widget_content.get());
break;
case 5: case 5:
widget_content = std::make_unique<TPMSView>();
add_child(widget_content.get());
break;
case 6: case 6:
widget_content = std::make_unique<Console>(); widget_content = std::make_unique<ERTView>();
add_child(widget_content.get()); add_child(widget_content.get());
break; break;