Rev 2110 Rev 2115
Line 6... Line 6...
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 #define TICK 150 // time in ms for one sampling (LEN values) -  
12 #define LEN 100 // number of sampled values -  
13   11  
14 #define DEF_SCALE 256 //divisor -  
15 #define DEF_SHIFT 0 -  
16   -  
17 #define LEFT 10 -  
18 #define TOP 10 -  
19 #define PIXPT_X 4 -  
20 #define PIXPT_Y 1 -  
21 #define WIDTH (PIXPT_X*LEN) -  
22 #define HEIGHT (PIXPT_Y*512) -  
23   -  
24   -  
25 Scope::Scope(QWidget *parent) { 12 Scope::Scope(QWidget *parent) { // CONSTRUCTOR, graphic stuff
26 dataAquired=0; 13 dataAquired=0;
27   14  
28 data=new int[LEN*sizeof(int)]; ; 15 // data=new int[LEN*sizeof(int)]; ;
29   16  
30 int i; 17 int i;
31 for(i=0;i<LEN;i++) 18 for(i=0;i<LEN;i++) // buffer init
32 data[i]=0; 19 data[i]=0;
33   20  
34 scale=DEF_SCALE; 21 scale=DEF_SCALE; // default coords: data[i] = (data[i] + shift)/scale;
35 shift=DEF_SHIFT; 22 shift=DEF_SHIFT;
36 23
37 scaleSlider = new QSlider(Qt::Horizontal); 24 scaleSlider = new QSlider(Qt::Horizontal,this);
38 scaleSlider->setRange(1, 256); 25 scaleSlider->setRange(1, 256);
39 scaleSlider->setValue(DEF_SCALE); 26 scaleSlider->setValue(DEF_SCALE);
40 connect(scaleSlider, SIGNAL(valueChanged(int)), this, SLOT(setScale(int))); 27 connect(scaleSlider, SIGNAL(valueChanged(int)), this, SLOT(setScale(int)));
41 28
42 shiftSlider = new QSlider(Qt::Horizontal); 29 shiftSlider = new QSlider(Qt::Horizontal,this);
43 shiftSlider->setRange(-60000, +60000); 30 shiftSlider->setRange(-60000, +60000);
44 shiftSlider->setValue(DEF_SHIFT); 31 shiftSlider->setValue(DEF_SHIFT);
45 connect(shiftSlider, SIGNAL(valueChanged(int)), this, SLOT(setShift(int))); 32 connect(shiftSlider, SIGNAL(valueChanged(int)), this, SLOT(setShift(int)));
46   33  
47 updateTimer = new QTimer(this); 34 updateTimer = new QTimer(this);
48 updateTimer->start(TICK); 35 updateTimer->start(TICK);
49 connect(updateTimer, SIGNAL(timeout()), this, SLOT(getData())); 36 connect(updateTimer, SIGNAL(timeout()), this, SLOT(getData()));
50 37
-   38 QLabel ( const QString & text, QWidget * parent = 0, Qt::WindowFlags f = 0 )
51 scaleSlider->setGeometry(QRect(50,250,400,10)); 39 scaleSlider->setGeometry(QRect(20,600,400,60));
52 scaleSlider->show(); 40 scaleSlider->show();
-   41
53 shiftSlider->setGeometry(QRect(50,300,800,10)); 42 shiftSlider->setGeometry(QRect(20,680,400,60));
54 shiftSlider->show(); 43 shiftSlider->show();
55   44  
56 // getData(); 45 // getData();
57   46  
58 } 47 }
Line 65... Line 54...
65   54  
66 void Scope::paintEvent(QPaintEvent* event) { 55 void Scope::paintEvent(QPaintEvent* event) {
67   56  
68 QPainter painter(this); 57 QPainter painter(this);
69   58  
70 /* if (head>-1) -  
71 painter.drawText(10,20,QString::number(data[head]));*/ -  
72   -  
73 if (dataAquired) { 59 if (dataAquired) {
74 painter.drawLine(LEFT-1,TOP,LEFT-1,TOP+HEIGHT); //verticals 60 painter.drawLine(LEFT-1,TOP,LEFT-1,TOP+HEIGHT); //vertical bound box lines
75 painter.drawLine(LEFT+WIDTH+1,TOP,LEFT+WIDTH+1,TOP+HEIGHT); 61 painter.drawLine(LEFT+WIDTH+1,TOP,LEFT+WIDTH+1,TOP+HEIGHT);
76   62  
77 painter.drawLine(LEFT-1,TOP,LEFT+WIDTH+1,TOP); //horizontals 63 painter.drawLine(LEFT-1,TOP,LEFT+WIDTH+1,TOP); //horizontals bound box lines
78 painter.drawLine(LEFT-1,TOP+HEIGHT,LEFT+WIDTH+1,TOP+HEIGHT); 64 painter.drawLine(LEFT-1,TOP+HEIGHT,LEFT+WIDTH+1,TOP+HEIGHT);
79 painter.drawLine(LEFT-1,TOP+HEIGHT/2,LEFT+WIDTH+1,TOP+HEIGHT/2); 65 painter.drawLine(LEFT-1,TOP+HEIGHT/2,LEFT+WIDTH+1,TOP+HEIGHT/2);
80   66  
81 int i; 67 int i;
82 68
Line 85... Line 71...
85 } 71 }
86   72  
87 for(i=0;i<LEN-1;i++) { 73 for(i=0;i<LEN-1;i++) {
88 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); 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);
89 } 75 }
-   76 cout << "Screen updated"<<endl;
90 } 77 }
91 } 78 }
92   79  
93   80  
94 void Scope::getData() { 81 void Scope::getData() {
95 char c='m'; 82 char c='m';
96 uint8_t buffer[2*LEN]; -  
97 -  
98   83  
-   84 cout << "Serial transfer start."<<endl;
99 FILE *serial=fopen("/dev/ttyUSB0","r+"); 85 FILE *serial=fopen("/dev/ttyUSB0","r+");
100 fwrite(&c,sizeof(char),1,serial); // poprosime o data -  
101 fread(buffer,sizeof(uint8_t),2*LEN,serial); // a berem je -  
102   86  
-   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
103 int i; 96 int i;
104 for(i=0;i<LEN;i++) { 97 for(i=0;i<LEN;i++) {
105 data[i]=buffer[2*i]*256+buffer[2*i+1]; 98 data[i]=buffer[2*i]*256+buffer[2*i+1];
106 cout << 1*data[i] <<endl; 99 // cout << 1*data[i] <<endl;
-   100 }
-   101 cout << "Screen updating...."<<endl;
-   102 dataAquired=1;
-   103 update();
107 } 104 }
108 dataAquired=1; -  
109 // head=LEN-1; -  
110   -  
111   -  
112 update(); -  
113 // cout << "Screen updated."<<endl; -  
114 } 105 }
115   106  
116   107  
117 void Scope::setScale(int val) { 108 void Scope::setScale(int val) {
118 scale=val; 109 scale=val;
119 } 110 }
120   111  
121 void Scope::setShift(int val) { 112 void Scope::setShift(int val) {
122 shift=val; 113 shift=val;
123 } 114 }
124   -  
125   -  
126   -