From 675011fd26a6eae89a1b7629a715d8068bdc3832 Mon Sep 17 00:00:00 2001 From: dreamsourcelabTAI Date: Mon, 10 Oct 2022 18:40:42 +0800 Subject: [PATCH] Code refactoring 25:repeat mode --- DSView/pv/dstimer.cpp | 14 +- DSView/pv/dstimer.h | 2 + DSView/pv/interface/icallbacks.h | 1 - DSView/pv/mainwindow.cpp | 27 +-- DSView/pv/mainwindow.h | 1 - DSView/pv/sigsession.cpp | 280 +++++++++++-------------------- DSView/pv/sigsession.h | 144 ++++++++++++---- DSView/pv/storesession.cpp | 1 - DSView/pv/view/dsosignal.cpp | 6 +- DSView/pv/view/viewport.cpp | 1 + DSView/pv/view/viewstatus.cpp | 3 +- 11 files changed, 236 insertions(+), 244 deletions(-) diff --git a/DSView/pv/dstimer.cpp b/DSView/pv/dstimer.cpp index d741e11b..84225a2e 100644 --- a/DSView/pv/dstimer.cpp +++ b/DSView/pv/dstimer.cpp @@ -36,9 +36,17 @@ void DsTimer::TimeOut(int millsec, CALLBACL_FUNC f) { _call = f; QTimer::singleShot(millsec, this, SLOT(on_timeout())); -} +} - void DsTimer::Start(int millsec, CALLBACL_FUNC f) +void DsTimer::TimeOut(int millsec) +{ + if (_call == NULL){ + assert(false); + } + QTimer::singleShot(millsec, this, SLOT(on_timeout())); +} + +void DsTimer::Start(int millsec, CALLBACL_FUNC f) { if (_isActived) return; @@ -53,7 +61,7 @@ void DsTimer::TimeOut(int millsec, CALLBACL_FUNC f) _timer.start(millsec); _isActived = true; _beginTime = high_resolution_clock::now(); - } +} void DsTimer::SetCallback(CALLBACL_FUNC f) { diff --git a/DSView/pv/dstimer.h b/DSView/pv/dstimer.h index c38a7cc5..276bc642 100644 --- a/DSView/pv/dstimer.h +++ b/DSView/pv/dstimer.h @@ -42,6 +42,8 @@ public: void TimeOut(int millsec, CALLBACL_FUNC f); + void TimeOut(int millsec); + void SetCallback(CALLBACL_FUNC f); void Start(int millsec, CALLBACL_FUNC f); diff --git a/DSView/pv/interface/icallbacks.h b/DSView/pv/interface/icallbacks.h index e6cebb4b..3d9b8f55 100644 --- a/DSView/pv/interface/icallbacks.h +++ b/DSView/pv/interface/icallbacks.h @@ -31,7 +31,6 @@ public: virtual void session_error()=0; virtual void session_save()=0; virtual void data_updated()=0; - virtual void repeat_resume()=0; virtual void update_capture()=0; virtual void cur_snap_samplerate_changed()=0; diff --git a/DSView/pv/mainwindow.cpp b/DSView/pv/mainwindow.cpp index 627331e5..edd84f47 100644 --- a/DSView/pv/mainwindow.cpp +++ b/DSView/pv/mainwindow.cpp @@ -331,13 +331,6 @@ namespace pv MsgBox::Show(NULL, error.toStdString().c_str(), this); } - void MainWindow::repeat_resume() - { - // while (_view->session().is_running_status()) - // QCoreApplication::processEvents(); - // _session->stop_capture(); - } - void MainWindow::session_error() { _event.session_error(); @@ -630,7 +623,7 @@ namespace pv // old version(<= 1.1.2), restore the language if (sessionObj["Version"].toInt() == BASE_SESSION_VERSION) { - switchLanguage(sessionObj["Language"].toInt()); + switchLanguage(sessionObj["Language"].toInt()); } if (_device_agent->is_hardware()) @@ -1419,8 +1412,8 @@ namespace pv if (_device_agent->is_hardware()) { - QString sessionFile = genSessionFileName(); - on_load_session(sessionFile); + QString ses_name = genSessionFileName(); + on_load_session(ses_name); } else if (_device_agent->is_demo()) { @@ -1429,8 +1422,7 @@ namespace pv { QString str = dir.absolutePath() + "/"; QString ses_name = str + _device_agent->driver_name() + QString::number(mode) + ".dsc"; - if (QFileInfo(ses_name).exists()) - on_load_session(ses_name); + on_load_session(ses_name); } } } @@ -1521,15 +1513,23 @@ namespace pv case DSV_MSG_START_COLLECT_WORK_PREV: _trigger_widget->try_commit_trigger(); _view->capture_init(); + _view->on_state_changed(false); break; case DSV_MSG_START_COLLECT_WORK: update_toolbar_view_status(); + _view->on_state_changed(false); + break; + + case DSV_MSG_COLLECT_END: + prgRate(0); + _view->repeat_unshow(); + _view->on_state_changed(true); break; case DSV_MSG_END_COLLECT_WORK: _session->device_event_object()->device_updated(); - update_toolbar_view_status(); + update_toolbar_view_status(); break; case DSV_MSG_CURRENT_DEVICE_CHANGE_PREV: @@ -1586,6 +1586,7 @@ namespace pv _sampling_bar->update_sample_rate_selector(); _view->mode_changed(); reset_all_view(); + load_device_config(); update_toolbar_view_status(); break; diff --git a/DSView/pv/mainwindow.h b/DSView/pv/mainwindow.h index 72b16bee..7c5c73e2 100644 --- a/DSView/pv/mainwindow.h +++ b/DSView/pv/mainwindow.h @@ -151,7 +151,6 @@ private: void show_error(QString error); void session_error(); void data_updated(); - void repeat_resume(); void update_capture(); void cur_snap_samplerate_changed(); diff --git a/DSView/pv/sigsession.cpp b/DSView/pv/sigsession.cpp index 914fc365..e75ca01e 100644 --- a/DSView/pv/sigsession.cpp +++ b/DSView/pv/sigsession.cpp @@ -97,6 +97,7 @@ namespace pv _is_decoding = false; _bClose = false; _callback = NULL; + _repeat_wait_prog_step = 10; _device_agent.set_callback(this); @@ -109,7 +110,8 @@ namespace pv _feed_timer.Stop(); _feed_timer.SetCallback(std::bind(&SigSession::feed_timeout, this)); - _repeate_timer.SetCallback(std::bind(&SigSession::repeat_capture_wait_timout, this)); + _repeat_timer.SetCallback(std::bind(&SigSession::repeat_capture_wait_timeout, this)); + _repeat_wait_prog_timer.SetCallback(std::bind(&SigSession::repeat_wait_prog_timeout, this)); } SigSession::SigSession(SigSession &o) @@ -211,7 +213,7 @@ namespace pv clear_all_decoder(); - RELEASE_ARRAY(_group_traces); + RELEASE_ARRAY(_group_traces); init_signals(); _cur_snap_samplerate = _device_agent.get_sample_rate(); @@ -379,7 +381,7 @@ namespace pv _feed_timer.Stop(); _noData_cnt = 0; - data_unlock(); + _data_lock = false; // container init container_init(); @@ -468,7 +470,14 @@ namespace pv _callback->trigger_message(DSV_MSG_START_COLLECT_WORK_PREV); - return exec_capture(); + if (exec_capture()) + { + _is_working = true; + _callback->trigger_message(DSV_MSG_START_COLLECT_WORK); + return true; + } + + return false; } bool SigSession::exec_capture() @@ -505,8 +514,6 @@ namespace pv return false; } - _is_working = true; - _callback->trigger_message(DSV_MSG_START_COLLECT_WORK); return true; } @@ -520,6 +527,8 @@ namespace pv if (_bClose) { _is_working = false; + _repeat_timer.Stop(); + _repeat_wait_prog_timer.Stop(); exit_capture(); return; } @@ -538,8 +547,23 @@ namespace pv if (!wait_upload) { _is_working = false; + _repeat_timer.Stop(); + _repeat_wait_prog_timer.Stop(); + + if (_repeat_hold_prg != 0 && _is_repeat_mode){ + _repeat_hold_prg = 0; + _callback->repeat_hold(_repeat_hold_prg); + } + _callback->trigger_message(DSV_MSG_END_COLLECT_WORK_PREV); + exit_capture(); + + if (_is_repeat_mode && _device_agent.is_collecting() == false) + { + // On repeat mode, the working status is changed, to post the event message. + _callback->trigger_message(DSV_MSG_END_COLLECT_WORK); + } } else { @@ -552,7 +576,7 @@ namespace pv _is_instant = false; //_feed_timer - _feed_timer.Stop(); + _feed_timer.Stop(); if (_device_agent.is_collecting()) _device_agent.stop(); @@ -876,7 +900,7 @@ namespace pv { ds_lock_guard lock(_data_mutex); - data_lock(); + _data_lock = true; if (_logic_data) { @@ -911,21 +935,6 @@ namespace pv _data_updated = true; } - void SigSession::data_lock() - { - _data_lock = true; - } - - void SigSession::data_unlock() - { - _data_lock = false; - } - - bool SigSession::get_data_lock() - { - return _data_lock; - } - void SigSession::data_auto_lock(int lock) { _data_auto_lock = lock; @@ -1113,17 +1122,19 @@ namespace pv _trigger_flag = dso.trig_flag; _trigger_ch = dso.trig_ch; + receive_data(dso.num_samples); if (!_is_instant) - data_lock(); + { + _data_lock = true; + } _data_updated = true; } void SigSession::feed_in_analog(const sr_datafeed_analog &analog) { - if (!_analog_data || _analog_data->snapshot()->memory_failed()) { dsv_err("%s", "Unexpected analog packet"); @@ -1331,7 +1342,6 @@ namespace pv try { - bool ret = false; // Create the decoder @@ -1407,11 +1417,6 @@ namespace pv return false; } - std::vector &SigSession::get_decode_signals() - { - return _decode_traces; - } - int SigSession::get_trace_index_by_key_handel(void *handel) { int dex = 0; @@ -1478,14 +1483,10 @@ namespace pv rst_decoder(dex); } - pv::data::DecoderModel *SigSession::get_decoder_model() - { - return _decoder_model; - } - void SigSession::spectrum_rebuild() { bool has_dso_signal = false; + for (auto &s : _signals) { view::DsoSignal *dsoSig = NULL; @@ -1494,9 +1495,12 @@ namespace pv has_dso_signal = true; // check already have auto iter = _spectrum_traces.begin(); - for (unsigned int i = 0; i < _spectrum_traces.size(); i++, iter++) + + for (unsigned int i = 0; i < _spectrum_traces.size(); i++, iter++){ if ((*iter)->get_index() == dsoSig->get_index()) break; + } + // if not, rebuild if (iter == _spectrum_traces.end()) { @@ -1515,11 +1519,6 @@ namespace pv signals_changed(); } - std::vector &SigSession::get_spectrum_traces() - { - return _spectrum_traces; - } - void SigSession::lissajous_rebuild(bool enable, int xindex, int yindex, double percent) { DESTROY_OBJECT(_lissajous_trace); @@ -1533,11 +1532,6 @@ namespace pv _lissajous_trace->set_enable(false); } - view::LissajousTrace *SigSession::get_lissajous_trace() - { - return _lissajous_trace; - } - void SigSession::math_rebuild(bool enable, view::DsoSignal *dsoSig1, view::DsoSignal *dsoSig2, data::MathStack::MathType type) @@ -1563,36 +1557,6 @@ namespace pv _math_trace->set_enable(false); } - view::MathTrace *SigSession::get_math_trace() - { - return _math_trace; - } - - void SigSession::set_session_time(QDateTime time) - { - _session_time = time; - } - - QDateTime SigSession::get_session_time() - { - return _session_time; - } - - uint64_t SigSession::get_trigger_pos() - { - return _trigger_pos; - } - - bool SigSession::trigd() - { - return _trigger_flag; - } - - uint8_t SigSession::trigd_ch() - { - return _trigger_ch; - } - void SigSession::nodata_timeout() { GVariant *gvar = _device_agent.get_config(NULL, NULL, SR_CONF_TRIGGER_SOURCE); @@ -1606,7 +1570,7 @@ namespace pv void SigSession::feed_timeout() { - data_unlock(); + _data_lock = false; if (!_data_updated) { @@ -1627,72 +1591,12 @@ namespace pv return NULL; } - SigSession::error_state SigSession::get_error() - { - return _error; - } - - void SigSession::set_error(error_state state) - { - _error = state; - } - void SigSession::clear_error() { _error_pattern = 0; _error = No_err; } - uint64_t SigSession::get_error_pattern() - { - return _error_pattern; - } - - double SigSession::get_repeat_intvl() - { - return _repeat_intvl; - } - - void SigSession::set_repeat_intvl(double interval) - { - _repeat_intvl = interval; - } - - bool SigSession::repeat_check() - { - if (!_is_working) - { - return false; - } - - if (_device_agent.get_work_mode() == LOGIC) - { - _repeat_hold_prg = 100; - _callback->repeat_hold(_repeat_hold_prg); - _out_timer.TimeOut(_repeat_intvl * 1000 / RepeatHoldDiv, std::bind(&SigSession::repeat_update, this)); - return true; - } - else - { - return false; - } - } - - void SigSession::repeat_update() - { - if (!_is_instant && _is_working && _is_repeat_mode) - { - _repeat_hold_prg -= 100 / RepeatHoldDiv; - if (_repeat_hold_prg != 0) - { - _out_timer.TimeOut(_repeat_intvl * 1000 / RepeatHoldDiv, std::bind(&SigSession::repeat_update, this)); - } - _callback->repeat_hold(_repeat_hold_prg); - if (_repeat_hold_prg == 0) - repeat_resume(); - } - } - int SigSession::get_repeat_hold() { if (!_is_instant && _is_working && _is_repeat_mode) @@ -1701,16 +1605,6 @@ namespace pv return 0; } - void SigSession::set_map_zoom(int index) - { - _map_zoom = index; - } - - int SigSession::get_map_zoom() - { - return _map_zoom; - } - void SigSession::auto_end() { for (auto &s : _signals) @@ -1723,36 +1617,6 @@ namespace pv } } - void SigSession::set_save_start(uint64_t start) - { - _save_start = start; - } - - void SigSession::set_save_end(uint64_t end) - { - _save_end = end; - } - - uint64_t SigSession::get_save_start() - { - return _save_start; - } - - uint64_t SigSession::get_save_end() - { - return _save_end; - } - - float SigSession::stop_scale() - { - return _stop_scale; - } - - void SigSession::set_stop_scale(float scale) - { - _stop_scale = scale; - } - void SigSession::Open() { } @@ -2049,9 +1913,29 @@ namespace pv } } - void SigSession::repeat_capture_wait_timout() + void SigSession::repeat_capture_wait_timeout() { - exec_capture(); + _repeat_timer.Stop(); + _repeat_wait_prog_timer.Stop(); + + _repeat_hold_prg = 0; + + if (_is_working) + { + _callback->repeat_hold(_repeat_hold_prg); + exec_capture(); + } + } + + void SigSession::repeat_wait_prog_timeout() + { + _repeat_hold_prg -= _repeat_wait_prog_step; + + if (_repeat_hold_prg < 0) + _repeat_hold_prg = 0; + + if (_is_working) + _callback->repeat_hold(_repeat_hold_prg); } void SigSession::OnMessage(int msg) @@ -2063,11 +1947,37 @@ namespace pv break; case DSV_MSG_TRIG_NEXT_COLLECT: - if (_repeat_intvl > 0) - _repeate_timer.Start(_repeat_intvl * 1000); - else - exec_capture(); - break; + { + if (_is_working) + { + if (_repeat_intvl > 0) + { + _repeat_hold_prg = 100; + _repeat_timer.Start(_repeat_intvl * 1000); + int intvl = _repeat_intvl * 1000 / 20; + + if (intvl >= 100){ + _repeat_wait_prog_step = 5; + } + else if (_repeat_intvl >= 1){ + intvl = _repeat_intvl * 1000 / 10; + _repeat_wait_prog_step = 10; + } + else{ + intvl = _repeat_intvl * 1000 / 5; + _repeat_wait_prog_step = 20; + } + + _repeat_wait_prog_timer.Start(intvl); + } + else + { + _repeat_hold_prg = 0; + exec_capture(); + } + } + } + break; } } diff --git a/DSView/pv/sigsession.h b/DSView/pv/sigsession.h index 596d4887..4751ee2f 100644 --- a/DSView/pv/sigsession.h +++ b/DSView/pv/sigsession.h @@ -152,9 +152,19 @@ public: void set_cur_snap_samplerate(uint64_t samplerate); void set_cur_samplelimits(uint64_t samplelimits); - void set_session_time(QDateTime time); - QDateTime get_session_time(); - uint64_t get_trigger_pos(); + + inline void set_session_time(QDateTime time){ + _session_time = time; + } + + inline QDateTime get_session_time(){ + return _session_time; + } + + inline uint64_t get_trigger_pos(){ + return _trigger_pos; + } + bool get_capture_status(bool &triggered, int &progress); std::set get_data(); @@ -166,20 +176,38 @@ public: int get_trace_index_by_key_handel(void *handel); void remove_decoder(int index); void remove_decoder_by_key_handel(void *handel); - std::vector& get_decode_signals(); + + inline std::vector& get_decode_signals(){ + return _decode_traces; + } + void rst_decoder(int index); void rst_decoder_by_key_handel(void *handel); - pv::data::DecoderModel* get_decoder_model(); - std::vector& get_spectrum_traces(); - view::LissajousTrace* get_lissajous_trace(); - view::MathTrace* get_math_trace(); + inline pv::data::DecoderModel* get_decoder_model(){ + return _decoder_model; + } + + inline std::vector& get_spectrum_traces(){ + return _spectrum_traces; + } + + inline view::LissajousTrace* get_lissajous_trace(){ + return _lissajous_trace; + } + + inline view::MathTrace* get_math_trace(){ + return _math_trace; + } void add_group(); void del_group(); uint16_t get_ch_num(int type); - bool get_data_lock(); + inline bool get_data_lock(){ + return _data_lock; + } + void data_auto_lock(int lock); void data_auto_unlock(); bool get_data_auto_lock(); @@ -193,27 +221,64 @@ public: data::MathStack::MathType type); void math_disable(); - bool trigd(); - uint8_t trigd_ch(); + + inline bool trigd(){ + return _trigger_flag; + } + + inline uint8_t trigd_ch(){ + return _trigger_ch; + } + data::Snapshot* get_snapshot(int type); - error_state get_error(); - void set_error(error_state state); + + inline error_state get_error(){ + return _error; + } + + inline void set_error(error_state state){ + _error = state; + } + void clear_error(); - uint64_t get_error_pattern(); - double get_repeat_intvl(); - void set_repeat_intvl(double interval); - bool repeat_check(); - int get_repeat_hold(); - int get_map_zoom(); - void set_save_start(uint64_t start); + inline uint64_t get_error_pattern(){ + return _error_pattern; + } - void set_save_end(uint64_t end); - uint64_t get_save_start(); - uint64_t get_save_end(); - void set_stop_scale(float scale); - float stop_scale(); - + inline double get_repeat_intvl(){ + return _repeat_intvl; + } + + inline void set_repeat_intvl(double interval){ + _repeat_intvl = interval; + } + + int get_repeat_hold(); + + inline void set_save_start(uint64_t start){ + _save_start = start; + } + + inline uint64_t get_save_start(){ + return _save_start; + } + + inline void set_save_end(uint64_t end){ + _save_end = end; + } + + inline uint64_t get_save_end(){ + return _save_end; + } + + inline void set_stop_scale(float scale){ + _stop_scale = scale; + } + + inline float stop_scale(){ + return _stop_scale; + } void clear_all_decoder(bool bUpdateView = true); @@ -260,10 +325,6 @@ public: _callback->session_save(); } - inline void repeat_resume(){ - _callback->repeat_resume(); - } - inline void show_region(uint64_t start, uint64_t end, bool keep){ _callback->show_region(start, end, keep); } @@ -287,7 +348,15 @@ public: void reload(); void refresh(int holdtime); void check_update(); - void set_map_zoom(int index); + + inline void set_map_zoom(int index){ + _map_zoom = index; + } + + inline int get_map_zoom(){ + return _map_zoom; + } + void auto_end(); bool have_hardware_data(); struct ds_device_base_info* get_device_list(int &out_count, int &actived_index); @@ -320,11 +389,9 @@ private: view::DecodeTrace* get_top_decode_task(); void capture_init(); - void data_lock(); - void data_unlock(); void nodata_timeout(); void feed_timeout(); - void repeat_update(); + void container_init(); void init_signals(); @@ -363,7 +430,8 @@ private: Snapshot* get_signal_snapshot(); - void repeat_capture_wait_timout(); + void repeat_capture_wait_timeout(); + void repeat_wait_prog_timeout(); private: mutable std::mutex _sampling_mutex; @@ -391,7 +459,8 @@ private: DsTimer _feed_timer; DsTimer _out_timer; - DsTimer _repeate_timer; + DsTimer _repeat_timer; + DsTimer _repeat_wait_prog_timer; int _noData_cnt; bool _data_lock; bool _data_updated; @@ -414,8 +483,9 @@ private: uint64_t _save_end; bool _is_working; bool _is_repeat_mode; - double _repeat_intvl; // The progress check timer interval. + double _repeat_intvl; // The progress wait timer interval. int _repeat_hold_prg; // The time sleep progress + int _repeat_wait_prog_step; bool _is_saving; bool _is_instant; int _device_status; diff --git a/DSView/pv/storesession.cpp b/DSView/pv/storesession.cpp index 4ae0c459..cec12c5a 100644 --- a/DSView/pv/storesession.cpp +++ b/DSView/pv/storesession.cpp @@ -60,7 +60,6 @@ #include "utility/encoding.h" #include "utility/path.h" #include "log.h" -#include namespace pv { diff --git a/DSView/pv/view/dsosignal.cpp b/DSView/pv/view/dsosignal.cpp index e61da76e..2bf314a5 100644 --- a/DSView/pv/view/dsosignal.cpp +++ b/DSView/pv/view/dsosignal.cpp @@ -188,8 +188,10 @@ void DsoSignal::set_enable(bool enable) g_variant_new_boolean(enable)); _view->update_hori_res(); + if (running) { - session->repeat_resume(); + session->stop_capture(); + session->start_capture(false); } _view->set_update(_viewport, true); @@ -1182,7 +1184,7 @@ void DsoSignal::paint_type_options(QPainter &p, int right, const QPoint pt, QCol p.drawText(acdc_rect, Qt::AlignCenter | Qt::AlignVCenter, (_acCoupling == SR_GND_COUPLING) ? tr(strings[2]): (_acCoupling == SR_DC_COUPLING) ? tr(strings[3]) : tr(strings[4])); - if (!session->get_device()->name().contains("virtual")) { + if (session->get_device()->is_hardware()) { p.setPen(Qt::transparent); p.setBrush(enabled() ? (auto_rect.contains(pt) ? _colour.darker() : _colour) : foreBack); p.drawRect(auto_rect); diff --git a/DSView/pv/view/viewport.cpp b/DSView/pv/view/viewport.cpp index 652e9684..8a126b5f 100644 --- a/DSView/pv/view/viewport.cpp +++ b/DSView/pv/view/viewport.cpp @@ -1224,6 +1224,7 @@ void Viewport::measure() { if (_view.session().get_data_lock()) return; + _measure_type = NO_MEASURE; if (_type == TIME_VIEW) { const uint64_t sample_rate = _view.session().cur_snap_samplerate(); diff --git a/DSView/pv/view/viewstatus.cpp b/DSView/pv/view/viewstatus.cpp index 5ff07548..64762466 100644 --- a/DSView/pv/view/viewstatus.cpp +++ b/DSView/pv/view/viewstatus.cpp @@ -76,7 +76,8 @@ void ViewStatus::paintEvent(QPaintEvent *) p.setPen(View::Blue); p.drawText(this->rect(), Qt::AlignCenter | Qt::AlignVCenter, _capture_status); - } else if (_session->get_device()->get_work_mode() == DSO) { + } + else if (_session->get_device()->get_work_mode() == DSO) { fore.setAlpha(View::BackAlpha); for(size_t i = 0; i < _mrects.size(); i++) { int sig_index = std::get<1>(_mrects[i]);