From dc71da1dcdd2d9578d7d739e7e8b3b5c8a87bde3 Mon Sep 17 00:00:00 2001 From: "Charles J. Cliffe" Date: Sun, 10 May 2015 01:07:48 -0400 Subject: [PATCH] freq<->str translation, center frequency change test --- src/FrequencyDialog.cpp | 73 +++++++++++++++++++++++++++++++++++++++-- src/FrequencyDialog.h | 4 ++- 2 files changed, 73 insertions(+), 4 deletions(-) diff --git a/src/FrequencyDialog.cpp b/src/FrequencyDialog.cpp index 01d2fd2..397e317 100644 --- a/src/FrequencyDialog.cpp +++ b/src/FrequencyDialog.cpp @@ -1,6 +1,9 @@ #include "FrequencyDialog.h" #include "wx/clipbrd.h" +#include +#include +#include "CubicSDR.h" wxBEGIN_EVENT_TABLE(FrequencyDialog, wxDialog) EVT_CHAR_HOOK(FrequencyDialog::OnChar) @@ -8,7 +11,7 @@ wxEND_EVENT_TABLE() FrequencyDialog::FrequencyDialog(wxWindow * parent, wxWindowID id, const wxString & title, const wxPoint & position, const wxSize & size, long style) : wxDialog(parent, id, title, position, size, style) { - wxString freqStr = "105.7Mhz"; + wxString freqStr = frequencyToStr(wxGetApp().getFrequency()); dialogText = new wxTextCtrl(this, wxID_FREQ_INPUT, freqStr, wxPoint(6, 1), wxSize(size.GetWidth() - 20, size.GetHeight() - 70), wxTE_PROCESS_ENTER); dialogText->SetFont(wxFont(20, wxFONTFAMILY_TELETYPE, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_BOLD)); @@ -25,14 +28,78 @@ std::string& FrequencyDialog::filterChars(std::string& s, const std::string& all return s; } +std::string FrequencyDialog::frequencyToStr(long long freq) { + long double freqTemp; + + freqTemp = freq; + std::string suffix(""); + std::stringstream freqStr; + + if (freqTemp >= 1.0e9) { + freqTemp /= 1.0e9; + freqStr << std::setprecision(10); + suffix = std::string("GHz"); + } else if (freqTemp >= 1.0e6) { + freqTemp /= 1.0e6; + freqStr << std::setprecision(7); + suffix = std::string("MHz"); + } else if (freqTemp >= 1.0e3) { + freqTemp /= 1.0e3; + freqStr << std::setprecision(4); + suffix = std::string("KHz"); + } + + freqStr << freqTemp; + freqStr << suffix; + + return freqStr.str(); +} + +long long FrequencyDialog::strToFrequency(std::string freqStr) { + std::string filterStr = filterChars(freqStr,std::string("0123456789.MKGmkg")); + + int numLen = filterStr.find_first_not_of("0123456789."); + + if (numLen == std::string::npos) { + numLen = freqStr.length(); + } + + std::string numPartStr = freqStr.substr(0,numLen); + std::string suffixStr = freqStr.substr(numLen); + + std::stringstream numPartStream; + numPartStream.str(numPartStr); + + long double freqTemp = 0; + + numPartStream >> freqTemp; + + if (suffixStr.length()) { + if (suffixStr.find_first_of("Gg") != std::string::npos) { + freqTemp *= 1.0e9; + } else if (suffixStr.find_first_of("Mm") != std::string::npos) { + freqTemp *= 1.0e6; + } else if (suffixStr.find_first_of("Kk") != std::string::npos) { + freqTemp *= 1.0e3; + } + } else if (numPartStr.find_first_of(".") != std::string::npos) { + freqTemp *= 1.0e6; + } + + return (long long)freqTemp; +} + void FrequencyDialog::OnChar(wxKeyEvent& event) { wxChar c = event.GetKeyCode(); + long long freq; switch (c) { case WXK_RETURN: case WXK_NUMPAD_ENTER: // Do Stuff - + freq = strToFrequency(dialogText->GetValue().ToStdString()); + wxGetApp().setFrequency(freq); + std::cout << freq << std::endl; Close(); break; case WXK_ESCAPE: @@ -50,7 +117,7 @@ void FrequencyDialog::OnChar(wxKeyEvent& event) { wxTextDataObject data; wxTheClipboard->GetData(data); std::string clipText = data.GetText().ToStdString(); - std::string pasteText = filterChars(clipText,std::string(allowed)); + std::string pasteText = filterChars(clipText, std::string(allowed)); wxTheClipboard->SetData(new wxTextDataObject(pasteText)); wxTheClipboard->Close(); event.Skip(); diff --git a/src/FrequencyDialog.h b/src/FrequencyDialog.h index b496a53..98b6b45 100644 --- a/src/FrequencyDialog.h +++ b/src/FrequencyDialog.h @@ -18,11 +18,13 @@ public: wxTextCtrl * dialogText; + long long strToFrequency(std::string freqStr); + std::string frequencyToStr(long long freq); + private: void OnEnter ( wxCommandEvent &event ); void OnChar ( wxKeyEvent &event ); std::string& filterChars(std::string& s, const std::string& allowed); - DECLARE_EVENT_TABLE() };