diff --git a/firmware/application/Makefile b/firmware/application/Makefile index c2b9237e..f02db587 100755 --- a/firmware/application/Makefile +++ b/firmware/application/Makefile @@ -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 \ diff --git a/firmware/application/main.cpp b/firmware/application/main.cpp index fe90b74d..25102c4d 100755 --- a/firmware/application/main.cpp +++ b/firmware/application/main.cpp @@ -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) { diff --git a/firmware/application/sd_card.cpp b/firmware/application/sd_card.cpp new file mode 100644 index 00000000..4e9989ab --- /dev/null +++ b/firmware/application/sd_card.cpp @@ -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 + +#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_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 */ diff --git a/firmware/application/sd_card.hpp b/firmware/application/sd_card.hpp index d7827b90..fef7849a 100644 --- a/firmware/application/sd_card.hpp +++ b/firmware/application/sd_card.hpp @@ -22,26 +22,25 @@ #ifndef __SD_CARD_H__ #define __SD_CARD_H__ -#include "ff.h" +#include + +#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_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__*/ diff --git a/firmware/application/ui_receiver.cpp b/firmware/application/ui_receiver.cpp index 0ff91feb..35815e56 100644 --- a/firmware/application/ui_receiver.cpp +++ b/firmware/application/ui_receiver.cpp @@ -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(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. } } diff --git a/firmware/application/ui_receiver.hpp b/firmware/application/ui_receiver.hpp index 6b2bda88..8ff647e3 100644 --- a/firmware/application/ui_receiver.hpp +++ b/firmware/application/ui_receiver.hpp @@ -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 */ diff --git a/firmware/common/message.hpp b/firmware/common/message.hpp index 9ad41875..9a290144 100644 --- a/firmware/common/message.hpp +++ b/firmware/common/message.hpp @@ -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;