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