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