Remove SDCardStatusMessage, replace with Signal, extract to separate files.

This commit is contained in:
Jared Boone 2015-12-01 10:38:35 -08:00
parent 600295f0db
commit c70d95dcbe
7 changed files with 105 additions and 72 deletions

View File

@ -168,6 +168,7 @@ CPPSRC = main.cpp \
receiver_model.cpp \
spectrum_color_lut.cpp \
ais_baseband.cpp \
sd_card.cpp \
../common/utility.cpp \
../common/chibios_cpp.cpp \
../common/debug.cpp \

View File

@ -128,33 +128,8 @@ private:
}
}
void update_sd_card_status() {
const auto sd_card_present_now = sdc_lld_is_card_inserted(&SDCD1);
if( sd_card_present_now != sd_card_present ) {
sd_card_present = sd_card_present_now;
SDCardStatusMessage message { sd_card_present ? SDCardStatusMessage::State::Present : SDCardStatusMessage::State::NotPresent };
if( sd_card_present ) {
if( sdcConnect(&SDCD1) == CH_SUCCESS ) {
if( sd_card::filesystem::mount() == FR_OK ) {
message.state = SDCardStatusMessage::State::Mounted;
} else {
message.state = SDCardStatusMessage::State::MountError;
}
} else {
message.state = SDCardStatusMessage::State::ConnectError;
}
} else {
sdcDisconnect(&SDCD1);
}
context.message_map().send(&message);
}
}
void handle_rtc_tick() {
update_sd_card_status();
sd_card::poll_inserted();
}
static ui::Widget* touch_widget(ui::Widget* const w, ui::TouchEvent event) {

View File

@ -0,0 +1,73 @@
/*
* 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 "sd_card.hpp"
#include <hal.h>
#include "ff.h"
namespace sd_card {
namespace {
bool card_present = false;
FATFS fs;
FRESULT mount() {
return f_mount(&fs, "", 0);
}
FRESULT unmount() {
return f_mount(NULL, "", 0);
}
} /* namespace */
Signal<Status> status_signal;
void poll_inserted() {
const auto card_present_now = sdc_lld_is_card_inserted(&SDCD1);
if( card_present_now != card_present ) {
card_present = card_present_now;
Status status { card_present ? Status::Present : Status::NotPresent };
if( card_present ) {
if( sdcConnect(&SDCD1) == CH_SUCCESS ) {
if( mount() == FR_OK ) {
status = Status::Mounted;
} else {
status = Status::MountError;
}
} else {
status = Status::ConnectError;
}
} else {
sdcDisconnect(&SDCD1);
}
status_signal.emit(status);
}
}
} /* namespace sd_card */

View File

@ -22,26 +22,25 @@
#ifndef __SD_CARD_H__
#define __SD_CARD_H__
#include "ff.h"
#include <cstdint>
#include "signal.hpp"
namespace sd_card {
namespace filesystem {
namespace {
enum class Status : int32_t {
IOError = -3,
MountError = -2,
ConnectError = -1,
NotPresent = 0,
Present = 1,
Mounted = 2,
};
FATFS fs;
extern Signal<Status> status_signal;
}
void poll_inserted();
FRESULT mount() {
return f_mount(&fs, "", 0);
}
FRESULT unmount() {
return f_mount(NULL, "", 0);
}
} /* namespace filesystem */
} /* namespace sd_card */
#endif/*__SD_CARD_H__*/

View File

@ -29,6 +29,7 @@ using namespace portapack;
#include "ais_baseband.hpp"
#include "sd_card.hpp"
#include "ff.h"
namespace ui {
@ -511,17 +512,16 @@ void ReceiverView::on_show() {
this->on_packet_tpms(*message);
}
);
message_map.register_handler(Message::ID::SDCardStatus,
[this](Message* const p) {
const auto message = static_cast<const SDCardStatusMessage*>(p);
this->on_sd_card_status(*message);
}
);
sd_card_status_signal_token = sd_card::status_signal += [this](const sd_card::Status status) {
this->on_sd_card_status(status);
};
}
void ReceiverView::on_hide() {
sd_card::status_signal -= sd_card_status_signal_token;
auto& message_map = context().message_map();
message_map.unregister_handler(Message::ID::SDCardStatus);
message_map.unregister_handler(Message::ID::TPMSPacket);
message_map.unregister_handler(Message::ID::AISPacket);
}
@ -585,18 +585,23 @@ void ReceiverView::on_packet_tpms(const TPMSPacketMessage& message) {
}
}
void ReceiverView::on_sd_card_status(const SDCardStatusMessage& message) {
if( message.state == SDCardStatusMessage::State::Mounted ) {
void ReceiverView::on_sd_card_status(const sd_card::Status status) {
if( status == sd_card::Status::Mounted ) {
const auto open_result = f_open(&fil_tpms, "tpms.txt", FA_WRITE | FA_OPEN_ALWAYS);
if( open_result == FR_OK ) {
const auto fil_size = f_size(&fil_tpms);
const auto seek_result = f_lseek(&fil_tpms, fil_size);
if( seek_result != FR_OK ) {
f_close(&fil_tpms);
// TODO: Error, indicate somehow.
} else {
// TODO: Indicate success.
}
} else {
// TODO: Error, indicate somehow.
}
} else {
// TODO: Indicate unmounted.
}
}

View File

@ -475,7 +475,9 @@ private:
void on_packet_ais(const AISPacketMessage& message);
void on_packet_tpms(const TPMSPacketMessage& message);
void on_sd_card_status(const SDCardStatusMessage& message);
void on_sd_card_status(const sd_card::Status status);
SignalToken sd_card_status_signal_token;
};
} /* namespace ui */

View File

@ -46,7 +46,6 @@ public:
TPMSPacket = 6,
Shutdown = 8,
AISPacket = 7,
SDCardStatus = 10,
MAX
};
@ -241,27 +240,6 @@ public:
}
};
class SDCardStatusMessage : public Message {
public:
enum class State : int32_t {
IOError = -3,
MountError = -2,
ConnectError = -1,
NotPresent = 0,
Present = 1,
Mounted = 2,
};
constexpr SDCardStatusMessage(
State state
) : Message { ID::SDCardStatus },
state { state }
{
}
State state;
};
class MessageHandlerMap {
public:
using MessageHandler = std::function<void(Message* const p)>;