2016-05-11 06:45:03 -04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015 Jared Boone, ShareBrained Technology, Inc.
|
|
|
|
* Copyright (C) 2016 Furrtek
|
|
|
|
*
|
|
|
|
* 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 "ui_closecall.hpp"
|
|
|
|
|
2017-01-15 22:45:44 -05:00
|
|
|
#include "rtc_time.hpp"
|
2016-05-11 06:45:03 -04:00
|
|
|
#include "event_m0.hpp"
|
|
|
|
#include "portapack.hpp"
|
|
|
|
#include "baseband_api.hpp"
|
2016-05-17 14:27:51 -04:00
|
|
|
#include "string_format.hpp"
|
2016-05-11 06:45:03 -04:00
|
|
|
|
|
|
|
#include <cstring>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
using namespace portapack;
|
|
|
|
|
|
|
|
namespace ui {
|
|
|
|
|
|
|
|
void CloseCallView::focus() {
|
2017-02-01 03:53:26 -05:00
|
|
|
field_frequency_min.focus();
|
2016-05-11 06:45:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
CloseCallView::~CloseCallView() {
|
2017-01-15 22:45:44 -05:00
|
|
|
rtc_time::signal_tick_second -= signal_token_tick_second;
|
2016-12-08 10:13:27 -05:00
|
|
|
receiver_model.disable();
|
|
|
|
baseband::shutdown();
|
2016-05-11 06:45:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void CloseCallView::do_detection() {
|
|
|
|
uint8_t xmax = 0;
|
2017-02-01 03:53:26 -05:00
|
|
|
int16_t imax = 0;
|
2016-05-17 14:27:51 -04:00
|
|
|
uint16_t iraw = 0, c;
|
|
|
|
uint8_t power;
|
|
|
|
rf::Frequency freq_low, freq_high;
|
2016-05-11 06:45:03 -04:00
|
|
|
|
2017-02-01 03:53:26 -05:00
|
|
|
mean /= (CC_BIN_NB_NO_DC * slices_max);
|
2016-05-11 06:45:03 -04:00
|
|
|
|
2017-02-01 03:53:26 -05:00
|
|
|
// Find max value over threshold for all slices
|
2016-05-17 14:27:51 -04:00
|
|
|
for (c = 0; c < slices_max; c++) {
|
2017-02-01 03:53:26 -05:00
|
|
|
power = slicemax_pow[c];
|
2016-05-17 14:27:51 -04:00
|
|
|
if (power >= min_threshold) {
|
|
|
|
if ((power - min_threshold >= mean) && (power > xmax)) {
|
|
|
|
xmax = power;
|
2016-05-16 06:02:45 -04:00
|
|
|
imax = slicemax_idx[c] + (c * CC_BIN_NB);
|
2016-05-17 14:27:51 -04:00
|
|
|
iraw = slicemax_idx[c];
|
2016-05-11 06:45:03 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lock / release
|
2017-02-01 03:53:26 -05:00
|
|
|
if ((imax >= last_channel - 2) && (imax <= last_channel + 2) && (imax)) {
|
|
|
|
// Staying around the same frequency
|
|
|
|
if (detect_counter >= (5 / slices_max)) { // Todo: ugly, change. Should depend on refresh rate.
|
2016-05-17 14:27:51 -04:00
|
|
|
if ((imax != locked_imax) || (!locked)) {
|
2016-12-08 10:13:27 -05:00
|
|
|
std::string finalstr;
|
2016-05-17 14:27:51 -04:00
|
|
|
|
2016-12-08 10:13:27 -05:00
|
|
|
if (locked) {
|
2017-02-01 03:53:26 -05:00
|
|
|
// Already locked, adjust
|
|
|
|
if (weight < 16) {
|
|
|
|
frequency_acc += (slice_frequency + (CC_BIN_WIDTH * (imax - 120))); // Average
|
|
|
|
weight++;
|
|
|
|
}
|
2016-12-08 10:13:27 -05:00
|
|
|
} else {
|
2017-02-01 03:53:26 -05:00
|
|
|
// Lost, locking
|
|
|
|
frequency_acc = slice_frequency + (CC_BIN_WIDTH * (imax - 120)); // Init
|
|
|
|
if ((frequency_acc >= f_min) && (frequency_acc <= f_max)) {
|
2016-05-11 06:45:03 -04:00
|
|
|
|
2016-12-08 10:13:27 -05:00
|
|
|
text_infos.set("Locked !");
|
|
|
|
big_display.set_style(&style_locked);
|
|
|
|
|
|
|
|
// Approximation/error display
|
2017-02-01 03:53:26 -05:00
|
|
|
freq_low = (frequency_acc - 4883) / 1000;
|
|
|
|
freq_high = (frequency_acc + 4883) / 1000;
|
|
|
|
finalstr = "~9.8kHz: " + to_string_dec_uint(freq_low / 1000) + "." + to_string_dec_uint(freq_low % 1000);
|
2016-12-08 10:13:27 -05:00
|
|
|
finalstr += "/" + to_string_dec_uint(freq_high / 1000) + "." + to_string_dec_uint(freq_high % 1000);
|
|
|
|
text_precision.set(finalstr);
|
|
|
|
|
|
|
|
locked = true;
|
2017-02-01 03:53:26 -05:00
|
|
|
weight = 1;
|
2016-12-08 10:13:27 -05:00
|
|
|
locked_imax = imax;
|
|
|
|
}
|
|
|
|
}
|
2017-02-01 03:53:26 -05:00
|
|
|
|
|
|
|
resolved_frequency = frequency_acc / weight;
|
|
|
|
big_display.set(resolved_frequency);
|
2016-05-11 06:45:03 -04:00
|
|
|
}
|
|
|
|
release_counter = 0;
|
|
|
|
} else {
|
|
|
|
detect_counter++;
|
|
|
|
}
|
|
|
|
} else {
|
2016-05-11 17:57:22 -04:00
|
|
|
detect_counter = 0;
|
2016-05-17 14:27:51 -04:00
|
|
|
if (locked) {
|
|
|
|
if (release_counter == 6) {
|
2016-05-11 06:45:03 -04:00
|
|
|
locked = false;
|
2016-12-08 10:13:27 -05:00
|
|
|
text_infos.set("Lost");
|
2016-05-14 20:35:08 -04:00
|
|
|
big_display.set_style(&style_grey);
|
2017-02-01 03:53:26 -05:00
|
|
|
//big_display.set(resolved_frequency);
|
2016-05-11 06:45:03 -04:00
|
|
|
} else {
|
|
|
|
release_counter++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
last_channel = imax;
|
2016-05-11 17:57:22 -04:00
|
|
|
scan_counter++;
|
2016-05-11 06:45:03 -04:00
|
|
|
|
2017-02-01 03:53:26 -05:00
|
|
|
portapack::display.fill_rectangle({last_pos, 90, 1, 6}, Color::black());
|
|
|
|
if (iraw < 120)
|
|
|
|
iraw += 2;
|
|
|
|
else
|
|
|
|
iraw -= 0;
|
|
|
|
last_pos = (ui::Coord)iraw;
|
|
|
|
portapack::display.fill_rectangle({last_pos, 90, 1, 6}, Color::red());
|
2016-05-11 06:45:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void CloseCallView::on_channel_spectrum(const ChannelSpectrum& spectrum) {
|
|
|
|
uint8_t xmax = 0;
|
2017-02-01 03:53:26 -05:00
|
|
|
int16_t imax = 0;
|
|
|
|
uint8_t power;
|
2016-05-11 17:57:22 -04:00
|
|
|
size_t i, m;
|
2017-02-01 03:53:26 -05:00
|
|
|
std::array<Color, 240> pixel_row;
|
2016-05-11 06:45:03 -04:00
|
|
|
|
2016-07-26 21:03:40 -04:00
|
|
|
baseband::spectrum_streaming_stop();
|
2016-05-27 00:48:04 -04:00
|
|
|
|
2017-02-01 03:53:26 -05:00
|
|
|
// Draw spectrum line (for debug)
|
|
|
|
for (i = 0; i < 120; i++) {
|
|
|
|
const auto pixel_color = spectrum_rgb3_lut[spectrum.db[134 + i]]; // 134~253 in 0~119
|
|
|
|
pixel_row[i] = pixel_color;
|
2016-05-27 00:48:04 -04:00
|
|
|
}
|
2017-02-01 03:53:26 -05:00
|
|
|
for (i = 120; i < 240; i++) {
|
|
|
|
const auto pixel_color = spectrum_rgb3_lut[spectrum.db[i - 118]]; // 2~121 in 120~239
|
|
|
|
pixel_row[i] = pixel_color;
|
2016-05-27 00:48:04 -04:00
|
|
|
}
|
|
|
|
display.draw_pixels(
|
|
|
|
{ { 0, 96 + slices_counter * 4 }, { pixel_row.size(), 1 } },
|
|
|
|
pixel_row
|
|
|
|
);
|
|
|
|
|
|
|
|
// Find max for this slice:
|
|
|
|
|
|
|
|
// Check if left of slice needs to be trimmed (masked)
|
2016-12-08 10:13:27 -05:00
|
|
|
//if (slices_counter == 0)
|
|
|
|
// i = slice_trim;
|
|
|
|
//else
|
2016-05-27 00:48:04 -04:00
|
|
|
i = 0;
|
2017-02-01 03:53:26 -05:00
|
|
|
for ( ; i < 120; i++) {
|
|
|
|
power = spectrum.db[134 + i];
|
|
|
|
mean += power;
|
|
|
|
if (power > xmax) {
|
|
|
|
xmax = power;
|
|
|
|
imax = i - 2;
|
2016-05-11 06:45:03 -04:00
|
|
|
}
|
2016-05-27 00:48:04 -04:00
|
|
|
}
|
|
|
|
// Check if right of slice needs to be trimmed (masked)
|
2017-02-01 03:53:26 -05:00
|
|
|
//if (slices_counter == (slices_max - 1))
|
|
|
|
// m = 240 - slice_trim;
|
|
|
|
//else
|
2016-05-27 00:48:04 -04:00
|
|
|
m = 240;
|
2017-02-01 03:53:26 -05:00
|
|
|
for (i = 120; i < m; i++) {
|
|
|
|
power = spectrum.db[i - 118];
|
|
|
|
mean += power;
|
|
|
|
if (power > xmax) {
|
|
|
|
xmax = power;
|
|
|
|
imax = i + 2;
|
2016-05-11 06:45:03 -04:00
|
|
|
}
|
2016-05-27 00:48:04 -04:00
|
|
|
}
|
2017-02-01 03:53:26 -05:00
|
|
|
|
|
|
|
slicemax_pow[slices_counter] = xmax;
|
2016-05-27 00:48:04 -04:00
|
|
|
slicemax_idx[slices_counter] = imax;
|
|
|
|
|
|
|
|
// Slice update
|
|
|
|
if (slicing) {
|
|
|
|
if (slices_counter >= (slices_max - 1)) {
|
2016-05-11 06:45:03 -04:00
|
|
|
do_detection();
|
2016-05-27 00:48:04 -04:00
|
|
|
mean = 0;
|
|
|
|
slices_counter = 0;
|
|
|
|
} else {
|
|
|
|
slices_counter++;
|
2016-05-11 06:45:03 -04:00
|
|
|
}
|
2016-05-27 00:48:04 -04:00
|
|
|
slice_frequency = slice_start + (slices_counter * CC_SLICE_WIDTH);
|
|
|
|
receiver_model.set_tuning_frequency(slice_frequency);
|
2016-05-11 06:45:03 -04:00
|
|
|
} else {
|
2016-05-27 00:48:04 -04:00
|
|
|
do_detection();
|
2016-05-11 06:45:03 -04:00
|
|
|
}
|
2016-05-27 00:48:04 -04:00
|
|
|
|
2016-07-26 21:03:40 -04:00
|
|
|
baseband::spectrum_streaming_start();
|
2016-05-11 06:45:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void CloseCallView::on_show() {
|
2016-07-26 21:03:40 -04:00
|
|
|
baseband::spectrum_streaming_start();
|
2016-05-11 06:45:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void CloseCallView::on_hide() {
|
|
|
|
baseband::spectrum_streaming_stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CloseCallView::on_range_changed() {
|
|
|
|
rf::Frequency slices_span;
|
2017-02-01 03:53:26 -05:00
|
|
|
rf::Frequency slice_width;
|
2016-05-11 06:45:03 -04:00
|
|
|
int64_t offset;
|
2016-05-27 00:48:04 -04:00
|
|
|
|
2016-05-11 06:45:03 -04:00
|
|
|
f_max = field_frequency_max.value();
|
|
|
|
f_min = field_frequency_min.value();
|
|
|
|
scan_span = abs(f_max - f_min);
|
2016-05-17 14:27:51 -04:00
|
|
|
|
2016-05-11 06:45:03 -04:00
|
|
|
if (scan_span > CC_SLICE_WIDTH) {
|
|
|
|
// ex: 100~115 (15): 102.5(97.5~107.5) -> 112.5(107.5~117.5) = 2.5 lost left and right
|
|
|
|
slices_max = (scan_span + CC_SLICE_WIDTH - 1) / CC_SLICE_WIDTH;
|
|
|
|
slices_span = slices_max * CC_SLICE_WIDTH;
|
2016-05-14 20:35:08 -04:00
|
|
|
offset = ((scan_span - slices_span) / 2) + (CC_SLICE_WIDTH / 2);
|
2016-05-11 06:45:03 -04:00
|
|
|
slice_start = std::min(f_min, f_max) + offset;
|
2016-05-14 20:35:08 -04:00
|
|
|
slice_trim = 0;
|
2016-05-11 06:45:03 -04:00
|
|
|
slicing = true;
|
2016-05-11 17:57:22 -04:00
|
|
|
|
|
|
|
// Todo: trims
|
2016-05-11 06:45:03 -04:00
|
|
|
} else {
|
|
|
|
slice_frequency = (f_max + f_min) / 2;
|
|
|
|
receiver_model.set_tuning_frequency(slice_frequency);
|
2016-05-11 17:57:22 -04:00
|
|
|
|
2017-02-01 03:53:26 -05:00
|
|
|
slice_width = abs(f_max - f_min);
|
|
|
|
slice_trim = (240 - (slice_width * 240 / CC_SLICE_WIDTH)) / 2;
|
2016-05-11 17:57:22 -04:00
|
|
|
|
2017-02-01 03:53:26 -05:00
|
|
|
portapack::display.fill_rectangle({0, 97, 240, 4}, Color::black());
|
|
|
|
portapack::display.fill_rectangle({0, 97, slice_trim, 4}, Color::orange());
|
|
|
|
portapack::display.fill_rectangle({240 - slice_trim, 97, slice_trim, 4}, Color::orange());
|
2016-05-11 17:57:22 -04:00
|
|
|
|
2016-05-17 14:27:51 -04:00
|
|
|
slices_max = 1;
|
2016-05-11 06:45:03 -04:00
|
|
|
slices_counter = 0;
|
|
|
|
slicing = false;
|
2016-05-27 00:48:04 -04:00
|
|
|
}
|
2016-05-11 17:57:22 -04:00
|
|
|
|
2016-05-27 00:48:04 -04:00
|
|
|
/*
|
2016-05-17 14:27:51 -04:00
|
|
|
f_min = field_frequency_min.value();
|
|
|
|
scan_span = 3000000;
|
|
|
|
slice_frequency = (f_min + 1500000);
|
|
|
|
slice_start = slice_frequency;
|
|
|
|
receiver_model.set_tuning_frequency(slice_frequency);
|
|
|
|
slice_trim = 0;
|
|
|
|
slices_max = 1;
|
|
|
|
slices_counter = 0;
|
|
|
|
slicing = false;
|
|
|
|
field_frequency_max.set_value(f_min + 3000000);
|
2016-05-27 00:48:04 -04:00
|
|
|
*/
|
2016-05-17 14:27:51 -04:00
|
|
|
|
|
|
|
text_slices.set(to_string_dec_int(slices_max));
|
2016-05-11 06:45:03 -04:00
|
|
|
slices_counter = 0;
|
|
|
|
}
|
|
|
|
|
2016-05-11 17:57:22 -04:00
|
|
|
void CloseCallView::on_lna_changed(int32_t v_db) {
|
|
|
|
receiver_model.set_lna(v_db);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CloseCallView::on_vga_changed(int32_t v_db) {
|
|
|
|
receiver_model.set_vga(v_db);
|
|
|
|
}
|
|
|
|
|
2016-05-12 19:18:04 -04:00
|
|
|
void CloseCallView::on_tick_second() {
|
2016-05-17 14:27:51 -04:00
|
|
|
// Update scan rate indication
|
2016-05-11 17:57:22 -04:00
|
|
|
text_rate.set(to_string_dec_uint(scan_counter, 3));
|
|
|
|
scan_counter = 0;
|
|
|
|
}
|
|
|
|
|
2016-05-11 06:45:03 -04:00
|
|
|
CloseCallView::CloseCallView(
|
|
|
|
NavigationView& nav
|
|
|
|
)
|
|
|
|
{
|
2017-02-01 03:53:26 -05:00
|
|
|
baseband::run_image(portapack::spi_flash::image_tag_wideband_spectrum);
|
2016-12-08 10:13:27 -05:00
|
|
|
|
2017-01-15 22:45:44 -05:00
|
|
|
add_children({
|
2017-02-13 00:35:13 -05:00
|
|
|
&labels,
|
2016-05-11 06:45:03 -04:00
|
|
|
&field_frequency_min,
|
|
|
|
&field_frequency_max,
|
2016-05-11 17:57:22 -04:00
|
|
|
&field_lna,
|
|
|
|
&field_vga,
|
|
|
|
&field_threshold,
|
|
|
|
&text_slices,
|
|
|
|
&text_rate,
|
2016-05-16 06:02:45 -04:00
|
|
|
&text_mhz,
|
2016-05-11 06:45:03 -04:00
|
|
|
&text_infos,
|
2016-05-17 14:27:51 -04:00
|
|
|
&text_precision,
|
2016-05-11 17:57:22 -04:00
|
|
|
&text_debug,
|
|
|
|
&big_display,
|
2016-05-11 06:45:03 -04:00
|
|
|
&button_exit
|
2017-01-15 22:45:44 -05:00
|
|
|
});
|
2016-05-11 17:57:22 -04:00
|
|
|
|
|
|
|
text_slices.set_style(&style_grey);
|
|
|
|
text_rate.set_style(&style_grey);
|
2016-05-16 06:02:45 -04:00
|
|
|
text_mhz.set_style(&style_grey);
|
|
|
|
big_display.set_style(&style_grey);
|
2016-05-11 17:57:22 -04:00
|
|
|
|
2017-02-01 03:53:26 -05:00
|
|
|
baseband::set_spectrum(CC_SLICE_WIDTH, 32);
|
2016-05-17 14:27:51 -04:00
|
|
|
|
2016-05-11 17:57:22 -04:00
|
|
|
field_threshold.set_value(80);
|
|
|
|
field_threshold.on_change = [this](int32_t v) {
|
|
|
|
min_threshold = v;
|
|
|
|
};
|
2016-05-11 06:45:03 -04:00
|
|
|
|
2016-05-11 17:57:22 -04:00
|
|
|
field_frequency_min.set_value(receiver_model.tuning_frequency());
|
2016-05-17 14:27:51 -04:00
|
|
|
field_frequency_min.set_step(100000);
|
2017-02-01 03:53:26 -05:00
|
|
|
field_frequency_min.on_change = [this](rf::Frequency) {
|
2016-05-11 06:45:03 -04:00
|
|
|
this->on_range_changed();
|
|
|
|
};
|
|
|
|
field_frequency_min.on_edit = [this, &nav]() {
|
|
|
|
auto new_view = nav.push<FrequencyKeypadView>(receiver_model.tuning_frequency());
|
|
|
|
new_view->on_changed = [this](rf::Frequency f) {
|
2016-12-08 10:13:27 -05:00
|
|
|
//this->on_range_changed();
|
2016-05-11 06:45:03 -04:00
|
|
|
this->field_frequency_min.set_value(f);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2016-12-08 10:13:27 -05:00
|
|
|
field_frequency_max.set_value(receiver_model.tuning_frequency() + 2000000);
|
2016-05-12 19:18:04 -04:00
|
|
|
field_frequency_max.set_step(100000);
|
2017-02-01 03:53:26 -05:00
|
|
|
field_frequency_max.on_change = [this](rf::Frequency) {
|
2016-05-11 06:45:03 -04:00
|
|
|
this->on_range_changed();
|
|
|
|
};
|
|
|
|
field_frequency_max.on_edit = [this, &nav]() {
|
|
|
|
auto new_view = nav.push<FrequencyKeypadView>(receiver_model.tuning_frequency());
|
|
|
|
new_view->on_changed = [this](rf::Frequency f) {
|
2016-12-08 10:13:27 -05:00
|
|
|
//this->on_range_changed();
|
2016-05-11 06:45:03 -04:00
|
|
|
this->field_frequency_max.set_value(f);
|
|
|
|
};
|
2016-12-08 10:13:27 -05:00
|
|
|
};
|
2016-05-11 06:45:03 -04:00
|
|
|
|
2016-05-11 17:57:22 -04:00
|
|
|
field_lna.set_value(receiver_model.lna());
|
2016-05-11 06:45:03 -04:00
|
|
|
field_lna.on_change = [this](int32_t v) {
|
|
|
|
this->on_lna_changed(v);
|
|
|
|
};
|
|
|
|
|
|
|
|
field_vga.set_value(receiver_model.vga());
|
|
|
|
field_vga.on_change = [this](int32_t v_db) {
|
|
|
|
this->on_vga_changed(v_db);
|
|
|
|
};
|
|
|
|
|
2016-05-11 17:57:22 -04:00
|
|
|
on_range_changed();
|
2016-05-11 06:45:03 -04:00
|
|
|
|
2016-05-11 17:57:22 -04:00
|
|
|
button_exit.on_select = [&nav](Button&){
|
|
|
|
nav.pop();
|
|
|
|
};
|
2016-05-11 06:45:03 -04:00
|
|
|
|
2017-01-15 22:45:44 -05:00
|
|
|
signal_token_tick_second = rtc_time::signal_tick_second += [this]() {
|
2016-05-12 19:18:04 -04:00
|
|
|
this->on_tick_second();
|
|
|
|
};
|
2016-12-08 10:13:27 -05:00
|
|
|
|
|
|
|
receiver_model.set_modulation(ReceiverModel::Mode::SpectrumAnalysis);
|
2017-02-01 03:53:26 -05:00
|
|
|
receiver_model.set_sampling_rate(CC_SLICE_WIDTH);
|
2016-12-08 10:13:27 -05:00
|
|
|
receiver_model.set_baseband_bandwidth(2500000);
|
2016-12-07 14:49:42 -05:00
|
|
|
receiver_model.enable();
|
2016-05-11 06:45:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
} /* namespace ui */
|