int main(int argc, char *argv[])
{
MainWindow w;
Global::w = &w;
Global::w->showFullScreen();
//Global::w->show();
init();
return a.exec();
}
void init()
{
pthread_t serial;
int thrid;
void *t;
thrid = pthread_create(&serial, NULL, &SerialRead, (void *) t);
Global::stigmaECG.updateUIEcgID = pthread_create(&(Global::stigmaECG.updateUIEcgThread), NULL, &updateUIEcg, (void *) Global::stigmaECG.tEcg);
Global::initialize();//I'm initializing integer values etc., no memory related process
}
void* SerialRead(void *t)
{
//serial port
StigmaSerialRead::fd = StigmaSerialRead::open_port();
StigmaSerialRead::set_port(StigmaSerialRead::fd);
while (1)
{
unsigned char* pack = stigmaSerialRead.SerialRead();
if (pack!=NULL)
{
int check = StigmaSerialUtil::checksum(pack, 8);
if (check == -2 || check == -1)
{
return;
}
unsigned char data_head = pack[1];
unsigned char ecg1_wave_high = pack[2];
unsigned char ecg1_wave_low = pack[3];
unsigned char ecg2_wave_high = pack[4];
unsigned char ecg2_wave_low = pack[5];
unsigned char status = pack[6];
if (data_head & 0x01);
else {
ecg1_wave_high &= 0x7F;
}
if (data_head & 0x02);
else {
ecg1_wave_low &= 0x7F;
}
if (data_head & 0x04);
else {
ecg2_wave_high &= 0x7F;
}
if (data_head & 0x08);
else {
ecg2_wave_low &= 0x7F;
}
if (data_head & 0x10);
else {
status &= 0x7F;
}
short int temp, mask;
mask = 0xFF00;
temp = 0;
temp = ecg1_wave_high;
temp = temp << 8;
temp = temp & mask;
temp += ecg1_wave_low;
short int differenceGG = temp - 2048;
pthread_mutex_lock( &(ecg_array_mutex));
ecg1Vector[beatX] = differenceGG;
pthread_mutex_unlock(&(ecg_array_mutex));
temp = 0;
temp = ecg2_wave_high;
temp = temp << 8;
temp = temp & mask;
temp += ecg2_wave_low;
pthread_mutex_lock( &(ecg_array_mutex));
ecg2Vector[beatX] = temp - 2048;
pthread_mutex_unlock(&(ecg_array_mutex));
char mask2 = 0x01;
temp = 0;
temp = status & mask2;
if (temp == 1)
{
printf("\007");
fflush(stdout);
}
mask2 = 0x02;
temp = 0;
temp = status & mask2;
if (temp == 1)
{
pacingPulse = 1;
printf("Pacing pulse");
fflush(stdout);
}
beatX = (++beatX) % zoommod; //2000
ecgHandlerIndexCounter++;
if(ecgHandlerIndexCounter == 40)
{
ecgHandlerIndexCounter = 0;
pthread_mutex_lock( &(ecg_update_mutex ));
pthread_cond_signal( &(ecg_condition_var));
pthread_mutex_unlock( &(ecg_update_mutex ));
}
internalCounter++;
}
}
close(StigmaSerialRead::fd);
}
void* updateUIEcg(void *tEcg)
{
for(;;)
{
pthread_mutex_lock( &(Global::stigmaECG.ecg_update_mutex ));
pthread_cond_wait( &(Global::stigmaECG.ecg_condition_var), &(Global::stigmaECG.ecg_update_mutex));
Global::w->refreshEcgGraphs();
pthread_mutex_unlock( &(Global::stigmaECG.ecg_update_mutex ));
}
}
void MainWindow::refreshEcgGraphs()
{
qreal scalex = ui->ecgChannelGraph1->calcScaleX(2000);
qreal scaley = ui->ecgChannelGraph1->calcScaleY();
qreal ty = ui->ecgChannelGraph1->calcTy();
QList<QPointF> ValueList1;
QList<QPointF> ValueList2;
int ind = countEcg*40;
for(int i=0;i<40;i++)
{
int sira = (ind + i)%2000;
pthread_mutex_lock( &(Global::stigmaECG.ecg_array_mutex));
double ecg1 = (double)(Global::stigmaECG.ecg1Vector[sira]);
double ecg2 = (double)(Global::stigmaECG.ecg2Vector[sira]);
pthread_mutex_unlock( &(Global::stigmaECG.ecg_array_mutex));
qreal orgX = (ind+i)%2000;
qreal xPos = orgX * scalex;
qreal orgY1 = ecg1;
qreal orgY2 = ecg2;
qreal yPos1 = (orgY1 + ty) * scaley;
qreal yPos2 = (orgY2 + ty) * scaley;
ValueList1.append(*ecg1Point);
ValueList2.append(*ecg2Point);
}
ui->ecgChannelGraph1->addPoint(ValueList1, QList<QList<QPointF> >(), "ECG1");
ui->ecgChannelGraph2->addPoint(ValueList2, QList<QList<QPointF> >(), "ECG2");
countEcg++;
countEcg = countEcg%(2000/40);
}
int QtBasicGraph::addPoint(const QList<QPointF> pointList, QList<QList<QPointF> > gridPointList, char* type)
{
try
{
Mutex.lock();
int size = pointList.size();
if(lines.isEmpty())
FirstPoint = pointList.first();
else
FirstPoint = lines.last().p2();
for (int i = 1 ; i < size; i++)
{
lines.
append(QLineF(FirstPoint, pt
));
FirstPoint = pt;
}
gridLines.clear();
for(int j = 0 ; j<gridPointList.size();j++)
{
QList<QPointF> gridLinePointPair = gridPointList.at(j);
QPointF start
= gridLinePointPair.
at(0);
QPointF end
= gridLinePointPair.
at(1);
gridLines.
append(QLineF(start, end
));
}
Mutex.unlock();
}
catch(char * str)
{
qDebug()<<"error"<<str;
}
}
//Repaint called every 50 ms and clearing lines, points
{
if (m_render_hints)
p.setRenderHints(m_render_hints);
QVarLengthArray<QLineF> temp;
QVarLengthArray<QLineF> tempGrids;
QVarLengthArray<QLineF> xLinelist;
QVarLengthArray<QLineF> yLinelist;
int lineIndex=0;
Mutex.lock();
int lineSize=lines.size();
if(lineSize!=0)
{
temp.append(lines[0]);
}
for (lineIndex = 0; lineIndex < lineSize-1; lineIndex++)
{
if(lines[lineIndex].p1().x()<lines[lineIndex+1].p1().x())
{
temp.append(lines[lineIndex+1]);
}
else
{
lines.erase(lines.begin(),lines.begin()+lineIndex+1);
if(temp[lineIndex].p2().x()<temp[lineIndex].p1().x())
{
temp.removeLast();
}
break;
}
}
if(lineIndex==lineSize-1)
{
lines.erase(lines.begin(), lines.end()-1);
}
for(int i = 0; i<gridLines.size();i++)
{
tempGrids.append(gridLines.at(i));
}
Mutex.unlock();
QRectF qrectf
(temp
[0].
p1().
x(),
0.0, temp
[temp.
size()-1].
p2().
x()-temp
[0].
p1().
x()+clearAfter, yRange
());
p.
fillRect(qrectf,
QBrush(Qt
::black, Qt
::SolidPattern));
p.setPen(gridPen);
p.drawLines(tempGrids.constData(), tempGrids.size());
if(temp[0].p1().x()<20)
{
for (int i = 0; i < xLines.size(); ++i)
{
qreal lineEnd=(temp[temp.size()-1].p2().x()+clearAfter) < 20 ? temp[temp.size()-1].p2().x()+clearAfter : 20;
xLinelist.
append( QLineF(temp
[0].
p1().
x(),
-xLines
[i
],lineEnd,
-xLines
[i
]));
}
p.drawLines(xLinelist.constData(), xLinelist.size());
}
p.setPen(lineColor);
p.drawLines(temp.constData(), temp.size());
temp.clear();
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
Global::w = &w;
Global::w->showFullScreen();
//Global::w->show();
init();
return a.exec();
}
void init()
{
pthread_t serial;
int thrid;
void *t;
thrid = pthread_create(&serial, NULL, &SerialRead, (void *) t);
Global::stigmaECG.updateUIEcgID = pthread_create(&(Global::stigmaECG.updateUIEcgThread), NULL, &updateUIEcg, (void *) Global::stigmaECG.tEcg);
Global::initialize();//I'm initializing integer values etc., no memory related process
}
void* SerialRead(void *t)
{
//serial port
StigmaSerialRead::fd = StigmaSerialRead::open_port();
StigmaSerialRead::set_port(StigmaSerialRead::fd);
while (1)
{
unsigned char* pack = stigmaSerialRead.SerialRead();
if (pack!=NULL)
{
int check = StigmaSerialUtil::checksum(pack, 8);
if (check == -2 || check == -1)
{
return;
}
unsigned char data_head = pack[1];
unsigned char ecg1_wave_high = pack[2];
unsigned char ecg1_wave_low = pack[3];
unsigned char ecg2_wave_high = pack[4];
unsigned char ecg2_wave_low = pack[5];
unsigned char status = pack[6];
if (data_head & 0x01);
else {
ecg1_wave_high &= 0x7F;
}
if (data_head & 0x02);
else {
ecg1_wave_low &= 0x7F;
}
if (data_head & 0x04);
else {
ecg2_wave_high &= 0x7F;
}
if (data_head & 0x08);
else {
ecg2_wave_low &= 0x7F;
}
if (data_head & 0x10);
else {
status &= 0x7F;
}
short int temp, mask;
mask = 0xFF00;
temp = 0;
temp = ecg1_wave_high;
temp = temp << 8;
temp = temp & mask;
temp += ecg1_wave_low;
short int differenceGG = temp - 2048;
pthread_mutex_lock( &(ecg_array_mutex));
ecg1Vector[beatX] = differenceGG;
pthread_mutex_unlock(&(ecg_array_mutex));
temp = 0;
temp = ecg2_wave_high;
temp = temp << 8;
temp = temp & mask;
temp += ecg2_wave_low;
pthread_mutex_lock( &(ecg_array_mutex));
ecg2Vector[beatX] = temp - 2048;
pthread_mutex_unlock(&(ecg_array_mutex));
char mask2 = 0x01;
temp = 0;
temp = status & mask2;
if (temp == 1)
{
printf("\007");
fflush(stdout);
}
mask2 = 0x02;
temp = 0;
temp = status & mask2;
if (temp == 1)
{
pacingPulse = 1;
printf("Pacing pulse");
fflush(stdout);
}
beatX = (++beatX) % zoommod; //2000
ecgHandlerIndexCounter++;
if(ecgHandlerIndexCounter == 40)
{
ecgHandlerIndexCounter = 0;
pthread_mutex_lock( &(ecg_update_mutex ));
pthread_cond_signal( &(ecg_condition_var));
pthread_mutex_unlock( &(ecg_update_mutex ));
}
internalCounter++;
}
}
close(StigmaSerialRead::fd);
}
void* updateUIEcg(void *tEcg)
{
for(;;)
{
pthread_mutex_lock( &(Global::stigmaECG.ecg_update_mutex ));
pthread_cond_wait( &(Global::stigmaECG.ecg_condition_var), &(Global::stigmaECG.ecg_update_mutex));
Global::w->refreshEcgGraphs();
pthread_mutex_unlock( &(Global::stigmaECG.ecg_update_mutex ));
}
}
void MainWindow::refreshEcgGraphs()
{
qreal scalex = ui->ecgChannelGraph1->calcScaleX(2000);
qreal scaley = ui->ecgChannelGraph1->calcScaleY();
qreal ty = ui->ecgChannelGraph1->calcTy();
QList<QPointF> ValueList1;
QList<QPointF> ValueList2;
int ind = countEcg*40;
for(int i=0;i<40;i++)
{
int sira = (ind + i)%2000;
pthread_mutex_lock( &(Global::stigmaECG.ecg_array_mutex));
double ecg1 = (double)(Global::stigmaECG.ecg1Vector[sira]);
double ecg2 = (double)(Global::stigmaECG.ecg2Vector[sira]);
pthread_mutex_unlock( &(Global::stigmaECG.ecg_array_mutex));
qreal orgX = (ind+i)%2000;
qreal xPos = orgX * scalex;
qreal orgY1 = ecg1;
qreal orgY2 = ecg2;
qreal yPos1 = (orgY1 + ty) * scaley;
qreal yPos2 = (orgY2 + ty) * scaley;
QPointF *ecg1Point = new QPointF(xPos, -yPos1);
QPointF *ecg2Point = new QPointF(xPos, -yPos2);
ValueList1.append(*ecg1Point);
ValueList2.append(*ecg2Point);
}
ui->ecgChannelGraph1->addPoint(ValueList1, QList<QList<QPointF> >(), "ECG1");
ui->ecgChannelGraph2->addPoint(ValueList2, QList<QList<QPointF> >(), "ECG2");
countEcg++;
countEcg = countEcg%(2000/40);
}
int QtBasicGraph::addPoint(const QList<QPointF> pointList, QList<QList<QPointF> > gridPointList, char* type)
{
try
{
Mutex.lock();
int size = pointList.size();
QPointF FirstPoint;
if(lines.isEmpty())
FirstPoint = pointList.first();
else
FirstPoint = lines.last().p2();
for (int i = 1 ; i < size; i++)
{
QPointF pt = pointList[i];
lines.append(QLineF(FirstPoint, pt));
FirstPoint = pt;
}
gridLines.clear();
for(int j = 0 ; j<gridPointList.size();j++)
{
QList<QPointF> gridLinePointPair = gridPointList.at(j);
QPointF start = gridLinePointPair.at(0);
QPointF end = gridLinePointPair.at(1);
gridLines.append(QLineF(start, end));
}
Mutex.unlock();
}
catch(char * str)
{
qDebug()<<"error"<<str;
}
}
//Repaint called every 50 ms and clearing lines, points
void QtBasicGraph::paintEvent(QPaintEvent *e)
{
QPainter p(this);
if (m_render_hints)
p.setRenderHints(m_render_hints);
QVarLengthArray<QLineF> temp;
QVarLengthArray<QLineF> tempGrids;
QVarLengthArray<QLineF> xLinelist;
QVarLengthArray<QLineF> yLinelist;
int lineIndex=0;
Mutex.lock();
int lineSize=lines.size();
if(lineSize!=0)
{
temp.append(lines[0]);
}
for (lineIndex = 0; lineIndex < lineSize-1; lineIndex++)
{
if(lines[lineIndex].p1().x()<lines[lineIndex+1].p1().x())
{
temp.append(lines[lineIndex+1]);
}
else
{
lines.erase(lines.begin(),lines.begin()+lineIndex+1);
if(temp[lineIndex].p2().x()<temp[lineIndex].p1().x())
{
temp.removeLast();
}
break;
}
}
if(lineIndex==lineSize-1)
{
lines.erase(lines.begin(), lines.end()-1);
}
for(int i = 0; i<gridLines.size();i++)
{
tempGrids.append(gridLines.at(i));
}
Mutex.unlock();
QRectF qrectf(temp[0].p1().x(), 0.0, temp[temp.size()-1].p2().x()-temp[0].p1().x()+clearAfter, yRange());
p.fillRect(qrectf, QBrush(Qt::black, Qt::SolidPattern));
p.setPen(gridPen);
p.drawLines(tempGrids.constData(), tempGrids.size());
if(temp[0].p1().x()<20)
{
for (int i = 0; i < xLines.size(); ++i)
{
qreal lineEnd=(temp[temp.size()-1].p2().x()+clearAfter) < 20 ? temp[temp.size()-1].p2().x()+clearAfter : 20;
xLinelist.append( QLineF(temp[0].p1().x(),-xLines[i],lineEnd,-xLines[i]));
}
p.drawLines(xLinelist.constData(), xLinelist.size());
}
p.setPen(lineColor);
p.drawLines(temp.constData(), temp.size());
temp.clear();
}
To copy to clipboard, switch view to plain text mode
Bookmarks