From: Soeren Apel Date: Sat, 3 Aug 2019 21:08:34 +0000 (+0200) Subject: Initial support for SRD_OUTPUT_LOGIC X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=04b0467582d8dfa989bb38fe18bfb1be20cea456;p=pulseview.git Initial support for SRD_OUTPUT_LOGIC --- diff --git a/pv/data/decode/decoder.cpp b/pv/data/decode/decoder.cpp index f644b8af..b1cf42d5 100644 --- a/pv/data/decode/decoder.cpp +++ b/pv/data/decode/decoder.cpp @@ -327,6 +327,26 @@ void Decoder::on_class_visibility_changed() annotation_visibility_changed(); } +bool Decoder::has_logic_output() const +{ + return (srd_decoder_->logic_output_channels != nullptr); +} + +const vector Decoder::logic_output_channels() const +{ + vector result; + + for (GSList *l = srd_decoder_->logic_output_channels; l; l = l->next) { + const srd_decoder_logic_output_channel* ch_data = + (srd_decoder_logic_output_channel*)l->data; + + result.emplace_back(QString::fromUtf8(ch_data->id), + QString::fromUtf8(ch_data->desc), ch_data->samplerate); + } + + return result; +} + } // namespace decode } // namespace data } // namespace pv diff --git a/pv/data/decode/decoder.hpp b/pv/data/decode/decoder.hpp index 86a371f2..e86deccd 100644 --- a/pv/data/decode/decoder.hpp +++ b/pv/data/decode/decoder.hpp @@ -89,6 +89,13 @@ struct DecodeChannel const srd_channel *pdch_; }; +struct DecoderLogicOutputChannel { + DecoderLogicOutputChannel (QString id, QString desc, uint64_t sr) : + id(id), desc(desc), samplerate(sr) {}; + QString id, desc; + uint64_t samplerate; +}; + struct DecodeBinaryClassInfo { uint32_t bin_class_id; @@ -139,6 +146,9 @@ public: uint32_t get_binary_class_count() const; const DecodeBinaryClassInfo* get_binary_class(uint32_t id) const; + bool has_logic_output() const; + const vector logic_output_channels() const; + Q_SIGNALS: void annotation_visibility_changed(); diff --git a/pv/data/decodesignal.cpp b/pv/data/decodesignal.cpp index 91bee1da..a3525922 100644 --- a/pv/data/decodesignal.cpp +++ b/pv/data/decodesignal.cpp @@ -33,6 +33,7 @@ #include #include +using std::dynamic_pointer_cast; using std::lock_guard; using std::make_shared; using std::min; @@ -95,6 +96,7 @@ void DecodeSignal::stack_decoder(const srd_decoder *decoder, bool restart_decode stack_config_changed_ = true; auto_assign_signals(dec); commit_decoder_channels(); + update_output_signals(); decoder_stacked((void*)dec.get()); @@ -358,6 +360,53 @@ int DecodeSignal::get_assigned_signal_count() const [](decode::DecodeChannel ch) { return ch.assigned_signal.get(); }); } +void DecodeSignal::update_output_signals() +{ + for (const shared_ptr& dec : stack_) { + assert(dec); + + if (dec->has_logic_output()) { + const vector logic_channels = + dec->logic_output_channels(); + + // All signals of a decoder share the same LogicSegment, so it's + // sufficient to check for the first channel only + const decode::DecoderLogicOutputChannel& first_ch = logic_channels[0]; + + bool ch_exists = false; + for (const shared_ptr& signal : output_signals_) + if (signal->internal_name() == first_ch.id) + ch_exists = true; + + if (!ch_exists) { + shared_ptr logic_data = make_shared(logic_channels.size()); + output_logic_[dec->get_srd_decoder()] = logic_data; + + shared_ptr logic_segment = make_shared( + *logic_data, 0, (logic_data->num_channels() + 7) / 8, first_ch.samplerate); + logic_data->push_segment(logic_segment); + + uint index = 0; + for (const decode::DecoderLogicOutputChannel& logic_ch : logic_channels) { + shared_ptr signal = + make_shared(nullptr, LogicChannel); + signal->set_internal_name(logic_ch.id); + signal->set_name(logic_ch.id); + signal->set_index(index); + signal->set_data(logic_data); + output_signals_.push_back(signal); + session_.add_generated_signal(signal); + index++; + } + } + } + } + + // TODO Delete signals that no longer have a corresponding decoder (also from session) + // TODO Check whether all sample rates are the same as the session's + // TODO Set colors to the same as the decoder's background color +} + void DecodeSignal::set_initial_pin_state(const uint16_t channel_id, const int init_state) { for (decode::DecodeChannel& ch : channels_) @@ -731,6 +780,8 @@ void DecodeSignal::save_settings(QSettings &settings) const settings.endGroup(); } + + // TODO Save logic output signal settings } void DecodeSignal::restore_settings(QSettings &settings) @@ -835,6 +886,9 @@ void DecodeSignal::restore_settings(QSettings &settings) stack_config_changed_ = true; update_channel_list(); commit_decoder_channels(); + update_output_signals(); + + // TODO Restore logic output signal settings begin_decode(); } @@ -1395,6 +1449,9 @@ void DecodeSignal::start_srd_session() srd_pd_output_callback_add(srd_session_, SRD_OUTPUT_BINARY, DecodeSignal::binary_callback, this); + srd_pd_output_callback_add(srd_session_, SRD_OUTPUT_LOGIC, + DecodeSignal::logic_output_callback, this); + srd_session_start(srd_session_); // We just recreated the srd session, so all stack changes are applied now @@ -1644,6 +1701,39 @@ void DecodeSignal::binary_callback(srd_proto_data *pdata, void *decode_signal) ds->new_binary_data(ds->current_segment_id_, (void*)dec, pdb->bin_class); } +void DecodeSignal::logic_output_callback(srd_proto_data *pdata, void *decode_signal) +{ + assert(pdata); + assert(decode_signal); + + DecodeSignal *const ds = (DecodeSignal*)decode_signal; + assert(ds); + + if (ds->decode_interrupt_) + return; + + lock_guard lock(ds->output_mutex_); + + assert(pdata->pdo); + assert(pdata->pdo->di); + const srd_decoder *const decc = pdata->pdo->di->decoder; + assert(decc); + + const srd_proto_data_logic *const pdl = (const srd_proto_data_logic*)pdata->data; + assert(pdl); + + shared_ptr output_logic = ds->output_logic_.at(decc); + shared_ptr last_segment = + dynamic_pointer_cast(output_logic->segments().back()); + assert(last_segment); + + last_segment->append_subsignal_payload(pdl->logic_class, (void*)pdl->data, pdl->size); + + qInfo() << "Received" << pdl->size << "bytes /" << pdl->size \ + << "samples of logic output for class" << pdl->logic_class << "from decoder" \ + << QString::fromUtf8(decc->name); +} + void DecodeSignal::on_capture_state_changed(int state) { // If a new acquisition was started, we need to start decoding from scratch diff --git a/pv/data/decodesignal.hpp b/pv/data/decodesignal.hpp index ab85246a..ca9f9e96 100644 --- a/pv/data/decodesignal.hpp +++ b/pv/data/decodesignal.hpp @@ -121,6 +121,8 @@ public: void assign_signal(const uint16_t channel_id, shared_ptr signal); int get_assigned_signal_count() const; + void update_output_signals(); + void set_initial_pin_state(const uint16_t channel_id, const int init_state); virtual double get_samplerate() const; @@ -216,6 +218,7 @@ private: static void annotation_callback(srd_proto_data *pdata, void *decode_signal); static void binary_callback(srd_proto_data *pdata, void *decode_signal); + static void logic_output_callback(srd_proto_data *pdata, void *decode_signal); Q_SIGNALS: void decoder_stacked(void* decoder); ///< decoder is of type decode::Decoder* @@ -260,6 +263,9 @@ private: atomic decode_interrupt_, logic_mux_interrupt_; bool decode_paused_; + + map> output_logic_; + vector< shared_ptr> output_signals_; }; } // namespace data diff --git a/pv/data/logicsegment.cpp b/pv/data/logicsegment.cpp index d0009227..354c14f8 100644 --- a/pv/data/logicsegment.cpp +++ b/pv/data/logicsegment.cpp @@ -363,6 +363,29 @@ void LogicSegment::append_payload(void *data, uint64_t data_size) prev_sample_count + 1, prev_sample_count + 1); } +void LogicSegment::append_subsignal_payload(unsigned int index, void *data, uint64_t data_size) +{ + static vector merged_data; // Using static also places it on the heap + + for (uint64_t i = 0; i < data_size * unit_size_; i++) + merged_data.emplace_back(0); + + // Set the bits for this sub-signal where needed + // Note: the bytes in *data must either be 0 or 1, nothing else + unsigned int index_byte = index / 8; + for (uint64_t i = 0; i < data_size; i++) { + unsigned int offs = i * unit_size_ + index_byte; + uint8_t* data_byte = merged_data.data() + offs; + *data_byte |= *((uint8_t*)data + i) << index; + } + + if (index == owner_.num_channels() - 1) { + // We gathered sample data of all sub-signals, let's append it + append_payload(merged_data.data(), merged_data.size()); + merged_data.clear(); + } +} + void LogicSegment::get_samples(int64_t start_sample, int64_t end_sample, uint8_t* dest) const { diff --git a/pv/data/logicsegment.hpp b/pv/data/logicsegment.hpp index 2e37ed2d..2b67c0ed 100644 --- a/pv/data/logicsegment.hpp +++ b/pv/data/logicsegment.hpp @@ -86,6 +86,18 @@ public: void append_payload(shared_ptr logic); void append_payload(void *data, uint64_t data_size); + /** + * Appends sample data for a single channel where each byte + * represents one sample - if it's 0 the state is low, if 1 high. + * Other values are not permitted. + * Assumes that all channels are having samples added and in the + * order of 0..n, not n..0. + * Also assumes the the number of samples added for each channel + * is constant for every invokation for 0..n. The number of samples + * hence may only change when index is 0. + */ + void append_subsignal_payload(unsigned int index, void *data, uint64_t data_size); + void get_samples(int64_t start_sample, int64_t end_sample, uint8_t* dest) const; /** diff --git a/pv/views/viewbase.hpp b/pv/views/viewbase.hpp index eff96231..972b04ea 100644 --- a/pv/views/viewbase.hpp +++ b/pv/views/viewbase.hpp @@ -91,15 +91,12 @@ public: vector< shared_ptr > signalbases() const; virtual void clear_signalbases(); - virtual void add_signalbase(const shared_ptr signalbase); virtual void remove_signalbase(const shared_ptr signalbase); #ifdef ENABLE_DECODE virtual void clear_decode_signals(); - virtual void add_decode_signal(shared_ptr signal); - virtual void remove_decode_signal(shared_ptr signal); #endif