state machine on a different thread

This commit is contained in:
maliakal_d 2020-07-02 15:50:22 +02:00
parent 93c5505285
commit 25ec47dfff
2 changed files with 107 additions and 95 deletions

View File

@ -44,7 +44,8 @@ int read_register(int);
int set_module(int); int set_module(int);
int set_settings(int); int set_settings(int);
int get_threshold_energy(int); int get_threshold_energy(int);
int start_state_machine(int blocking, int file_des); int acquire(int blocking, int file_des);
void *start_state_machine(void *arg);
int start_acquisition(int); int start_acquisition(int);
int stop_acquisition(int); int stop_acquisition(int);
int get_run_status(int); int get_run_status(int);

View File

@ -6,6 +6,7 @@
#include "sls_detector_funcs.h" #include "sls_detector_funcs.h"
#include <arpa/inet.h> #include <arpa/inet.h>
#include <pthread.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
@ -50,6 +51,7 @@ int detectorId = -1;
// Local variables // Local variables
int (*flist[NUM_DET_FUNCTIONS])(int); int (*flist[NUM_DET_FUNCTIONS])(int);
pthread_t pthread_tid;
// scan variables // scan variables
int scan = 0; int scan = 0;
@ -1651,7 +1653,7 @@ int get_threshold_energy(int file_des) {
return Server_SendResult(file_des, INT32, &retval, sizeof(retval)); return Server_SendResult(file_des, INT32, &retval, sizeof(retval));
} }
int start_state_machine(int blocking, int file_des) { int acquire(int blocking, int file_des) {
ret = OK; ret = OK;
memset(mess, 0, sizeof(mess)); memset(mess, 0, sizeof(mess));
if (blocking) { if (blocking) {
@ -1729,11 +1731,26 @@ int start_state_machine(int blocking, int file_des) {
"already running!\n"); "already running!\n");
LOG(logERROR, (mess)); LOG(logERROR, (mess));
} else { } else {
// start acquisition thread should start here
int times = 1;
sharedMemory_setScanStop(0); sharedMemory_setScanStop(0);
sharedMemory_setScanStatus(IDLE); // if it was error sharedMemory_setScanStatus(IDLE); // if it was error
if (pthread_create(&pthread_tid, NULL, &start_state_machine,
&blocking)) {
ret = FAIL;
strcpy(mess, "Could not start acquisition thread!\n");
LOG(logERROR, (mess));
} else {
if (blocking) {
pthread_join(pthread_tid, NULL);
}
}
}
}
return Server_SendResult(file_des, INT32, NULL, 0);
}
void *start_state_machine(void *arg) {
int *blocking = (int *)arg;
int times = 1;
// start of scan // start of scan
if (scan) { if (scan) {
sharedMemory_setScanStatus(RUNNING); sharedMemory_setScanStatus(RUNNING);
@ -1754,8 +1771,8 @@ int start_state_machine(int blocking, int file_des) {
} }
// trimbits scan // trimbits scan
if (scanTrimbits) { if (scanTrimbits) {
LOG(logINFOBLUE, ("Trimbits scan %d/%d: [%d]\n", i, LOG(logINFOBLUE,
times, scanSteps[i])); ("Trimbits scan %d/%d: [%d]\n", i, times, scanSteps[i]));
#if !defined(EIGERD) && !defined(MYTHEN3D) #if !defined(EIGERD) && !defined(MYTHEN3D)
ret = FAIL; ret = FAIL;
sprintf(mess, "trimbit scan not implemented for this " sprintf(mess, "trimbit scan not implemented for this "
@ -1769,14 +1786,13 @@ int start_state_machine(int blocking, int file_des) {
} }
// dac scan // dac scan
else { else {
LOG(logINFOBLUE, ("Dac [%d] scan %d/%d: [%d]\n", LOG(logINFOBLUE, ("Dac [%d] scan %d/%d: [%d]\n", scanDac, i,
scanDac, i, times, scanSteps[i])); times, scanSteps[i]));
setDAC(scanDac, scanSteps[i], 0); setDAC(scanDac, scanSteps[i], 0);
int retval = getDAC(scanDac, 0); int retval = getDAC(scanDac, 0);
if (abs(retval - scanSteps[i]) > 5) { if (abs(retval - scanSteps[i]) > 5) {
ret = FAIL; ret = FAIL;
sprintf( sprintf(mess,
mess,
"Could not scan. Setting dac %d : wrote %d but " "Could not scan. Setting dac %d : wrote %d but "
"read %d\n", "read %d\n",
scanDac, scanSteps[i], scanSteps[i]); scanDac, scanSteps[i], scanSteps[i]);
@ -1800,9 +1816,7 @@ int start_state_machine(int blocking, int file_des) {
LOG(logDEBUG2, ("Starting Acquisition ret: %d\n", ret)); LOG(logDEBUG2, ("Starting Acquisition ret: %d\n", ret));
if (ret == FAIL) { if (ret == FAIL) {
#if defined(CHIPTESTBOARDD) || defined(MOENCHD) || defined(VIRTUAL) #if defined(CHIPTESTBOARDD) || defined(MOENCHD) || defined(VIRTUAL)
sprintf( sprintf(mess, "Could not start acquisition. Could not create udp "
mess,
"Could not start acquisition. Could not create udp "
"socket in server. Check udp_dstip & udp_dstport.\n"); "socket in server. Check udp_dstip & udp_dstport.\n");
#else #else
sprintf(mess, "Could not start acquisition\n"); sprintf(mess, "Could not start acquisition\n");
@ -1814,20 +1828,18 @@ int start_state_machine(int blocking, int file_des) {
break; break;
} }
// blocking or scan // blocking or scan
if (blocking || times > 1) { if (*blocking || times > 1) {
readFrame(&ret, mess); readFrame(&ret, mess);
} }
} }
}
// end of scan // end of scan
if (scan) { if (scan) {
sharedMemory_setScanStatus(IDLE); sharedMemory_setScanStatus(IDLE);
} }
} return NULL;
return Server_SendResult(file_des, INT32, NULL, 0);
} }
int start_acquisition(int file_des) { return start_state_machine(0, file_des); } int start_acquisition(int file_des) { return acquire(0, file_des); }
int stop_acquisition(int file_des) { int stop_acquisition(int file_des) {
ret = OK; ret = OK;
@ -1858,9 +1870,7 @@ int get_run_status(int file_des) {
return Server_SendResult(file_des, INT32, &retval, sizeof(retval)); return Server_SendResult(file_des, INT32, &retval, sizeof(retval));
} }
int start_and_read_all(int file_des) { int start_and_read_all(int file_des) { return acquire(1, file_des); }
return start_state_machine(1, file_des);
}
int read_all(int file_des) { int read_all(int file_des) {
ret = OK; ret = OK;
@ -7682,6 +7692,7 @@ int set_scan(int file_des) {
LOG(logDEBUG1, LOG(logDEBUG1,
("retval num frames %lld\n", (long long int)retval)); ("retval num frames %lld\n", (long long int)retval));
validate64(arg, retval, "set number of frames", DEC); validate64(arg, retval, "set number of frames", DEC);
retval = numScanSteps;
} }
} }
} }