Extract to_string_* functions from ui_widget.

This commit is contained in:
Jared Boone 2015-12-02 13:38:17 -08:00
parent d8c59e2ce2
commit 76845c4335
12 changed files with 178 additions and 122 deletions

View File

@ -177,6 +177,7 @@ CPPSRC = main.cpp \
sd_card.cpp \ sd_card.cpp \
log_file.cpp \ log_file.cpp \
manchester.cpp \ manchester.cpp \
string_format.cpp \
../common/utility.cpp \ ../common/utility.cpp \
../common/chibios_cpp.cpp \ ../common/chibios_cpp.cpp \
../common/debug.cpp \ ../common/debug.cpp \

View File

@ -26,8 +26,7 @@ using namespace portapack;
#include "crc.hpp" #include "crc.hpp"
// TODO: Move string formatting elsewhere!!! #include "string_format.hpp"
#include "ui_widget.hpp"
namespace baseband { namespace baseband {
namespace ais { 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 t = (normalized * 5) / 3;
const int32_t degrees = t / (100 * 10000); const int32_t degrees = t / (100 * 10000);
const int32_t fraction = std::abs(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( static std::string format_datetime(
const DateTime& datetime const DateTime& datetime
) { ) {
return ui::to_string_dec_uint(datetime.year, 4, '0') + "/" + return to_string_dec_uint(datetime.year, 4, '0') + "/" +
ui::to_string_dec_uint(datetime.month, 2, '0') + "/" + to_string_dec_uint(datetime.month, 2, '0') + "/" +
ui::to_string_dec_uint(datetime.day, 2, '0') + " " + to_string_dec_uint(datetime.day, 2, '0') + " " +
ui::to_string_dec_uint(datetime.hour, 2, '0') + ":" + to_string_dec_uint(datetime.hour, 2, '0') + ":" +
ui::to_string_dec_uint(datetime.minute, 2, '0') + ":" + to_string_dec_uint(datetime.minute, 2, '0') + ":" +
ui::to_string_dec_uint(datetime.second, 2, '0'); to_string_dec_uint(datetime.second, 2, '0');
} }
static std::string format_navigational_status(const unsigned int value) { 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) { 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()) { switch(packet.message_id()) {
case 1: case 1:

View File

@ -28,8 +28,7 @@ using namespace portapack;
// #include "lpc43xx_cpp.hpp" // #include "lpc43xx_cpp.hpp"
// using namespace lpc43xx; // using namespace lpc43xx;
// TODO: SERIOUSLY!? Including this, just to use to_string_hex?! Refactor!!!1 #include "string_format.hpp"
#include "ui_widget.hpp"
TPMSModel::TPMSModel() { TPMSModel::TPMSModel() {
receiver_model.set_baseband_configuration({ receiver_model.set_baseband_configuration({
@ -50,16 +49,16 @@ ManchesterFormatted TPMSModel::on_packet(const TPMSPacketMessage& message) {
rtc::RTC datetime; rtc::RTC datetime;
rtcGetTime(&RTCD1, &datetime); rtcGetTime(&RTCD1, &datetime);
std::string timestamp = std::string timestamp =
ui::to_string_dec_uint(datetime.year(), 4, '0') + to_string_dec_uint(datetime.year(), 4, '0') +
ui::to_string_dec_uint(datetime.month(), 2, '0') + to_string_dec_uint(datetime.month(), 2, '0') +
ui::to_string_dec_uint(datetime.day(), 2, '0') + to_string_dec_uint(datetime.day(), 2, '0') +
ui::to_string_dec_uint(datetime.hour(), 2, '0') + to_string_dec_uint(datetime.hour(), 2, '0') +
ui::to_string_dec_uint(datetime.minute(), 2, '0') + to_string_dec_uint(datetime.minute(), 2, '0') +
ui::to_string_dec_uint(datetime.second(), 2, '0'); to_string_dec_uint(datetime.second(), 2, '0');
const auto tuning_frequency = receiver_model.tuning_frequency(); const auto tuning_frequency = receiver_model.tuning_frequency();
// TODO: function doesn't take uint64_t, so when >= 1<<32, weirdness will ensue! // 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"; std::string log = timestamp + " " + tuning_frequency_str + " FSK 38.4 19.2 " + hex_formatted.data + "/" + hex_formatted.errors + "\r\n";
log_file.write(log); log_file.write(log);

View File

@ -21,8 +21,7 @@
#include "manchester.hpp" #include "manchester.hpp"
// TODO: SERIOUSLY!? Including this, just to use to_string_hex?! Refactor!!!1 #include "string_format.hpp"
#include "ui_widget.hpp"
ManchesterDecoder::DecodedSymbol ManchesterDecoder::operator[](const size_t index) const { ManchesterDecoder::DecodedSymbol ManchesterDecoder::operator[](const size_t index) const {
const size_t encoded_index = index * 2; const size_t encoded_index = index * 2;
@ -63,8 +62,8 @@ ManchesterFormatted format_manchester(
error |= symbol.error; error |= symbol.error;
if( (i & 3) == 3 ) { if( (i & 3) == 3 ) {
hex_data += ui::to_string_hex(data & 0xf, 1); hex_data += to_string_hex(data & 0xf, 1);
hex_error += ui::to_string_hex(error & 0xf, 1); hex_error += to_string_hex(error & 0xf, 1);
} }
} }

View File

@ -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;
}

View File

@ -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 <cstdint>
#include <string>
// 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__*/

View File

@ -27,6 +27,8 @@
#include "hackrf_hal.hpp" #include "hackrf_hal.hpp"
using namespace hackrf::one; using namespace hackrf::one;
#include "string_format.hpp"
namespace ui { namespace ui {
/* BasebandStatsView *****************************************************/ /* BasebandStatsView *****************************************************/

View File

@ -24,6 +24,7 @@
#include "ch.h" #include "ch.h"
#include "radio.hpp" #include "radio.hpp"
#include "string_format.hpp"
namespace ui { namespace ui {

View File

@ -24,6 +24,8 @@
#include "portapack.hpp" #include "portapack.hpp"
using namespace portapack; using namespace portapack;
#include "string_format.hpp"
#include "app_analog_audio.hpp" #include "app_analog_audio.hpp"
#include "app_ais.hpp" #include "app_ais.hpp"
#include "app_tpms.hpp" #include "app_tpms.hpp"

View File

@ -35,6 +35,8 @@ using namespace portapack;
#include <cmath> #include <cmath>
#include <array> #include <array>
#include "string_format.hpp"
namespace ui { namespace ui {
namespace spectrum { namespace spectrum {

View File

@ -26,6 +26,8 @@
#include <cstddef> #include <cstddef>
#include <algorithm> #include <algorithm>
#include "string_format.hpp"
namespace ui { namespace ui {
static bool ui_dirty = true; static bool ui_dirty = true;
@ -42,99 +44,6 @@ bool is_dirty() {
return ui_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 ****************************************************************/ /* Widget ****************************************************************/
Point Widget::screen_pos() { Point Widget::screen_pos() {

View File

@ -41,12 +41,6 @@ void dirty_set();
void dirty_clear(); void dirty_clear();
bool is_dirty(); 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 { class Context {
public: public:
FocusManager& focus_manager() { FocusManager& focus_manager() {