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