From 90781c35e4d9d090059093b0a9da74559d8dfcf4 Mon Sep 17 00:00:00 2001 From: Lucjan Bryndza Date: Sat, 25 May 2013 22:19:58 +0200 Subject: [PATCH] Transmiter handler --- libpsk/include/codec/trx_device_base.hpp | 4 +--- libpsk/include/port/pulse/pulse_device.hpp | 5 +++-- libpsk/include/psk/modulator.hpp | 2 +- libpsk/src/port/pulse/pulse_device.cpp | 20 +++++++++++++------- libpsk/src/psk/modulator.cpp | 7 ++++--- psk31test.cpp | 7 +++++-- 6 files changed, 27 insertions(+), 18 deletions(-) diff --git a/libpsk/include/codec/trx_device_base.hpp b/libpsk/include/codec/trx_device_base.hpp index a237531..28ab576 100644 --- a/libpsk/include/codec/trx_device_base.hpp +++ b/libpsk/include/codec/trx_device_base.hpp @@ -27,7 +27,7 @@ struct event tx_char, //Transmit char event type spectrum, //Spectrum event type imd_rdy, //IMD signal event - clk_err //CLK error event + clk_err, //CLK error event } evt; struct imd_s { @@ -96,8 +96,6 @@ class tx_codec tx_codec& operator=(const tx_codec&) = delete; public: typedef std::function handler_t; - tx_codec() - {} tx_codec( handler_t callback ) : m_callback( callback ) {} diff --git a/libpsk/include/port/pulse/pulse_device.hpp b/libpsk/include/port/pulse/pulse_device.hpp index d67983d..32fc0a6 100644 --- a/libpsk/include/port/pulse/pulse_device.hpp +++ b/libpsk/include/port/pulse/pulse_device.hpp @@ -49,9 +49,9 @@ private: //Hardware sound thread func void hardware_sound_thread(); //Receive and transmit thread - bool receive_thread(); + int receive_thread(); //Transmit thread - bool transmit_thread(); + int transmit_thread(); //Hardware funcs int enable_hw_rx(); int disable_hw_rx(); @@ -62,6 +62,7 @@ private: std::unique_ptr< std::thread > m_thread; //TX thread std::mutex m_thrmutex; //Thread mutex volatile bool m_thread_running {}; //Thread + volatile int m_thread_status {}; //Thread error status pa_simple *m_pa_ctx; //PA CTX static constexpr auto audio_buf_len = 1024; std::array m_audio_buf; diff --git a/libpsk/include/psk/modulator.hpp b/libpsk/include/psk/modulator.hpp index a3bba91..3b43ee3 100644 --- a/libpsk/include/psk/modulator.hpp +++ b/libpsk/include/psk/modulator.hpp @@ -53,7 +53,7 @@ public: b125 //Baudrate 125 }; //Constructor - explicit modulator( int sample_freq, int tx_freq, std::size_t char_que_len ); + explicit modulator( int sample_freq, int tx_freq, std::size_t char_que_len, tx_codec::handler_t callback ); //Operator on new samples virtual bool operator()( sample_type* sample, size_t len ); //Set char into the modulator diff --git a/libpsk/src/port/pulse/pulse_device.cpp b/libpsk/src/port/pulse/pulse_device.cpp index 30ce2be..706b812 100644 --- a/libpsk/src/port/pulse/pulse_device.cpp +++ b/libpsk/src/port/pulse/pulse_device.cpp @@ -47,7 +47,7 @@ void pulse_device::lock( bool lock ) // Initialize sound hardware in selected mode int pulse_device::setup_sound_hardware( trx_device_base::mode m ) { - + //Hardware SETUP state if( m == trx_device_base::mode::on && get_mode()!=trx_device_base::mode::on ) { if( get_mode()==trx_device_base::mode::transmit ) @@ -68,28 +68,32 @@ int pulse_device::setup_sound_hardware( trx_device_base::mode m ) const int ret = enable_hw_tx(); if( ret != 0 ) return ret; } + //Thread specific switch if( get_mode()!=trx_device_base::mode::off && m == trx_device_base::mode::off ) { //Disable and wait for stop m_thread_running = false; m_thread->join(); + return m_thread_status; } else if( get_mode()==trx_device_base::mode::off && m != trx_device_base::mode::off ) { m_thread_running = true; m_thread.reset( new std::thread( &pulse_device::hardware_sound_thread, this ) ); + return 0; } - return 0; + return trx_device_base::INVALID; } /* ------------------------------------------------------------------------- */ //Hardware sound thread func void pulse_device::hardware_sound_thread() { - for(;m_thread_running;) + int errcode = 0; + for(; m_thread_running && !errcode ;) { m_thrmutex.lock(); - if ( get_mode()==trx_device_base::mode::on ) receive_thread(); - else if( get_mode()==trx_device_base::mode::transmit ) transmit_thread(); + if ( get_mode()==trx_device_base::mode::on ) errcode = receive_thread(); + else if( get_mode()==trx_device_base::mode::transmit ) errcode = transmit_thread(); m_thrmutex.unlock(); } if( get_mode()==trx_device_base::mode::on ) @@ -104,10 +108,11 @@ void pulse_device::hardware_sound_thread() set_mode_off(); disable_hw_tx(); } + m_thread_status = errcode; } /* ------------------------------------------------------------------------- */ //Receive and transmit thread -bool pulse_device::receive_thread() +int pulse_device::receive_thread() { int error = 0; /* Record some data ... */ @@ -120,7 +125,7 @@ bool pulse_device::receive_thread() } /* ------------------------------------------------------------------------- */ //Transmit thread -bool pulse_device::transmit_thread() +int pulse_device::transmit_thread() { int error = 0; dac_hardware_isr( &m_audio_buf[0], audio_buf_len ); @@ -175,6 +180,7 @@ int pulse_device::disable_hw_tx() m_pa_ctx = nullptr; return error; } + /* ------------------------------------------------------------------------- */ } /* namespace psk */ } /* namespace ham */ diff --git a/libpsk/src/psk/modulator.cpp b/libpsk/src/psk/modulator.cpp index b401b91..30318b1 100644 --- a/libpsk/src/psk/modulator.cpp +++ b/libpsk/src/psk/modulator.cpp @@ -137,9 +137,10 @@ constexpr int modulator::m_vect_lookup[6][2]; /* ------------------------------------------------------------------------- */ //Modulator -modulator::modulator( int sample_freq, int tx_freq, std::size_t char_que_len ) - : m_sample_freq( sample_freq ), m_p_psk_tx_i(psk_shapes::Z), m_p_psk_tx_q(psk_shapes::Z), - m_chqueue( char_que_len ) +modulator::modulator( int sample_freq, int tx_freq, std::size_t char_que_len, tx_codec::handler_t callback ) + : tx_codec( callback ), + m_sample_freq( sample_freq ), m_p_psk_tx_i(psk_shapes::Z), + m_p_psk_tx_q(psk_shapes::Z), m_chqueue( char_que_len ) { //TODO convert to array diff --git a/psk31test.cpp b/psk31test.cpp index 5bfea6a..8c15b91 100644 --- a/psk31test.cpp +++ b/psk31test.cpp @@ -476,7 +476,7 @@ private: int encoder_main( const char *filename ) { class audio_writer ww( filename ); - ham::psk::modulator mod( ww.get_samplerate(), 2125, 1024 ); + ham::psk::modulator mod( ww.get_samplerate(), 2125, 1024, nullptr ); //mod.set_auto_shutoff( false ); const char txt[] = "Ala ma kota a KOT ma ale teraz bedzie nieco dluzszy tekst a im tekst dluzszy tym lepszy"; mod.set_mode( ham::psk::modulator::mode::qpsku, ham::psk::modulator::baudrate::b31); @@ -559,6 +559,9 @@ void decoder_callback( const ham::psk::event &ev ) case event::type::imd_rdy: cout << "IMDRDY value " << ev.imd.value << " " << ev.imd.noise << endl; break; + case event::type::tx_char: + cout << "CHRTX " << char(ev.chr) << endl; + break; default: cout << "Unknown evt" << endl; } @@ -570,7 +573,7 @@ int main(int argc, const char * const *argv ) { namespace psk = ham::psk; //FIX this API - psk::modulator * const mod = new psk::modulator(8000, 2124, 1024); + psk::modulator * const mod = new psk::modulator(8000, 2124, 1024, decoder_callback); psk::decoder * dec = new psk::decoder(8000, decoder_callback ); psk::pulse_device pulse( decoder_callback ); pulse.add_tx_codec( mod );