2015-07-08 11:39:24 -04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015 Jared Boone, ShareBrained Technology, Inc.
|
|
|
|
*
|
|
|
|
* 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_spectrum.hpp"
|
|
|
|
|
2015-12-16 22:33:30 -05:00
|
|
|
#include "spectrum_color_lut.hpp"
|
|
|
|
|
|
|
|
#include "portapack.hpp"
|
|
|
|
using namespace portapack;
|
|
|
|
|
2016-02-10 23:11:19 -05:00
|
|
|
#include "baseband_api.hpp"
|
|
|
|
|
2015-12-16 22:33:30 -05:00
|
|
|
#include "string_format.hpp"
|
|
|
|
|
|
|
|
#include <cmath>
|
|
|
|
#include <array>
|
|
|
|
|
2015-07-08 11:39:24 -04:00
|
|
|
namespace ui {
|
2015-12-16 22:33:30 -05:00
|
|
|
namespace spectrum {
|
|
|
|
|
2018-05-21 23:43:04 -04:00
|
|
|
/* AudioSpectrumView******************************************************/
|
|
|
|
|
|
|
|
AudioSpectrumView::AudioSpectrumView(
|
2023-05-18 16:16:05 -04:00
|
|
|
const Rect parent_rect)
|
|
|
|
: View{parent_rect} {
|
|
|
|
set_focusable(true);
|
|
|
|
|
|
|
|
add_children({&labels,
|
|
|
|
&field_frequency,
|
|
|
|
&waveform});
|
|
|
|
|
|
|
|
field_frequency.on_change = [this](int32_t) {
|
|
|
|
set_dirty();
|
|
|
|
};
|
|
|
|
field_frequency.set_value(0);
|
2018-05-21 23:43:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void AudioSpectrumView::paint(Painter& painter) {
|
2023-05-18 16:16:05 -04:00
|
|
|
const auto r = screen_rect();
|
|
|
|
|
|
|
|
painter.fill_rectangle(r, Color::black());
|
|
|
|
|
|
|
|
// if( !spectrum_sampling_rate ) return;
|
|
|
|
|
|
|
|
// Cursor
|
|
|
|
const Rect r_cursor{
|
|
|
|
field_frequency.value() / (48000 / 240), r.bottom() - 32 - cursor_band_height,
|
|
|
|
1, cursor_band_height};
|
|
|
|
painter.fill_rectangle(
|
|
|
|
r_cursor,
|
|
|
|
Color::red());
|
2018-05-21 23:43:04 -04:00
|
|
|
}
|
|
|
|
|
2018-06-12 02:55:12 -04:00
|
|
|
void AudioSpectrumView::on_audio_spectrum(const AudioSpectrum* spectrum) {
|
2023-05-18 16:16:05 -04:00
|
|
|
for (size_t i = 0; i < spectrum->db.size(); i++)
|
|
|
|
audio_spectrum[i] = ((int16_t)spectrum->db[i] - 127) * 256;
|
|
|
|
waveform.set_dirty();
|
2018-05-21 23:43:04 -04:00
|
|
|
}
|
|
|
|
|
2015-12-16 22:33:30 -05:00
|
|
|
/* FrequencyScale ********************************************************/
|
|
|
|
|
|
|
|
void FrequencyScale::on_show() {
|
2023-05-18 16:16:05 -04:00
|
|
|
clear();
|
2015-12-16 22:33:30 -05:00
|
|
|
}
|
|
|
|
|
2016-01-23 20:53:16 -05:00
|
|
|
void FrequencyScale::set_spectrum_sampling_rate(const int new_sampling_rate) {
|
2023-05-18 16:16:05 -04:00
|
|
|
if ((spectrum_sampling_rate != new_sampling_rate)) {
|
|
|
|
spectrum_sampling_rate = new_sampling_rate;
|
|
|
|
set_dirty();
|
|
|
|
}
|
2015-12-16 22:33:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void FrequencyScale::set_channel_filter(
|
2023-05-18 16:16:05 -04:00
|
|
|
const int low_frequency,
|
|
|
|
const int high_frequency,
|
|
|
|
const int transition) {
|
|
|
|
if ((channel_filter_low_frequency != low_frequency) ||
|
|
|
|
(channel_filter_high_frequency != high_frequency) ||
|
|
|
|
(channel_filter_transition != transition)) {
|
|
|
|
channel_filter_low_frequency = low_frequency;
|
|
|
|
channel_filter_high_frequency = high_frequency;
|
|
|
|
channel_filter_transition = transition;
|
|
|
|
set_dirty();
|
|
|
|
}
|
2015-12-16 22:33:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void FrequencyScale::paint(Painter& painter) {
|
2023-05-18 16:16:05 -04:00
|
|
|
const auto r = screen_rect();
|
|
|
|
|
|
|
|
clear_background(painter, r);
|
|
|
|
|
|
|
|
if (!spectrum_sampling_rate) {
|
|
|
|
// Can't draw without non-zero scale.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
draw_filter_ranges(painter, r);
|
|
|
|
draw_frequency_ticks(painter, r);
|
|
|
|
|
|
|
|
if (_blink) {
|
|
|
|
const Rect r_cursor{
|
|
|
|
118 + cursor_position, r.bottom() - filter_band_height,
|
|
|
|
5, filter_band_height};
|
|
|
|
painter.fill_rectangle(
|
|
|
|
r_cursor,
|
|
|
|
Color::red());
|
|
|
|
}
|
2015-12-16 22:33:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void FrequencyScale::clear() {
|
2023-05-18 16:16:05 -04:00
|
|
|
spectrum_sampling_rate = 0;
|
|
|
|
set_dirty();
|
2015-12-16 22:33:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void FrequencyScale::clear_background(Painter& painter, const Rect r) {
|
2023-05-18 16:16:05 -04:00
|
|
|
painter.fill_rectangle(r, Color::black());
|
2015-12-16 22:33:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void FrequencyScale::draw_frequency_ticks(Painter& painter, const Rect r) {
|
2023-05-18 16:16:05 -04:00
|
|
|
const auto x_center = r.width() / 2;
|
|
|
|
|
|
|
|
const Rect tick{r.left() + x_center, r.top(), 1, r.height()};
|
|
|
|
painter.fill_rectangle(tick, Color::white());
|
|
|
|
|
|
|
|
constexpr int tick_count_max = 4;
|
|
|
|
float rough_tick_interval = float(spectrum_sampling_rate) / tick_count_max;
|
|
|
|
int magnitude = 1;
|
|
|
|
int magnitude_n = 0;
|
|
|
|
while (rough_tick_interval >= 10.0f) {
|
|
|
|
rough_tick_interval /= 10;
|
|
|
|
magnitude *= 10;
|
|
|
|
magnitude_n += 1;
|
|
|
|
}
|
|
|
|
const int tick_interval = std::ceil(rough_tick_interval);
|
|
|
|
|
|
|
|
auto tick_offset = tick_interval;
|
|
|
|
while ((tick_offset * magnitude) < spectrum_sampling_rate / 2) {
|
|
|
|
const Dim pixel_offset = tick_offset * magnitude * spectrum_bins / spectrum_sampling_rate;
|
|
|
|
|
|
|
|
const std::string zero_pad =
|
|
|
|
((magnitude_n % 3) == 0) ? "" : ((magnitude_n % 3) == 1) ? "0"
|
|
|
|
: "00";
|
|
|
|
const std::string unit =
|
|
|
|
(magnitude_n >= 6) ? "M" : (magnitude_n >= 3) ? "k"
|
|
|
|
: "";
|
|
|
|
const std::string label = to_string_dec_uint(tick_offset) + zero_pad + unit;
|
|
|
|
const auto label_width = style().font.size_of(label).width();
|
|
|
|
|
|
|
|
const Coord offset_low = r.left() + x_center - pixel_offset;
|
|
|
|
const Rect tick_low{offset_low, r.top(), 1, r.height()};
|
|
|
|
painter.fill_rectangle(tick_low, Color::white());
|
|
|
|
painter.draw_string({offset_low + 2, r.top()}, style(), label);
|
|
|
|
|
|
|
|
const Coord offset_high = r.left() + x_center + pixel_offset;
|
|
|
|
const Rect tick_high{offset_high, r.top(), 1, r.height()};
|
|
|
|
painter.fill_rectangle(tick_high, Color::white());
|
|
|
|
painter.draw_string({offset_high - 2 - label_width, r.top()}, style(), label);
|
|
|
|
|
|
|
|
tick_offset += tick_interval;
|
|
|
|
}
|
2015-12-16 22:33:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void FrequencyScale::draw_filter_ranges(Painter& painter, const Rect r) {
|
2023-05-18 16:16:05 -04:00
|
|
|
if (channel_filter_low_frequency != channel_filter_high_frequency) {
|
|
|
|
const auto x_center = r.width() / 2;
|
|
|
|
|
|
|
|
const auto x_low = x_center + channel_filter_low_frequency * spectrum_bins / spectrum_sampling_rate;
|
|
|
|
const auto x_high = x_center + channel_filter_high_frequency * spectrum_bins / spectrum_sampling_rate;
|
|
|
|
|
|
|
|
if (channel_filter_transition) {
|
|
|
|
const auto trans = channel_filter_transition * spectrum_bins / spectrum_sampling_rate;
|
|
|
|
|
|
|
|
const Rect r_all{
|
|
|
|
r.left() + x_low - trans, r.bottom() - filter_band_height,
|
|
|
|
x_high - x_low + trans * 2, filter_band_height};
|
|
|
|
painter.fill_rectangle(
|
|
|
|
r_all,
|
|
|
|
Color::yellow());
|
|
|
|
}
|
|
|
|
|
|
|
|
const Rect r_pass{
|
|
|
|
r.left() + x_low, r.bottom() - filter_band_height,
|
|
|
|
x_high - x_low, filter_band_height};
|
|
|
|
painter.fill_rectangle(
|
|
|
|
r_pass,
|
|
|
|
Color::green());
|
|
|
|
}
|
2015-12-16 22:33:30 -05:00
|
|
|
}
|
|
|
|
|
2018-05-21 23:43:04 -04:00
|
|
|
void FrequencyScale::on_focus() {
|
2023-05-18 16:16:05 -04:00
|
|
|
_blink = true;
|
|
|
|
on_tick_second();
|
|
|
|
signal_token_tick_second = rtc_time::signal_tick_second += [this]() {
|
|
|
|
this->on_tick_second();
|
|
|
|
};
|
2018-05-21 23:43:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void FrequencyScale::on_blur() {
|
2023-05-18 16:16:05 -04:00
|
|
|
rtc_time::signal_tick_second -= signal_token_tick_second;
|
|
|
|
_blink = false;
|
|
|
|
set_dirty();
|
2018-05-21 23:43:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool FrequencyScale::on_encoder(const EncoderEvent delta) {
|
2023-05-18 16:16:05 -04:00
|
|
|
cursor_position += delta;
|
|
|
|
|
|
|
|
cursor_position = std::min<int32_t>(cursor_position, 119);
|
|
|
|
cursor_position = std::max<int32_t>(cursor_position, -120);
|
|
|
|
|
|
|
|
set_dirty();
|
|
|
|
|
|
|
|
return true;
|
2018-05-21 23:43:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool FrequencyScale::on_key(const KeyEvent key) {
|
2023-05-18 16:16:05 -04:00
|
|
|
if (key == KeyEvent::Select) {
|
|
|
|
if (on_select) {
|
|
|
|
on_select((cursor_position * spectrum_sampling_rate) / 240);
|
|
|
|
cursor_position = 0;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2018-05-21 23:43:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void FrequencyScale::on_tick_second() {
|
2023-05-18 16:16:05 -04:00
|
|
|
set_dirty();
|
|
|
|
_blink = !_blink;
|
2018-05-21 23:43:04 -04:00
|
|
|
}
|
|
|
|
|
2015-12-16 22:33:30 -05:00
|
|
|
/* WaterfallView *********************************************************/
|
|
|
|
|
|
|
|
void WaterfallView::on_show() {
|
2023-05-18 16:16:05 -04:00
|
|
|
clear();
|
2015-12-16 22:33:30 -05:00
|
|
|
|
2023-05-18 16:16:05 -04:00
|
|
|
const auto screen_r = screen_rect();
|
|
|
|
display.scroll_set_area(screen_r.top(), screen_r.bottom());
|
2015-12-16 22:33:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void WaterfallView::on_hide() {
|
2023-05-18 16:16:05 -04:00
|
|
|
/* TODO: Clear region to eliminate brief flash of content at un-shifted
|
|
|
|
* position?
|
|
|
|
*/
|
|
|
|
display.scroll_disable();
|
2015-12-16 22:33:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void WaterfallView::paint(Painter& painter) {
|
2023-05-18 16:16:05 -04:00
|
|
|
// Do nothing.
|
|
|
|
(void)painter;
|
2015-12-16 22:33:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void WaterfallView::on_channel_spectrum(
|
2023-05-18 16:16:05 -04:00
|
|
|
const ChannelSpectrum& spectrum) {
|
|
|
|
/* TODO: static_assert that message.spectrum.db.size() >= pixel_row.size() */
|
|
|
|
|
|
|
|
std::array<Color, 240> pixel_row;
|
|
|
|
for (size_t i = 0; i < 120; i++) {
|
|
|
|
const auto pixel_color = spectrum_rgb3_lut[spectrum.db[256 - 120 + i]];
|
|
|
|
pixel_row[i] = pixel_color;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 120; i < 240; i++) {
|
|
|
|
const auto pixel_color = spectrum_rgb3_lut[spectrum.db[i - 120]];
|
|
|
|
pixel_row[i] = pixel_color;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto draw_y = display.scroll(1);
|
|
|
|
|
|
|
|
display.draw_pixels(
|
|
|
|
{{0, draw_y}, {pixel_row.size(), 1}},
|
|
|
|
pixel_row);
|
2015-12-16 22:33:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void WaterfallView::clear() {
|
2023-05-18 16:16:05 -04:00
|
|
|
display.fill_rectangle(
|
|
|
|
screen_rect(),
|
|
|
|
Color::black());
|
2015-12-16 22:33:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* WaterfallWidget *******************************************************/
|
|
|
|
|
2018-04-18 17:44:41 -04:00
|
|
|
WaterfallWidget::WaterfallWidget(const bool cursor) {
|
2023-05-18 16:16:05 -04:00
|
|
|
add_children({&waterfall_view,
|
|
|
|
&frequency_scale});
|
|
|
|
|
|
|
|
frequency_scale.set_focusable(cursor);
|
|
|
|
|
|
|
|
// Making the event climb up all the way up to here kinda sucks
|
|
|
|
frequency_scale.on_select = [this](int32_t offset) {
|
|
|
|
if (on_select) on_select(offset);
|
|
|
|
};
|
2018-04-18 17:44:41 -04:00
|
|
|
}
|
|
|
|
|
2015-12-16 22:33:30 -05:00
|
|
|
void WaterfallWidget::on_show() {
|
2023-05-18 16:16:05 -04:00
|
|
|
baseband::spectrum_streaming_start();
|
2015-12-16 22:33:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void WaterfallWidget::on_hide() {
|
2023-05-18 16:16:05 -04:00
|
|
|
baseband::spectrum_streaming_stop();
|
2015-12-16 22:33:30 -05:00
|
|
|
}
|
|
|
|
|
2018-05-21 23:43:04 -04:00
|
|
|
void WaterfallWidget::show_audio_spectrum_view(const bool show) {
|
2023-05-18 16:16:05 -04:00
|
|
|
if ((audio_spectrum_view && show) || (!audio_spectrum_view && !show)) return;
|
|
|
|
|
|
|
|
if (show) {
|
|
|
|
audio_spectrum_view = std::make_unique<AudioSpectrumView>(audio_spectrum_view_rect);
|
|
|
|
add_child(audio_spectrum_view.get());
|
|
|
|
update_widgets_rect();
|
|
|
|
} else {
|
|
|
|
audio_spectrum_update = false;
|
|
|
|
remove_child(audio_spectrum_view.get());
|
|
|
|
audio_spectrum_view.reset();
|
|
|
|
update_widgets_rect();
|
|
|
|
}
|
2018-05-21 13:46:48 -04:00
|
|
|
}
|
|
|
|
|
2018-05-21 23:43:04 -04:00
|
|
|
void WaterfallWidget::update_widgets_rect() {
|
2023-05-18 16:16:05 -04:00
|
|
|
if (audio_spectrum_view) {
|
|
|
|
frequency_scale.set_parent_rect({0, audio_spectrum_height, screen_rect().width(), scale_height});
|
|
|
|
waterfall_view.set_parent_rect(waterfall_reduced_rect);
|
|
|
|
} else {
|
|
|
|
frequency_scale.set_parent_rect({0, 0, screen_rect().width(), scale_height});
|
|
|
|
waterfall_view.set_parent_rect(waterfall_normal_rect);
|
|
|
|
}
|
|
|
|
waterfall_view.on_show();
|
2018-05-21 13:46:48 -04:00
|
|
|
}
|
|
|
|
|
2015-12-16 22:33:30 -05:00
|
|
|
void WaterfallWidget::set_parent_rect(const Rect new_parent_rect) {
|
2023-05-18 16:16:05 -04:00
|
|
|
View::set_parent_rect(new_parent_rect);
|
|
|
|
|
|
|
|
waterfall_normal_rect = {0, scale_height, new_parent_rect.width(), new_parent_rect.height() - scale_height};
|
|
|
|
waterfall_reduced_rect = {0, audio_spectrum_height + scale_height, new_parent_rect.width(), new_parent_rect.height() - scale_height - audio_spectrum_height};
|
|
|
|
|
|
|
|
update_widgets_rect();
|
2015-12-16 22:33:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void WaterfallWidget::paint(Painter& painter) {
|
2023-05-18 16:16:05 -04:00
|
|
|
// TODO:
|
|
|
|
(void)painter;
|
2015-12-16 22:33:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void WaterfallWidget::on_channel_spectrum(const ChannelSpectrum& spectrum) {
|
2023-05-18 16:16:05 -04:00
|
|
|
waterfall_view.on_channel_spectrum(spectrum);
|
|
|
|
sampling_rate = spectrum.sampling_rate;
|
|
|
|
frequency_scale.set_spectrum_sampling_rate(sampling_rate);
|
|
|
|
frequency_scale.set_channel_filter(
|
|
|
|
spectrum.channel_filter_low_frequency,
|
|
|
|
spectrum.channel_filter_high_frequency,
|
|
|
|
spectrum.channel_filter_transition);
|
2015-12-16 22:33:30 -05:00
|
|
|
}
|
2015-07-08 11:39:24 -04:00
|
|
|
|
2018-06-12 02:55:12 -04:00
|
|
|
void WaterfallWidget::on_audio_spectrum() {
|
2023-05-18 16:16:05 -04:00
|
|
|
audio_spectrum_view->on_audio_spectrum(audio_spectrum_data);
|
2018-05-21 23:43:04 -04:00
|
|
|
}
|
|
|
|
|
2015-12-16 22:33:30 -05:00
|
|
|
} /* namespace spectrum */
|
2015-07-08 11:39:24 -04:00
|
|
|
} /* namespace ui */
|