79a27ddd05c97996cc6f7332b3b6b3a7ab4e4058
[pulseview.git] / logicsignal.cpp
1 /*
2  * This file is part of the sigrok 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 #define GL_GLEXT_PROTOTYPES
22 #include <GL/gl.h>
23 #include <GL/glext.h>
24
25 #include <math.h>
26
27 #include "extdef.h"
28
29 #include "logicdata.h"
30 #include "logicdatasnapshot.h"
31 #include "logicsignal.h"
32
33 using namespace boost;
34 using namespace std;
35
36 const float Log2 = logf(2.0f);
37
38 const float LogicSignal::Margin = 10.0f;
39
40 const float LogicSignal::EdgeColour[3] =        {0.50f, 0.50f, 0.50f};
41 const float LogicSignal::HighColour[3] =        {0.00f, 0.75f, 0.00f};
42 const float LogicSignal::LowColour[3] = {0.75f, 0.00f, 0.00f};
43
44 const QColor LogicSignal::LogicSignalColours[10] = {
45         QColor(0x16, 0x19, 0x1A),       // Black
46         QColor(0x8F, 0x52, 0x02),       // Brown
47         QColor(0xCC, 0x00, 0x00),       // Red
48         QColor(0xF5, 0x79, 0x00),       // Orange
49         QColor(0xED, 0xD4, 0x00),       // Yellow
50         QColor(0x73, 0xD2, 0x16),       // Green
51         QColor(0x34, 0x65, 0xA4),       // Blue
52         QColor(0x75, 0x50, 0x7B),       // Violet
53         QColor(0x88, 0x8A, 0x85),       // Grey
54         QColor(0xEE, 0xEE, 0xEC),       // White
55 };
56
57 LogicSignal::LogicSignal(QString name, shared_ptr<LogicData> data,
58         int probe_index) :
59         Signal(name),
60         _data(data),
61         _probe_index(probe_index)
62 {
63         assert(_probe_index >= 0);
64 }
65
66 void LogicSignal::paint(QGLWidget &widget, const QRect &rect,
67         double scale, double offset)
68 {
69         Point2F *vertex;
70
71         vector< pair<int64_t, bool> > edges;
72
73         assert(scale > 0);
74         assert(_data);
75
76         const float high_offset = rect.top() + Margin;
77         const float low_offset = rect.bottom() - Margin;
78
79         const queue< shared_ptr<LogicDataSnapshot> > &snapshots =
80                 _data->get_snapshots();
81         if(snapshots.empty())
82                 return;
83
84         const shared_ptr<LogicDataSnapshot> &snapshot = snapshots.front();
85
86         const double pixels_offset = offset / scale;
87         const double samplerate = _data->get_samplerate();
88         const double start_time = _data->get_start_time();
89         const int64_t last_sample = snapshot->get_sample_count() - 1;
90         const double samples_per_pixel = samplerate * scale;
91         const double start = samplerate * (offset - start_time);
92         const double end = start + samples_per_pixel * rect.width();
93         const int64_t quantization_length = 1LL << (int64_t)floorf(
94                 max(logf((float)samples_per_pixel) / Log2, 0.0f));
95
96         snapshot->get_subsampled_edges(edges,
97                 min(max((int64_t)floor(start), (int64_t)0), last_sample),
98                 min(max((int64_t)ceil(end), (int64_t)0), last_sample),
99                 quantization_length, _probe_index);
100
101         // Paint the edges
102         const unsigned int edge_point_count = (edges.size() - 2) * 2;
103         Point2F *const edge_points = new Point2F[edge_point_count];
104         vertex = edge_points;
105
106         for(vector<LogicDataSnapshot::EdgePair>::const_iterator i = edges.begin() + 1;
107             i != edges.end() - 1; i++)
108         {
109                 const int x = (int)((*i).first / samples_per_pixel - pixels_offset) +
110                         rect.left();
111
112                 vertex->x = x, vertex->y = high_offset;
113                 vertex++;
114                 vertex->x = x, vertex->y = low_offset;
115                 vertex++;
116         }
117
118         glColor3fv(EdgeColour);
119         paint_lines(edge_points, edge_point_count);
120         delete[] edge_points;
121
122         // Paint the caps
123         const unsigned int max_cap_point_count = (edges.size() - 1) * 2;
124         Point2F *const cap_points = new Point2F[max_cap_point_count];
125
126         glColor3fv(HighColour);
127         paint_caps(cap_points, edges, true, samples_per_pixel,
128                 pixels_offset, rect.left(), high_offset);
129         glColor3fv(LowColour);
130         paint_caps(cap_points, edges, false, samples_per_pixel,
131                 pixels_offset, rect.left(), low_offset);
132
133         delete[] cap_points;
134 }
135
136 int LogicSignal::paint_caps(Point2F *const cap_points,
137         vector< pair<int64_t, bool> > &edges, bool level,
138         double samples_per_pixel, double pixels_offset, int x_offset,
139         int y_offset)
140 {
141         Point2F *vertex = cap_points;
142
143         for(vector<LogicDataSnapshot::EdgePair>::const_iterator i = edges.begin();
144             i != (edges.end() - 1); i++)
145                 if((*i).second == level)
146                 {
147                         vertex->x = (int)((*i).first / samples_per_pixel -
148                                 pixels_offset) + x_offset - 1;
149                         vertex->y = y_offset;
150                         vertex++;
151
152                         vertex->x = (int)((*(i+1)).first / samples_per_pixel -
153                                 pixels_offset) + x_offset;
154                         vertex->y = y_offset;
155                         vertex++;
156                 }
157
158         paint_lines(cap_points, vertex - cap_points);
159 }
160
161 void LogicSignal::paint_lines(Point2F *points, int count)
162 {
163         GLuint vbo_id;
164
165         assert(points);
166
167         glGenBuffers(1, &vbo_id);
168         glBindBuffer(GL_ARRAY_BUFFER, vbo_id);
169
170         const unsigned int vbo_length = count * sizeof(Point2F);
171         glBufferData(GL_ARRAY_BUFFER, vbo_length, NULL, GL_STATIC_DRAW);
172         glBufferSubData(GL_ARRAY_BUFFER, 0, vbo_length, points);
173
174         glBindBuffer(GL_ARRAY_BUFFER, vbo_id);
175
176         glVertexPointer(2, GL_FLOAT, sizeof(Point2F), 0);
177
178         glEnableClientState(GL_VERTEX_ARRAY);
179         glDrawArrays(GL_LINES,  0,  count);
180         glDisableClientState(GL_VERTEX_ARRAY);
181
182         glDeleteBuffers(1, &vbo_id);
183 }
184
185 QColor LogicSignal::get_colour() const
186 {
187         return LogicSignalColours[_probe_index % countof(LogicSignalColours)];
188 }
189
190 int LogicSignal::get_nominal_offset(const QRect &rect) const
191 {
192         return rect.bottom() - Margin;
193 }