Last active
September 15, 2020 16:13
-
-
Save symroc/fb003f0fc7e99c8bc97a098c247312e9 to your computer and use it in GitHub Desktop.
Enhanced Libreplot cartesian Example (Can update data via TCP protocol or COM port)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* This file is part of libreplot. | |
* Copyright (C) 2014-2016 Kuldeep Singh Dhaka <kuldeep@madresistor.com> | |
* | |
* libreplot is free software: you can redistribute it and/or modify | |
* it under the terms of the GNU General Public License as published by | |
* the Free Software Foundation, either version 3 of the License, or | |
* (at your option) any later version. | |
* | |
* libreplot is distributed in the hope that it will be useful, | |
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
* GNU General Public License for more details. | |
* | |
* You should have received a copy of the GNU General Public License | |
* along with libreplot. If not, see <http://www.gnu.org/licenses/>. | |
*/ | |
#include "cartesian.h" | |
#include <QMouseEvent> | |
#include <QApplication> | |
#include <QDesktopWidget> | |
#include <QPaintDevice> | |
#include <Qt> | |
#include <math.h> | |
#include <QDebug> | |
Cartesian::Cartesian(QWidget *parent): | |
QOpenGLWidget(parent), | |
m_cartesian(NULL), | |
m_axis_left(NULL), | |
m_axis_bottom(NULL), | |
// m_curve(NULL), | |
// m_circle(NULL), | |
// m_text(NULL), | |
m_surface_width(0), | |
m_surface_height(0) | |
{ | |
m_data_len = 0; | |
m_data = (lp_vec2f *) malloc(sizeof(lp_vec2f) * (mySize)); | |
// m_data=(lp_vec2f *)calloc(mySize, sizeof(lp_vec2f)); | |
// l.resize(mySize); | |
// std::iota(l.begin(), l.end(), -mySize); | |
// xaxis.resize(mySize); | |
// std::copy(begin(l), end(l), begin(xaxis)); | |
// std::transform(xaxis.begin(), xaxis.end(), xaxis.begin(), | |
// std::bind(std::divides<float>(), std::placeholders::_1, 1000)); | |
// for (uint i = 0; i < m_data_len; i++) { | |
// float x = 0; | |
// float y = 0; | |
// m_data[i].x = x; | |
// m_data[i].y = y; | |
// } | |
} | |
void Cartesian::setCh(int mych) | |
{ | |
ch=mych; | |
} | |
Cartesian::~Cartesian() | |
{ | |
makeCurrent(); | |
lp_cartesian_del(m_cartesian); | |
lp_cartesian_axis_del(m_axis_left); | |
lp_cartesian_axis_del(m_axis_bottom); | |
lp_cartesian_curve_del(m_curve); | |
lp_circle_del(m_circle); | |
lp_text_del(m_text); | |
free(m_data); | |
doneCurrent(); | |
} | |
void Cartesian::setData(float * data,uint size) | |
{ | |
// for (uint i=0; i<size; i++){ | |
// myVec.push_front(data[i]); | |
// } | |
// if (m_data_len>=mySize){ | |
// memset(m_data, 0, sizeof(m_data)); | |
// replot(0); | |
// myVec.remove(myVec.length()-1001,1000); | |
// m_data_len=mySize-1000; | |
// } | |
// for (uint i = 0; i<myVec.length(); i++) { | |
// double x = static_cast<double>(i)/static_cast<double>(1000)*-1; | |
// float y = myVec[i]; | |
// m_data[i].x = x; | |
// m_data[i].y = y; | |
// if (myVec[i]<global_min) global_min=myVec[i]; | |
// if (myVec[i]>global_max) global_max=myVec[i]; | |
// } | |
// qDebug()<<m_data_len; | |
// qDebug()<<test++; | |
if (m_data_len>=mySize){ | |
memmove(&m_data[1000],&m_data[0],sizeof(lp_vec2f) * (m_data_len-1000)); | |
m_data_len=mySize-1000; | |
replot(m_data_len); | |
} | |
else{ | |
memmove(&m_data[1000],&m_data[0],sizeof(lp_vec2f) * (m_data_len)); | |
} | |
for (uint i = 0; i < m_data_len; i++) { | |
double tl=static_cast<double> (i)/static_cast<double>(1000); | |
m_data[i+1000].x=m_data[i].x-1; | |
} | |
memset(m_data,0,sizeof(lp_vec2f)*1000); | |
for (uint i = 0; i < size; i++) { | |
double x = static_cast<double>(size-i-1)/static_cast<double>(1000)*-1; | |
float y = data[i]; | |
m_data[size-i-1].x = x; | |
m_data[size-i-1].y = y; | |
if (data[i]<global_min) global_min=data[i]; | |
if (data[i]>global_max) global_max=data[i]; | |
} | |
m_data_len+=size; | |
//if (autoZoom){ | |
// lp_cartesian_axis_2float(m_axis_left, LP_CARTESIAN_AXIS_VALUE_RANGE, 1.2*global_min, 1.2*global_max); | |
//} | |
// for (uint i = 0; i <size; i++) { | |
// double x = static_cast<double>(m_data_len+i)/static_cast<double>(1000); | |
// float y = data[i]; | |
// m_data[m_data_len+i].x = x; | |
// m_data[m_data_len+i].y = y; | |
// if (data[i]<global_min) global_min=data[i]; | |
// if (data[i]>global_max) global_max=data[i]; | |
// } | |
if (firstPlot){ | |
//lp_cartesian_axis_2float(m_axis_bottom, LP_CARTESIAN_AXIS_VALUE_RANGE, 0,mySize/1000); | |
lp_cartesian_axis_2float(m_axis_bottom, LP_CARTESIAN_AXIS_VALUE_RANGE, -(mySize/1000),0); | |
lp_cartesian_axis_2float(m_axis_left, LP_CARTESIAN_AXIS_VALUE_RANGE, 1.2*global_min, 1.2*global_max); | |
firstPlot=false; | |
} | |
replot(m_data_len); | |
} | |
void Cartesian::replot(uint size){ | |
m_curve = lp_cartesian_curve_gen(); | |
lp_cartesian_pointer(m_cartesian, LP_CARTESIAN_AXIS_AT_LEFT, m_axis_left); | |
lp_cartesian_pointer(m_cartesian, LP_CARTESIAN_AXIS_AT_BOTTOM, m_axis_bottom); | |
switch (ch){ | |
case 1: | |
lp_cartesian_curve_4float(m_curve, LP_CARTESIAN_CURVE_LINE_COLOR, 0.9529 , 0.4039, 0.2902, 1); | |
break; | |
case 2: | |
lp_cartesian_curve_4float(m_curve, LP_CARTESIAN_CURVE_LINE_COLOR, 0.1725 , 0.3725, 0.1765, 1); | |
break; | |
default: | |
lp_cartesian_curve_4float(m_curve, LP_CARTESIAN_CURVE_LINE_COLOR, 0 , 0.4470 , 0.8410, 1); | |
break; | |
} | |
lp_cartesian_curve_float(m_curve, LP_CARTESIAN_CURVE_LINE_WIDTH, 1); | |
lp_cartesian_curve_data(m_curve, LP_CARTESIAN_CURVE_X, | |
0, GL_FLOAT, sizeof(lp_vec2f), &m_data[0].x, size); | |
lp_cartesian_curve_data(m_curve, LP_CARTESIAN_CURVE_Y, | |
0, GL_FLOAT, sizeof(lp_vec2f), &m_data[0].y, size); | |
update(); | |
} | |
void Cartesian::mousePressEvent(QMouseEvent *e) | |
{ | |
if (e->buttons() & (Qt::LeftButton | Qt::RightButton)) { | |
last_x = e->x(); | |
last_y = e->y(); | |
e->accept(); | |
} | |
} | |
void Cartesian::axis_translate(lp_cartesian_axis *axis, int surface, float diff,bool isHorizontal) | |
{ | |
float min, max, tmp; | |
lp_get_cartesian_axis_2float(axis, LP_CARTESIAN_AXIS_VALUE_RANGE, &min, &max); | |
tmp = (max - min) * (diff / surface); | |
min -= tmp; | |
max -= tmp; | |
lp_cartesian_axis_2float(axis, LP_CARTESIAN_AXIS_VALUE_RANGE, min, max); | |
if (isHorizontal){ | |
QVector<float> values={min,max}; | |
emit xmoved(values);} | |
} | |
void Cartesian::axis_scale(lp_cartesian_axis *axis, int surface, float diff,bool isHorizontal) | |
{ | |
float min, max, tmp; | |
lp_get_cartesian_axis_2float(axis, LP_CARTESIAN_AXIS_VALUE_RANGE, &min, &max); | |
tmp = ((max - min) / 2) * (diff / surface); | |
tmp *= 5; /* speed up */ | |
min += tmp; | |
max -= tmp; | |
lp_cartesian_axis_2float(axis, LP_CARTESIAN_AXIS_VALUE_RANGE, min, max); | |
if (isHorizontal){ | |
QVector<float> values={min,max}; | |
emit xmoved(values);} | |
} | |
void Cartesian::SyncTranslation(QVector<float> l) | |
{ | |
float xmin,xmax,ymin,ymax,min,max; | |
uint start,finish; | |
lp_cartesian_axis_2float(m_axis_bottom, LP_CARTESIAN_AXIS_VALUE_RANGE, l[0], l[1]); | |
lp_get_cartesian_axis_2float(m_axis_bottom, LP_CARTESIAN_AXIS_VALUE_RANGE, &xmin, &xmax); | |
// qDebug()<<ch<<xmin<<xmax; | |
if (m_data_len>50){ | |
lp_get_cartesian_axis_2float(m_axis_left, LP_CARTESIAN_AXIS_VALUE_RANGE, &min, &max); | |
finish=(uint)(-xmin*1000); | |
start=(uint)(-xmax*1000); | |
if (start!=finish && start<m_data_len && finish<m_data_len){ | |
ymin=m_data[start].y; | |
ymax=m_data[start].y; | |
} | |
else if (finish<m_data_len){ | |
ymin=m_data[0].y; | |
ymax=m_data[0].y; | |
start=0; | |
}else if (start<m_data_len){ | |
ymin=m_data[start].y; | |
ymax=m_data[start].y; | |
finish=m_data_len; | |
}else{ | |
ymin=m_data[0].y; | |
ymax=m_data[0].y; | |
start=0; | |
finish=m_data_len; | |
} | |
// qDebug()<<start<<finish<<m_data_len; | |
for (uint i = start; i < finish; i++) { | |
if(m_data[i].y>ymax) ymax=m_data[i].y; | |
if(m_data[i].y<ymin) ymin=m_data[i].y; | |
// qDebug()<<ch<<xmin<<xmax<<ymin<<ymax; | |
// qDebug()<<ch<<min<<max; | |
if (abs(min)<abs(ymin))min=1.2*ymin; | |
if (max<ymax)max=1.2*ymax; | |
if (abs(min)>abs(2*ymin)&&ymin<0)min=1.2*ymin; | |
if (max>2*ymax)max=1.2*ymax; | |
lp_cartesian_axis_2float(m_axis_left, LP_CARTESIAN_AXIS_VALUE_RANGE, min, max); | |
} | |
} | |
update(); | |
} | |
void Cartesian::mouseMoveEvent(QMouseEvent *e) | |
{ | |
int x = e->x(), y = e->y(); | |
/* -1 multiplied because: | |
* from top->bottom [mouse coordinate] | |
* to bottom->top [graph]. | |
* (only for Y axis) */ | |
if (e->buttons() & Qt::LeftButton) { | |
axis_translate(m_axis_left, m_surface_height, -1 * (y - last_y),false); | |
axis_translate(m_axis_bottom, m_surface_width, (x - last_x),true); | |
} else if (e->buttons() & Qt::RightButton) { | |
axis_scale(m_axis_left, m_surface_height, -1 * (y - last_y),false); | |
axis_scale(m_axis_bottom, m_surface_width, x - last_x,true); | |
} else { | |
return; | |
} | |
last_x = x; | |
last_y = y; | |
e->accept(); | |
update(); | |
} | |
void Cartesian::mouseReleaseEvent(QMouseEvent *e) | |
{ | |
e->accept(); | |
update(); | |
} | |
void Cartesian::axis_scroll(lp_cartesian_axis *axis, int steps,bool isVertical) | |
{ | |
float min, max, tmp; | |
lp_get_cartesian_axis_2float(axis, LP_CARTESIAN_AXIS_VALUE_RANGE, &min, &max); | |
tmp = ((max - min) / 6) * steps; | |
min += tmp; | |
max -= tmp; | |
if (!isVertical){ | |
QVector<float> values={min,max}; | |
emit xmoved(values); | |
} | |
lp_cartesian_axis_2float(axis, LP_CARTESIAN_AXIS_VALUE_RANGE, min, max); | |
} | |
void Cartesian::wheelEvent(QWheelEvent *e) | |
{ | |
QPoint numPixels = e->pixelDelta(); | |
QPoint numDegrees = e->angleDelta() / 8; | |
qreal numSteps; | |
if (!numPixels.isNull()) { | |
numSteps = numPixels.y() / 15.0; | |
} else if (!numDegrees.isNull()) { | |
numSteps = numDegrees.y() / 15.0; | |
} else { | |
numSteps = 0; | |
} | |
axis_scroll(m_axis_left, numSteps,true); | |
axis_scroll(m_axis_bottom, numSteps,false); | |
update(); | |
e->accept(); | |
} | |
void Cartesian::initializeGL() | |
{ | |
initializeOpenGLFunctions(); | |
lp_init(); | |
m_cartesian = lp_cartesian_gen(); | |
m_dpi.x = physicalDpiX(); | |
m_dpi.y = physicalDpiY(); | |
lp_cartesian_2float(m_cartesian, LP_CARTESIAN_DPI, m_dpi.x, m_dpi.y); | |
m_axis_bottom = lp_cartesian_axis_gen(); | |
m_axis_left = lp_cartesian_axis_gen(); | |
lp_cartesian_axis_2float(m_axis_bottom, LP_CARTESIAN_AXIS_VALUE_RANGE, -10, +10); | |
lp_cartesian_axis_2float(m_axis_left, LP_CARTESIAN_AXIS_VALUE_RANGE, -10, +10); | |
lp_cartesian_pointer(m_cartesian, LP_CARTESIAN_AXIS_AT_LEFT, m_axis_left); | |
lp_cartesian_pointer(m_cartesian, LP_CARTESIAN_AXIS_AT_BOTTOM, m_axis_bottom); | |
m_curve = lp_cartesian_curve_gen(); | |
lp_cartesian_curve_4float(m_curve, LP_CARTESIAN_CURVE_LINE_COLOR, 0, 0, 1, 1); | |
lp_cartesian_curve_float(m_curve, LP_CARTESIAN_CURVE_LINE_WIDTH, 3); | |
lp_cartesian_curve_data(m_curve, LP_CARTESIAN_CURVE_X, | |
0, GL_FLOAT, sizeof(lp_vec2f), &m_data[0].x, m_data_len); | |
lp_cartesian_curve_data(m_curve, LP_CARTESIAN_CURVE_Y, | |
0, GL_FLOAT, sizeof(lp_vec2f), &m_data[0].y, m_data_len); | |
lp_texture *marker = lp_texture_gen_text((uint8_t *)"◩", 7, lp_font_gen(), &m_dpi); | |
//lp_texture *marker = lp_texture_gen_png_file("/home/kuldeep/Desktop/not-gate-md.png"); | |
lp_cartesian_curve_pointer(m_curve, LP_CARTESIAN_CURVE_MARKER_TEXTURE, marker); | |
lp_texture_del(marker); | |
lp_cartesian_curve_bool(m_curve, LP_CARTESIAN_CURVE_LINE_SHOW, true); | |
lp_cartesian_curve_bool(m_curve, LP_CARTESIAN_CURVE_MARKER_SHOW, false); | |
// m_circle = lp_circle_gen(); | |
// lp_circle_float(m_circle, LP_CIRCLE_RADIUS, 3); | |
// m_text = lp_text_gen(); | |
// lp_text_pointer(m_text, LP_TEXT_STRING, (void *) "Libreplot"); | |
} | |
void Cartesian::resizeGL(int w, int h) | |
{ | |
glViewport(0, 0, w, h); | |
lp_cartesian_4uint(m_cartesian, LP_CARTESIAN_SURFACE, 0, 0, w, h); | |
m_surface_width = w; | |
m_surface_height = h; | |
} | |
void Cartesian::paintGL() | |
{ | |
glClear(GL_COLOR_BUFFER_BIT); | |
lp_cartesian_draw_start(m_cartesian); | |
lp_cartesian_draw_curve(m_cartesian, m_curve); | |
// lp_cartesian_draw_circle(m_cartesian, m_circle); | |
// lp_cartesian_draw_text(m_cartesian, m_text); | |
lp_cartesian_draw_end(m_cartesian); | |
} | |
void Cartesian::clear(){ | |
myVec.clear(); | |
myVec.squeeze(); | |
memset(m_data, 0, sizeof(m_data)); | |
replot(0); | |
m_data_len=0; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* This file is part of libreplot. | |
* Copyright (C) 2014-2016 Kuldeep Singh Dhaka <kuldeep@madresistor.com> | |
* | |
* libreplot is free software: you can redistribute it and/or modify | |
* it under the terms of the GNU General Public License as published by | |
* the Free Software Foundation, either version 3 of the License, or | |
* (at your option) any later version. | |
* | |
* libreplot is distributed in the hope that it will be useful, | |
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
* GNU General Public License for more details. | |
* | |
* You should have received a copy of the GNU General Public License | |
* along with libreplot. If not, see <http://www.gnu.org/licenses/>. | |
*/ | |
#ifndef CARTESIAN_H | |
#define CARTESIAN_H | |
#include <QOpenGLWidget> | |
#include <QOpenGLFunctions> | |
#include <QQuaternion> | |
#include <QVector2D> | |
#include <QVector> | |
#include <functional> | |
extern "C"{ | |
#include <libreplot/libreplot.h> | |
} | |
class Cartesian : public QOpenGLWidget, protected QOpenGLFunctions | |
{ | |
Q_OBJECT | |
public: | |
explicit Cartesian(QWidget *parent = nullptr); | |
virtual ~Cartesian(); | |
void setData(float * data,uint size); | |
void setCh(int mych); | |
public slots: | |
void SyncTranslation(QVector<float> l); | |
void clear(); | |
protected: | |
float global_min=-1.0; | |
float global_max=1.0; | |
void mousePressEvent(QMouseEvent *e) Q_DECL_OVERRIDE; | |
void mouseReleaseEvent(QMouseEvent *e) Q_DECL_OVERRIDE; | |
void mouseMoveEvent(QMouseEvent * event) Q_DECL_OVERRIDE; | |
void wheelEvent(QWheelEvent * event) Q_DECL_OVERRIDE; | |
void axis_scale(lp_cartesian_axis *axis, int surface, float diff,bool isHorizontal); | |
void axis_translate(lp_cartesian_axis *axis, int surface, float diff,bool isHorizontal); | |
void axis_scroll(lp_cartesian_axis *axis, int steps,bool isVertical); | |
void initializeGL() Q_DECL_OVERRIDE; | |
void resizeGL(int w, int h) Q_DECL_OVERRIDE; | |
void paintGL() Q_DECL_OVERRIDE; | |
void replot(uint size); | |
lp_cartesian *m_cartesian; | |
lp_cartesian_axis *m_axis_left; | |
lp_cartesian_axis *m_axis_bottom; | |
lp_cartesian_curve *m_curve; | |
lp_circle *m_circle; | |
lp_text *m_text; | |
lp_vec2f *m_data; | |
lp_vec2f m_dpi; | |
uint m_data_len; | |
uint previous=0; | |
bool translate_active, scale_active; | |
bool firstPlot=true; | |
bool autoZoom=true; | |
int last_x, last_y; | |
int ch; | |
int m_surface_width, m_surface_height; | |
uint mySize=60*1000; | |
QVector <float> myVec; | |
int test=0; | |
signals: | |
void xmoved(QVector<float> l); | |
}; | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment