From: Joel Holdsworth Date: Mon, 17 Dec 2012 21:54:48 +0000 (+0000) Subject: Added basic analog signal plotting X-Git-Tag: pulseview-0.1.0~191 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=a8acb46e05a84d7487e2cf7c914ba82215f812cc;p=pulseview.git Added basic analog signal plotting --- diff --git a/pv/analogdatasnapshot.cpp b/pv/analogdatasnapshot.cpp index 2203bb57..c66e9b9c 100644 --- a/pv/analogdatasnapshot.cpp +++ b/pv/analogdatasnapshot.cpp @@ -46,8 +46,12 @@ void AnalogDataSnapshot::append_payload( const sr_datafeed_analog &analog) { lock_guard lock(_mutex); - append_data(analog.data, analog.num_samples); } +const float* AnalogDataSnapshot::get_samples() const +{ + return (const float*)_data; +} + } // namespace pv diff --git a/pv/analogdatasnapshot.h b/pv/analogdatasnapshot.h index 03184c0f..ce8a99da 100644 --- a/pv/analogdatasnapshot.h +++ b/pv/analogdatasnapshot.h @@ -34,6 +34,8 @@ public: AnalogDataSnapshot(const sr_datafeed_analog &analog); void append_payload(const sr_datafeed_analog &analog); + + const float* get_samples() const; }; } // namespace pv diff --git a/pv/view/analogsignal.cpp b/pv/view/analogsignal.cpp index 73ab1686..126ecb1f 100644 --- a/pv/view/analogsignal.cpp +++ b/pv/view/analogsignal.cpp @@ -42,11 +42,54 @@ AnalogSignal::AnalogSignal(QString name, shared_ptr data) : void AnalogSignal::paint(QPainter &p, const QRect &rect, double scale, double offset) { + assert(scale > 0); + assert(_data); + + const deque< shared_ptr > &snapshots = + _data->get_snapshots(); + if (snapshots.empty()) + return; + + const shared_ptr &snapshot = + snapshots.front(); + + const double pixels_offset = offset / scale; + const double samplerate = _data->get_samplerate(); + const double start_time = _data->get_start_time(); + const int64_t last_sample = snapshot->get_sample_count() - 1; + const double samples_per_pixel = samplerate * scale; + const double start = samplerate * (offset - start_time); + const double end = start + samples_per_pixel * rect.width(); + + const int64_t start_sample = min(max((int64_t)floor(start), + (int64_t)0), last_sample); + const int64_t end_sample = min(max((int64_t)ceil(end), + (int64_t)0), last_sample); + + const int y_offset = rect.center().y(); + + const float* samples = snapshot->get_samples(); + assert(samples); + + QPointF *points = new QPointF[end_sample - start_sample]; + QPointF *point = points; + + for (int64_t sample = start_sample; + sample != end_sample; sample++) { + const float x = (sample / samples_per_pixel - + pixels_offset) + rect.left(); + const float y = samples[sample] + y_offset; + *point++ = QPointF(x, y); + } + + p.drawPoints(points, point - points); + + delete[] points; } int AnalogSignal::get_nominal_offset(const QRect &rect) const { - return rect.bottom(); + return rect.center().y(); } } // namespace view