]> sigrok.org Git - pulseview.git/blame - logicsignal.cpp
Initial rendering of traces
[pulseview.git] / logicsignal.cpp
CommitLineData
28a4c9c5
JH
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
81a635d1
JH
21#define GL_GLEXT_PROTOTYPES
22#include <GL/gl.h>
23#include <GL/glext.h>
24
2858b391
JH
25#include "logicdata.h"
26#include "logicdatasnapshot.h"
28a4c9c5
JH
27#include "logicsignal.h"
28
2858b391
JH
29using namespace boost;
30using namespace std;
81a635d1 31
2858b391 32LogicSignal::LogicSignal(QString name, shared_ptr<LogicData> data,
28a4c9c5 33 int probe_index) :
2858b391
JH
34 Signal(name),
35 _data(data),
28a4c9c5
JH
36 _probe_index(probe_index)
37{
38 assert(_probe_index >= 0);
39}
e3f65ace 40
3b18c57d
JH
41void LogicSignal::paint(QGLWidget &widget, const QRect &rect,
42 uint64_t scale, int64_t offset)
e3f65ace 43{
2858b391
JH
44 Point2F *vertex;
45
46 vector< pair<int64_t, bool> > edges;
47
48 assert(_data);
49
50 const queue< shared_ptr<LogicDataSnapshot> > &snapshots =
51 _data->get_snapshots();
52 if(snapshots.empty())
53 return;
54
55 const shared_ptr<LogicDataSnapshot> &snapshot = snapshots.front();
56
57 const int64_t start = 0;
58 const int64_t end = 8000;
59 const int64_t quantization_length = 4;
60
61 snapshot->get_subsampled_edges(edges, start, end,
62 quantization_length, _probe_index);
63
64 // Paint the edges
65 const unsigned int edge_point_count = (edges.size() - 2) * 2;
66 Point2F *const edge_points = new Point2F[edge_point_count];
67 vertex = edge_points;
68
69 for(vector<LogicDataSnapshot::EdgePair>::const_iterator i = edges.begin() + 1;
70 i != edges.end() - 1; i++)
71 {
72 const int x = edge.first / quantization_length +
73 rect.left();
74
75 vertex->x = x, vertex->y = 10 + rect.top() - 1;
76 vertex++;
77 vertex->x = x, vertex->y = 40 + rect.top();
78 vertex++;
79 }
81a635d1
JH
80
81 glColor3f(0,0,1);
2858b391
JH
82 paint_lines(edge_points, edge_point_count);
83 delete[] edge_points;
84
85 // Paint the caps
86 const unsigned int cap_point_count = (edges.size() - 1) * 2;
87 Point2F *const cap_points = new Point2F[cap_point_count];
88 vertex = cap_points;
89
90 for(vector<LogicDataSnapshot::EdgePair>::const_iterator i = edges.begin();
91 i != (edges.end() - 1); i++)
92 {
93 const int y = ((*i).second ? 10 : 40) + rect.top();
94
95 vertex->x = (*i).first / quantization_length +
96 rect.left() - 1;
97 vertex->y = y;
98 vertex++;
99
100 vertex->x = (*(i+1)).first / quantization_length +
101 rect.left();
102 vertex->y = y;
103 vertex++;
104 }
105
106 glColor3f(0,0,1);
107 paint_lines(cap_points, cap_point_count);
108 delete[] cap_points;
109}
110
111void LogicSignal::paint_lines(Point2F *points, int count)
112{
113 GLuint vbo_id;
114
115 assert(points);
81a635d1
JH
116
117 glGenBuffers(1, &vbo_id);
118 glBindBuffer(GL_ARRAY_BUFFER, vbo_id);
119
2858b391
JH
120 const unsigned int vbo_length = count * sizeof(Point2F);
121 glBufferData(GL_ARRAY_BUFFER, vbo_length, NULL, GL_STATIC_DRAW);
122 glBufferSubData(GL_ARRAY_BUFFER, 0, vbo_length, points);
81a635d1
JH
123
124 glBindBuffer(GL_ARRAY_BUFFER, vbo_id);
125
2858b391 126 glVertexPointer(2, GL_FLOAT, sizeof(Point2F), 0);
81a635d1
JH
127
128 glEnableClientState(GL_VERTEX_ARRAY);
2858b391 129 glDrawArrays(GL_LINES, 0, count);
81a635d1 130 glDisableClientState(GL_VERTEX_ARRAY);
2858b391
JH
131
132 glDeleteBuffers(1, &vbo_id);
e3f65ace 133}