Merge branch 'new-antenna-length-calculator'

This commit is contained in:
eried 2020-06-28 21:35:36 +02:00
commit 9096532eee
5 changed files with 260 additions and 150 deletions

View File

@ -30,85 +30,149 @@
using namespace portapack; using namespace portapack;
namespace ui { namespace ui
{
void WhipCalcView::focus() { void WhipCalcView::focus()
{
field_frequency.focus(); field_frequency.focus();
} }
double ui::WhipCalcView::get_decimals(double num, int16_t mult, bool round) { double ui::WhipCalcView::get_decimals(double num, int16_t mult, bool round)
{
num -= int(num); //keep decimals only num -= int(num); //keep decimals only
num *= mult; //Shift decimals into integers num *= mult; //Shift decimals into integers
if (!round) return num; if (!round)
return num;
int16_t intnum = int(num); //Round it up if necessary int16_t intnum = int(num); //Round it up if necessary
num -= intnum; //Get decimal part num -= intnum; //Get decimal part
if (num > .5) intnum++; //Round up if (num > .5)
intnum++; //Round up
return intnum; return intnum;
} }
void WhipCalcView::update_result() { void WhipCalcView::update_result()
{
double length, calclength, divider; double length, calclength, divider;
console.clear(true);
divider = ((double)options_type.selected_index_value() / 8.0); divider = ((double)options_type.selected_index_value() / 8.0);
// Antenna lengths fields
if (field_frequency.value() > 0)
{
// Metric // Metric
length = (speed_of_light_mps / (double)field_frequency.value()) * divider; length = (speed_of_light_mps / (double)field_frequency.value()) * divider;
auto m = to_string_dec_int((int)length, 0);
auto m = to_string_dec_int((int)length, 2); //auto cm = to_string_dec_int(int(length * 100.0) % 100, 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 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), 0);
calclength = get_decimals(length,100); //cm auto mm = to_string_dec_int(int(get_decimals(calclength, 10, true)), 0);
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"); text_result_metric.set(m + "m " + cm + "." + mm + "cm");
// ANT500 elements for crude adjustment
length /= 0.14;
if (int(length) <= 4) {
auto elements = to_string_dec_int((int)length, 1);
text_result_ant500.set(elements + " " + frac_str[((int(length * 10.0) % 10) + 1) / 3] + "ANT500 elements");
} else {
text_result_ant500.set("-");
}
// Imperial // Imperial
calclength = (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(calclength), 0);
/* auto feet = to_string_dec_int((int)length, 3); calclength = get_decimals(calclength, 12); //inches
auto inch = to_string_dec_int(int(length * 10.0) % 12, 2); auto inch = to_string_dec_int(int(calclength), 0);
auto inch_c = to_string_dec_int(int(length * 100.0) % 10, 1); */ auto inch_c = to_string_dec_int(int(get_decimals(calclength, 10, true)), 0);
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"); text_result_imperial.set(feet + "ft " + inch + "." + inch_c + "in");
} }
else
{
text_result_metric.set("infinity+");
text_result_imperial.set("infinity+");
return;
}
WhipCalcView::WhipCalcView( uint8_t ant_count = 9; //Shown antennas counter
NavigationView& nav length *= 1000; //Get length in mm needed to extend the antenna
) { for (antenna_entry antenna : antenna_db)
{ //go thru all antennas available
uint16_t element, refined_quarter = 0;
for (element = 0; element < antenna.elements.size(); element++)
{
if (length == antenna.elements[element]) //Exact element in length
{
element++; //Real element is +1 (zero based vector)
break; //Done with this ant
}
else if (length < antenna.elements[element])
{
double remain, this_element, quarter = 0;
remain = length - antenna.elements[element - 1]; //mm needed from this element to reach length
this_element = antenna.elements[element] - antenna.elements[element - 1]; //total mm on this element
quarter = (remain * 4) / this_element; //havoc & portack ended on this int(quarter) resolution.
if (quarter - int(quarter) > 0.5)
{ //rounding gave a measure closer to next quarter
refined_quarter = int(quarter) + 1;
if (refined_quarter == 4)
{ //rounding gave a measure closer to next element
refined_quarter = 0;
element++;
}
}
else
{
refined_quarter = int(quarter);
}
break; //Done with this ant
}
}
/*if (!ant_count)
{
console.write(" and more ...");
break;
}*/
console.write(antenna.label + ": " + to_string_dec_int(element, 1) + frac_str[refined_quarter] + " elements\n");
ant_count--; // For now, just showing all.
}
}
add_children({ WhipCalcView::WhipCalcView(NavigationView &nav)
&labels, {
add_children({&labels,
//&antennas_on_memory,
&field_frequency, &field_frequency,
&options_type, &options_type,
&text_result_metric, &text_result_metric,
&text_result_imperial, &text_result_imperial,
&text_result_ant500, &console,
&button_exit &button_exit});
});
File antennas_file;
auto error = antennas_file.open("WHIPCALC/ANTENNAS.TXT");
if (!error.is_valid())
{
std::string line;
char one_char[1];
for (size_t pointer = 0; pointer < antennas_file.size(); pointer++)
{
antennas_file.seek(pointer);
antennas_file.read(one_char, 1);
if ((int)one_char[0] >= ' ')
line += one_char[0]; //Add it to the textline
else if (one_char[0] == '\n')
{ //New Line
txtline_process(line); //make sense of this textline
line.clear(); //Ready for next textline
}
}
if (line.length() > 0)
txtline_process(line); //Last line had no newline at end ?
}
if (!antenna_db.size())
add_default_antenna();
//antennas_on_memory.set(to_string_dec_int(antenna_db.size(),0) + " antennas"); //tell user
options_type.set_selected_index(2); // Quarter wave
options_type.on_change = [this](size_t, OptionsField::value_t) { options_type.on_change = [this](size_t, OptionsField::value_t) {
this->update_result(); this->update_result();
}; };
options_type.set_selected_index(2); // Quarter wave
field_frequency.set_value(transmitter_model.tuning_frequency()); field_frequency.set_step(1000000); // 1MHz step
field_frequency.set_step(500000); // 500kHz step
field_frequency.on_change = [this](rf::Frequency) { field_frequency.on_change = [this](rf::Frequency) {
this->update_result(); this->update_result();
}; };
@ -116,16 +180,53 @@ WhipCalcView::WhipCalcView(
// TODO: Provide separate modal method/scheme? // TODO: Provide separate modal method/scheme?
auto new_view = nav.push<FrequencyKeypadView>(transmitter_model.tuning_frequency()); auto new_view = nav.push<FrequencyKeypadView>(transmitter_model.tuning_frequency());
new_view->on_changed = [this](rf::Frequency f) { new_view->on_changed = [this](rf::Frequency f) {
this->update_result();
this->field_frequency.set_value(f); this->field_frequency.set_value(f);
this->update_result();
}; };
}; };
button_exit.on_select = [this, &nav](Button&) { button_exit.on_select = [this, &nav](Button &) {
nav.pop(); nav.pop();
}; };
update_result(); field_frequency.set_value(transmitter_model.tuning_frequency());
} }
} void ui::WhipCalcView::txtline_process(std::string &line)
{
if (line.find("#") != std::string::npos)
return; //Line is just a comment
char separator = ',';
size_t previous = 0;
uint16_t value = 0;
antenna_entry new_antenna;
size_t current = line.find(separator);
while (current != std::string::npos)
{
if (!previous)
new_antenna.label.assign(line, 0, current); //antenna label
else
{
value = std::stoi(line.substr(previous, current - previous));
if (!value) return; //No element length? abort antenna
new_antenna.elements.push_back(value); //Store this new element
}
previous = current + 1;
current = line.find(separator, previous); //Search for next space delimiter
}
if (!previous) return; //Not even a label ? drop this antenna!
value = std::stoi(line.substr(previous, current - previous)); //Last element
if (!value) return;
new_antenna.elements.push_back(value);
antenna_db.push_back(new_antenna); //Add this antenna
}
void ui::WhipCalcView::add_default_antenna()
{
antenna_db.push_back({"ANT500", {185, 315, 450, 586, 724, 862}}); //store a default ant500
}
} // namespace ui

