]> sigrok.org Git - pulseview.git/blob - pv/logicsignal.cpp
Render ruler and signals with sub-pixel accuracy
[pulseview.git] / pv / logicsignal.cpp
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 <math.h>
24
25 #include "logicdata.h"
26 #include "logicdatasnapshot.h"
27 #include "logicsignal.h"
28
29 using namespace boost;
30 using namespace std;
31
32 namespace pv {
33
34 const float LogicSignal::Margin = 10.0f;
35 const float LogicSignal::Oversampling = 2.0f;
36
37 const QColor LogicSignal::EdgeColour(0x80, 0x80, 0x80);
38 const QColor LogicSignal::HighColour(0x00, 0xC0, 0x00);
39 const QColor LogicSignal::LowColour(0xC0, 0x00, 0x00);
40
41 const QColor LogicSignal::LogicSignalColours[10] = {
42         QColor(0x16, 0x19, 0x1A),       // Black
43         QColor(0x8F, 0x52, 0x02),       // Brown
44         QColor(0xCC, 0x00, 0x00),       // Red
45         QColor(0xF5, 0x79, 0x00),       // Orange
46         QColor(0xED, 0xD4, 0x00),       // Yellow
47         QColor(0x73, 0xD2, 0x16),       // Green
48         QColor(0x34, 0x65, 0xA4),       // Blue
49         QColor(0x75, 0x50, 0x7B),       // Violet
50         QColor(0x88, 0x8A, 0x85),       // Grey
51         QColor(0xEE, 0xEE, 0xEC),       // White
52 };
53
54 LogicSignal::LogicSignal(QString name, shared_ptr<LogicData> data,
55         int probe_index) :
56         Signal(name),
57         _probe_index(probe_index),
58         _data(data)
59 {
60         assert(_probe_index >= 0);
61 }
62
63 void LogicSignal::paint(QPainter &p, const QRect &rect, double scale,
64         double offset)
65 {
66         QLineF *line;
67
68         vector< pair<int64_t, bool> > edges;
69
70         assert(scale > 0);
71         assert(_data);
72
73         const float high_offset = rect.top() + Margin + 0.5f;
74         const float low_offset = rect.bottom() - Margin + 0.5f;
75
76         const deque< shared_ptr<LogicDataSnapshot> > &snapshots =
77                 _data->get_snapshots();
78         if(snapshots.empty())
79                 return;
80
81         const shared_ptr<LogicDataSnapshot> &snapshot = snapshots.front();
82
83         const double pixels_offset = offset / scale;
84         const double samplerate = _data->get_samplerate();
85         const double start_time = _data->get_start_time();
86         const int64_t last_sample = snapshot->get_sample_count() - 1;
87         const double samples_per_pixel = samplerate * scale;
88         const double start = samplerate * (offset - start_time);
89         const double end = start + samples_per_pixel * rect.width();
90
91         snapshot->get_subsampled_edges(edges,
92                 min(max((int64_t)floor(start), (int64_t)0), last_sample),
93                 min(max((int64_t)ceil(end), (int64_t)0), last_sample),
94                 samples_per_pixel / Oversampling, _probe_index);
95         assert(edges.size() >= 2);
96
97         // Paint the edges
98         const unsigned int edge_count = edges.size() - 2;
99         QLineF *const edge_lines = new QLineF[edge_count];
100         line = edge_lines;
101
102         for(vector<LogicDataSnapshot::EdgePair>::const_iterator i =
103                         edges.begin() + 1;
104                 i != edges.end() - 1; i++) {
105                 const float x = ((*i).first / samples_per_pixel -
106                         pixels_offset) + rect.left();
107                 *line++ = QLineF(x, high_offset, x, low_offset);
108         }
109
110         p.setPen(EdgeColour);
111         p.drawLines(edge_lines, edge_count);
112         delete[] edge_lines;
113
114         // Paint the caps
115         const unsigned int max_cap_line_count = (edges.size() - 1);
116         QLineF *const cap_lines = new QLineF[max_cap_line_count];
117
118         p.setPen(HighColour);
119         paint_caps(p, cap_lines, edges, true, samples_per_pixel,
120                 pixels_offset, rect.left(), high_offset);
121         p.setPen(LowColour);
122         paint_caps(p, cap_lines, edges, false, samples_per_pixel,
123                 pixels_offset, rect.left(), low_offset);
124
125         delete[] cap_lines;
126 }
127
128 void LogicSignal::paint_caps(QPainter &p, QLineF *const lines,
129         vector< pair<int64_t, bool> > &edges, bool level,
130         double samples_per_pixel, double pixels_offset, float x_offset,
131         float y_offset)
132 {
133         QLineF *line = lines;
134
135         for(vector<LogicDataSnapshot::EdgePair>::const_iterator i = edges.begin();
136             i != (edges.end() - 1); i++)
137                 if((*i).second == level) {
138                         *line++ = QLineF(
139                                 ((*i).first / samples_per_pixel -
140                                         pixels_offset) + x_offset, y_offset,
141                                 ((*(i+1)).first / samples_per_pixel -
142                                         pixels_offset) + x_offset, y_offset);
143                 }
144
145         p.drawLines(lines, line - lines);
146 }
147
148 QColor LogicSignal::get_colour() const
149 {
150         return LogicSignalColours[_probe_index % countof(LogicSignalColours)];
151 }
152
153 int LogicSignal::get_nominal_offset(const QRect &rect) const
154 {
155         return rect.bottom() - Margin;
156 }
157
158 } // namespace pv