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