View File

@ -28,69 +28,71 @@
#include "ui_receiver.hpp" #include "ui_receiver.hpp"
#include "ui_navigation.hpp" #include "ui_navigation.hpp"
#include "string_format.hpp" #include "string_format.hpp"
#include <vector>
namespace ui { namespace ui
{
class WhipCalcView : public View { class WhipCalcView : public View
public: {
WhipCalcView(NavigationView& nav); public:
WhipCalcView(NavigationView &nav);
void focus() override; void focus() override;
std::string title() const override { return "Antenna length"; }; std::string title() const override { return "Antenna length"; };
private: private:
const double speed_of_light_mps = 299792458.0; // m/s const double speed_of_light_mps = 299792458.0; // m/s
const double speed_of_light_fps = 983571087.90472; // feet/s const double speed_of_light_fps = 983571087.90472; // feet/s
const std::string frac_str[4] = {"", " 1/4", " 1/2", " 3/4"};
const std::string frac_str[4] = { "", "1/4 ", "1/2 ", "3/4 " }; struct antenna_entry
{
std::string label{};
std::vector<uint16_t> elements{};
};
std::vector<antenna_entry> antenna_db{};
double get_decimals(double num, int16_t mult, bool round = false); double get_decimals(double num, int16_t mult, bool round = false);
void update_result(); void update_result();
uint16_t string_to_number(std::string);
void txtline_process(std::string &);
void add_default_antenna();
Labels labels { Labels labels{
{ { 2 * 8, 2 * 16 }, "Frequency:", Color::light_grey() }, {{2 * 8, 1 * 16}, "Frequency:", Color::light_grey()},
{ { 2 * 8, 3 * 16 }, "Type:", Color::light_grey() } {{7 * 8, 2 * 16}, "Wave:", Color::light_grey()},
{{5 * 8, 3 * 16}, "Metric:", Color::light_grey()},
{{3 * 8, 4 * 16}, "Imperial:", Color::light_grey()}};
FrequencyField field_frequency{
{13 * 8, 1 * 16},
}; };
FrequencyField field_frequency { OptionsField options_type{
{ 13 * 8, 2 * 16 }, {13 * 8, 2 * 16},
}; 7,
{{"Full", 8},
{"Half", 4},
{"Quarter", 2},
{"3/4", 6},
{"1/8", 1},
{"3/8", 3},
{"5/8", 5},
{"7/8", 7}}};
OptionsField options_type { Text text_result_metric{
{ 8 * 8, 3 * 16 }, {13 * 8, 3 * 16, 10 * 16, 16},
12, "-"};
{ Text text_result_imperial{
{ "Full wave", 8 }, {13 * 8, 4 * 16, 10 * 16, 16},
{ "Half wave", 4 }, "-"};
{ "Quarter wave", 2 }, Console console{
{ "3/4 wave", 6 }, {0, 6 * 16, 240, 160}};
{ "1/8 wave", 1 },
{ "3/8 wave", 3 },
{ "5/8 wave", 5 },
{ "7/8 wave", 7 }
}
};
Text text_result_metric { Button button_exit{
{ 3 * 8, 5 * 16, 10 * 16, 16 }, {72, 17 * 16, 96, 32},
"-" "Back"};
}; };
Text text_result_imperial {
{ 2 * 8, 6 * 16, 10 * 16, 16 },
"-"
};
Text text_result_ant500 {
{ 2 * 8, 8 * 16, 26 * 16, 16 },
"-"
};
Button button_exit {
{ 72, 264, 96, 32 },
"Exit"
};
};
} /* namespace ui */ } /* namespace ui */
#endif/*__UI_WHIPCALC__*/ #endif /*__UI_WHIPCALC__*/

View File

@ -570,7 +570,10 @@ Console::Console(
{ {
} }
void Console::clear() { void Console::clear(bool clear_buffer = false) {
if(clear_buffer)
buffer.clear();
display.fill_rectangle( display.fill_rectangle(
screen_rect(), screen_rect(),
Color::black() Color::black()

View File

@ -312,7 +312,7 @@ class Console : public Widget {
public: public:
Console(Rect parent_rect); Console(Rect parent_rect);
void clear(); void clear(bool clear_buffer);
void write(std::string message); void write(std::string message);
void writeln(std::string message); void writeln(std::string message);

View File

@ -0,0 +1,4 @@
#antenna label,elements length in mm, separated by a commas
ANT700,95,134,175,206,230,245
ANT500,185,315,450,586,724,862
Cheap 11cm,118,183,253,326,399,476