]> sigrok.org Git - pulseview.git/blame - logicsignal.cpp
Added extdef.h
[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
131e8012
JH
36const float LogicSignal::Margin = 10.0f;
37
38const float LogicSignal::EdgeColour[3] = {0.50f, 0.50f, 0.50f};
39const float LogicSignal::HighColour[3] = {0.00f, 0.75f, 0.00f};
40const float LogicSignal::LowColour[3] = {0.75f, 0.00f, 0.00f};
41
2858b391 42LogicSignal::LogicSignal(QString name, shared_ptr<LogicData> data,
28a4c9c5 43 int probe_index) :
2858b391
JH
44 Signal(name),
45 _data(data),
28a4c9c5
JH
46 _probe_index(probe_index)
47{
48 assert(_probe_index >= 0);
49}
e3f65ace 50
3b18c57d 51void LogicSignal::paint(QGLWidget &widget, const QRect &rect,
fb0d32cb 52 double scale, double offset)
e3f65ace 53{
2858b391
JH
54 Point2F *vertex;
55
56 vector< pair<int64_t, bool> > edges;
57
fb0d32cb 58 assert(scale > 0);
2858b391
JH
59 assert(_data);
60
131e8012
JH
61 const float high_offset = rect.top() + Margin;
62 const float low_offset = rect.bottom() - Margin;
63
2858b391
JH
64 const queue< shared_ptr<LogicDataSnapshot> > &snapshots =
65 _data->get_snapshots();
66 if(snapshots.empty())
67 return;
68
69 const shared_ptr<LogicDataSnapshot> &snapshot = snapshots.front();
70
fb0d32cb
JH
71 const double pixels_offset = offset / scale;
72 const double samplerate = _data->get_samplerate();
73 const double start_time = _data->get_start_time();
74 const int64_t last_sample = (int64_t)snapshot->get_sample_count() - 1;
75 const double samples_per_pixel = samplerate * scale;
76 const int64_t start = min(max((int64_t)(samplerate * (offset - start_time)),
77 (int64_t)0), last_sample);
78 const int64_t end = min((int64_t)(start + samples_per_pixel * rect.width()),
79 last_sample);
7cd5faf8
JH
80 const int64_t quantization_length = 1LL << (int64_t)floorf(
81 max(logf(samples_per_pixel / Log2), 0.0f));
2858b391
JH
82
83 snapshot->get_subsampled_edges(edges, start, end,
84 quantization_length, _probe_index);
85
86 // Paint the edges
87 const unsigned int edge_point_count = (edges.size() - 2) * 2;
88 Point2F *const edge_points = new Point2F[edge_point_count];
89 vertex = edge_points;
90
91 for(vector<LogicDataSnapshot::EdgePair>::const_iterator i = edges.begin() + 1;
92 i != edges.end() - 1; i++)
93 {
fb0d32cb 94 const int x = (int)((*i).first / samples_per_pixel - pixels_offset) +
2858b391
JH
95 rect.left();
96
131e8012 97 vertex->x = x, vertex->y = high_offset;
2858b391 98 vertex++;
131e8012 99 vertex->x = x, vertex->y = low_offset;
2858b391
JH
100 vertex++;
101 }
81a635d1 102
131e8012 103 glColor3fv(EdgeColour);
2858b391
JH
104 paint_lines(edge_points, edge_point_count);
105 delete[] edge_points;
106
107 // Paint the caps
131e8012
JH
108 const unsigned int max_cap_point_count = (edges.size() - 1) * 2;
109 Point2F *const cap_points = new Point2F[max_cap_point_count];
2858b391 110
131e8012
JH
111 glColor3fv(HighColour);
112 paint_caps(cap_points, edges, true, samples_per_pixel,
113 pixels_offset, rect.left(), high_offset);
114 glColor3fv(LowColour);
115 paint_caps(cap_points, edges, false, samples_per_pixel,
116 pixels_offset, rect.left(), low_offset);
2858b391 117
131e8012
JH
118 delete[] cap_points;
119}
2858b391 120
131e8012
JH
121int LogicSignal::paint_caps(Point2F *const cap_points,
122 vector< pair<int64_t, bool> > &edges, bool level,
123 double samples_per_pixel, double pixels_offset, int x_offset,
124 int y_offset)
125{
126 Point2F *vertex = cap_points;
2858b391 127
131e8012
JH
128 for(vector<LogicDataSnapshot::EdgePair>::const_iterator i = edges.begin();
129 i != (edges.end() - 1); i++)
130 if((*i).second == level)
131 {
132 vertex->x = (int)((*i).first / samples_per_pixel -
133 pixels_offset) + x_offset - 1;
134 vertex->y = y_offset;
135 vertex++;
136
137 vertex->x = (int)((*(i+1)).first / samples_per_pixel -
138 pixels_offset) + x_offset;
139 vertex->y = y_offset;
140 vertex++;
141 }
142
143 paint_lines(cap_points, vertex - cap_points);
2858b391
JH
144}
145
146void LogicSignal::paint_lines(Point2F *points, int count)
147{
148 GLuint vbo_id;
149
150 assert(points);
81a635d1
JH
151
152 glGenBuffers(1, &vbo_id);
153 glBindBuffer(GL_ARRAY_BUFFER, vbo_id);
154
2858b391
JH
155 const unsigned int vbo_length = count * sizeof(Point2F);
156 glBufferData(GL_ARRAY_BUFFER, vbo_length, NULL, GL_STATIC_DRAW);
157 glBufferSubData(GL_ARRAY_BUFFER, 0, vbo_length, points);
81a635d1
JH
158
159 glBindBuffer(GL_ARRAY_BUFFER, vbo_id);
160
2858b391 161 glVertexPointer(2, GL_FLOAT, sizeof(Point2F), 0);
81a635d1
JH
162
163 glEnableClientState(GL_VERTEX_ARRAY);
2858b391 164 glDrawArrays(GL_LINES, 0, count);
81a635d1 165 glDisableClientState(GL_VERTEX_ARRAY);
2858b391
JH
166
167 glDeleteBuffers(1, &vbo_id);
e3f65ace 168}