-
Notifications
You must be signed in to change notification settings - Fork 3
/
samplingthread.cpp
116 lines (102 loc) · 2.62 KB
/
samplingthread.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include "signaldata.h"
#include <qwt_math.h>
#include <math.h>
#include <QDebug>
#include <QString>
#include "samplingthread.h"
#include <QStringList>
#if QT_VERSION < 0x040600
#define qFastSin(x) ::sin(x)
#endif
SamplingThread::SamplingThread( QObject *parent ):
QwtSamplingThread( parent ),
// d_frequency( 5.0 ),
d_amplitude( 400.0 )
{
factor=1.8;
}
void SamplingThread::setPort(QString qsport)
{
qDebug() << qsport;
portname=qsport;
qDebug() <<portname;
// http://automon.donaloconnor.net/qt-and-qextserialport/34/
PortSettings portSettings;
portSettings.BaudRate = BAUD57600;
portSettings.DataBits = DATA_8;
portSettings.Parity = PAR_NONE;
// portSettings.StopBits = STOP_1;
// portSettings.FlowControl = FLOW_OFF;
// portSettings.Timeout_Millisec = 0;
//
//
// Setting port here:
qDebug() <<portname;
port = new QextSerialPort(portname,portSettings);
bool res = false;
res = port->open(QextSerialPort::ReadOnly);
if(res){
qDebug("Connected!!\n");
// Throws away data waiting:
QByteArray inpt;
int a = port->bytesAvailable();
qDebug()<< a << "\n";
inpt.resize(a);
port->read(inpt.data(), inpt.size());
}
else
qDebug("Connection failed!!\n");
}
void SamplingThread::setFrequency( double frequency )
{
d_frequency = frequency;
}
double SamplingThread::frequency() const
{
return d_frequency;
}
void SamplingThread::setAmplitude( double amplitude )
{
d_amplitude = amplitude;
}
double SamplingThread::amplitude() const
{
return d_amplitude;
}
void SamplingThread::sample( double elapsed )
{
if ( d_frequency > 0.0 )
{
const QPointF s( elapsed, value( elapsed ) );
SignalData::instance().append( s );
}
}
double SamplingThread::value( double timeStamp )
{
double v;
traway++;
emit overload(false); // turns of overload led (in case it was on)
if(buffer.count("\n")>10){
buffer=buffer.mid(buffer.lastIndexOf("\n")+1,-1);
qDebug()<<traway << "overrun\n";
traway=0;
emit overload(true); // makes the red "led" flash.
}
QByteArray inpt;
int a = port->bytesAvailable();
inpt.resize(a);
port->read(inpt.data(), inpt.size());
QString strng=buffer+QString::fromAscii(inpt);
int j=strng.indexOf("\n");
if(j==1) {
qDebug() << "-"<<strng <<"-";
strng=strng.right(1);
}
if(j>-1){
QString s=strng.left(j-1);
v=s.toFloat();
// Saves for next time what has so far not been read:
buffer=strng.mid(j+1,-1);
return v*d_amplitude/5;
}
}