Rev Author Line No. Line
2108 paro 1 #include "scope.h"
2  
3 #include <QPainter>
4 #include <QString>
5  
6 #include <iostream>
7 #include <stdio.h>
8  
9 using namespace std;
10  
2110 paro 11  
2115 paro 12 Scope::Scope(QWidget *parent) { // CONSTRUCTOR, graphic stuff
2108 paro 13 dataAquired=0;
14  
2115 paro 15 // data=new int[LEN*sizeof(int)]; ;
2108 paro 16  
17 int i;
2115 paro 18 for(i=0;i<LEN;i++) // buffer init
2108 paro 19 data[i]=0;
20  
2115 paro 21 scale=DEF_SCALE; // default coords: data[i] = (data[i] + shift)/scale;
2108 paro 22 shift=DEF_SHIFT;
23  
2115 paro 24 scaleSlider = new QSlider(Qt::Horizontal,this);
2110 paro 25 scaleSlider->setRange(1, 256);
2108 paro 26 scaleSlider->setValue(DEF_SCALE);
27 connect(scaleSlider, SIGNAL(valueChanged(int)), this, SLOT(setScale(int)));
28  
2115 paro 29 shiftSlider = new QSlider(Qt::Horizontal,this);
2108 paro 30 shiftSlider->setRange(-60000, +60000);
31 shiftSlider->setValue(DEF_SHIFT);
32 connect(shiftSlider, SIGNAL(valueChanged(int)), this, SLOT(setShift(int)));
33  
34 updateTimer = new QTimer(this);
2110 paro 35 updateTimer->start(TICK);
2108 paro 36 connect(updateTimer, SIGNAL(timeout()), this, SLOT(getData()));
37  
2115 paro 38 QLabel ( const QString & text, QWidget * parent = 0, Qt::WindowFlags f = 0 )
39 scaleSlider->setGeometry(QRect(20,600,400,60));
2108 paro 40 scaleSlider->show();
2115 paro 41  
42 shiftSlider->setGeometry(QRect(20,680,400,60));
2108 paro 43 shiftSlider->show();
44  
45 // getData();
46  
47 }
48  
49  
50 Scope::~Scope() {
51 delete [] data;
52 }
53  
54  
55 void Scope::paintEvent(QPaintEvent* event) {
56  
57 QPainter painter(this);
58  
59 if (dataAquired) {
2115 paro 60 painter.drawLine(LEFT-1,TOP,LEFT-1,TOP+HEIGHT); //vertical bound box lines
2110 paro 61 painter.drawLine(LEFT+WIDTH+1,TOP,LEFT+WIDTH+1,TOP+HEIGHT);
2108 paro 62  
2115 paro 63 painter.drawLine(LEFT-1,TOP,LEFT+WIDTH+1,TOP); //horizontals bound box lines
2110 paro 64 painter.drawLine(LEFT-1,TOP+HEIGHT,LEFT+WIDTH+1,TOP+HEIGHT);
65 painter.drawLine(LEFT-1,TOP+HEIGHT/2,LEFT+WIDTH+1,TOP+HEIGHT/2);
2108 paro 66  
67 int i;
68  
69 for(i=0;i<LEN;i++) {
70 data[i] = (data[i] + shift)/scale;
71 }
72  
73 for(i=0;i<LEN-1;i++) {
2110 paro 74 painter.drawLine(PIXPT_X*i+LEFT,TOP+HEIGHT/2-PIXPT_Y*data[i],PIXPT_X*(i+1)+LEFT,TOP+HEIGHT/2-data[i+1]*PIXPT_Y);
2108 paro 75 }
2115 paro 76 cout << "Screen updated"<<endl;
2108 paro 77 }
78 }
79  
2110 paro 80  
2108 paro 81 void Scope::getData() {
82 char c='m';
83  
2115 paro 84 cout << "Serial transfer start."<<endl;
2108 paro 85 FILE *serial=fopen("/dev/ttyUSB0","r+");
86  
2115 paro 87 if (serial==NULL) {
88 cout << "Serial port error."<<endl;
89 } else {
90 cout << "Serial port open."<<endl;
91 fwrite(&c,sizeof(char),1,serial); // poprosime o data
92 fread(buffer,sizeof(uint8_t),2*LEN,serial); // a berem je
93 cout << "Serial transfer completed."<<endl;
94 fclose(serial);
95  
96 int i;
97 for(i=0;i<LEN;i++) {
98 data[i]=buffer[2*i]*256+buffer[2*i+1];
99 // cout << 1*data[i] <<endl;
100 }
101 cout << "Screen updating...."<<endl;
102 dataAquired=1;
103 update();
2108 paro 104 }
105 }
106  
107  
108 void Scope::setScale(int val) {
109 scale=val;
110 }
111  
112 void Scope::setShift(int val) {
113 shift=val;
114 }