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