+uint8_t SignalBase::convert_a2l_threshold(float threshold, float value)
+{
+ return (value >= threshold) ? 1 : 0;
+}
+
+uint8_t SignalBase::convert_a2l_schmitt_trigger(float lo_thr, float hi_thr,
+ float value, uint8_t &state)
+{
+ if (value < lo_thr)
+ state = 0;
+ else if (value > hi_thr)
+ state = 1;
+
+ return state;
+}
+
+void SignalBase::conversion_thread_proc(QObject* segment, uint64_t start_sample,
+ uint64_t end_sample)
+{
+ const uint64_t block_size = 4096;
+
+ // TODO Support for multiple segments is missing
+
+ if ((channel_type_ == AnalogChannel) &&
+ ((conversion_type_ == A2LConversionByTreshold) ||
+ (conversion_type_ == A2LConversionBySchmittTrigger))) {
+
+ AnalogSegment *asegment = qobject_cast<AnalogSegment*>(segment);
+
+ // Create the logic data container if needed
+ shared_ptr<Logic> logic_data;
+ if (!converted_data_) {
+ logic_data = make_shared<Logic>(1); // Contains only one channel
+ converted_data_ = logic_data;
+ } else
+ logic_data = dynamic_pointer_cast<Logic>(converted_data_);
+
+ // Create the initial logic data segment if needed
+ if (logic_data->segments().size() == 0) {
+ shared_ptr<LogicSegment> lsegment =
+ make_shared<LogicSegment>(*logic_data.get(), 1, asegment->samplerate());
+ logic_data->push_segment(lsegment);
+ }
+
+ LogicSegment *lsegment = dynamic_cast<LogicSegment*>(logic_data->segments().front().get());
+
+ // start_sample=end_sample=0 means we need to figure out the unprocessed range
+ if ((start_sample == 0) && (end_sample == 0)) {
+ start_sample = lsegment->get_sample_count();
+ end_sample = asegment->get_sample_count();
+ }
+
+ if (start_sample == end_sample)
+ return; // Nothing to do
+
+ float min_v, max_v;
+ tie(min_v, max_v) = asegment->get_min_max();
+
+ vector<uint8_t> lsamples;
+ lsamples.reserve(block_size);
+
+ uint64_t i = start_sample;
+
+ if (conversion_type_ == A2LConversionByTreshold) {
+ const float threshold = (min_v + max_v) * 0.5; // middle between min and max
+
+ // Convert as many sample blocks as we can
+ while ((end_sample - i) > block_size) {
+ const float* asamples = asegment->get_samples(i, i + block_size);
+ for (uint32_t j = 0; j < block_size; j++)
+ lsamples.push_back(convert_a2l_threshold(threshold, asamples[j]));
+ lsegment->append_payload(lsamples.data(), lsamples.size());
+ i += block_size;
+ lsamples.clear();
+ delete[] asamples;
+ }
+
+ // Convert remaining samples
+ const float* asamples = asegment->get_samples(i, end_sample);
+ for (uint32_t j = 0; j < (end_sample - i); j++)
+ lsamples.push_back(convert_a2l_threshold(threshold, asamples[j]));
+ lsegment->append_payload(lsamples.data(), lsamples.size());
+ delete[] asamples;
+
+ samples_added(lsegment, start_sample, end_sample);
+ }
+
+ if (conversion_type_ == A2LConversionBySchmittTrigger) {
+ const float amplitude = max_v - min_v;
+ const float lo_thr = min_v + (amplitude * 0.1); // 10% above min
+ const float hi_thr = max_v - (amplitude * 0.1); // 10% below max
+ uint8_t state = 0; // TODO Use value of logic sample n-1 instead of 0
+
+ // Convert as many sample blocks as we can
+ while ((end_sample - i) > block_size) {
+ const float* asamples = asegment->get_samples(i, i + block_size);
+ for (uint32_t j = 0; j < block_size; j++)
+ lsamples.push_back(convert_a2l_schmitt_trigger(lo_thr, hi_thr, asamples[j], state));
+ lsegment->append_payload(lsamples.data(), lsamples.size());
+ i += block_size;
+ lsamples.clear();
+ delete[] asamples;
+ }
+
+ // Convert remaining samples
+ const float* asamples = asegment->get_samples(i, end_sample);
+ for (uint32_t j = 0; j < (end_sample - i); j++)
+ lsamples.push_back(convert_a2l_schmitt_trigger(lo_thr, hi_thr, asamples[j], state));
+ lsegment->append_payload(lsamples.data(), lsamples.size());
+ delete[] asamples;
+
+ samples_added(lsegment, start_sample, end_sample);
+ }
+ }
+}
+
+void SignalBase::on_samples_cleared()
+{
+ if (converted_data_)
+ converted_data_->clear();
+
+ samples_cleared();
+}
+
+void SignalBase::on_samples_added(QObject* segment, uint64_t start_sample,
+ uint64_t end_sample)
+{
+ if (conversion_type_ != NoConversion) {
+
+ // Wait for the currently ongoing conversion to finish
+ if (conversion_thread_.joinable())
+ conversion_thread_.join();
+
+ conversion_thread_ = std::thread(
+ &SignalBase::conversion_thread_proc, this,
+ segment, start_sample, end_sample);
+ }
+
+ samples_added(segment, start_sample, end_sample);
+}
+
+void SignalBase::on_capture_state_changed(int state)
+{
+ return;
+ if (state == Session::Stopped) {
+ // Make sure that all data is converted
+
+ if ((channel_type_ == AnalogChannel) &&
+ ((conversion_type_ == A2LConversionByTreshold) ||
+ (conversion_type_ == A2LConversionBySchmittTrigger))) {
+
+ shared_ptr<Analog> analog_data = dynamic_pointer_cast<Analog>(data_);
+
+ if (analog_data->analog_segments().size() > 0) {
+ // TODO Support for multiple segments is missing
+ AnalogSegment *asegment = analog_data->analog_segments().front().get();
+
+ if (conversion_thread_.joinable())
+ conversion_thread_.join();
+
+ conversion_thread_ = std::thread(
+ &SignalBase::conversion_thread_proc, this, asegment, 0, 0);
+ }
+ }
+ }
+}