From 46c076c296af6f54417b913299b9e7843c4c7cf6 Mon Sep 17 00:00:00 2001 From: euquiq <31453004+euquiq@users.noreply.github.com> Date: Sat, 13 Jun 2020 21:21:32 -0300 Subject: [PATCH] Antenna length Calculator fix Calculation now rounds up decimals so result is more accurate. --- firmware/application/apps/ui_whipcalc.cpp | 31 ++++++++++++++++++----- firmware/application/apps/ui_whipcalc.hpp | 3 ++- 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/firmware/application/apps/ui_whipcalc.cpp b/firmware/application/apps/ui_whipcalc.cpp index 9c99a556..64de95b4 100644 --- a/firmware/application/apps/ui_whipcalc.cpp +++ b/firmware/application/apps/ui_whipcalc.cpp @@ -36,8 +36,18 @@ void WhipCalcView::focus() { field_frequency.focus(); } +double ui::WhipCalcView::get_decimals(double num, int16_t mult, bool round) { + num -= int(num); //keep decimals only + num *= mult; //Shift decimals into integers + if (!round) return num; + int16_t intnum = int(num); //Round it up if necessary + num -= intnum; //Get decimal part + if (num > .5) intnum++; //Round up + return intnum; + } + void WhipCalcView::update_result() { - double length, divider; + double length, calclength, divider; divider = ((double)options_type.selected_index_value() / 8.0); @@ -45,8 +55,12 @@ void WhipCalcView::update_result() { length = (speed_of_light_mps / (double)field_frequency.value()) * divider; auto m = to_string_dec_int((int)length, 2); - auto cm = to_string_dec_int(int(length * 100.0) % 100, 2); - auto mm = to_string_dec_int(int(length * 1000.0) % 10, 1); +/* auto cm = to_string_dec_int(int(length * 100.0) % 100, 2); + auto mm = to_string_dec_int(int(length * 1000.0) % 10, 1); */ + + calclength = get_decimals(length,100); //cm + auto cm = to_string_dec_int(int(calclength), 2); + auto mm = to_string_dec_int(int(get_decimals(calclength,10,true)), 1); text_result_metric.set(m + "m " + cm + "." + mm + "cm"); @@ -60,11 +74,16 @@ void WhipCalcView::update_result() { } // Imperial - length = (speed_of_light_fps / (double)field_frequency.value()) * divider; + calclength = (speed_of_light_fps / (double)field_frequency.value()) * divider; - auto feet = to_string_dec_int((int)length, 3); +/* auto feet = to_string_dec_int((int)length, 3); auto inch = to_string_dec_int(int(length * 10.0) % 12, 2); - auto inch_c = to_string_dec_int(int(length * 100.0) % 10, 1); + auto inch_c = to_string_dec_int(int(length * 100.0) % 10, 1); */ + + auto feet = to_string_dec_int(int(calclength), 3); + calclength = get_decimals(calclength,12); //inches + auto inch = to_string_dec_int(int(calclength), 2); + auto inch_c = to_string_dec_int(int(get_decimals(calclength,10,true)), 1); text_result_imperial.set(feet + "ft " + inch + "." + inch_c + "in"); } diff --git a/firmware/application/apps/ui_whipcalc.hpp b/firmware/application/apps/ui_whipcalc.hpp index 00a691af..d9a66343 100644 --- a/firmware/application/apps/ui_whipcalc.hpp +++ b/firmware/application/apps/ui_whipcalc.hpp @@ -37,7 +37,7 @@ public: void focus() override; - std::string title() const override { return "Whip calculator"; }; + std::string title() const override { return "Antenna length"; }; private: const double speed_of_light_mps = 299792458.0; // m/s @@ -45,6 +45,7 @@ private: const std::string frac_str[4] = { "", "1/4 ", "1/2 ", "3/4 " }; + double get_decimals(double num, int16_t mult, bool round = false); void update_result(); Labels labels {