]>
Commit | Line | Data |
---|---|---|
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 | ||
23 | #include <cassert> | |
24 | #include <cmath> | |
25 | #include <cstdlib> | |
26 | #include <limits> | |
27 | ||
28 | #include "analogsignal.hpp" | |
29 | #include "pv/data/analog.hpp" | |
30 | #include "pv/data/analogsegment.hpp" | |
31 | #include "pv/view/view.hpp" | |
32 | ||
33 | #include <libsigrokcxx/libsigrokcxx.hpp> | |
34 | ||
35 | using std::max; | |
36 | using std::make_pair; | |
37 | using std::min; | |
38 | using std::shared_ptr; | |
39 | using std::deque; | |
40 | ||
41 | using sigrok::Channel; | |
42 | ||
43 | namespace pv { | |
44 | namespace view { | |
45 | ||
46 | const int AnalogSignal::NominalHeight = 80; | |
47 | ||
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 | ||
55 | const float AnalogSignal::EnvelopeThreshold = 256.0f; | |
56 | ||
57 | AnalogSignal::AnalogSignal( | |
58 | pv::Session &session, | |
59 | shared_ptr<Channel> channel, | |
60 | shared_ptr<data::Analog> data) : | |
61 | Signal(session, channel), | |
62 | data_(data), | |
63 | scale_index_(0), | |
64 | scale_index_drag_offset_(0) | |
65 | { | |
66 | set_colour(SignalColours[channel_->index() % countof(SignalColours)]); | |
67 | } | |
68 | ||
69 | shared_ptr<pv::data::SignalData> AnalogSignal::data() const | |
70 | { | |
71 | return data_; | |
72 | } | |
73 | ||
74 | shared_ptr<pv::data::Analog> AnalogSignal::analog_data() const | |
75 | { | |
76 | return data_; | |
77 | } | |
78 | ||
79 | std::pair<int, int> AnalogSignal::v_extents() const | |
80 | { | |
81 | const int h = NominalHeight / 2; | |
82 | return make_pair(-h, h); | |
83 | } | |
84 | ||
85 | int AnalogSignal::scale_handle_offset() const | |
86 | { | |
87 | return ((scale_index_drag_offset_ - scale_index_) * | |
88 | NominalHeight / 4) - NominalHeight / 2; | |
89 | } | |
90 | ||
91 | void AnalogSignal::scale_handle_dragged(int offset) | |
92 | { | |
93 | scale_index_ = scale_index_drag_offset_ - | |
94 | (offset + NominalHeight / 2) / (NominalHeight / 4); | |
95 | } | |
96 | ||
97 | void AnalogSignal::scale_handle_drag_release() | |
98 | { | |
99 | scale_index_drag_offset_ = scale_index_; | |
100 | } | |
101 | ||
102 | void AnalogSignal::paint_back(QPainter &p, const ViewItemPaintParams &pp) | |
103 | { | |
104 | if (channel_->enabled()) { | |
105 | Trace::paint_back(p, pp); | |
106 | paint_axis(p, pp, get_visual_y()); | |
107 | } | |
108 | } | |
109 | ||
110 | void AnalogSignal::paint_mid(QPainter &p, const ViewItemPaintParams &pp) | |
111 | { | |
112 | assert(data_); | |
113 | assert(owner_); | |
114 | ||
115 | const int y = get_visual_y(); | |
116 | ||
117 | if (!channel_->enabled()) | |
118 | return; | |
119 | ||
120 | const deque< shared_ptr<pv::data::AnalogSegment> > &segments = | |
121 | data_->analog_segments(); | |
122 | if (segments.empty()) | |
123 | return; | |
124 | ||
125 | const shared_ptr<pv::data::AnalogSegment> &segment = | |
126 | segments.front(); | |
127 | ||
128 | const double pixels_offset = pp.pixels_offset(); | |
129 | const double samplerate = max(1.0, segment->samplerate()); | |
130 | const pv::util::Timestamp& start_time = segment->start_time(); | |
131 | const int64_t last_sample = segment->get_sample_count() - 1; | |
132 | const double samples_per_pixel = samplerate * pp.scale(); | |
133 | const pv::util::Timestamp start = samplerate * (pp.offset() - start_time); | |
134 | const pv::util::Timestamp end = start + samples_per_pixel * pp.width(); | |
135 | ||
136 | const int64_t start_sample = min(max(floor(start).convert_to<int64_t>(), | |
137 | (int64_t)0), last_sample); | |
138 | const int64_t end_sample = min(max((ceil(end) + 1).convert_to<int64_t>(), | |
139 | (int64_t)0), last_sample); | |
140 | ||
141 | if (samples_per_pixel < EnvelopeThreshold) | |
142 | paint_trace(p, segment, y, pp.left(), | |
143 | start_sample, end_sample, | |
144 | pixels_offset, samples_per_pixel); | |
145 | else | |
146 | paint_envelope(p, segment, y, pp.left(), | |
147 | start_sample, end_sample, | |
148 | pixels_offset, samples_per_pixel); | |
149 | } | |
150 | ||
151 | void AnalogSignal::paint_trace(QPainter &p, | |
152 | const shared_ptr<pv::data::AnalogSegment> &segment, | |
153 | int y, int left, const int64_t start, const int64_t end, | |
154 | const double pixels_offset, const double samples_per_pixel) | |
155 | { | |
156 | const float scale = this->scale(); | |
157 | const int64_t sample_count = end - start; | |
158 | ||
159 | const float *const samples = segment->get_samples(start, end); | |
160 | assert(samples); | |
161 | ||
162 | p.setPen(colour_); | |
163 | ||
164 | QPointF *points = new QPointF[sample_count]; | |
165 | QPointF *point = points; | |
166 | ||
167 | for (int64_t sample = start; sample != end; sample++) { | |
168 | const float x = (sample / samples_per_pixel - | |
169 | pixels_offset) + left; | |
170 | *point++ = QPointF(x, | |
171 | y - samples[sample - start] * scale); | |
172 | } | |
173 | ||
174 | p.drawPolyline(points, point - points); | |
175 | ||
176 | delete[] samples; | |
177 | delete[] points; | |
178 | } | |
179 | ||
180 | void AnalogSignal::paint_envelope(QPainter &p, | |
181 | const shared_ptr<pv::data::AnalogSegment> &segment, | |
182 | int y, int left, const int64_t start, const int64_t end, | |
183 | const double pixels_offset, const double samples_per_pixel) | |
184 | { | |
185 | using pv::data::AnalogSegment; | |
186 | ||
187 | const float scale = this->scale(); | |
188 | ||
189 | AnalogSegment::EnvelopeSection e; | |
190 | segment->get_envelope_section(e, start, end, samples_per_pixel); | |
191 | ||
192 | if (e.length < 2) | |
193 | return; | |
194 | ||
195 | p.setPen(QPen(Qt::NoPen)); | |
196 | p.setBrush(colour_); | |
197 | ||
198 | QRectF *const rects = new QRectF[e.length]; | |
199 | QRectF *rect = rects; | |
200 | ||
201 | for (uint64_t sample = 0; sample < e.length-1; sample++) { | |
202 | const float x = ((e.scale * sample + e.start) / | |
203 | samples_per_pixel - pixels_offset) + left; | |
204 | const AnalogSegment::EnvelopeSample *const s = | |
205 | e.samples + sample; | |
206 | ||
207 | // We overlap this sample with the next so that vertical | |
208 | // gaps do not appear during steep rising or falling edges | |
209 | const float b = y - max(s->max, (s+1)->min) * scale; | |
210 | const float t = y - min(s->min, (s+1)->max) * scale; | |
211 | ||
212 | float h = b - t; | |
213 | if (h >= 0.0f && h <= 1.0f) | |
214 | h = 1.0f; | |
215 | if (h <= 0.0f && h >= -1.0f) | |
216 | h = -1.0f; | |
217 | ||
218 | *rect++ = QRectF(x, t, 1.0f, h); | |
219 | } | |
220 | ||
221 | p.drawRects(rects, e.length); | |
222 | ||
223 | delete[] rects; | |
224 | delete[] e.samples; | |
225 | } | |
226 | ||
227 | float AnalogSignal::scale() const | |
228 | { | |
229 | const float seq[] = {1.0f, 2.0f, 5.0f}; | |
230 | const int offset = std::numeric_limits<int>::max() / (2 * countof(seq)); | |
231 | const std::div_t d = std::div( | |
232 | (int)(scale_index_ + countof(seq) * offset), | |
233 | countof(seq)); | |
234 | return powf(10.0f, d.quot - offset) * seq[d.rem]; | |
235 | } | |
236 | ||
237 | } // namespace view | |
238 | } // namespace pv |