Looking Glass optimizations

This commit is contained in:
euquiq 2023-02-27 23:29:13 -03:00
parent c8774e82d9
commit 19fed78b7f

View File

@ -22,156 +22,160 @@
#include "ui_looking_glass_app.hpp" #include "ui_looking_glass_app.hpp"
using namespace portapack; using namespace portapack;
namespace ui namespace ui
{ {
void GlassView::focus() { void GlassView::focus()
{
field_marker.focus(); field_marker.focus();
} }
GlassView::~GlassView() { GlassView::~GlassView()
{
receiver_model.set_sampling_rate(3072000); // Just a hack to avoid hanging other apps receiver_model.set_sampling_rate(3072000); // Just a hack to avoid hanging other apps
receiver_model.disable(); receiver_model.disable();
baseband::shutdown(); baseband::shutdown();
}
void GlassView::on_lna_changed(int32_t v_db) {
receiver_model.set_lna(v_db);
}
void GlassView::on_vga_changed(int32_t v_db) {
receiver_model.set_vga(v_db);
}
void GlassView::add_spectrum_pixel(Color color)
{
spectrum_row[pixel_index++] = color;
if (pixel_index == 240) //got an entire waterfall line
{
const auto draw_y = display.scroll(1); //Scroll 1 pixel down
display.draw_pixels( {{0, draw_y}, {240, 1}}, spectrum_row); //new line at top
pixel_index = 0; //Start New cascade line
} }
}
//Apparently, the spectrum object returns an array of 256 bins void GlassView::on_lna_changed(int32_t v_db)
//Each having the radio signal power for it's corresponding frequency slot {
void GlassView::on_channel_spectrum(const ChannelSpectrum &spectrum) receiver_model.set_lna(v_db);
{ }
void GlassView::on_vga_changed(int32_t v_db)
{
receiver_model.set_vga(v_db);
}
void GlassView::add_spectrum_pixel(Color color)
{
spectrum_row[pixel_index++] = color;
if (pixel_index == 240) // got an entire waterfall line
{
display.draw_pixels({{0, display.scroll(1)}, {240, 1}}, spectrum_row); // new line at top, one less var, speedier
pixel_index = 0; // Start New cascade line
}
}
// Apparently, the spectrum object returns an array of 256 bins
// Each having the radio signal power for it's corresponding frequency slot
void GlassView::on_channel_spectrum(const ChannelSpectrum &spectrum)
{
baseband::spectrum_streaming_stop(); baseband::spectrum_streaming_stop();
// Convert bins of this spectrum slice into a representative max_power and when enough, into pixels // Convert bins of this spectrum slice into a representative max_power and when enough, into pixels
// Spectrum.db has 256 bins. Center 12 bins are ignored (DC spike is blanked) Leftmost and rightmost 2 bins are ignored // Spectrum.db has 256 bins. Center 12 bins are ignored (DC spike is blanked) Leftmost and rightmost 2 bins are ignored
// All things said and done, we actually need 240 of those bins: // All things said and done, we actually need 240 of those bins:
for(uint8_t bin = 0; bin < 240; bin++) for (uint8_t bin = 0; bin < 240; bin++)
{ {
if (bin < 120) { if (bin < 120)
if (spectrum.db[134 + bin] > max_power) {
if (spectrum.db[134 + bin] > max_power) // 134
max_power = spectrum.db[134 + bin]; max_power = spectrum.db[134 + bin];
} }
else else
{ {
if (spectrum.db[bin - 118] > max_power) if (spectrum.db[bin - 118] > max_power) // 118
max_power = spectrum.db[bin - 118]; max_power = spectrum.db[bin - 118];
} }
bins_Hz_size += each_bin_size; //add this bin Hz count into the "pixel fulfilled bag of Hz" bins_Hz_size += each_bin_size; // add this bin Hz count into the "pixel fulfilled bag of Hz"
if (bins_Hz_size >= marker_pixel_step) //new pixel fullfilled if (bins_Hz_size >= marker_pixel_step) // new pixel fullfilled
{ {
if (min_color_power < max_power) if (min_color_power < max_power)
add_spectrum_pixel(spectrum_rgb3_lut[max_power]); //Pixel will represent max_power add_spectrum_pixel(spectrum_rgb3_lut[max_power]); // Pixel will represent max_power
else else
add_spectrum_pixel(0); //Filtered out, show black add_spectrum_pixel(0); // Filtered out, show black
max_power = 0; max_power = 0;
if (!pixel_index) //Received indication that a waterfall line has been completed if (!pixel_index) // Received indication that a waterfall line has been completed
{ {
bins_Hz_size = 0; //Since this is an entire pixel line, we don't carry "Pixels into next bin" bins_Hz_size = 0; // Since this is an entire pixel line, we don't carry "Pixels into next bin"
break; f_center = f_center_ini; // Start a new sweep
} else { radio::set_tuning_frequency(f_center); // tune rx for this new slice directly, faster than using persistent memory saving
bins_Hz_size -= marker_pixel_step; //reset bins size, but carrying the eventual excess Hz into next pixel chThdSleepMilliseconds(10);
baseband::spectrum_streaming_start(); // Do the RX
return;
} }
bins_Hz_size -= marker_pixel_step; // reset bins size, but carrying the eventual excess Hz into next pixel
} }
} }
if (pixel_index) f_center += LOOKING_GLASS_SLICE_WIDTH; // Move into the next bandwidth slice NOTE: spectrum.sampling_rate = LOOKING_GLASS_SLICE_WIDTH
f_center += LOOKING_GLASS_SLICE_WIDTH; //Move into the next bandwidth slice NOTE: spectrum.sampling_rate = LOOKING_GLASS_SLICE_WIDTH radio::set_tuning_frequency(f_center); // tune rx for this new slice directly, faster than using persistent memory saving
else chThdSleepMilliseconds(5);
f_center = f_center_ini; //Start a new sweep
receiver_model.set_tuning_frequency(f_center); //tune rx for this slice // receiver_model.set_tuning_frequency(f_center); //tune rx for this slice
baseband::spectrum_streaming_start(); //Do the RX baseband::spectrum_streaming_start(); // Do the RX
} }
void GlassView::on_hide() void GlassView::on_hide()
{ {
baseband::spectrum_streaming_stop(); baseband::spectrum_streaming_stop();
display.scroll_disable(); display.scroll_disable();
} }
void GlassView::on_show() void GlassView::on_show()
{ {
display.scroll_set_area( 109, 319); //Restart scroll on the correct coordinates display.scroll_set_area(109, 319); // Restart scroll on the correct coordinates
baseband::spectrum_streaming_start(); baseband::spectrum_streaming_start();
} }
void GlassView::on_range_changed() void GlassView::on_range_changed()
{ {
f_min = field_frequency_min.value(); f_min = field_frequency_min.value();
f_max = field_frequency_max.value(); f_max = field_frequency_max.value();
search_span = f_max - f_min; search_span = f_max - f_min;
field_marker.set_range(f_min, f_max); //Move the marker between range field_marker.set_range(f_min, f_max); // Move the marker between range
field_marker.set_value(f_min + (search_span / 2)); //Put MARKER AT MIDDLE RANGE field_marker.set_value(f_min + (search_span / 2)); // Put MARKER AT MIDDLE RANGE
text_range.set(to_string_dec_uint(search_span)); text_range.set(to_string_dec_uint(search_span));
f_min = (f_min)*MHZ_DIV; //Transpose into full frequency realm f_min = (f_min)*MHZ_DIV; // Transpose into full frequency realm
f_max = (f_max)*MHZ_DIV; f_max = (f_max)*MHZ_DIV;
search_span = search_span * MHZ_DIV; search_span = search_span * MHZ_DIV;
marker_pixel_step = search_span / 240; //Each pixel value in Hz marker_pixel_step = search_span / 240; // Each pixel value in Hz
text_marker_pm.set(to_string_dec_uint((marker_pixel_step / X2_MHZ_DIV) + 1)); // Give idea of +/- marker precision text_marker_pm.set(to_string_dec_uint((marker_pixel_step / X2_MHZ_DIV) + 1)); // Give idea of +/- marker precision
int32_t marker_step = marker_pixel_step / MHZ_DIV; int32_t marker_step = marker_pixel_step / MHZ_DIV;
if (!marker_step) if (!marker_step)
field_marker.set_step(1); //in case selected range is less than 240 (pixels) field_marker.set_step(1); // in case selected range is less than 240 (pixels)
else else
field_marker.set_step(marker_step); //step needs to be a pixel wide. field_marker.set_step(marker_step); // step needs to be a pixel wide.
f_center_ini = f_min + (LOOKING_GLASS_SLICE_WIDTH / 2); //Initial center frequency for sweep f_center_ini = f_min + (LOOKING_GLASS_SLICE_WIDTH / 2); // Initial center frequency for sweep
f_center_ini += LOOKING_GLASS_SLICE_WIDTH; //euquiq: Why do I need to move the center ???!!! (shift needed for marker accuracy)
PlotMarker(field_marker.value()); //Refresh marker on screen PlotMarker(field_marker.value()); // Refresh marker on screen
f_center = f_center_ini; //Reset sweep into first slice f_center = f_center_ini; // Reset sweep into first slice
pixel_index = 0; //reset pixel counter pixel_index = 0; // reset pixel counter
max_power = 0; max_power = 0;
bins_Hz_size = 0; //reset amount of Hz filled up by pixels bins_Hz_size = 0; // reset amount of Hz filled up by pixels
baseband::set_spectrum(LOOKING_GLASS_SLICE_WIDTH, field_trigger.value()); baseband::set_spectrum(LOOKING_GLASS_SLICE_WIDTH, field_trigger.value());
receiver_model.set_tuning_frequency(f_center_ini); //tune rx for this slice receiver_model.set_tuning_frequency(f_center_ini); // tune rx for this slice
} }
void GlassView::PlotMarker(rf::Frequency fpos) void GlassView::PlotMarker(rf::Frequency pos)
{ {
int64_t pos = fpos * MHZ_DIV; pos = pos * MHZ_DIV;
pos -= f_min; pos -= f_min;
pos = pos / marker_pixel_step; //Real pixel pos = pos / marker_pixel_step; // Real pixel
portapack::display.fill_rectangle({0, 100, 240, 8}, Color::black()); //Clear old marker and whole marker rectangle btw portapack::display.fill_rectangle({0, 100, 240, 8}, Color::black()); // Clear old marker and whole marker rectangle btw
portapack::display.fill_rectangle({(int16_t)pos - 2, 100, 5, 3}, Color::red()); //Red marker middle portapack::display.fill_rectangle({(int)pos - 2, 100, 5, 3}, Color::red()); // Red marker top
portapack::display.fill_rectangle({(int16_t)pos - 1, 103, 3, 3}, Color::red()); //Red marker middle portapack::display.fill_rectangle({(int)pos - 1, 103, 3, 3}, Color::red()); // Red marker middle
portapack::display.fill_rectangle({(int16_t)pos, 106, 1, 2}, Color::red()); //Red marker middle portapack::display.fill_rectangle({(int)pos, 106, 1, 2}, Color::red()); // Red marker bottom
} }
GlassView::GlassView( GlassView::GlassView(
NavigationView &nav) : nav_(nav) NavigationView &nav) : nav_(nav)
{ {
baseband::run_image(portapack::spi_flash::image_tag_wideband_spectrum); baseband::run_image(portapack::spi_flash::image_tag_wideband_spectrum);
add_children({&labels, add_children({&labels,
@ -185,152 +189,173 @@ GlassView::GlassView(
&range_presets, &range_presets,
&field_marker, &field_marker,
&text_marker_pm, &text_marker_pm,
&field_trigger &field_trigger});
});
load_Presets(); //Load available presets from TXT files (or default) load_Presets(); // Load available presets from TXT files (or default)
field_frequency_min.set_value(presets_db[0].min); //Defaults to first preset field_frequency_min.set_value(presets_db[0].min); // Defaults to first preset
field_frequency_min.on_change = [this](int32_t v) { field_frequency_min.on_change = [this](int32_t v)
{
if (v >= field_frequency_max.value()) if (v >= field_frequency_max.value())
field_frequency_max.set_value(v + 240); field_frequency_max.set_value(v + 240);
this->on_range_changed(); this->on_range_changed();
}; };
field_frequency_max.set_value(presets_db[0].max); //Defaults to first preset field_frequency_max.set_value(presets_db[0].max); // Defaults to first preset
field_frequency_max.on_change = [this](int32_t v) { field_frequency_max.on_change = [this](int32_t v)
{
if (v <= field_frequency_min.value()) if (v <= field_frequency_min.value())
field_frequency_min.set_value(v - 240); field_frequency_min.set_value(v - 240);
this->on_range_changed(); this->on_range_changed();
}; };
field_lna.set_value(receiver_model.lna()); field_lna.set_value(receiver_model.lna());
field_lna.on_change = [this](int32_t v) { field_lna.on_change = [this](int32_t v)
{
this->on_lna_changed(v); this->on_lna_changed(v);
}; };
field_vga.set_value(receiver_model.vga()); field_vga.set_value(receiver_model.vga());
field_vga.on_change = [this](int32_t v_db) { field_vga.on_change = [this](int32_t v_db)
{
this->on_vga_changed(v_db); this->on_vga_changed(v_db);
}; };
filter_config.set_selected_index(0); filter_config.set_selected_index(0);
filter_config.on_change = [this](size_t n, OptionsField::value_t v) { filter_config.on_change = [this](size_t n, OptionsField::value_t v)
{
(void)n; (void)n;
min_color_power = v; min_color_power = v;
}; };
range_presets.on_change = [this](size_t n, OptionsField::value_t v) { range_presets.on_change = [this](size_t n, OptionsField::value_t v)
{
(void)n; (void)n;
field_frequency_min.set_value(presets_db[v].min,false); field_frequency_min.set_value(presets_db[v].min, false);
field_frequency_max.set_value(presets_db[v].max,false); field_frequency_max.set_value(presets_db[v].max, false);
this->on_range_changed(); this->on_range_changed();
}; };
field_marker.on_change = [this](int32_t v) { field_marker.on_change = [this](int32_t v)
PlotMarker(v); //Refresh marker on screen {
PlotMarker(v); // Refresh marker on screen
}; };
field_marker.on_select = [this](NumberField&) { field_marker.on_select = [this](NumberField &)
{
f_center = field_marker.value(); f_center = field_marker.value();
f_center = f_center * MHZ_DIV; f_center = f_center * MHZ_DIV;
receiver_model.set_tuning_frequency(f_center); //Center tune rx in marker freq. receiver_model.set_tuning_frequency(f_center); // Center tune rx in marker freq.
receiver_model.set_frequency_step(MHZ_DIV); // Preset a 1 MHz frequency step into RX -> AUDIO receiver_model.set_frequency_step(MHZ_DIV); // Preset a 1 MHz frequency step into RX -> AUDIO
nav_.pop(); nav_.pop();
nav_.push<AnalogAudioView>(); //Jump into audio view nav_.push<AnalogAudioView>(); // Jump into audio view
}; };
field_trigger.set_value(32); //Defaults to 32, as normal triggering resolution field_trigger.set_value(32); // Defaults to 32, as normal triggering resolution
field_trigger.on_change = [this](int32_t v) { field_trigger.on_change = [this](int32_t v)
{
baseband::set_spectrum(LOOKING_GLASS_SLICE_WIDTH, v); baseband::set_spectrum(LOOKING_GLASS_SLICE_WIDTH, v);
}; };
display.scroll_set_area( 109, 319); display.scroll_set_area(109, 319);
baseband::set_spectrum(LOOKING_GLASS_SLICE_WIDTH, field_trigger.value()); //trigger: baseband::set_spectrum(LOOKING_GLASS_SLICE_WIDTH, field_trigger.value()); // trigger:
// Discord User jteich: WidebandSpectrum::on_message to set the trigger value. In WidebandSpectrum::execute , // Discord User jteich: WidebandSpectrum::on_message to set the trigger value. In WidebandSpectrum::execute ,
// it keeps adding the output of the fft to the buffer until "trigger" number of calls are made, // it keeps adding the output of the fft to the buffer until "trigger" number of calls are made,
//at which time it pushes the buffer up with channel_spectrum.feed // at which time it pushes the buffer up with channel_spectrum.feed
on_range_changed(); on_range_changed();
receiver_model.set_modulation(ReceiverModel::Mode::SpectrumAnalysis); receiver_model.set_modulation(ReceiverModel::Mode::SpectrumAnalysis);
receiver_model.set_sampling_rate(LOOKING_GLASS_SLICE_WIDTH); //20mhz receiver_model.set_sampling_rate(LOOKING_GLASS_SLICE_WIDTH); // 20mhz
receiver_model.set_baseband_bandwidth(LOOKING_GLASS_SLICE_WIDTH); // possible values: 1.75/2.5/3.5/5/5.5/6/7/8/9/10/12/14/15/20/24/28MHz receiver_model.set_baseband_bandwidth(LOOKING_GLASS_SLICE_WIDTH); // possible values: 1.75/2.5/3.5/5/5.5/6/7/8/9/10/12/14/15/20/24/28MHz
receiver_model.set_squelch_level(0); receiver_model.set_squelch_level(0);
receiver_model.enable(); receiver_model.enable();
} }
void GlassView::load_Presets() { void GlassView::load_Presets()
File presets_file; //LOAD /WHIPCALC/ANTENNAS.TXT from microSD {
File presets_file; // LOAD /WHIPCALC/ANTENNAS.TXT from microSD
auto result = presets_file.open("LOOKINGGLASS/PRESETS.TXT"); auto result = presets_file.open("LOOKINGGLASS/PRESETS.TXT");
presets_db.clear(); //Start with fresh db presets_db.clear(); // Start with fresh db
if (result.is_valid()) { if (result.is_valid())
presets_Default(); //There is no txt, store a default range {
} else { presets_Default(); // There is no txt, store a default range
}
std::string line; //There is a txt file else
char one_char[1]; //Read it char by char {
for (size_t pointer=0; pointer < presets_file.size();pointer++) { std::string line; // There is a txt file
char one_char[1]; // Read it char by char
for (size_t pointer = 0; pointer < presets_file.size(); pointer++)
{
presets_file.seek(pointer); presets_file.seek(pointer);
presets_file.read(one_char, 1); presets_file.read(one_char, 1);
if ((int)one_char[0] > 31) { //ascii space upwards if ((int)one_char[0] > 31)
line += one_char[0]; //Add it to the textline { // ascii space upwards
line += one_char[0]; // Add it to the textline
} }
else if (one_char[0] == '\n') { //New Line else if (one_char[0] == '\n')
txtline_process(line); //make sense of this textline { // New Line
line.clear(); //Ready for next textline 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 (line.length() > 0)
if (!presets_db.size()) presets_Default(); //no antenna on txt, use default txtline_process(line); // Last line had no newline at end ?
if (!presets_db.size())
presets_Default(); // no antenna on txt, use default
} }
populate_Presets(); populate_Presets();
} }
void GlassView::txtline_process(std::string& line) void GlassView::txtline_process(std::string &line)
{ {
if (line.find("#") != std::string::npos) return; //Line is just a comment if (line.find("#") != std::string::npos)
return; // Line is just a comment
size_t comma = line.find(","); //Get first comma position size_t comma = line.find(","); // Get first comma position
if (comma == std::string::npos) return; //No comma at all if (comma == std::string::npos)
return; // No comma at all
size_t previous = 0; size_t previous = 0;
preset_entry new_preset; preset_entry new_preset;
new_preset.min = std::stoi(line.substr(0,comma)); new_preset.min = std::stoi(line.substr(0, comma));
if (!new_preset.min) return; //No frequency! if (!new_preset.min)
return; // No frequency!
previous = comma + 1; previous = comma + 1;
comma = line.find(",",previous); //Search for next delimiter comma = line.find(",", previous); // Search for next delimiter
if (comma == std::string::npos) return; //No comma at all if (comma == std::string::npos)
return; // No comma at all
new_preset.max = std::stoi(line.substr(previous,comma - previous)); new_preset.max = std::stoi(line.substr(previous, comma - previous));
if (!new_preset.max) return; //No frequency! if (!new_preset.max)
return; // No frequency!
new_preset.label = line.substr(comma + 1); new_preset.label = line.substr(comma + 1);
if (new_preset.label.size() == 0) return; //No label ? if (new_preset.label.size() == 0)
return; // No label ?
presets_db.push_back(new_preset); //Add this preset. presets_db.push_back(new_preset); // Add this preset.
} }
void GlassView::populate_Presets() void GlassView::populate_Presets()
{ {
using option_t = std::pair<std::string, int32_t>; using option_t = std::pair<std::string, int32_t>;
using options_t = std::vector<option_t>; using options_t = std::vector<option_t>;
options_t entries; options_t entries;
for (preset_entry preset : presets_db) for (preset_entry preset : presets_db)
{ //go thru all available presets { // go thru all available presets
entries.emplace_back(preset.label,entries.size()); entries.emplace_back(preset.label, entries.size());
} }
range_presets.set_options(entries); range_presets.set_options(entries);
} }
void GlassView::presets_Default() void GlassView::presets_Default()
{ {
presets_db.clear(); presets_db.clear();
presets_db.push_back({2320, 2560, "DEFAULT WIFI 2.4GHz"}); presets_db.push_back({2320, 2560, "DEFAULT WIFI 2.4GHz"});
} }
} }