]> sigrok.org Git - pulseview.git/commitdiff
Added basic analog signal plotting
authorJoel Holdsworth <redacted>
Mon, 17 Dec 2012 21:54:48 +0000 (21:54 +0000)
committerJoel Holdsworth <redacted>
Thu, 20 Dec 2012 08:02:44 +0000 (08:02 +0000)
pv/analogdatasnapshot.cpp
pv/analogdatasnapshot.h
pv/view/analogsignal.cpp

index 2203bb572b4052abdd5864fd92a6504ae1297a74..c66e9b9c69c57350b40ce0747f199b6561de2121 100644 (file)
@@ -46,8 +46,12 @@ void AnalogDataSnapshot::append_payload(
        const sr_datafeed_analog &analog)
 {
        lock_guard<recursive_mutex> lock(_mutex);
-
        append_data(analog.data, analog.num_samples);
 }
 
+const float* AnalogDataSnapshot::get_samples() const
+{
+       return (const float*)_data;
+}
+
 } // namespace pv
index 03184c0f3eb27253bc1bf63f47e5055dec71fb8b..ce8a99da2924e4cf6ac6adbd2fd361b904101743 100644 (file)
@@ -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
index 73ab168685e28e531bb7aff9927afbddb1fa4df8..126ecb1f0b1330515c774f2f69fce673b3268ab4 100644 (file)
@@ -42,11 +42,54 @@ AnalogSignal::AnalogSignal(QString name, shared_ptr<AnalogData> data) :
 void AnalogSignal::paint(QPainter &p, const QRect &rect, double scale,
        double offset)
 {
+       assert(scale > 0);
+       assert(_data);
+
+       const deque< shared_ptr<pv::AnalogDataSnapshot> > &snapshots =
+               _data->get_snapshots();
+       if (snapshots.empty())
+               return;
+
+       const shared_ptr<pv::AnalogDataSnapshot> &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