From 76845c4335320d87b012eb12ebbb63a5301ea2a9 Mon Sep 17 00:00:00 2001 From: Jared Boone Date: Wed, 2 Dec 2015 13:38:17 -0800 Subject: [PATCH] Extract to_string_* functions from ui_widget. --- firmware/application/Makefile | 1 + firmware/application/app_ais.cpp | 19 ++- firmware/application/app_tpms.cpp | 17 ++- firmware/application/manchester.cpp | 7 +- firmware/application/string_format.cpp | 114 ++++++++++++++++++ firmware/application/string_format.hpp | 34 ++++++ .../application/ui_baseband_stats_view.cpp | 2 + firmware/application/ui_debug.cpp | 1 + firmware/application/ui_receiver.cpp | 2 + firmware/application/ui_spectrum.hpp | 2 + firmware/common/ui_widget.cpp | 95 +-------------- firmware/common/ui_widget.hpp | 6 - 12 files changed, 178 insertions(+), 122 deletions(-) create mode 100644 firmware/application/string_format.cpp create mode 100644 firmware/application/string_format.hpp diff --git a/firmware/application/Makefile b/firmware/application/Makefile index 3ecf44e2..403056dd 100755 --- a/firmware/application/Makefile +++ b/firmware/application/Makefile @@ -177,6 +177,7 @@ CPPSRC = main.cpp \ sd_card.cpp \ log_file.cpp \ manchester.cpp \ + string_format.cpp \ ../common/utility.cpp \ ../common/chibios_cpp.cpp \ ../common/debug.cpp \ diff --git a/firmware/application/app_ais.cpp b/firmware/application/app_ais.cpp index 89b33ac2..a4da213e 100644 --- a/firmware/application/app_ais.cpp +++ b/firmware/application/app_ais.cpp @@ -26,8 +26,7 @@ using namespace portapack; #include "crc.hpp" -// TODO: Move string formatting elsewhere!!! -#include "ui_widget.hpp" +#include "string_format.hpp" namespace baseband { namespace ais { @@ -152,18 +151,18 @@ static std::string format_latlon_normalized(const int32_t normalized) { const int32_t t = (normalized * 5) / 3; const int32_t degrees = t / (100 * 10000); const int32_t fraction = std::abs(t) % (100 * 10000); - return ui::to_string_dec_int(degrees) + "." + ui::to_string_dec_int(fraction, 6, '0'); + return to_string_dec_int(degrees) + "." + to_string_dec_int(fraction, 6, '0'); } static std::string format_datetime( const DateTime& datetime ) { - return ui::to_string_dec_uint(datetime.year, 4, '0') + "/" + - ui::to_string_dec_uint(datetime.month, 2, '0') + "/" + - ui::to_string_dec_uint(datetime.day, 2, '0') + " " + - ui::to_string_dec_uint(datetime.hour, 2, '0') + ":" + - ui::to_string_dec_uint(datetime.minute, 2, '0') + ":" + - ui::to_string_dec_uint(datetime.second, 2, '0'); + return to_string_dec_uint(datetime.year, 4, '0') + "/" + + to_string_dec_uint(datetime.month, 2, '0') + "/" + + to_string_dec_uint(datetime.day, 2, '0') + " " + + to_string_dec_uint(datetime.hour, 2, '0') + ":" + + to_string_dec_uint(datetime.minute, 2, '0') + ":" + + to_string_dec_uint(datetime.second, 2, '0'); } static std::string format_navigational_status(const unsigned int value) { @@ -332,7 +331,7 @@ void AISView::on_hide() { } void AISView::log(const baseband::ais::Packet& packet) { - std::string result { ui::to_string_dec_uint(packet.message_id(), 2) + " " + ui::to_string_dec_uint(packet.source_id(), 10) }; + std::string result { to_string_dec_uint(packet.message_id(), 2) + " " + to_string_dec_uint(packet.source_id(), 10) }; switch(packet.message_id()) { case 1: diff --git a/firmware/application/app_tpms.cpp b/firmware/application/app_tpms.cpp index d159cecf..db74c108 100644 --- a/firmware/application/app_tpms.cpp +++ b/firmware/application/app_tpms.cpp @@ -28,8 +28,7 @@ using namespace portapack; // #include "lpc43xx_cpp.hpp" // using namespace lpc43xx; -// TODO: SERIOUSLY!? Including this, just to use to_string_hex?! Refactor!!!1 -#include "ui_widget.hpp" +#include "string_format.hpp" TPMSModel::TPMSModel() { receiver_model.set_baseband_configuration({ @@ -50,16 +49,16 @@ ManchesterFormatted TPMSModel::on_packet(const TPMSPacketMessage& message) { rtc::RTC datetime; rtcGetTime(&RTCD1, &datetime); std::string timestamp = - ui::to_string_dec_uint(datetime.year(), 4, '0') + - ui::to_string_dec_uint(datetime.month(), 2, '0') + - ui::to_string_dec_uint(datetime.day(), 2, '0') + - ui::to_string_dec_uint(datetime.hour(), 2, '0') + - ui::to_string_dec_uint(datetime.minute(), 2, '0') + - ui::to_string_dec_uint(datetime.second(), 2, '0'); + to_string_dec_uint(datetime.year(), 4, '0') + + to_string_dec_uint(datetime.month(), 2, '0') + + to_string_dec_uint(datetime.day(), 2, '0') + + to_string_dec_uint(datetime.hour(), 2, '0') + + to_string_dec_uint(datetime.minute(), 2, '0') + + to_string_dec_uint(datetime.second(), 2, '0'); const auto tuning_frequency = receiver_model.tuning_frequency(); // TODO: function doesn't take uint64_t, so when >= 1<<32, weirdness will ensue! - const auto tuning_frequency_str = ui::to_string_dec_uint(tuning_frequency, 10); + const auto tuning_frequency_str = to_string_dec_uint(tuning_frequency, 10); std::string log = timestamp + " " + tuning_frequency_str + " FSK 38.4 19.2 " + hex_formatted.data + "/" + hex_formatted.errors + "\r\n"; log_file.write(log); diff --git a/firmware/application/manchester.cpp b/firmware/application/manchester.cpp index 877fc03b..8a9aa724 100644 --- a/firmware/application/manchester.cpp +++ b/firmware/application/manchester.cpp @@ -21,8 +21,7 @@ #include "manchester.hpp" -// TODO: SERIOUSLY!? Including this, just to use to_string_hex?! Refactor!!!1 -#include "ui_widget.hpp" +#include "string_format.hpp" ManchesterDecoder::DecodedSymbol ManchesterDecoder::operator[](const size_t index) const { const size_t encoded_index = index * 2; @@ -63,8 +62,8 @@ ManchesterFormatted format_manchester( error |= symbol.error; if( (i & 3) == 3 ) { - hex_data += ui::to_string_hex(data & 0xf, 1); - hex_error += ui::to_string_hex(error & 0xf, 1); + hex_data += to_string_hex(data & 0xf, 1); + hex_error += to_string_hex(error & 0xf, 1); } } diff --git a/firmware/application/string_format.cpp b/firmware/application/string_format.cpp new file mode 100644 index 00000000..be5f988c --- /dev/null +++ b/firmware/application/string_format.cpp @@ -0,0 +1,114 @@ +/* + * Copyright (C) 2014 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 "string_format.hpp" + +static char* to_string_dec_uint_internal( + char* p, + uint32_t n +) { + *p = 0; + auto q = p; + + do { + const uint32_t d = n % 10; + const char c = d + 48; + *(--q) = c; + n /= 10; + } while( n != 0 ); + + return q; +} + +static char* to_string_dec_uint_pad_internal( + char* const term, + const uint32_t n, + const int32_t l, + const char fill +) { + auto q = to_string_dec_uint_internal(term, n); + + if( fill ) { + while( (term - q) < l ) { + *(--q) = fill; + } + } + + return q; +} + +std::string to_string_dec_uint( + const uint32_t n, + const int32_t l, + const char fill +) { + char p[16]; + auto term = p + sizeof(p) - 1; + auto q = to_string_dec_uint_pad_internal(term, n, l, fill); + + // Right justify. + while( (term - q) < l ) { + *(--q) = ' '; + } + + return q; +} + +std::string to_string_dec_int( + const int32_t n, + const int32_t l, + const char fill +) { + const size_t negative = (n < 0) ? 1 : 0; + uint32_t n_abs = negative ? -n : n; + + char p[16]; + auto term = p + sizeof(p) - 1; + auto q = to_string_dec_uint_pad_internal(term, n_abs, l - negative, fill); + + // Add sign. + if( negative ) { + *(--q) = '-'; + } + + // Right justify. + while( (term - q) < l ) { + *(--q) = ' '; + } + + return q; +} + +static void to_string_hex_internal(char* p, const uint32_t n, const int32_t l) { + const uint32_t d = n & 0xf; + p[l] = (d > 9) ? (d + 87) : (d + 48); + if( l > 0 ) { + to_string_hex_internal(p, n >> 4, l - 1); + } +} + +std::string to_string_hex(const uint32_t n, const int32_t l) { + char p[16]; + to_string_hex_internal(p, n, l - 1); + p[l] = 0; + return p; +} + diff --git a/firmware/application/string_format.hpp b/firmware/application/string_format.hpp new file mode 100644 index 00000000..0eba017f --- /dev/null +++ b/firmware/application/string_format.hpp @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2014 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. + */ + +#ifndef __STRING_FORMAT_H__ +#define __STRING_FORMAT_H__ + +#include +#include + +// TODO: Allow l=0 to not fill/justify? Already using this way in ui_spectrum.hpp... + +std::string to_string_dec_uint(const uint32_t n, const int32_t l = 0, const char fill = 0); +std::string to_string_dec_int(const int32_t n, const int32_t l = 0, const char fill = 0); +std::string to_string_hex(const uint32_t n, const int32_t l = 0); + +#endif/*__STRING_FORMAT_H__*/ diff --git a/firmware/application/ui_baseband_stats_view.cpp b/firmware/application/ui_baseband_stats_view.cpp index fcf0816e..76cccb5a 100644 --- a/firmware/application/ui_baseband_stats_view.cpp +++ b/firmware/application/ui_baseband_stats_view.cpp @@ -27,6 +27,8 @@ #include "hackrf_hal.hpp" using namespace hackrf::one; +#include "string_format.hpp" + namespace ui { /* BasebandStatsView *****************************************************/ diff --git a/firmware/application/ui_debug.cpp b/firmware/application/ui_debug.cpp index 28d99abf..4441268d 100644 --- a/firmware/application/ui_debug.cpp +++ b/firmware/application/ui_debug.cpp @@ -24,6 +24,7 @@ #include "ch.h" #include "radio.hpp" +#include "string_format.hpp" namespace ui { diff --git a/firmware/application/ui_receiver.cpp b/firmware/application/ui_receiver.cpp index 9af66476..421442e1 100644 --- a/firmware/application/ui_receiver.cpp +++ b/firmware/application/ui_receiver.cpp @@ -24,6 +24,8 @@ #include "portapack.hpp" using namespace portapack; +#include "string_format.hpp" + #include "app_analog_audio.hpp" #include "app_ais.hpp" #include "app_tpms.hpp" diff --git a/firmware/application/ui_spectrum.hpp b/firmware/application/ui_spectrum.hpp index 6a10088f..fe2fab91 100644 --- a/firmware/application/ui_spectrum.hpp +++ b/firmware/application/ui_spectrum.hpp @@ -35,6 +35,8 @@ using namespace portapack; #include #include +#include "string_format.hpp" + namespace ui { namespace spectrum { diff --git a/firmware/common/ui_widget.cpp b/firmware/common/ui_widget.cpp index 79ddafe1..d2b040db 100644 --- a/firmware/common/ui_widget.cpp +++ b/firmware/common/ui_widget.cpp @@ -26,6 +26,8 @@ #include #include +#include "string_format.hpp" + namespace ui { static bool ui_dirty = true; @@ -42,99 +44,6 @@ bool is_dirty() { return ui_dirty; } -constexpr size_t to_string_max_length = 16; - -static char* to_string_dec_uint_internal( - char* p, - uint32_t n -) { - *p = 0; - auto q = p; - - do { - const uint32_t d = n % 10; - const char c = d + 48; - *(--q) = c; - n /= 10; - } while( n != 0 ); - - return q; -} - -static char* to_string_dec_uint_pad_internal( - char* const term, - const uint32_t n, - const int32_t l, - const char fill -) { - auto q = to_string_dec_uint_internal(term, n); - - if( fill ) { - while( (term - q) < l ) { - *(--q) = fill; - } - } - - return q; -} - -std::string to_string_dec_uint( - const uint32_t n, - const int32_t l, - const char fill -) { - char p[16]; - auto term = p + sizeof(p) - 1; - auto q = to_string_dec_uint_pad_internal(term, n, l, fill); - - // Right justify. - while( (term - q) < l ) { - *(--q) = ' '; - } - - return q; -} - -std::string to_string_dec_int( - const int32_t n, - const int32_t l, - const char fill -) { - const size_t negative = (n < 0) ? 1 : 0; - uint32_t n_abs = negative ? -n : n; - - char p[16]; - auto term = p + sizeof(p) - 1; - auto q = to_string_dec_uint_pad_internal(term, n_abs, l - negative, fill); - - // Add sign. - if( negative ) { - *(--q) = '-'; - } - - // Right justify. - while( (term - q) < l ) { - *(--q) = ' '; - } - - return q; -} - -static void to_string_hex_internal(char* p, const uint32_t n, const int32_t l) { - const uint32_t d = n & 0xf; - p[l] = (d > 9) ? (d + 87) : (d + 48); - if( l > 0 ) { - to_string_hex_internal(p, n >> 4, l - 1); - } -} - -std::string to_string_hex(const uint32_t n, const int32_t l) { - char p[16]; - to_string_hex_internal(p, n, l - 1); - p[l] = 0; - return p; -} - /* Widget ****************************************************************/ Point Widget::screen_pos() { diff --git a/firmware/common/ui_widget.hpp b/firmware/common/ui_widget.hpp index f54bc677..2426e409 100644 --- a/firmware/common/ui_widget.hpp +++ b/firmware/common/ui_widget.hpp @@ -41,12 +41,6 @@ void dirty_set(); void dirty_clear(); bool is_dirty(); -// TODO: Move these somewhere else! -// TODO: Allow l=0 to not fill/justify? Already using this way in ui_spectrum.hpp... -std::string to_string_dec_uint(const uint32_t n, const int32_t l = 0, const char fill = 0); -std::string to_string_dec_int(const int32_t n, const int32_t l = 0, const char fill = 0); -std::string to_string_hex(const uint32_t n, const int32_t l = 0); - class Context { public: FocusManager& focus_manager() {