Files
oscilloscope/osciwidget.cpp
T

128 lines
3.2 KiB
C++
Raw Normal View History

2019-08-25 18:22:31 +02:00
#include "osciwidget.h"
2019-09-07 23:00:37 +02:00
// Qt includes
2019-09-05 20:46:54 +02:00
#include <QLineF>
#include <QDebug>
#include <QPainter>
#include <QTimerEvent>
2019-08-25 18:22:31 +02:00
2019-09-16 18:09:44 +02:00
// system includes
#include <cmath>
2019-08-25 22:42:34 +02:00
OsciWidget::OsciWidget(QWidget *parent) :
2019-09-07 12:00:14 +02:00
QOpenGLWidget{parent},
2019-09-05 20:46:54 +02:00
m_redrawTimerId(startTimer(1000/m_fps))
2019-08-25 22:42:34 +02:00
{
2019-09-07 19:19:24 +02:00
m_statsTimer.start();
2019-08-25 22:42:34 +02:00
}
int OsciWidget::lightspeed() const
{
return std::cbrt(m_lightspeed) * 20.f;
}
2019-09-05 20:46:54 +02:00
void OsciWidget::setFps(int fps)
{
killTimer(m_redrawTimerId);
2019-09-07 12:00:14 +02:00
2019-09-05 20:46:54 +02:00
m_fps = fps;
2019-09-07 12:00:14 +02:00
2019-09-05 20:46:54 +02:00
m_redrawTimerId = startTimer(1000/m_fps);
}
void OsciWidget::setAfterglow(float afterglow){
m_afterglow = afterglow;
// percentage of the image that should be visible after one second
// i.e. factor^fps=afterglow -> factor = afterglow^(1/fps)
m_afterglowColor = 255 * pow(afterglow, 1.0/m_fps);
}
void OsciWidget::setLightspeed(int lightspeed) {
const auto temp = (float(lightspeed)/20.f);
m_lightspeed = temp*temp*temp;
qDebug() << m_lightspeed;
}
2019-08-25 22:42:34 +02:00
void OsciWidget::renderSamples(const SamplePair *begin, const SamplePair *end)
2019-08-25 18:22:31 +02:00
{
2019-09-07 12:00:14 +02:00
m_callbacksCounter++;
2019-08-25 18:22:31 +02:00
2019-09-07 12:00:14 +02:00
m_buffer.insert(m_buffer.end(), begin, end);
2019-08-25 18:22:31 +02:00
}
void OsciWidget::paintEvent(QPaintEvent *event)
{
2019-09-05 20:46:54 +02:00
QWidget::paintEvent(event);
m_frameCounter++;
2019-09-07 19:19:24 +02:00
if (m_statsTimer.hasExpired(1000))
2019-09-05 20:46:54 +02:00
{
2019-09-07 19:19:24 +02:00
emit statusUpdate(QString("%0FPS (%1 audio callbacks)").arg(m_frameCounter).arg(m_callbacksCounter));
2019-09-05 20:46:54 +02:00
m_frameCounter = 0;
2019-09-07 12:00:14 +02:00
m_callbacksCounter = 0;
2019-09-07 19:19:24 +02:00
m_statsTimer.restart();
2019-09-05 20:46:54 +02:00
}
2019-08-25 18:22:31 +02:00
QPainter painter(this);
painter.drawPixmap(0, 0, m_pixmap);
}
void OsciWidget::updateFrameBuffer()
{
// Workaround for flickering (do not update, when there is no new data)
if(m_buffer.empty()) return;
2019-09-07 19:19:24 +02:00
if (m_pixmap.size() != size())
m_pixmap = QPixmap(size());
2019-09-05 20:46:54 +02:00
QPainter painter(&m_pixmap);
2019-09-07 19:19:24 +02:00
// darkening last frame
painter.setCompositionMode(QPainter::CompositionMode_Multiply);
painter.setPen({});
painter.setBrush(QColor(m_afterglowColor, m_afterglowColor, m_afterglowColor));
2019-09-07 19:19:24 +02:00
painter.drawRect(m_pixmap.rect());
2019-09-07 12:00:14 +02:00
// drawing new lines ontop
QPen pen;
pen.setWidth(2);
pen.setColor(QColor(0, 255, 0));
painter.setPen(pen);
2019-09-07 19:19:24 +02:00
painter.translate(m_pixmap.width()/2, m_pixmap.height()/2);
2019-09-07 12:00:14 +02:00
painter.setCompositionMode(QPainter::CompositionMode_Plus);
2019-09-07 19:19:24 +02:00
const auto pointToCoordinates = [width=m_pixmap.width()/2,height=m_pixmap.height()/2,factor=m_factor](const QPointF &point)
2019-09-07 12:00:14 +02:00
{
return QPoint{
int(point.x() * factor * width),
int(point.y() * factor * height)
};
};
for (const auto &i : m_buffer)
{
const QPointF p{
float(i.first) / std::numeric_limits<qint16>::max(),
float(-i.second) / std::numeric_limits<qint16>::max()
};
const QLineF line(m_lastPoint, p);
painter.setOpacity(std::min(1.0, 1. / ((line.length() * m_lightspeed) + 1)));
2019-09-07 12:00:14 +02:00
painter.drawLine(pointToCoordinates(m_lastPoint), pointToCoordinates(p));
m_lastPoint = p;
}
2019-09-07 19:19:24 +02:00
m_buffer.clear();
2019-09-05 20:46:54 +02:00
}
void OsciWidget::timerEvent(QTimerEvent *event)
{
QWidget::timerEvent(event);
if (event->timerId() == m_redrawTimerId){
updateFrameBuffer();
2019-09-05 20:46:54 +02:00
repaint();
}
2019-08-25 18:22:31 +02:00
}