]> sigrok.org Git - pulseview.git/commitdiff
Added analog envelope painting
authorJoel Holdsworth <redacted>
Sat, 16 Mar 2013 17:30:51 +0000 (17:30 +0000)
committerJoel Holdsworth <redacted>
Thu, 21 Mar 2013 22:44:19 +0000 (22:44 +0000)
pv/view/analogsignal.cpp
pv/view/analogsignal.h

index 75b5d3dfd245bcc5b982a4545245461b7cf627af..e29930865f6601eccf8b43986db1f44f9523b090 100644 (file)
@@ -39,6 +39,8 @@ const QColor AnalogSignal::SignalColours[4] = {
        QColor(0x4E, 0x9A, 0x06)        // Green
 };
 
+const float AnalogSignal::EnvelopeThreshold = 256.0f;
+
 AnalogSignal::AnalogSignal(QString name, shared_ptr<data::Analog> data,
        int probe_index) :
        Signal(name),
@@ -82,28 +84,90 @@ void AnalogSignal::paint(QPainter &p, int y, int left, int right, double scale,
                (int64_t)0), last_sample);
        const int64_t end_sample = min(max((int64_t)ceil(end),
                (int64_t)0), last_sample);
-       const int64_t sample_count = end_sample - start_sample;
 
-       const float* samples = snapshot->get_samples(
-               start_sample, end_sample);
+       if (samples_per_pixel < EnvelopeThreshold)
+               paint_trace(p, snapshot, y, left,
+                       start_sample, end_sample,
+                       pixels_offset, samples_per_pixel);
+       else
+               paint_envelope(p, snapshot, y, left,
+                       start_sample, end_sample,
+                       pixels_offset, samples_per_pixel);
+}
+
+void AnalogSignal::paint_trace(QPainter &p,
+       const shared_ptr<pv::data::AnalogSnapshot> &snapshot,
+       int y, int left, const int64_t start, const int64_t end,
+       const double pixels_offset, const double samples_per_pixel)
+{
+       const int64_t sample_count = end - start;
+
+       const float *const samples = snapshot->get_samples(start, end);
        assert(samples);
 
+       p.setPen(_colour);
+
        QPointF *points = new QPointF[sample_count];
        QPointF *point = points;
 
-       for (int64_t sample = start_sample;
-               sample != end_sample; sample++) {
+       for (int64_t sample = start; sample != end; sample++) {
                const float x = (sample / samples_per_pixel -
                        pixels_offset) + left;
                *point++ = QPointF(x,
-                       y - samples[sample - start_sample] * _scale);
+                       y - samples[sample - start] * _scale);
        }
 
-       p.setPen(_colour);
        p.drawPoints(points, point - points);
 
+       delete[] samples;
        delete[] points;
 }
 
+void AnalogSignal::paint_envelope(QPainter &p,
+       const shared_ptr<pv::data::AnalogSnapshot> &snapshot,
+       int y, int left, const int64_t start, const int64_t end,
+       const double pixels_offset, const double samples_per_pixel)
+{
+       using namespace Qt;
+       using pv::data::AnalogSnapshot;
+
+       AnalogSnapshot::EnvelopeSection e;
+       snapshot->get_envelope_section(e, start, end, samples_per_pixel);
+
+       if (e.length < 2)
+               return;
+
+       p.setPen(QPen(NoPen));
+       p.setBrush(_colour);
+
+       QRectF *const rects = new QRectF[e.length];
+       QRectF *rect = rects;
+
+       for(uint64_t sample = 0; sample < e.length-1; sample++) {
+               const float x = ((e.scale * sample + e.start) /
+                       samples_per_pixel - pixels_offset) + left;
+               const AnalogSnapshot::EnvelopeSample *const s =
+                       e.samples + sample;
+
+               // We overlap this sample with the next so that vertical
+               // gaps do not appear during steep rising or falling edges
+               const float b = y - max(s->max, (s+1)->min) * _scale;
+               const float t = y - min(s->min, (s+1)->max) * _scale;
+
+               float h = b - t;
+               if(h >= 0.0f && h <= 1.0f)
+                       h = 1.0f;
+               if(h <= 0.0f && h >= -1.0f)
+                       h = -1.0f;
+
+               *rect++ = QRectF(x, t, 1.0f, h);
+       }
+
+       p.drawRects(rects, e.length);
+
+       delete[] rects;
+       delete[] e.samples;
+}
+
 } // namespace view
 } // namespace pv
index bb4a2109c0bd4d8d461434afae9a552ce39645da..ce1dff105041a77934355f302e5a80168f1cdbff 100644 (file)
@@ -29,6 +29,7 @@ namespace pv {
 
 namespace data {
 class Analog;
+class AnalogSnapshot;
 }
 
 namespace view {
@@ -38,6 +39,8 @@ class AnalogSignal : public Signal
 private:
        static const QColor SignalColours[4];
 
+       static const float EnvelopeThreshold;
+
 public:
        AnalogSignal(QString name,
                boost::shared_ptr<pv::data::Analog> data, int probe_index);
@@ -57,6 +60,17 @@ public:
        void paint(QPainter &p, int y, int left, int right, double scale,
                double offset);
 
+private:
+       void paint_trace(QPainter &p,
+               const boost::shared_ptr<pv::data::AnalogSnapshot> &snapshot,
+               int y, int left, const int64_t start, const int64_t end,
+               const double pixels_offset, const double samples_per_pixel);
+
+       void paint_envelope(QPainter &p,
+               const boost::shared_ptr<pv::data::AnalogSnapshot> &snapshot,
+               int y, int left, const int64_t start, const int64_t end,
+               const double pixels_offset, const double samples_per_pixel);
+
 private:
        boost::shared_ptr<pv::data::Analog> _data;
        float _scale;