diff --git a/libpsk/include/codec/trx_device_base.hpp b/libpsk/include/codec/trx_device_base.hpp index aa2959e..15e6e15 100644 --- a/libpsk/include/codec/trx_device_base.hpp +++ b/libpsk/include/codec/trx_device_base.hpp @@ -213,6 +213,11 @@ public: { return m_tx_codec; } + //Set FFT interval + void set_fft_interval( unsigned short timeout ) + { + m_spectrum_timeout = timeout; + } //Lock and unlock device virtual void lock( bool lock ) = 0; private: @@ -229,11 +234,13 @@ protected: m_mode = mode::off; } private: - tx_codec* m_tx_codec { nullptr }; /* Transmit codec ptr */ + tx_codec* m_tx_codec { nullptr }; /* Transmit codec ptr */ std::array m_rx_codecs {{}}; /* RX codecs array */ - spectrum_calculator m_spectrum; /* Spectrum calculator object */ - const event_handler_t m_evt_callback; /* Event handler object */ - mode m_mode { mode::off }; /* Current device mode */ + spectrum_calculator m_spectrum; /* Spectrum calculator object */ + unsigned m_spectrum_tmr {}; /* Spectrum time */ + unsigned short m_spectrum_timeout { 250 }; /* Spectrum timeout */ + const event_handler_t m_evt_callback; /* Event handler object */ + mode m_mode { mode::off }; /* Current device mode */ }; diff --git a/libpsk/include/psk/spectrum_calculator.hpp b/libpsk/include/psk/spectrum_calculator.hpp index 7c6fd09..764b1af 100644 --- a/libpsk/include/psk/spectrum_calculator.hpp +++ b/libpsk/include/psk/spectrum_calculator.hpp @@ -42,11 +42,22 @@ public: spectrum_calculator() {} public: - //Copy sample buffer - void copy_samples( const pow_t* samples ) + bool copy_samples( const pow_t* samples, size_t len ) { - std::memcpy( m_real, samples, sizeof(pow_t) * WIDTH ); + size_t cpyn = WIDTH - m_sample_buf_cnt; + if( len < cpyn ) cpyn = len; + std::memcpy( &m_real[m_sample_buf_cnt], samples, sizeof(pow_t) * cpyn ); + m_sample_buf_cnt += cpyn; m_energy_calculated = false; + if( m_sample_buf_cnt >= WIDTH ) + { + m_sample_buf_cnt = 0; + return true; + } + else + { + return false; + } } //Get buffer samples and calculate it const pow_t& operator[]( size_t idx ) @@ -104,6 +115,7 @@ private: bool m_energy_calculated { false }; //Energy is calculated scale m_scale { scale::log }; //Current scale pow_t m_factor { std::numeric_limits::max() }; + short m_sample_buf_cnt {}; //Sample buffer counter }; /*----------------------------------------------------------*/ diff --git a/libpsk/src/codec/trx_device_base.cpp b/libpsk/src/codec/trx_device_base.cpp index 5cb685a..c2877a6 100644 --- a/libpsk/src/codec/trx_device_base.cpp +++ b/libpsk/src/codec/trx_device_base.cpp @@ -7,7 +7,6 @@ /* ------------------------------------------------------------------------- */ #include "codec/trx_device_base.hpp" - /* ------------------------------------------------------------------------- */ //Namespace def namespace ham { @@ -74,6 +73,14 @@ void trx_device_base::adc_process( const sample_type *buf, size_t len ) if( v != nullptr ) (*v)( buf, len ); } + //Calculate the tout sample + m_spectrum_tmr += (1000*len)/get_rx_sample_rate(); + if( m_spectrum_tmr >= m_spectrum_timeout ) + { + //True if all samples completed + if( m_spectrum.copy_samples( buf, len ) ) + m_spectrum_tmr = 0; + } } /* ------------------------------------------------------------------------- */ //DAC vector func return true if finished