]> sigrok.org Git - pulseview.git/blob - logicsignal.cpp
5377287362065aed52da22a35b0a867cd9bd9542
[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 "logicdata.h"
28 #include "logicdatasnapshot.h"
29 #include "logicsignal.h"
30
31 using namespace boost;
32 using namespace std;
33
34 const float Log2 = logf(2.0f);
35
36 LogicSignal::LogicSignal(QString name, shared_ptr<LogicData> data,
37         int probe_index) :
38         Signal(name),
39         _data(data),
40         _probe_index(probe_index)
41 {
42         assert(_probe_index >= 0);
43 }
44
45 void LogicSignal::paint(QGLWidget &widget, const QRect &rect,
46         double scale, double offset)
47 {
48         Point2F *vertex;
49
50         vector< pair<int64_t, bool> > edges;
51
52         assert(scale > 0);
53         assert(_data);
54
55         const queue< shared_ptr<LogicDataSnapshot> > &snapshots =
56                 _data->get_snapshots();
57         if(snapshots.empty())
58                 return;
59
60         const shared_ptr<LogicDataSnapshot> &snapshot = snapshots.front();
61
62         const double pixels_offset = offset / scale;
63         const double samplerate = _data->get_samplerate();
64         const double start_time = _data->get_start_time();
65         const int64_t last_sample = (int64_t)snapshot->get_sample_count() - 1;
66         const double samples_per_pixel = samplerate * scale;
67         const int64_t start = min(max((int64_t)(samplerate * (offset - start_time)),
68                 (int64_t)0), last_sample);
69         const int64_t end = min((int64_t)(start + samples_per_pixel * rect.width()),
70                 last_sample);
71         const int64_t quantization_length = 1LL << (int64_t)floorf(
72                 max(logf(samples_per_pixel / Log2), 0.0f));
73
74         snapshot->get_subsampled_edges(edges, start, end,
75                 quantization_length, _probe_index);
76
77         // Paint the edges
78         const unsigned int edge_point_count = (edges.size() - 2) * 2;
79         Point2F *const edge_points = new Point2F[edge_point_count];
80         vertex = edge_points;
81
82         for(vector<LogicDataSnapshot::EdgePair>::const_iterator i = edges.begin() + 1;
83             i != edges.end() - 1; i++)
84         {
85                 const int x = (int)((*i).first / samples_per_pixel - pixels_offset) +
86                         rect.left();
87
88                 vertex->x = x, vertex->y = 10 + rect.top() - 1;
89                 vertex++;
90                 vertex->x = x, vertex->y = 40 + rect.top();
91                 vertex++;
92         }
93
94         glColor3f(0,0,1);
95         paint_lines(edge_points, edge_point_count);
96         delete[] edge_points;
97
98         // Paint the caps
99         const unsigned int cap_point_count = (edges.size() - 1) * 2;
100         Point2F *const cap_points = new Point2F[cap_point_count];
101         vertex = cap_points;
102
103         for(vector<LogicDataSnapshot::EdgePair>::const_iterator i = edges.begin();
104             i != (edges.end() - 1); i++)
105         {
106                 const int y = ((*i).second ? 10 : 40) + rect.top();
107
108                 vertex->x = (int)((*i).first / samples_per_pixel - pixels_offset) +
109                         rect.left() - 1;
110                 vertex->y = y;
111                 vertex++;
112
113                 vertex->x = (int)((*(i+1)).first / samples_per_pixel - pixels_offset) +
114                         rect.left();
115                 vertex->y = y;
116                 vertex++;
117         }
118
119         glColor3f(0,0,1);
120         paint_lines(cap_points, cap_point_count);
121         delete[] cap_points;
122 }
123
124 void LogicSignal::paint_lines(Point2F *points, int count)
125 {
126         GLuint vbo_id;
127
128         assert(points);
129
130         glGenBuffers(1, &vbo_id);
131         glBindBuffer(GL_ARRAY_BUFFER, vbo_id);
132
133         const unsigned int vbo_length = count * sizeof(Point2F);
134         glBufferData(GL_ARRAY_BUFFER, vbo_length, NULL, GL_STATIC_DRAW);
135         glBufferSubData(GL_ARRAY_BUFFER, 0, vbo_length, points);
136
137         glBindBuffer(GL_ARRAY_BUFFER, vbo_id);
138
139         glVertexPointer(2, GL_FLOAT, sizeof(Point2F), 0);
140
141         glEnableClientState(GL_VERTEX_ARRAY);
142         glDrawArrays(GL_LINES,  0,  count);
143         glDisableClientState(GL_VERTEX_ARRAY);
144
145         glDeleteBuffers(1, &vbo_id);
146 }