]>
Commit | Line | Data |
---|---|---|
aba1dd16 JH |
1 | /* |
2 | * This file is part of the PulseView project. | |
3 | * | |
4 | * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk> | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; either version 2 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program; if not, write to the Free Software | |
18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
19 | */ | |
20 | ||
21 | #include <extdef.h> | |
22 | ||
3b68d03d JH |
23 | #include <cassert> |
24 | #include <cmath> | |
8a2fafcc JH |
25 | #include <cstdlib> |
26 | #include <limits> | |
aba1dd16 | 27 | |
2acdb232 JH |
28 | #include "analogsignal.hpp" |
29 | #include "pv/data/analog.hpp" | |
f3d66e52 | 30 | #include "pv/data/analogsegment.hpp" |
2acdb232 | 31 | #include "pv/view/view.hpp" |
aba1dd16 | 32 | |
fe3a1c21 | 33 | #include <libsigrokcxx/libsigrokcxx.hpp> |
e8d00928 | 34 | |
819f4c25 | 35 | using std::max; |
a5d93c27 | 36 | using std::make_pair; |
819f4c25 | 37 | using std::min; |
f9abf97e | 38 | using std::shared_ptr; |
819f4c25 | 39 | using std::deque; |
aba1dd16 | 40 | |
e8d00928 ML |
41 | using sigrok::Channel; |
42 | ||
aba1dd16 JH |
43 | namespace pv { |
44 | namespace view { | |
45 | ||
a5d93c27 JH |
46 | const int AnalogSignal::NominalHeight = 80; |
47 | ||
568b90d4 JH |
48 | const QColor AnalogSignal::SignalColours[4] = { |
49 | QColor(0xC4, 0xA0, 0x00), // Yellow | |
50 | QColor(0x87, 0x20, 0x7A), // Magenta | |
51 | QColor(0x20, 0x4A, 0x87), // Blue | |
52 | QColor(0x4E, 0x9A, 0x06) // Green | |
53 | }; | |
54 | ||
7c68ddae JH |
55 | const float AnalogSignal::EnvelopeThreshold = 256.0f; |
56 | ||
b86aa8f4 | 57 | AnalogSignal::AnalogSignal( |
2b81ae46 | 58 | pv::Session &session, |
b86aa8f4 | 59 | shared_ptr<Channel> channel, |
e8d00928 | 60 | shared_ptr<data::Analog> data) : |
b86aa8f4 | 61 | Signal(session, channel), |
8dbbc7f0 | 62 | data_(data), |
8a2fafcc JH |
63 | scale_index_(0), |
64 | scale_index_drag_offset_(0) | |
aba1dd16 | 65 | { |
8dbbc7f0 | 66 | colour_ = SignalColours[channel_->index() % countof(SignalColours)]; |
aba1dd16 JH |
67 | } |
68 | ||
f459c540 JH |
69 | AnalogSignal::~AnalogSignal() |
70 | { | |
71 | } | |
72 | ||
3009b5b3 JH |
73 | shared_ptr<pv::data::SignalData> AnalogSignal::data() const |
74 | { | |
8dbbc7f0 | 75 | return data_; |
3009b5b3 JH |
76 | } |
77 | ||
78 | shared_ptr<pv::data::Analog> AnalogSignal::analog_data() const | |
9a0cd293 | 79 | { |
8dbbc7f0 | 80 | return data_; |
9a0cd293 JH |
81 | } |
82 | ||
a5d93c27 JH |
83 | std::pair<int, int> AnalogSignal::v_extents() const |
84 | { | |
8a2fafcc JH |
85 | const int h = NominalHeight / 2; |
86 | return make_pair(-h, h); | |
a5d93c27 JH |
87 | } |
88 | ||
214470fc JH |
89 | int AnalogSignal::scale_handle_offset() const |
90 | { | |
8a2fafcc JH |
91 | return ((scale_index_drag_offset_ - scale_index_) * |
92 | NominalHeight / 4) - NominalHeight / 2; | |
214470fc JH |
93 | } |
94 | ||
95 | void AnalogSignal::scale_handle_dragged(int offset) | |
96 | { | |
8a2fafcc JH |
97 | scale_index_ = scale_index_drag_offset_ - |
98 | (offset + NominalHeight / 2) / (NominalHeight / 4); | |
99 | } | |
100 | ||
101 | void AnalogSignal::scale_handle_drag_release() | |
102 | { | |
103 | scale_index_drag_offset_ = scale_index_; | |
214470fc JH |
104 | } |
105 | ||
5b5fa4da | 106 | void AnalogSignal::paint_back(QPainter &p, const ViewItemPaintParams &pp) |
fe08b6e8 | 107 | { |
8dbbc7f0 | 108 | if (channel_->enabled()) |
97904bf7 | 109 | paint_axis(p, pp, get_visual_y()); |
fe08b6e8 JH |
110 | } |
111 | ||
5b5fa4da | 112 | void AnalogSignal::paint_mid(QPainter &p, const ViewItemPaintParams &pp) |
aba1dd16 | 113 | { |
8dbbc7f0 | 114 | assert(data_); |
8dbbc7f0 | 115 | assert(owner_); |
a8acb46e | 116 | |
be9e7b4b | 117 | const int y = get_visual_y(); |
01fd3263 | 118 | |
8dbbc7f0 | 119 | if (!channel_->enabled()) |
cec48d16 JH |
120 | return; |
121 | ||
f3d66e52 JH |
122 | const deque< shared_ptr<pv::data::AnalogSegment> > &segments = |
123 | data_->analog_segments(); | |
124 | if (segments.empty()) | |
a8acb46e JH |
125 | return; |
126 | ||
f3d66e52 JH |
127 | const shared_ptr<pv::data::AnalogSegment> &segment = |
128 | segments.front(); | |
a8acb46e | 129 | |
4c8a6a6d | 130 | const double pixels_offset = pp.pixels_offset(); |
f3d66e52 | 131 | const double samplerate = segment->samplerate(); |
60d9b99a | 132 | const pv::util::Timestamp& start_time = segment->start_time(); |
f3d66e52 | 133 | const int64_t last_sample = segment->get_sample_count() - 1; |
4c8a6a6d | 134 | const double samples_per_pixel = samplerate * pp.scale(); |
60d9b99a JS |
135 | const pv::util::Timestamp start = samplerate * (pp.offset() - start_time); |
136 | const pv::util::Timestamp end = start + samples_per_pixel * pp.width(); | |
a8acb46e | 137 | |
60d9b99a | 138 | const int64_t start_sample = min(max(floor(start).convert_to<int64_t>(), |
a8acb46e | 139 | (int64_t)0), last_sample); |
60d9b99a | 140 | const int64_t end_sample = min(max((ceil(end) + 1).convert_to<int64_t>(), |
a8acb46e JH |
141 | (int64_t)0), last_sample); |
142 | ||
7c68ddae | 143 | if (samples_per_pixel < EnvelopeThreshold) |
f3d66e52 | 144 | paint_trace(p, segment, y, pp.left(), |
7c68ddae JH |
145 | start_sample, end_sample, |
146 | pixels_offset, samples_per_pixel); | |
147 | else | |
f3d66e52 | 148 | paint_envelope(p, segment, y, pp.left(), |
7c68ddae JH |
149 | start_sample, end_sample, |
150 | pixels_offset, samples_per_pixel); | |
151 | } | |
152 | ||
153 | void AnalogSignal::paint_trace(QPainter &p, | |
f3d66e52 | 154 | const shared_ptr<pv::data::AnalogSegment> &segment, |
7c68ddae JH |
155 | int y, int left, const int64_t start, const int64_t end, |
156 | const double pixels_offset, const double samples_per_pixel) | |
157 | { | |
8a2fafcc | 158 | const float scale = this->scale(); |
7c68ddae JH |
159 | const int64_t sample_count = end - start; |
160 | ||
f3d66e52 | 161 | const float *const samples = segment->get_samples(start, end); |
a8acb46e JH |
162 | assert(samples); |
163 | ||
8dbbc7f0 | 164 | p.setPen(colour_); |
7c68ddae | 165 | |
d3758367 | 166 | QPointF *points = new QPointF[sample_count]; |
a8acb46e JH |
167 | QPointF *point = points; |
168 | ||
7c68ddae | 169 | for (int64_t sample = start; sample != end; sample++) { |
a8acb46e | 170 | const float x = (sample / samples_per_pixel - |
2658961b | 171 | pixels_offset) + left; |
d3758367 | 172 | *point++ = QPointF(x, |
8a2fafcc | 173 | y - samples[sample - start] * scale); |
a8acb46e JH |
174 | } |
175 | ||
306d43a7 | 176 | p.drawPolyline(points, point - points); |
a8acb46e | 177 | |
7c68ddae | 178 | delete[] samples; |
a8acb46e | 179 | delete[] points; |
aba1dd16 JH |
180 | } |
181 | ||
7c68ddae | 182 | void AnalogSignal::paint_envelope(QPainter &p, |
f3d66e52 | 183 | const shared_ptr<pv::data::AnalogSegment> &segment, |
7c68ddae JH |
184 | int y, int left, const int64_t start, const int64_t end, |
185 | const double pixels_offset, const double samples_per_pixel) | |
186 | { | |
f3d66e52 | 187 | using pv::data::AnalogSegment; |
7c68ddae | 188 | |
8a2fafcc JH |
189 | const float scale = this->scale(); |
190 | ||
f3d66e52 JH |
191 | AnalogSegment::EnvelopeSection e; |
192 | segment->get_envelope_section(e, start, end, samples_per_pixel); | |
7c68ddae JH |
193 | |
194 | if (e.length < 2) | |
195 | return; | |
196 | ||
819f4c25 | 197 | p.setPen(QPen(Qt::NoPen)); |
8dbbc7f0 | 198 | p.setBrush(colour_); |
7c68ddae JH |
199 | |
200 | QRectF *const rects = new QRectF[e.length]; | |
201 | QRectF *rect = rects; | |
202 | ||
f3290553 | 203 | for (uint64_t sample = 0; sample < e.length-1; sample++) { |
7c68ddae JH |
204 | const float x = ((e.scale * sample + e.start) / |
205 | samples_per_pixel - pixels_offset) + left; | |
f3d66e52 | 206 | const AnalogSegment::EnvelopeSample *const s = |
7c68ddae JH |
207 | e.samples + sample; |
208 | ||
209 | // We overlap this sample with the next so that vertical | |
210 | // gaps do not appear during steep rising or falling edges | |
8a2fafcc JH |
211 | const float b = y - max(s->max, (s+1)->min) * scale; |
212 | const float t = y - min(s->min, (s+1)->max) * scale; | |
7c68ddae JH |
213 | |
214 | float h = b - t; | |
f3290553 | 215 | if (h >= 0.0f && h <= 1.0f) |
7c68ddae | 216 | h = 1.0f; |
f3290553 | 217 | if (h <= 0.0f && h >= -1.0f) |
7c68ddae JH |
218 | h = -1.0f; |
219 | ||
220 | *rect++ = QRectF(x, t, 1.0f, h); | |
221 | } | |
222 | ||
223 | p.drawRects(rects, e.length); | |
224 | ||
225 | delete[] rects; | |
226 | delete[] e.samples; | |
227 | } | |
228 | ||
8a2fafcc JH |
229 | float AnalogSignal::scale() const |
230 | { | |
231 | const float seq[] = {1.0f, 2.0f, 5.0f}; | |
232 | const int offset = std::numeric_limits<int>::max() / (2 * countof(seq)); | |
233 | const std::div_t d = std::div( | |
234 | (int)(scale_index_ + countof(seq) * offset), | |
235 | countof(seq)); | |
236 | return powf(10.0f, d.quot - offset) * seq[d.rem]; | |
237 | } | |
238 | ||
aba1dd16 JH |
239 | } // namespace view |
240 | } // namespace pv |