SOLARIS_QT6_DAQ/CustomThreads.h

105 lines
2.8 KiB
C
Raw Normal View History

2023-09-15 17:31:07 -04:00
#ifndef CUSTOMTHREADS_H
#define CUSTOMTHREADS_H
#include <QThread>
#include <QMutex>
2023-03-16 17:28:55 -04:00
#include "macro.h"
#include "ClassDigitizer2Gen.h"
2023-03-16 17:28:55 -04:00
static QMutex digiMTX[MaxNumberOfDigitizer];
//^#===================================================== ReadData Thread
class ReadDataThread : public QThread {
Q_OBJECT
public:
2023-03-16 17:28:55 -04:00
ReadDataThread(Digitizer2Gen * dig, int digiID, QObject * parent = 0) : QThread(parent){
this->digi = dig;
2023-03-16 17:28:55 -04:00
this->ID = digiID;
2023-02-14 17:39:49 -05:00
isSaveData = false;
2023-09-15 17:31:07 -04:00
stop = false;
canSendMsg = true;
}
void SuppressFileSizeMsg() {canSendMsg = false;}
2023-09-15 17:31:07 -04:00
void Stop(){ this->stop = true;}
2023-02-14 17:39:49 -05:00
void SetSaveData(bool onOff) {this->isSaveData = onOff;}
void run(){
canSendMsg = true;
2023-09-15 17:31:07 -04:00
stop = false;
clock_gettime(CLOCK_REALTIME, &ta);
emit sendMsg("Digi-" + QString::number(digi->GetSerialNumber()) + " ReadDataThread started.");
2023-09-15 17:31:07 -04:00
while(!stop){
2023-03-16 17:28:55 -04:00
digiMTX[ID].lock();
int ret = digi->ReadData();
2023-03-16 17:28:55 -04:00
digiMTX[ID].unlock();
if( ret == CAEN_FELib_Success){
2023-02-14 17:39:49 -05:00
if( isSaveData) digi->SaveDataToFile();
}else if(ret == CAEN_FELib_Stop){
digi->ErrorMsg("ReadData Thread No more data");
//emit endOfLastData();
break;
}else{
2023-02-14 18:44:10 -05:00
//digi->ErrorMsg("ReadDataLoop()");
2023-09-26 10:41:24 -04:00
digi->hit->ClearTrace();
}
if( isSaveData && canSendMsg ){
clock_gettime(CLOCK_REALTIME, &tb);
if( tb.tv_sec - ta.tv_sec > 2 ) {
emit sendMsg("FileSize ("+ QString::number(digi->GetSerialNumber()) +"): " + QString::number(digi->GetTotalFilesSize()/1024./1024.) + " MB");
//emit checkFileSize();
//double duration = tb.tv_nsec-ta.tv_nsec + tb.tv_sec*1e+9 - ta.tv_sec*1e+9;
//printf("%4d, duration : %10.0f, %6.1f\n", readCount, duration, 1e9/duration);
ta = tb;
}
}
}
emit sendMsg("Digi-" + QString::number(digi->GetSerialNumber()) + " ReadDataThread stopped.");
}
signals:
void sendMsg(const QString &msg);
//void endOfLastData();
//void checkFileSize();
private:
Digitizer2Gen * digi;
2023-03-16 17:28:55 -04:00
int ID;
timespec ta, tb;
bool isSaveData, stop, canSendMsg;
};
2023-04-25 17:23:50 -04:00
//^#======================================================= Timing Thread, for some action need to be done periodically
class TimingThread : public QThread {
Q_OBJECT
public:
2023-04-25 17:23:50 -04:00
TimingThread(QObject * parent = 0 ) : QThread(parent){
2023-02-13 17:07:26 -05:00
waitTime = 20; // 10 x 100 milisec
stop = false;
}
void Stop() { this->stop = true;}
2023-04-25 17:23:50 -04:00
float GetWaitTimeinSec() const {return waitTime/10.;}
void SetWaitTimeSec(float sec) {waitTime = sec * 10;}
2023-02-13 17:07:26 -05:00
void run(){
unsigned int count = 0;
stop = false;
do{
usleep(100000);
count ++;
if( count % waitTime == 0){
2023-04-25 17:23:50 -04:00
emit TimeUp();
2023-02-13 17:07:26 -05:00
}
}while(!stop);
}
signals:
2023-04-25 17:23:50 -04:00
void TimeUp();
2023-02-13 17:07:26 -05:00
private:
bool stop;
unsigned int waitTime;
};
#endif