Rev 2116 Rev 2222
1 #include "scope.h" 1 #include "scope.h"
2   2  
3 #include <QPainter> 3 #include <QPainter>
4 #include <iostream> 4 #include <iostream>
5 #include <stdio.h> 5 #include <stdio.h>
6   6  
7 using namespace std; 7 using namespace std;
8   8  
9 #define LEN 128 9 #define LEN 128
10   10  
11   11  
12 Scope::Scope(QWidget *parent) { 12 Scope::Scope(QWidget *parent) {
13 dataAquired=0; 13 dataAquired=0;
14   14  
15 updateTimer = new QTimer(this); 15 updateTimer = new QTimer(this);
16 updateTimer->start(250); 16 updateTimer->start(250);
17 connect(updateTimer, SIGNAL(timeout()), this, SLOT(getData())); 17 connect(updateTimer, SIGNAL(timeout()), this, SLOT(getData()));
18 } 18 }
19   19  
20   20  
21 Scope::~Scope() { 21 Scope::~Scope() {
22 delete [] data; 22 delete [] data;
23 } 23 }
24   24  
25   25  
26 void Scope::paintEvent(QPaintEvent* event) { 26 void Scope::paintEvent(QPaintEvent* event) {
27 if (dataAquired) { 27 if (dataAquired) {
28 QPainter painter(this); 28 QPainter painter(this);
29   29  
30 painter.drawLine(9,45,9,300); 30 painter.drawLine(9,45,9,300);
31 painter.drawLine(522,45,522,300); 31 painter.drawLine(522,45,522,300);
32 painter.drawLine(9,45,522,45); 32 painter.drawLine(9,45,522,45);
33 painter.drawLine(9,300,522,300); 33 painter.drawLine(9,300,522,300);
34 int i; 34 int i;
35 for(i=0;i<LEN-1;i++) { 35 for(i=0;i<LEN-1;i++) {
36 painter.drawLine(4*i+10,300-(unsigned char)data[i],4*i+4+10,300-(unsigned char)data[i+1]); 36 painter.drawLine(4*i+10,300-(unsigned char)data[i],4*i+4+10,300-(unsigned char)data[i+1]);
37 } 37 }
38 } 38 }
39 } 39 }
40   40  
41 void Scope::getData() { 41 void Scope::getData() {
42 char c='a'; 42 char c='a';
43 data=new char[LEN*sizeof(char)]; 43 data=new char[LEN*sizeof(char)];
44   44  
45 FILE *serial=fopen("/dev/ttyUSB0","r+"); 45 FILE *serial=fopen("/dev/ttyUSB0","r+");
46 fwrite(&c,sizeof(char),1,serial); // ask for data 46 fwrite(&c,sizeof(char),1,serial); // ask for data
47 fread(data,sizeof(char),LEN,serial); // and take them 47 fread(data,sizeof(char),LEN,serial); // and take them
48 fclose(serial); 48 fclose(serial);
49   49  
50 dataAquired=1; 50 dataAquired=1;
51 update(); 51 update();
52 } 52 }
53   53