Rev 2119 Rev 3500
1 #include "scope.h" 1 #include "scope.h"
2   2  
3 #include <QPainter> 3 #include <QPainter>
4 #include <QString> 4 #include <QString>
5   5  
6 #include <iostream> 6 #include <iostream>
7 #include <stdio.h> 7 #include <stdio.h>
8   8  
9 using namespace std; 9 using namespace std;
10   10  
11 void UI(void); 11 void UI(void);
12   12  
13 Scope::Scope(QWidget *parent) { // CONSTRUCTOR, graphic stuff 13 Scope::Scope(QWidget *parent) { // CONSTRUCTOR, graphic stuff
14 dataAquired=0; 14 dataAquired=0;
15   15  
16 // data=new int[LEN*sizeof(int)]; ; 16 // data=new int[LEN*sizeof(int)]; ;
17   17  
18 int i; 18 int i;
19 for(i=0;i<LEN;i++) // buffer init 19 for(i=0;i<LEN;i++) // buffer init
20 data[i]=0; 20 data[i]=0;
21   21  
22 scale=DEF_SCALE; // default coords: data[i] = (data[i] + shift)/scale; 22 scale=DEF_SCALE; // default coords: data[i] = (data[i] + shift)/scale;
23 shift=DEF_SHIFT; 23 shift=DEF_SHIFT;
24 24
25 scaleSlider = new QSlider(Qt::Horizontal,this); 25 scaleSlider = new QSlider(Qt::Horizontal,this);
26 scaleSlider->setRange(1, 256); 26 scaleSlider->setRange(1, 256);
27 scaleSlider->setValue(DEF_SCALE); 27 scaleSlider->setValue(DEF_SCALE);
28 connect(scaleSlider, SIGNAL(valueChanged(int)), this, SLOT(setScale(int))); 28 connect(scaleSlider, SIGNAL(valueChanged(int)), this, SLOT(setScale(int)));
29 29
30 shiftSlider = new QSlider(Qt::Horizontal,this); 30 shiftSlider = new QSlider(Qt::Horizontal,this);
31 shiftSlider->setRange(0, +60000); 31 shiftSlider->setRange(0, +60000);
32 shiftSlider->setValue(DEF_SHIFT); 32 shiftSlider->setValue(DEF_SHIFT);
33 connect(shiftSlider, SIGNAL(valueChanged(int)), this, SLOT(setShift(int))); 33 connect(shiftSlider, SIGNAL(valueChanged(int)), this, SLOT(setShift(int)));
34   34  
35 updateTimer = new QTimer(this); 35 updateTimer = new QTimer(this);
36 updateTimer->start(TICK); 36 updateTimer->start(TICK);
37 connect(updateTimer, SIGNAL(timeout()), this, SLOT(getData())); 37 connect(updateTimer, SIGNAL(timeout()), this, SLOT(getData()));
38 38
39 labels[1] = new QLabel(this); 39 labels[1] = new QLabel(this);
40 /*QString tempStr; 40 /*QString tempStr;
41 tempStr.setNum(shift); 41 tempStr.setNum(shift);
42 labels[1]->setText("Zero level: " + tempStr); 42 labels[1]->setText("Zero level: " + tempStr);
43 labels[1]->setGeometry (20, 550, 200, 20); 43 labels[1]->setGeometry (20, 550, 200, 20);
44 labels[1]->show(); 44 labels[1]->show();
45 shiftSlider->setGeometry(QRect(20,580,400,60)); 45 shiftSlider->setGeometry(QRect(20,580,400,60));
46 shiftSlider->show();*/ 46 shiftSlider->show();*/
47   47  
48   48  
49 labels[2] = new QLabel(this); 49 labels[2] = new QLabel(this);
50 this->UI();/* 50 this->UI();/*
51 tempStr.setNum(scale); 51 tempStr.setNum(scale);
52 labels[2]->setText("Divisor: " + tempStr); 52 labels[2]->setText("Divisor: " + tempStr);
53 labels[2]->setGeometry (20, 630, 400, 20); 53 labels[2]->setGeometry (20, 630, 400, 20);
54 labels[2]->show(); 54 labels[2]->show();
55 scaleSlider->setGeometry(QRect(20,660,400,60)); 55 scaleSlider->setGeometry(QRect(20,660,400,60));
56 scaleSlider->show();*/ 56 scaleSlider->show();*/
57 57
58 } 58 }
59   59  
60 void Scope::UI(void) { 60 void Scope::UI(void) {
61 QString tempStr; 61 QString tempStr;
62 tempStr.setNum(shift); 62 tempStr.setNum(shift);
63 labels[1]->setText("Zero level: " + tempStr); 63 labels[1]->setText("Zero level: " + tempStr);
64 labels[1]->setGeometry (20, 550, 200, 20); 64 labels[1]->setGeometry (20, 550, 200, 20);
65 labels[1]->show(); 65 labels[1]->show();
66 shiftSlider->setGeometry(QRect(20,580,400,60)); 66 shiftSlider->setGeometry(QRect(20,580,400,60));
67 shiftSlider->show(); 67 shiftSlider->show();
68   68  
69 tempStr.setNum(scale); 69 tempStr.setNum(scale);
70 labels[2]->setText("Divisor: " + tempStr); 70 labels[2]->setText("Divisor: " + tempStr);
71 labels[2]->setGeometry (20, 630, 400, 20); 71 labels[2]->setGeometry (20, 630, 400, 20);
72 labels[2]->show(); 72 labels[2]->show();
73 scaleSlider->setGeometry(QRect(20,660,400,60)); 73 scaleSlider->setGeometry(QRect(20,660,400,60));
74 scaleSlider->show(); 74 scaleSlider->show();
75 } 75 }
76   76  
77   77  
78   78  
79   79  
80   80  
81   81  
82 Scope::~Scope() { 82 Scope::~Scope() {
83 delete [] data; 83 delete [] data;
84 } 84 }
85   85  
86   86  
87 void Scope::paintEvent(QPaintEvent* event) { 87 void Scope::paintEvent(QPaintEvent* event) {
88   88  
89 QPainter painter(this); 89 QPainter painter(this);
90   90  
91 if (dataAquired) { 91 if (dataAquired) {
92 painter.drawLine(LEFT-1,TOP,LEFT-1,TOP+HEIGHT); //vertical bound box lines 92 painter.drawLine(LEFT-1,TOP,LEFT-1,TOP+HEIGHT); //vertical bound box lines
93 painter.drawLine(LEFT+WIDTH+1,TOP,LEFT+WIDTH+1,TOP+HEIGHT); 93 painter.drawLine(LEFT+WIDTH+1,TOP,LEFT+WIDTH+1,TOP+HEIGHT);
94   94  
95 painter.drawLine(LEFT-1,TOP,LEFT+WIDTH+1,TOP); //horizontals bound box lines 95 painter.drawLine(LEFT-1,TOP,LEFT+WIDTH+1,TOP); //horizontals bound box lines
96 painter.drawLine(LEFT-1,TOP+HEIGHT,LEFT+WIDTH+1,TOP+HEIGHT); 96 painter.drawLine(LEFT-1,TOP+HEIGHT,LEFT+WIDTH+1,TOP+HEIGHT);
97 painter.drawLine(LEFT-1,TOP+HEIGHT/2,LEFT+WIDTH+1,TOP+HEIGHT/2); 97 painter.drawLine(LEFT-1,TOP+HEIGHT/2,LEFT+WIDTH+1,TOP+HEIGHT/2);
98   98  
99 int i; 99 int i;
100 100
101 for(i=0;i<LEN;i++) { 101 for(i=0;i<LEN;i++) {
102 data[i] = (data[i] - shift)/scale; 102 data[i] = (data[i] - shift)/scale;
103 } 103 }
104   104  
105 for(i=0;i<LEN-1;i++) { 105 for(i=0;i<LEN-1;i++) {
106 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); 106 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);
107 } 107 }
108 //cout << "Screen updated"<<endl; 108 //cout << "Screen updated"<<endl;
109 } 109 }
110 } 110 }
111   111  
112   112  
113 void Scope::getData() { 113 void Scope::getData() {
114 char c='m'; 114 char c='m';
115   115  
116 cout << "Serial transfer start."<<endl; 116 cout << "Serial transfer start."<<endl;
117 FILE *serial=fopen("/dev/ttyUSB0","r+"); 117 FILE *serial=fopen("/dev/ttyUSB0","r+");
118   118  
119 if (serial==NULL) { 119 if (serial==NULL) {
120 cout << "Serial port error."<<endl; 120 cout << "Serial port error."<<endl;
121 } else { 121 } else {
122 //cout << "Serial port open."<<endl; 122 //cout << "Serial port open."<<endl;
123 123
124 fwrite(&c,sizeof(char),1,serial); // poprosime o data 124 fwrite(&c,sizeof(char),1,serial); // poprosime o data
125 fread(buffer,sizeof(uint8_t),2*LEN,serial); // a berem je 125 fread(buffer,sizeof(uint8_t),2*LEN,serial); // a berem je
126 126
127 // cout << "Serial transfer completed."<<endl; 127 // cout << "Serial transfer completed."<<endl;
128 fclose(serial); 128 fclose(serial);
129 129
130 int i; 130 int i;
131 for(i=0;i<LEN;i++) { 131 for(i=0;i<LEN;i++) {
132 data[i]=buffer[2*i]*256+buffer[2*i+1]; 132 data[i]=buffer[2*i]*256+buffer[2*i+1];
133 // cout << 1*data[i] <<endl; 133 // cout << 1*data[i] <<endl;
134 } 134 }
135 // cout << "Screen updating...."<<endl; 135 // cout << "Screen updating...."<<endl;
136 136
137 dataAquired=1; 137 dataAquired=1;
138 update(); 138 update();
139 } 139 }
140 } 140 }
141   141  
142   142  
143 void Scope::setScale(int val) { 143 void Scope::setScale(int val) {
144 scale=val; 144 scale=val;
145 UI(); 145 UI();
146 } 146 }
147   147  
148 void Scope::setShift(int val) { 148 void Scope::setShift(int val) {
149 shift=val; 149 shift=val;
150 UI(); 150 UI();
151 } 151 }