2116 |
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 256 |
|
|
12 |
#define DEF_SCALE 64 |
|
|
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 long[LEN*sizeof(long)]; ; |
|
|
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(1); |
|
|
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 |
|
|
|
54 |
|
|
|
55 |
Scope::~Scope() { |
|
|
56 |
delete [] data; |
|
|
57 |
} |
|
|
58 |
|
|
|
59 |
|
|
|
60 |
void Scope::paintEvent(QPaintEvent* event) { |
|
|
61 |
|
|
|
62 |
QPainter painter(this); |
|
|
63 |
|
|
|
64 |
if (head>-1) |
|
|
65 |
painter.drawText(10,20,QString::number(data[head])); |
|
|
66 |
|
|
|
67 |
if (dataAquired) { |
|
|
68 |
painter.drawLine(LEFT-1,45,LEFT-1,HEIGHT); |
|
|
69 |
painter.drawLine(LEFT+WIDTH,45,LEFT+WIDTH,HEIGHT); |
|
|
70 |
painter.drawLine(LEFT-1,45,LEFT+WIDTH,45); |
|
|
71 |
painter.drawLine(LEFT-1,HEIGHT,LEFT+WIDTH,HEIGHT); |
|
|
72 |
|
|
|
73 |
painter.drawLine(LEFT-1,HEIGHT/2,LEFT+WIDTH,HEIGHT/2); |
|
|
74 |
|
|
|
75 |
int i; |
|
|
76 |
|
|
|
77 |
data[head] = (data[head] + shift)/scale; |
|
|
78 |
|
|
|
79 |
data[head] = data[head] + HEIGHT/2; |
|
|
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 |
} |
|
|
88 |
} |
|
|
89 |
|
|
|
90 |
void Scope::getData() { |
|
|
91 |
char c='v'; |
|
|
92 |
|
|
|
93 |
FILE *serial=fopen("/dev/ttyUSB0","r+"); |
|
|
94 |
fwrite(&c,sizeof(char),1,serial); // poprosime o data |
|
|
95 |
|
|
|
96 |
if (head != LEN-1) |
|
|
97 |
head++; |
|
|
98 |
else |
|
|
99 |
head=0; |
|
|
100 |
|
|
|
101 |
fscanf(serial,"%ld",&data[head]); |
|
|
102 |
fclose(serial); |
|
|
103 |
|
|
|
104 |
cout << data[head] <<endl; |
|
|
105 |
|
|
|
106 |
if (head>0) |
|
|
107 |
dataAquired=1; |
|
|
108 |
|
|
|
109 |
update(); |
|
|
110 |
} |
|
|
111 |
|
|
|
112 |
|
|
|
113 |
void Scope::setScale(int val) { |
|
|
114 |
scale=val; |
|
|
115 |
} |
|
|
116 |
|
|
|
117 |
void Scope::setShift(int val) { |
|
|
118 |
shift=val; |
|
|
119 |
} |