#include "scope.h"
#include <QPainter>
#include <QString>
#include <iostream>
#include <stdio.h>
using namespace std;
#define LEN 256
#define DEF_SCALE 64
#define DEF_SHIFT 0
#define LEFT 10
#define WIDTH 512
#define HEIGHT 600
#define PIXPT 2
Scope::Scope(QWidget *parent) {
dataAquired=0;
head=-1;
data=new long[LEN*sizeof(long)]; ;
int i;
for(i=1;i<LEN;i++)
data[i]=0;
scale=DEF_SCALE;
shift=DEF_SHIFT;
scaleSlider = new QSlider(Qt::Horizontal);
scaleSlider->setRange(1, 128);
scaleSlider->setValue(DEF_SCALE);
connect(scaleSlider, SIGNAL(valueChanged(int)), this, SLOT(setScale(int)));
shiftSlider = new QSlider(Qt::Horizontal);
shiftSlider->setRange(-60000, +60000);
shiftSlider->setValue(DEF_SHIFT);
connect(shiftSlider, SIGNAL(valueChanged(int)), this, SLOT(setShift(int)));
updateTimer = new QTimer(this);
updateTimer->start(1);
connect(updateTimer, SIGNAL(timeout()), this, SLOT(getData()));
scaleSlider->setGeometry(QRect(50,250,400,10));
scaleSlider->show();
shiftSlider->setGeometry(QRect(50,300,800,10));
shiftSlider->show();
}
Scope::~Scope() {
delete [] data;
}
void Scope::paintEvent(QPaintEvent* event) {
QPainter painter(this);
if (head>-1)
painter.drawText(10,20,QString::number(data[head]));
if (dataAquired) {
painter.drawLine(LEFT-1,45,LEFT-1,HEIGHT);
painter.drawLine(LEFT+WIDTH,45,LEFT+WIDTH,HEIGHT);
painter.drawLine(LEFT-1,45,LEFT+WIDTH,45);
painter.drawLine(LEFT-1,HEIGHT,LEFT+WIDTH,HEIGHT);
painter.drawLine(LEFT-1,HEIGHT/2,LEFT+WIDTH,HEIGHT/2);
int i;
data[head] = (data[head] + shift)/scale;
data[head] = data[head] + HEIGHT/2;
for(i=head+1;i<LEN-1;i++)
painter.drawLine(PIXPT*(i-head)+LEFT,HEIGHT-data[i],PIXPT*(i-head+1)+LEFT,HEIGHT-data[i+1]);
painter.drawLine(PIXPT*(LEN-head-1)+LEFT,HEIGHT-data[LEN-1],PIXPT*(LEN-head+1)+LEFT,HEIGHT-data[0]);
for(i=0;i<head;i++)
painter.drawLine(PIXPT*(i+LEN-head)+LEFT,HEIGHT-data[i],PIXPT*(i+LEN-head+1)+10,HEIGHT-data[i+1]);
}
}
void Scope::getData() {
char c='v';
FILE *serial=fopen("/dev/ttyUSB0","r+");
fwrite(&c,sizeof(char),1,serial); // poprosime o data
if (head != LEN-1)
head++;
else
head=0;
fscanf(serial,"%ld",&data[head]);
fclose(serial);
cout << data[head] <<endl;
if (head>0)
dataAquired=1;
update();
}
void Scope::setScale(int val) {
scale=val;
}
void Scope::setShift(int val) {
shift=val;
}