From 992d4b4b8d3f3a630a2cf00d593ee4ce4dfa7ce8 Mon Sep 17 00:00:00 2001 From: Christoph Berg Date: Thu, 9 Jan 2025 22:12:06 +0100 Subject: New upstream version 2.7.0~rc8+repack --- WSPR/WSPRBandHopping.cpp | 1000 +++++++++++++++++++++++----------------------- WSPR/WSPRBandHopping.hpp | 170 ++++---- WSPR/WsprTxScheduler.cpp | 456 ++++++++++----------- WSPR/WsprTxScheduler.h | 14 +- 4 files changed, 820 insertions(+), 820 deletions(-) (limited to 'WSPR') diff --git a/WSPR/WSPRBandHopping.cpp b/WSPR/WSPRBandHopping.cpp index a20137f6..c5652f52 100755 --- a/WSPR/WSPRBandHopping.cpp +++ b/WSPR/WSPRBandHopping.cpp @@ -1,500 +1,500 @@ -#include "WSPRBandHopping.hpp" - -#include - -#include -#include -#include -#include -#include -#include - -#include "SettingsGroup.hpp" -#include "Configuration.hpp" -#include "models/Bands.hpp" -#include "models/FrequencyList.hpp" -#include "WsprTxScheduler.h" -#include "pimpl_impl.hpp" -#include "moc_WSPRBandHopping.cpp" - -extern "C" -{ -#ifndef CMAKE_BUILD -#define FC_grayline grayline_ -#else -#include "FC.h" - void FC_grayline (int const * year, int const * month, int const * nday, float const * uth, char const * my_grid - , int const * nduration, int * isun - , int my_grid_len); -#endif -}; - -namespace -{ - char const * const title = "WSPR Band Hopping"; - char const * const periods[] = {"Sunrise grayline", "Day", "Sunset grayline", "Night", "Tune", "Rx only"}; - size_t constexpr num_periods {sizeof (periods) / sizeof (periods[0])}; - // These 10 bands are globally coordinated - QList const coordinated_bands = {"160m", "80m", "60m", "40m", "30m", "20m", "17m", "15m", "12m", "10m"}; -} - -// -// Dialog - maintenance of band hopping options -// -class Dialog - : public QDialog -{ - Q_OBJECT - -public: - using BandList = QList; - - Dialog (QSettings *, Configuration const *, BandList const * WSPT_bands, QBitArray * bands - , int * gray_line_duration, QWidget * parent = nullptr); - ~Dialog (); - - Q_SLOT void frequencies_changed (); - void resize_to_maximum (); - -private: - void closeEvent (QCloseEvent *) override; - void save_window_state (); - - QSettings * settings_; - Configuration const * configuration_; - BandList const * WSPR_bands_; - QBitArray * bands_; - int * gray_line_duration_; - QPointer bands_table_; - QBrush coord_background_brush_; - QPointer gray_line_width_spin_box_; - static int constexpr band_index_role {Qt::UserRole}; -}; - -#include "WSPRBandHopping.moc" - -Dialog::Dialog (QSettings * settings, Configuration const * configuration, BandList const * WSPR_bands - , QBitArray * bands, int * gray_line_duration, QWidget * parent) - : QDialog {parent, Qt::Window | Qt::WindowTitleHint | Qt::WindowCloseButtonHint | Qt::WindowMinimizeButtonHint} - , settings_ {settings} - , configuration_ {configuration} - , WSPR_bands_ {WSPR_bands} - , bands_ {bands} - , gray_line_duration_ {gray_line_duration} - , bands_table_ {new QTableWidget {this}} - , coord_background_brush_ {Qt::yellow} - , gray_line_width_spin_box_ {new QSpinBox {this}} -{ - setWindowTitle (windowTitle () + ' ' + tr (title)); - { - SettingsGroup g {settings_, title}; - restoreGeometry (settings_->value ("geometry", saveGeometry ()).toByteArray ()); - } - - QVBoxLayout * main_layout {new QVBoxLayout}; - - bands_table_->setRowCount (num_periods); - bands_table_->setVerticalScrollBarPolicy (Qt::ScrollBarAlwaysOff); - bands_table_->setHorizontalScrollBarPolicy (Qt::ScrollBarAlwaysOff); - bands_table_->setSizePolicy (QSizePolicy::Expanding, QSizePolicy::Expanding); - frequencies_changed (); - main_layout->addWidget (bands_table_); - // recalculate table when frequencies change - connect (configuration_->frequencies (), &QAbstractItemModel::layoutChanged - , this, &Dialog::frequencies_changed); - // handle changes by updating the underlying flags - connect (bands_table_.data (), &QTableWidget::itemChanged, [this] (QTableWidgetItem * item) { - auto band_number = item->data (band_index_role).toInt (); - bands_[item->row ()].setBit (band_number, Qt::Checked == item->checkState ()); - }); - - // set up the gray line duration spin box - gray_line_width_spin_box_->setRange (1, 60 * 2); - gray_line_width_spin_box_->setSuffix ("min"); - gray_line_width_spin_box_->setValue (*gray_line_duration_); - QFormLayout * form_layout = new QFormLayout; - form_layout->addRow (tr ("Gray time:"), gray_line_width_spin_box_); - connect (gray_line_width_spin_box_.data () - , static_cast (&QSpinBox::valueChanged) - , [this] (int new_value) {*gray_line_duration_ = new_value;}); - - QHBoxLayout * bottom_layout = new QHBoxLayout; - bottom_layout->addStretch (); - bottom_layout->addLayout (form_layout); - main_layout->addLayout (bottom_layout); - - setLayout (main_layout); -} - -Dialog::~Dialog () -{ - // do this here too because ESC or parent shutdown closing this - // window doesn't queue a close event - save_window_state (); -} - -void Dialog::closeEvent (QCloseEvent * e) -{ - save_window_state (); - QDialog::closeEvent (e); -} - -void Dialog::save_window_state () -{ - SettingsGroup g {settings_, title}; - settings_->setValue ("geometry", saveGeometry ()); -} - -void Dialog::frequencies_changed () -{ - bands_table_->setColumnCount (WSPR_bands_->size ()); - // set up and load the table of check boxes - for (auto row = 0u; row < num_periods; ++row) - { - auto vertical_header = new QTableWidgetItem {periods[row]}; - vertical_header->setTextAlignment (Qt::AlignRight | Qt::AlignVCenter); - bands_table_->setVerticalHeaderItem (row, vertical_header); - int column {0}; - int band_number {0}; - for (auto const& band : *configuration_->bands ()) - { - if (WSPR_bands_->contains (band)) - { - if (0 == row) - { - auto horizontal_header = new QTableWidgetItem {band}; - bands_table_->setHorizontalHeaderItem (column, horizontal_header); - } - auto item = new QTableWidgetItem; - item->setFlags (Qt::ItemIsUserCheckable | Qt::ItemIsEnabled); - item->setCheckState (bands_[row].testBit (band_number) ? Qt::Checked : Qt::Unchecked); - item->setData (band_index_role, band_number); - if (coordinated_bands.contains (band)) - { - item->setBackground (coord_background_brush_); - } - bands_table_->setItem (row, column, item); - ++column; - } - ++band_number; - } - } - bands_table_->resizeColumnsToContents (); - auto is_visible = isVisible (); - show (); - resize_to_maximum (); - adjustSize (); // fix the size - if (!is_visible) - { - hide (); - } -} - -// to get the dialog window exactly the right size to contain the -// widgets without needing scroll bars we need to measure the size of -// the table widget and set its minimum size to the measured size -void Dialog::resize_to_maximum () -{ - bands_table_->setMinimumSize ({ - bands_table_->horizontalHeader ()->length () - + bands_table_->verticalHeader ()->width () - + 2 * bands_table_->frameWidth () - , bands_table_->verticalHeader ()->length () - + bands_table_->horizontalHeader ()->height () - + 2 * bands_table_->frameWidth () - }); - bands_table_->setMaximumSize (bands_table_->minimumSize ()); -} - -class WSPRBandHopping::impl -{ -public: - using BandList = Dialog::BandList; - - impl (QSettings * settings, Configuration const * configuration, QWidget * parent_widget) - : settings_ {settings} - , configuration_ {configuration} - , tx_percent_ {0} - , parent_widget_ {parent_widget} - , last_was_tx_ {false} - , carry_ {false} - , seed_ {{rand (), rand (), rand (), rand (), rand (), rand (), rand (), rand ()}} - , gen_ {seed_} - , dist_ {0, 99} - { - auto num_bands = configuration_->bands ()->rowCount (); - for (auto& flags : bands_) - { - flags.resize (num_bands); - } - } - - bool simple_scheduler (); - - QSettings * settings_; - Configuration const * configuration_; - int tx_percent_; - BandList WSPR_bands_; - BandList rx_permutation_; - BandList tx_permutation_; - QWidget * parent_widget_; - - // 5 x 10 bit flags representing each hopping band in each period - // and tune - QBitArray bands_[num_periods]; - - int gray_line_duration_; - QPointer dialog_; - bool last_was_tx_; - bool carry_; - std::seed_seq seed_; - std::mt19937 gen_; - std::uniform_int_distribution dist_; -}; - -bool WSPRBandHopping::impl::simple_scheduler () -{ - auto tx = carry_ || tx_percent_ > dist_ (gen_); - if (carry_) - { - carry_ = false; - } - else if (tx_percent_ < 40 && last_was_tx_ && tx) - { - // if percentage is less than 40 then avoid consecutive tx but - // always catch up on the next round - tx = false; - carry_ = true; - } - last_was_tx_ = tx; - return tx; -} - -WSPRBandHopping::WSPRBandHopping (QSettings * settings, Configuration const * configuration, QWidget * parent_widget) - : m_ {settings, configuration, parent_widget} -{ - // detect changes to the working frequencies model - m_->WSPR_bands_ = m_->configuration_->frequencies ()->all_bands (m_->configuration_->region (), Modes::WSPR).values (); - connect (m_->configuration_->frequencies (), &QAbstractItemModel::layoutChanged - , [this] () { - m_->WSPR_bands_ = m_->configuration_->frequencies ()->all_bands (m_->configuration_->region (), Modes::WSPR).values (); - }); - - // load settings - SettingsGroup g {m_->settings_, title}; - size_t size = m_->settings_->beginReadArray ("phases"); - for (auto i = 0u; i < size; ++i) - { - if (i < num_periods) - { - m_->settings_->setArrayIndex (i); - m_->bands_[i] = m_->settings_->value ("bands").toBitArray (); - } - } - m_->settings_->endArray (); - m_->gray_line_duration_ = m_->settings_->value ("GrayLineDuration", 60).toUInt (); -} - -WSPRBandHopping::~WSPRBandHopping () -{ - // save settings - SettingsGroup g {m_->settings_, title}; - m_->settings_->beginWriteArray ("phases"); - for (auto i = 0u; i < num_periods; ++i) - { - m_->settings_->setArrayIndex (i); - m_->settings_->setValue ("bands", m_->bands_[i]); - } - m_->settings_->endArray (); - m_->settings_->setValue ("GrayLineDuration", m_->gray_line_duration_); -} - -// pop up the maintenance dialog window -void WSPRBandHopping::show_dialog (bool /* checked */) -{ - if (!m_->dialog_) - { - m_->dialog_ = new Dialog {m_->settings_, m_->configuration_, &m_->WSPR_bands_, m_->bands_ - , &m_->gray_line_duration_, m_->parent_widget_}; - } - m_->dialog_->show (); - m_->dialog_->raise (); - m_->dialog_->activateWindow (); -} - -int WSPRBandHopping::tx_percent () const -{ - return m_->tx_percent_; -} - -void WSPRBandHopping::set_tx_percent (int new_value) -{ - m_->tx_percent_ = new_value; -} - -// determine the parameters of the hop, if any -auto WSPRBandHopping::next_hop (bool tx_enabled) -> Hop -{ - auto const& now = QDateTime::currentDateTimeUtc (); - auto const& date = now.date (); - auto year = date.year (); - auto month = date.month (); - auto day = date.day (); - auto const& time = now.time (); - float uth = time.hour () + time.minute () / 60. - + (time.second () + .001 * time.msec ()) / 3600.; - auto my_grid = m_->configuration_->my_grid (); - int period_index; - int band_index; - int tx_next; - - my_grid = (my_grid + " ").left (6); // hopping doesn't like - // short grids - - // look up the period for this time - FC_grayline (&year, &month, &day, &uth, my_grid.toLatin1 ().constData () - , &m_->gray_line_duration_, &period_index - , my_grid.size ()); - - band_index = next_hopping_band(); - - tx_next = next_is_tx () && tx_enabled; - - int frequencies_index {-1}; - auto const& frequencies = m_->configuration_->frequencies (); - auto const& bands = m_->configuration_->bands (); - auto band_name = bands->data (bands->index (band_index + 3, 0)).toString (); - if (m_->bands_[period_index].testBit (band_index + 3) // +3 for - // coordinated bands - && m_->WSPR_bands_.contains (band_name)) - { - // here we have a band that has been enabled in the hopping - // matrix so check it it has a configured working frequency - frequencies_index = frequencies->best_working_frequency (band_name); - } - - // if we do not have a configured working frequency on the selected - // coordinated hopping band we next pick from a random permutation - // of the other enabled bands in the hopping bands matrix - if (frequencies_index < 0) - { - // build sets of available rx and tx bands -#if QT_VERSION < QT_VERSION_CHECK (5, 14, 0) - auto target_rx_bands = m_->WSPR_bands_.toSet (); -#else - QSet target_rx_bands {m_->WSPR_bands_.begin (), m_->WSPR_bands_.end ()}; -#endif - auto target_tx_bands = target_rx_bands; - for (auto i = 0; i < m_->bands_[period_index].size (); ++i) - { - auto const& band = bands->data (bands->index (i, 0)).toString (); - // remove bands that are not enabled for hopping in this phase - if (!m_->bands_[period_index].testBit (i)) - { - target_rx_bands.remove (band); - target_tx_bands.remove (band); - } - // remove rx only bands from transmit list and vice versa - if (m_->bands_[5].testBit (i)) - { - target_tx_bands.remove (band); - } - else - { - target_rx_bands.remove (band); - } - } - // if we have some bands to permute - if (target_rx_bands.size () + target_tx_bands.size ()) - { -#if QT_VERSION < QT_VERSION_CHECK (5, 14, 0) - auto rx_permutations = m_->rx_permutation_.toSet (); - auto tx_permutations = m_->tx_permutation_.toSet (); -#else - QSet rx_permutations {m_->rx_permutation_.begin (), m_->rx_permutation_.end ()}; - QSet tx_permutations {m_->tx_permutation_.begin (), m_->tx_permutation_.end ()}; -#endif - if (!(m_->rx_permutation_.size () + m_->tx_permutation_.size ()) // all used up - // or rx list contains a band no longer scheduled - || !target_rx_bands.contains (rx_permutations) - // or tx list contains a band no longer scheduled for tx - || !target_tx_bands.contains (tx_permutations)) - { - // build new random permutations - m_->rx_permutation_ = target_rx_bands.values (); - std::random_shuffle (std::begin (m_->rx_permutation_), std::end (m_->rx_permutation_)); - m_->tx_permutation_ = target_tx_bands.values (); - std::random_shuffle (std::begin (m_->tx_permutation_), std::end (m_->tx_permutation_)); - // qDebug () << "New random Rx permutation:" << m_->rx_permutation_ - // << "random Tx permutation:" << m_->tx_permutation_; - } - if ((tx_next && m_->tx_permutation_.size ()) || !m_->rx_permutation_.size ()) - { - Q_ASSERT (m_->tx_permutation_.size ()); - // use one from the current random tx permutation - band_name = m_->tx_permutation_.takeFirst (); - } - else - { - Q_ASSERT (m_->rx_permutation_.size ()); - // use one from the current random rx permutation - band_name = m_->rx_permutation_.takeFirst (); - } - // find the first WSPR working frequency for the chosen band - frequencies_index = frequencies->best_working_frequency (band_name); - if (frequencies_index >= 0) // should be a redundant check, - // but to be safe - { - // we can use the random choice - // qDebug () << "random:" << frequencies->data (frequencies->index (frequencies_index, FrequencyList_v2::frequency_column)).toString (); - band_index = bands->find (band_name); - if (band_index < 0) // this shouldn't happen - { - Q_ASSERT (band_index >= 0); - frequencies_index = -1; - } - } - } - } - else - { - band_index += 3; - // qDebug () << "scheduled:" << frequencies->data (frequencies->index (frequencies_index, FrequencyList_v2::frequency_column)).toString (); - // remove from random permutations to stop the coordinated bands - // getting too high a weighting - not perfect but surely helps - m_->rx_permutation_.removeOne (band_name); - m_->tx_permutation_.removeOne (band_name); - } - - return { - periods[period_index] - - , frequencies_index - - , frequencies_index >= 0 // new band - && tx_enabled // transmit is allowed - && m_->bands_[4].testBit (band_index) // tune up required - && !m_->bands_[5].testBit (band_index) // not an Rx only band - - , frequencies_index >= 0 // new band - && tx_next // Tx scheduled - && !m_->bands_[5].testBit (band_index) // not an Rx only band - }; -} - -bool WSPRBandHopping::next_is_tx (bool simple_schedule) -{ - if (simple_schedule) - { - return m_->simple_scheduler (); - } - if (100 == m_->tx_percent_) - { - return true; - } - else - { - // consult scheduler to determine if next period should be a tx interval - return next_tx_state(m_->tx_percent_); - } -} +#include "WSPRBandHopping.hpp" + +#include + +#include +#include +#include +#include +#include +#include + +#include "SettingsGroup.hpp" +#include "Configuration.hpp" +#include "models/Bands.hpp" +#include "models/FrequencyList.hpp" +#include "WsprTxScheduler.h" +#include "pimpl_impl.hpp" +#include "moc_WSPRBandHopping.cpp" + +extern "C" +{ +#ifndef CMAKE_BUILD +#define FC_grayline grayline_ +#else +#include "FC.h" + void FC_grayline (int const * year, int const * month, int const * nday, float const * uth, char const * my_grid + , int const * nduration, int * isun + , int my_grid_len); +#endif +}; + +namespace +{ + char const * const title = "WSPR Band Hopping"; + char const * const periods[] = {"Sunrise grayline", "Day", "Sunset grayline", "Night", "Tune", "Rx only"}; + size_t constexpr num_periods {sizeof (periods) / sizeof (periods[0])}; + // These 10 bands are globally coordinated + QList const coordinated_bands = {"160m", "80m", "60m", "40m", "30m", "20m", "17m", "15m", "12m", "10m"}; +} + +// +// Dialog - maintenance of band hopping options +// +class Dialog + : public QDialog +{ + Q_OBJECT + +public: + using BandList = QList; + + Dialog (QSettings *, Configuration const *, BandList const * WSPT_bands, QBitArray * bands + , int * gray_line_duration, QWidget * parent = nullptr); + ~Dialog (); + + Q_SLOT void frequencies_changed (); + void resize_to_maximum (); + +private: + void closeEvent (QCloseEvent *) override; + void save_window_state (); + + QSettings * settings_; + Configuration const * configuration_; + BandList const * WSPR_bands_; + QBitArray * bands_; + int * gray_line_duration_; + QPointer bands_table_; + QBrush coord_background_brush_; + QPointer gray_line_width_spin_box_; + static int constexpr band_index_role {Qt::UserRole}; +}; + +#include "WSPRBandHopping.moc" + +Dialog::Dialog (QSettings * settings, Configuration const * configuration, BandList const * WSPR_bands + , QBitArray * bands, int * gray_line_duration, QWidget * parent) + : QDialog {parent, Qt::Window | Qt::WindowTitleHint | Qt::WindowCloseButtonHint | Qt::WindowMinimizeButtonHint} + , settings_ {settings} + , configuration_ {configuration} + , WSPR_bands_ {WSPR_bands} + , bands_ {bands} + , gray_line_duration_ {gray_line_duration} + , bands_table_ {new QTableWidget {this}} + , coord_background_brush_ {Qt::yellow} + , gray_line_width_spin_box_ {new QSpinBox {this}} +{ + setWindowTitle (windowTitle () + ' ' + tr (title)); + { + SettingsGroup g {settings_, title}; + restoreGeometry (settings_->value ("geometry", saveGeometry ()).toByteArray ()); + } + + QVBoxLayout * main_layout {new QVBoxLayout}; + + bands_table_->setRowCount (num_periods); + bands_table_->setVerticalScrollBarPolicy (Qt::ScrollBarAlwaysOff); + bands_table_->setHorizontalScrollBarPolicy (Qt::ScrollBarAlwaysOff); + bands_table_->setSizePolicy (QSizePolicy::Expanding, QSizePolicy::Expanding); + frequencies_changed (); + main_layout->addWidget (bands_table_); + // recalculate table when frequencies change + connect (configuration_->frequencies (), &QAbstractItemModel::layoutChanged + , this, &Dialog::frequencies_changed); + // handle changes by updating the underlying flags + connect (bands_table_.data (), &QTableWidget::itemChanged, [this] (QTableWidgetItem * item) { + auto band_number = item->data (band_index_role).toInt (); + bands_[item->row ()].setBit (band_number, Qt::Checked == item->checkState ()); + }); + + // set up the gray line duration spin box + gray_line_width_spin_box_->setRange (1, 60 * 2); + gray_line_width_spin_box_->setSuffix ("min"); + gray_line_width_spin_box_->setValue (*gray_line_duration_); + QFormLayout * form_layout = new QFormLayout; + form_layout->addRow (tr ("Gray time:"), gray_line_width_spin_box_); + connect (gray_line_width_spin_box_.data () + , static_cast (&QSpinBox::valueChanged) + , [this] (int new_value) {*gray_line_duration_ = new_value;}); + + QHBoxLayout * bottom_layout = new QHBoxLayout; + bottom_layout->addStretch (); + bottom_layout->addLayout (form_layout); + main_layout->addLayout (bottom_layout); + + setLayout (main_layout); +} + +Dialog::~Dialog () +{ + // do this here too because ESC or parent shutdown closing this + // window doesn't queue a close event + save_window_state (); +} + +void Dialog::closeEvent (QCloseEvent * e) +{ + save_window_state (); + QDialog::closeEvent (e); +} + +void Dialog::save_window_state () +{ + SettingsGroup g {settings_, title}; + settings_->setValue ("geometry", saveGeometry ()); +} + +void Dialog::frequencies_changed () +{ + bands_table_->setColumnCount (WSPR_bands_->size ()); + // set up and load the table of check boxes + for (auto row = 0u; row < num_periods; ++row) + { + auto vertical_header = new QTableWidgetItem {periods[row]}; + vertical_header->setTextAlignment (Qt::AlignRight | Qt::AlignVCenter); + bands_table_->setVerticalHeaderItem (row, vertical_header); + int column {0}; + int band_number {0}; + for (auto const& band : *configuration_->bands ()) + { + if (WSPR_bands_->contains (band)) + { + if (0 == row) + { + auto horizontal_header = new QTableWidgetItem {band}; + bands_table_->setHorizontalHeaderItem (column, horizontal_header); + } + auto item = new QTableWidgetItem; + item->setFlags (Qt::ItemIsUserCheckable | Qt::ItemIsEnabled); + item->setCheckState (bands_[row].testBit (band_number) ? Qt::Checked : Qt::Unchecked); + item->setData (band_index_role, band_number); + if (coordinated_bands.contains (band)) + { + item->setBackground (coord_background_brush_); + } + bands_table_->setItem (row, column, item); + ++column; + } + ++band_number; + } + } + bands_table_->resizeColumnsToContents (); + auto is_visible = isVisible (); + show (); + resize_to_maximum (); + adjustSize (); // fix the size + if (!is_visible) + { + hide (); + } +} + +// to get the dialog window exactly the right size to contain the +// widgets without needing scroll bars we need to measure the size of +// the table widget and set its minimum size to the measured size +void Dialog::resize_to_maximum () +{ + bands_table_->setMinimumSize ({ + bands_table_->horizontalHeader ()->length () + + bands_table_->verticalHeader ()->width () + + 2 * bands_table_->frameWidth () + , bands_table_->verticalHeader ()->length () + + bands_table_->horizontalHeader ()->height () + + 2 * bands_table_->frameWidth () + }); + bands_table_->setMaximumSize (bands_table_->minimumSize ()); +} + +class WSPRBandHopping::impl +{ +public: + using BandList = Dialog::BandList; + + impl (QSettings * settings, Configuration const * configuration, QWidget * parent_widget) + : settings_ {settings} + , configuration_ {configuration} + , tx_percent_ {0} + , parent_widget_ {parent_widget} + , last_was_tx_ {false} + , carry_ {false} + , seed_ {{rand (), rand (), rand (), rand (), rand (), rand (), rand (), rand ()}} + , gen_ {seed_} + , dist_ {0, 99} + { + auto num_bands = configuration_->bands ()->rowCount (); + for (auto& flags : bands_) + { + flags.resize (num_bands); + } + } + + bool simple_scheduler (); + + QSettings * settings_; + Configuration const * configuration_; + int tx_percent_; + BandList WSPR_bands_; + BandList rx_permutation_; + BandList tx_permutation_; + QWidget * parent_widget_; + + // 5 x 10 bit flags representing each hopping band in each period + // and tune + QBitArray bands_[num_periods]; + + int gray_line_duration_; + QPointer dialog_; + bool last_was_tx_; + bool carry_; + std::seed_seq seed_; + std::mt19937 gen_; + std::uniform_int_distribution dist_; +}; + +bool WSPRBandHopping::impl::simple_scheduler () +{ + auto tx = carry_ || tx_percent_ > dist_ (gen_); + if (carry_) + { + carry_ = false; + } + else if (tx_percent_ < 40 && last_was_tx_ && tx) + { + // if percentage is less than 40 then avoid consecutive tx but + // always catch up on the next round + tx = false; + carry_ = true; + } + last_was_tx_ = tx; + return tx; +} + +WSPRBandHopping::WSPRBandHopping (QSettings * settings, Configuration const * configuration, QWidget * parent_widget) + : m_ {settings, configuration, parent_widget} +{ + // detect changes to the working frequencies model + m_->WSPR_bands_ = m_->configuration_->frequencies ()->all_bands (m_->configuration_->region (), Modes::WSPR).values (); + connect (m_->configuration_->frequencies (), &QAbstractItemModel::layoutChanged + , [this] () { + m_->WSPR_bands_ = m_->configuration_->frequencies ()->all_bands (m_->configuration_->region (), Modes::WSPR).values (); + }); + + // load settings + SettingsGroup g {m_->settings_, title}; + size_t size = m_->settings_->beginReadArray ("phases"); + for (auto i = 0u; i < size; ++i) + { + if (i < num_periods) + { + m_->settings_->setArrayIndex (i); + m_->bands_[i] = m_->settings_->value ("bands").toBitArray (); + } + } + m_->settings_->endArray (); + m_->gray_line_duration_ = m_->settings_->value ("GrayLineDuration", 60).toUInt (); +} + +WSPRBandHopping::~WSPRBandHopping () +{ + // save settings + SettingsGroup g {m_->settings_, title}; + m_->settings_->beginWriteArray ("phases"); + for (auto i = 0u; i < num_periods; ++i) + { + m_->settings_->setArrayIndex (i); + m_->settings_->setValue ("bands", m_->bands_[i]); + } + m_->settings_->endArray (); + m_->settings_->setValue ("GrayLineDuration", m_->gray_line_duration_); +} + +// pop up the maintenance dialog window +void WSPRBandHopping::show_dialog (bool /* checked */) +{ + if (!m_->dialog_) + { + m_->dialog_ = new Dialog {m_->settings_, m_->configuration_, &m_->WSPR_bands_, m_->bands_ + , &m_->gray_line_duration_, m_->parent_widget_}; + } + m_->dialog_->show (); + m_->dialog_->raise (); + m_->dialog_->activateWindow (); +} + +int WSPRBandHopping::tx_percent () const +{ + return m_->tx_percent_; +} + +void WSPRBandHopping::set_tx_percent (int new_value) +{ + m_->tx_percent_ = new_value; +} + +// determine the parameters of the hop, if any +auto WSPRBandHopping::next_hop (bool tx_enabled) -> Hop +{ + auto const& now = QDateTime::currentDateTimeUtc (); + auto const& date = now.date (); + auto year = date.year (); + auto month = date.month (); + auto day = date.day (); + auto const& time = now.time (); + float uth = time.hour () + time.minute () / 60. + + (time.second () + .001 * time.msec ()) / 3600.; + auto my_grid = m_->configuration_->my_grid (); + int period_index; + int band_index; + int tx_next; + + my_grid = (my_grid + " ").left (6); // hopping doesn't like + // short grids + + // look up the period for this time + FC_grayline (&year, &month, &day, &uth, my_grid.toLatin1 ().constData () + , &m_->gray_line_duration_, &period_index + , my_grid.size ()); + + band_index = next_hopping_band(); + + tx_next = next_is_tx () && tx_enabled; + + int frequencies_index {-1}; + auto const& frequencies = m_->configuration_->frequencies (); + auto const& bands = m_->configuration_->bands (); + auto band_name = bands->data (bands->index (band_index + 3, 0)).toString (); + if (m_->bands_[period_index].testBit (band_index + 3) // +3 for + // coordinated bands + && m_->WSPR_bands_.contains (band_name)) + { + // here we have a band that has been enabled in the hopping + // matrix so check it it has a configured working frequency + frequencies_index = frequencies->best_working_frequency (band_name); + } + + // if we do not have a configured working frequency on the selected + // coordinated hopping band we next pick from a random permutation + // of the other enabled bands in the hopping bands matrix + if (frequencies_index < 0) + { + // build sets of available rx and tx bands +#if QT_VERSION < QT_VERSION_CHECK (5, 14, 0) + auto target_rx_bands = m_->WSPR_bands_.toSet (); +#else + QSet target_rx_bands {m_->WSPR_bands_.begin (), m_->WSPR_bands_.end ()}; +#endif + auto target_tx_bands = target_rx_bands; + for (auto i = 0; i < m_->bands_[period_index].size (); ++i) + { + auto const& band = bands->data (bands->index (i, 0)).toString (); + // remove bands that are not enabled for hopping in this phase + if (!m_->bands_[period_index].testBit (i)) + { + target_rx_bands.remove (band); + target_tx_bands.remove (band); + } + // remove rx only bands from transmit list and vice versa + if (m_->bands_[5].testBit (i)) + { + target_tx_bands.remove (band); + } + else + { + target_rx_bands.remove (band); + } + } + // if we have some bands to permute + if (target_rx_bands.size () + target_tx_bands.size ()) + { +#if QT_VERSION < QT_VERSION_CHECK (5, 14, 0) + auto rx_permutations = m_->rx_permutation_.toSet (); + auto tx_permutations = m_->tx_permutation_.toSet (); +#else + QSet rx_permutations {m_->rx_permutation_.begin (), m_->rx_permutation_.end ()}; + QSet tx_permutations {m_->tx_permutation_.begin (), m_->tx_permutation_.end ()}; +#endif + if (!(m_->rx_permutation_.size () + m_->tx_permutation_.size ()) // all used up + // or rx list contains a band no longer scheduled + || !target_rx_bands.contains (rx_permutations) + // or tx list contains a band no longer scheduled for tx + || !target_tx_bands.contains (tx_permutations)) + { + // build new random permutations + m_->rx_permutation_ = target_rx_bands.values (); + std::random_shuffle (std::begin (m_->rx_permutation_), std::end (m_->rx_permutation_)); + m_->tx_permutation_ = target_tx_bands.values (); + std::random_shuffle (std::begin (m_->tx_permutation_), std::end (m_->tx_permutation_)); + // qDebug () << "New random Rx permutation:" << m_->rx_permutation_ + // << "random Tx permutation:" << m_->tx_permutation_; + } + if ((tx_next && m_->tx_permutation_.size ()) || !m_->rx_permutation_.size ()) + { + Q_ASSERT (m_->tx_permutation_.size ()); + // use one from the current random tx permutation + band_name = m_->tx_permutation_.takeFirst (); + } + else + { + Q_ASSERT (m_->rx_permutation_.size ()); + // use one from the current random rx permutation + band_name = m_->rx_permutation_.takeFirst (); + } + // find the first WSPR working frequency for the chosen band + frequencies_index = frequencies->best_working_frequency (band_name); + if (frequencies_index >= 0) // should be a redundant check, + // but to be safe + { + // we can use the random choice + // qDebug () << "random:" << frequencies->data (frequencies->index (frequencies_index, FrequencyList_v2::frequency_column)).toString (); + band_index = bands->find (band_name); + if (band_index < 0) // this shouldn't happen + { + Q_ASSERT (band_index >= 0); + frequencies_index = -1; + } + } + } + } + else + { + band_index += 3; + // qDebug () << "scheduled:" << frequencies->data (frequencies->index (frequencies_index, FrequencyList_v2::frequency_column)).toString (); + // remove from random permutations to stop the coordinated bands + // getting too high a weighting - not perfect but surely helps + m_->rx_permutation_.removeOne (band_name); + m_->tx_permutation_.removeOne (band_name); + } + + return { + periods[period_index] + + , frequencies_index + + , frequencies_index >= 0 // new band + && tx_enabled // transmit is allowed + && m_->bands_[4].testBit (band_index) // tune up required + && !m_->bands_[5].testBit (band_index) // not an Rx only band + + , frequencies_index >= 0 // new band + && tx_next // Tx scheduled + && !m_->bands_[5].testBit (band_index) // not an Rx only band + }; +} + +bool WSPRBandHopping::next_is_tx (bool simple_schedule) +{ + if (simple_schedule) + { + return m_->simple_scheduler (); + } + if (100 == m_->tx_percent_) + { + return true; + } + else + { + // consult scheduler to determine if next period should be a tx interval + return next_tx_state(m_->tx_percent_); + } +} diff --git a/WSPR/WSPRBandHopping.hpp b/WSPR/WSPRBandHopping.hpp index 045bc0d7..9f65de17 100755 --- a/WSPR/WSPRBandHopping.hpp +++ b/WSPR/WSPRBandHopping.hpp @@ -1,85 +1,85 @@ -#ifndef WSPR_BAND_HOPPING_HPP__ -#define WSPR_BAND_HOPPING_HPP__ - -#include - -#include "pimpl_h.hpp" - -class QSettings; -class Configuration; -class QWidget; - -// -// WSPR Band Hopping Control -// -// WSPR specifies a globally coordinated band hopping schedule and -// this class implements that. -// -// Responsibilities -// -// Provides a maintenance dialog allowing the user to define which -// bands are allowed from the band hopping schedule as defined here: -// -// http://physics.princeton.edu/pulsar/K1JT/doc/wspr/wspr-main.html -// -// Along with selecting bands a flag indicating that a short tune up -// signal is required for specified bands before they are used for -// transmit or receive, unless they are flagged as Rx Only (intended -// to protect Rx active aerials and non-licensed bands). -// -// Provides a Qt property that holds the Tx percentage which is used -// to generate a semi-randomized schedule of period to transmit. This -// schedule is random but adjusted to limit the number of consecutive -// transmission periods, it also adjusts the schedule to ensure that -// the overall number of transmission periods in any two hour hopping -// schedule reflects the percentage provided. -// -// Collaborations -// -// Settings including the selected bands with periods, the tune up -// flags and the gray line duration are maintained in persistent -// storage using the provided QSettings object instance. -// -// A passed in Configuration object instance is used to query the -// FrequencyList_v2_101 model to determine working frequencies for each -// band. The row index of this model is returned by this classes -// hopping scheduling method so it may be conveniently used to select -// a new working frequency by a client. -// -class WSPRBandHopping - : public QObject -{ - Q_OBJECT; - Q_PROPERTY (int tx_percent READ tx_percent WRITE set_tx_percent); - -public: - WSPRBandHopping (QSettings *, Configuration const *, QWidget * parent = nullptr); - ~WSPRBandHopping (); - - // display the band hopping maintenance dialog - Q_SLOT void show_dialog (bool); - - // Property tx_percent implementation - int tx_percent () const; - Q_SLOT void set_tx_percent (int); - - // structure that defines the results of the next_hop() method - struct Hop - { - QString period_name_; - int frequencies_index_; // may be -1 indicating no change - bool tune_required_; - bool tx_next_; - }; - // return the next band parameters - Hop next_hop (bool tx_enabled); - // determine if the next period should be a transmit period - bool next_is_tx (bool simple_schedule = false); - -private: - // implementation hidden from public interface - class impl; - pimpl m_; -}; - -#endif +#ifndef WSPR_BAND_HOPPING_HPP__ +#define WSPR_BAND_HOPPING_HPP__ + +#include + +#include "pimpl_h.hpp" + +class QSettings; +class Configuration; +class QWidget; + +// +// WSPR Band Hopping Control +// +// WSPR specifies a globally coordinated band hopping schedule and +// this class implements that. +// +// Responsibilities +// +// Provides a maintenance dialog allowing the user to define which +// bands are allowed from the band hopping schedule as defined here: +// +// http://physics.princeton.edu/pulsar/K1JT/doc/wspr/wspr-main.html +// +// Along with selecting bands a flag indicating that a short tune up +// signal is required for specified bands before they are used for +// transmit or receive, unless they are flagged as Rx Only (intended +// to protect Rx active aerials and non-licensed bands). +// +// Provides a Qt property that holds the Tx percentage which is used +// to generate a semi-randomized schedule of period to transmit. This +// schedule is random but adjusted to limit the number of consecutive +// transmission periods, it also adjusts the schedule to ensure that +// the overall number of transmission periods in any two hour hopping +// schedule reflects the percentage provided. +// +// Collaborations +// +// Settings including the selected bands with periods, the tune up +// flags and the gray line duration are maintained in persistent +// storage using the provided QSettings object instance. +// +// A passed in Configuration object instance is used to query the +// FrequencyList_v2_101 model to determine working frequencies for each +// band. The row index of this model is returned by this classes +// hopping scheduling method so it may be conveniently used to select +// a new working frequency by a client. +// +class WSPRBandHopping + : public QObject +{ + Q_OBJECT; + Q_PROPERTY (int tx_percent READ tx_percent WRITE set_tx_percent); + +public: + WSPRBandHopping (QSettings *, Configuration const *, QWidget * parent = nullptr); + ~WSPRBandHopping (); + + // display the band hopping maintenance dialog + Q_SLOT void show_dialog (bool); + + // Property tx_percent implementation + int tx_percent () const; + Q_SLOT void set_tx_percent (int); + + // structure that defines the results of the next_hop() method + struct Hop + { + QString period_name_; + int frequencies_index_; // may be -1 indicating no change + bool tune_required_; + bool tx_next_; + }; + // return the next band parameters + Hop next_hop (bool tx_enabled); + // determine if the next period should be a transmit period + bool next_is_tx (bool simple_schedule = false); + +private: + // implementation hidden from public interface + class impl; + pimpl m_; +}; + +#endif diff --git a/WSPR/WsprTxScheduler.cpp b/WSPR/WsprTxScheduler.cpp index 898d40bc..73469e81 100755 --- a/WSPR/WsprTxScheduler.cpp +++ b/WSPR/WsprTxScheduler.cpp @@ -1,228 +1,228 @@ -#include -#include -#include -#include -#include - -namespace -{ - char tx[6][10]; - int tx_table_2hr_slot=-1, tx_table_pctx=0; -}; - -int tx_band_sum(char bsum[10]) -{ - int i,j; - - for (j=0; j<10; j++) { - bsum[j]=0; - for (i=0; i<6; i++) { - bsum[j]=bsum[j]+tx[i][j]; - } - } - return 1; -} - -int tx_add_to_band(int band) -{ - // add tx cycle to a band without regard to ntxlim - int i,islot; - for ( i=0; i<10; i++) { - islot=rand()%6; - if( tx[islot][band] != 1 ) { - tx[islot][band]=1; - return 1; - } - } - return 0; -} - -int tx_sum() -{ - int i,j,sum=0; - for (i=0; i<6; i++) { - for (j=0; j<10; j++) { - sum=sum+tx[i][j]; - } - } - return sum; -} - -int tx_add_one(char* tx) -{ - int i, j, txflag, ngap; - // adds one tx slot to an existing array without - // creating successive tx slots. - // try to fill largest gaps first - // try gap sizes of 13, 11, 9, 7, 5, and finally 3 - for (ngap=13; ngap>=3; ngap=ngap-2) { - for (i=0; i< 60-ngap; i++) { - txflag=0; - for (j=0; j ntxlim ) { - tx[i]=0; - nrun=0; - } - } else { - nrun=0; - } - } - sum=0; - for (i=0; i<60; i++) { - sum=sum+tx[i]; - } - return sum; -} - -void tx_print() -{ - int i,j; - for (i=0; i<6; i++) { - for (j=0; j<10; j++) { - if( (i*10+j)%10 == 0 && i>=0 ) printf("\n"); - printf("%d ",tx[i][j]); - } - } - printf("\n"); - fflush(stdout); -} - -int create_tx_schedule(int pctx) -{ - char bsum[10]; - int i, j, k, sum, ntxlim, ntxbandmin, needed; - int iflag, nrx; - float rxavg,x; - - needed=60*(pctx/100.0)+0.5; - - memset(tx,0,sizeof(char)*60); - - if( pctx == 0 ) return 0; - - if( pctx <= 25 ) { // Use K1JT's algorithm in this regime - rxavg=100.0/pctx-1.0; - i=0; - while(1) { - x=(rand()%100)/100.0; - nrx=(rxavg+3.0*x-1.0); //2-5 for 25% - i=i+nrx+1; - if( i < 60 ) { - tx[i/10][i%10]=1; - } else { - break; - } - } - return 0; - } else if( pctx > 25 && pctx < 33 ) { - ntxlim=1; - ntxbandmin=1; - } else if( pctx >= 33 && pctx < 50 ) { - ntxlim=1; - ntxbandmin=2; - } else if( pctx >= 50 && pctx < 60 ) { - ntxlim=2; - ntxbandmin=3; - } else { - ntxlim=3; - ntxbandmin=4; - } - - // when txpct>25% create a table that guarantees that all - // bands will be visited 1, 2, or 3 times, as appropriate. - // - // start by filling each band slot with ntxbandmin tx's - for (i=0; itm_hour; - int minute = ltm->tm_min; - - int tx_2hr_slot = hour/2; - int tx_20min_slot = (hour-tx_2hr_slot*2)*3 + minute/20; - int tx_2min_slot = (minute%20)/2; - - if( (tx_2hr_slot != tx_table_2hr_slot) || (tx_table_pctx != pctx) ) { - create_tx_schedule(pctx); - tx_table_2hr_slot = tx_2hr_slot; - tx_table_pctx = pctx; - } - -// tx_print(); - return tx[tx_20min_slot][tx_2min_slot]; -} - -int next_hopping_band() -{ - time_t now=time(0)+30; - tm *ltm = gmtime(&now); - int minute = ltm->tm_min; - int tx_2min_slot = (minute%20)/2; - return tx_2min_slot; -} +#include +#include +#include +#include +#include + +namespace +{ + char tx[6][10]; + int tx_table_2hr_slot=-1, tx_table_pctx=0; +}; + +int tx_band_sum(char bsum[10]) +{ + int i,j; + + for (j=0; j<10; j++) { + bsum[j]=0; + for (i=0; i<6; i++) { + bsum[j]=bsum[j]+tx[i][j]; + } + } + return 1; +} + +int tx_add_to_band(int band) +{ + // add tx cycle to a band without regard to ntxlim + int i,islot; + for ( i=0; i<10; i++) { + islot=rand()%6; + if( tx[islot][band] != 1 ) { + tx[islot][band]=1; + return 1; + } + } + return 0; +} + +int tx_sum() +{ + int i,j,sum=0; + for (i=0; i<6; i++) { + for (j=0; j<10; j++) { + sum=sum+tx[i][j]; + } + } + return sum; +} + +int tx_add_one(char* tx) +{ + int i, j, txflag, ngap; + // adds one tx slot to an existing array without + // creating successive tx slots. + // try to fill largest gaps first + // try gap sizes of 13, 11, 9, 7, 5, and finally 3 + for (ngap=13; ngap>=3; ngap=ngap-2) { + for (i=0; i< 60-ngap; i++) { + txflag=0; + for (j=0; j ntxlim ) { + tx[i]=0; + nrun=0; + } + } else { + nrun=0; + } + } + sum=0; + for (i=0; i<60; i++) { + sum=sum+tx[i]; + } + return sum; +} + +void tx_print() +{ + int i,j; + for (i=0; i<6; i++) { + for (j=0; j<10; j++) { + if( (i*10+j)%10 == 0 && i>=0 ) printf("\n"); + printf("%d ",tx[i][j]); + } + } + printf("\n"); + fflush(stdout); +} + +int create_tx_schedule(int pctx) +{ + char bsum[10]; + int i, j, k, sum, ntxlim, ntxbandmin, needed; + int iflag, nrx; + float rxavg,x; + + needed=60*(pctx/100.0)+0.5; + + memset(tx,0,sizeof(char)*60); + + if( pctx == 0 ) return 0; + + if( pctx <= 25 ) { // Use K1JT's algorithm in this regime + rxavg=100.0/pctx-1.0; + i=0; + while(1) { + x=(rand()%100)/100.0; + nrx=(rxavg+3.0*x-1.0); //2-5 for 25% + i=i+nrx+1; + if( i < 60 ) { + tx[i/10][i%10]=1; + } else { + break; + } + } + return 0; + } else if( pctx > 25 && pctx < 33 ) { + ntxlim=1; + ntxbandmin=1; + } else if( pctx >= 33 && pctx < 50 ) { + ntxlim=1; + ntxbandmin=2; + } else if( pctx >= 50 && pctx < 60 ) { + ntxlim=2; + ntxbandmin=3; + } else { + ntxlim=3; + ntxbandmin=4; + } + + // when txpct>25% create a table that guarantees that all + // bands will be visited 1, 2, or 3 times, as appropriate. + // + // start by filling each band slot with ntxbandmin tx's + for (i=0; itm_hour; + int minute = ltm->tm_min; + + int tx_2hr_slot = hour/2; + int tx_20min_slot = (hour-tx_2hr_slot*2)*3 + minute/20; + int tx_2min_slot = (minute%20)/2; + + if( (tx_2hr_slot != tx_table_2hr_slot) || (tx_table_pctx != pctx) ) { + create_tx_schedule(pctx); + tx_table_2hr_slot = tx_2hr_slot; + tx_table_pctx = pctx; + } + +// tx_print(); + return tx[tx_20min_slot][tx_2min_slot]; +} + +int next_hopping_band() +{ + time_t now=time(0)+30; + tm *ltm = gmtime(&now); + int minute = ltm->tm_min; + int tx_2min_slot = (minute%20)/2; + return tx_2min_slot; +} diff --git a/WSPR/WsprTxScheduler.h b/WSPR/WsprTxScheduler.h index 39791947..ccafd227 100755 --- a/WSPR/WsprTxScheduler.h +++ b/WSPR/WsprTxScheduler.h @@ -1,7 +1,7 @@ -#ifndef WSPR_TX_SCHEDULER_H_ -#define WSPR_TX_SCHEDULER_H_ - -int next_tx_state (int pctx); -int next_hopping_band(); - -#endif +#ifndef WSPR_TX_SCHEDULER_H_ +#define WSPR_TX_SCHEDULER_H_ + +int next_tx_state (int pctx); +int next_hopping_band(); + +#endif -- cgit v1.2.3