]> sigrok.org Git - pulseview.git/commitdiff
AnalogSignal: Use correct scaling factor for the grid to work
authorSoeren Apel <redacted>
Thu, 7 Apr 2016 20:03:05 +0000 (22:03 +0200)
committerSoeren Apel <redacted>
Thu, 7 Apr 2016 20:03:05 +0000 (22:03 +0200)
pv/view/analogsignal.cpp
pv/view/analogsignal.hpp

index b340de67edc223f16dd7cbe823a917d4b422788b..b92792d7f6e630f431f8c14430a20bcec89ea3f5 100644 (file)
@@ -63,12 +63,14 @@ AnalogSignal::AnalogSignal(
        shared_ptr<data::Analog> data) :
        Signal(session, channel),
        data_(data),
        shared_ptr<data::Analog> data) :
        Signal(session, channel),
        data_(data),
-       scale_index_(0),
+       scale_index_(4), // 20 per div
        scale_index_drag_offset_(0),
        div_height_(3 * QFontMetrics(QApplication::font()).height()),
        scale_index_drag_offset_(0),
        div_height_(3 * QFontMetrics(QApplication::font()).height()),
-       vdivs_(1)
+       vdivs_(1),
+       resolution_(0)
 {
        set_colour(SignalColours[channel_->index() % countof(SignalColours)]);
 {
        set_colour(SignalColours[channel_->index() % countof(SignalColours)]);
+       update_scale();
 }
 
 shared_ptr<pv::data::SignalData> AnalogSignal::data() const
 }
 
 shared_ptr<pv::data::SignalData> AnalogSignal::data() const
@@ -101,11 +103,14 @@ void AnalogSignal::scale_handle_dragged(int offset)
 
        scale_index_ = scale_index_drag_offset_ -
                (offset + h / 2) / (h / 4);
 
        scale_index_ = scale_index_drag_offset_ -
                (offset + h / 2) / (h / 4);
+
+       update_scale();
 }
 
 void AnalogSignal::scale_handle_drag_release()
 {
        scale_index_drag_offset_ = scale_index_;
 }
 
 void AnalogSignal::scale_handle_drag_release()
 {
        scale_index_drag_offset_ = scale_index_;
+       update_scale();
 }
 
 void AnalogSignal::paint_back(QPainter &p, const ViewItemPaintParams &pp)
 }
 
 void AnalogSignal::paint_back(QPainter &p, const ViewItemPaintParams &pp)
@@ -188,7 +193,6 @@ void AnalogSignal::paint_trace(QPainter &p,
        int y, int left, const int64_t start, const int64_t end,
        const double pixels_offset, const double samples_per_pixel)
 {
        int y, int left, const int64_t start, const int64_t end,
        const double pixels_offset, const double samples_per_pixel)
 {
-       const float scale = this->scale();
        const int64_t sample_count = end - start;
 
        const float *const samples = segment->get_samples(start, end);
        const int64_t sample_count = end - start;
 
        const float *const samples = segment->get_samples(start, end);
@@ -203,7 +207,7 @@ void AnalogSignal::paint_trace(QPainter &p,
                const float x = (sample / samples_per_pixel -
                        pixels_offset) + left;
                *point++ = QPointF(x,
                const float x = (sample / samples_per_pixel -
                        pixels_offset) + left;
                *point++ = QPointF(x,
-                       y - samples[sample - start] * scale);
+                       y - samples[sample - start] * scale_);
        }
 
        p.drawPolyline(points, point - points);
        }
 
        p.drawPolyline(points, point - points);
@@ -219,8 +223,6 @@ void AnalogSignal::paint_envelope(QPainter &p,
 {
        using pv::data::AnalogSegment;
 
 {
        using pv::data::AnalogSegment;
 
-       const float scale = this->scale();
-
        AnalogSegment::EnvelopeSection e;
        segment->get_envelope_section(e, start, end, samples_per_pixel);
 
        AnalogSegment::EnvelopeSection e;
        segment->get_envelope_section(e, start, end, samples_per_pixel);
 
@@ -241,8 +243,8 @@ void AnalogSignal::paint_envelope(QPainter &p,
 
                // We overlap this sample with the next so that vertical
                // gaps do not appear during steep rising or falling edges
 
                // 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;
+               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)
 
                float h = b - t;
                if (h >= 0.0f && h <= 1.0f)
@@ -259,14 +261,17 @@ void AnalogSignal::paint_envelope(QPainter &p,
        delete[] e.samples;
 }
 
        delete[] e.samples;
 }
 
-float AnalogSignal::scale() const
+void AnalogSignal::update_scale()
 {
        const float seq[] = {1.0f, 2.0f, 5.0f};
 {
        const float seq[] = {1.0f, 2.0f, 5.0f};
+
        const int offset = std::numeric_limits<int>::max() / (2 * countof(seq));
        const std::div_t d = std::div(
                (int)(scale_index_ + countof(seq) * offset),
                countof(seq));
        const int offset = std::numeric_limits<int>::max() / (2 * countof(seq));
        const std::div_t d = std::div(
                (int)(scale_index_ + countof(seq) * offset),
                countof(seq));
-       return powf(10.0f, d.quot - offset) * seq[d.rem];
+
+       resolution_ = powf(10.0f, d.quot - offset) * seq[d.rem];
+       scale_ = div_height_ / resolution_;
 }
 
 } // namespace view
 }
 
 } // namespace view
index dd2b31dc74f20e4d536accc5652c125af29d5947..7fdddf2d16880678f222c845fd306b2ee4ce4e31 100644 (file)
@@ -103,18 +103,20 @@ private:
                const double pixels_offset, const double samples_per_pixel);
 
        /**
                const double pixels_offset, const double samples_per_pixel);
 
        /**
-        * Computes the scale factor from the scale index.
+        * Computes the scale factor from the scale index and vdiv settings.
         */
         */
-       float scale() const;
+       void update_scale();
 
 private:
        std::shared_ptr<pv::data::Analog> data_;
 
 
 private:
        std::shared_ptr<pv::data::Analog> data_;
 
+       float scale_;
        int scale_index_;
        int scale_index_drag_offset_;
 
        int div_height_;
        int vdivs_;  // divs per positive/negative side
        int scale_index_;
        int scale_index_drag_offset_;
 
        int div_height_;
        int vdivs_;  // divs per positive/negative side
+       float resolution_; // e.g. 10 for 10 V/div
 };
 
 } // namespace view
 };
 
 } // namespace view