Detector_HaoBo.cpp 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283
  1. #include <dlfcn.h>
  2. #include "Detector_HaoBo.h"
  3. #include "CCOS.Dev.FPD.HaoBo.h"
  4. #include "LogLocalHelper.h"
  5. #include "Log4CPP.h"
  6. Detector_HaoBoRF* g_pDetector = nullptr;
  7. //#define LOAD_PROC_ADDRESS(handle, func) \
  8. // do { \
  9. // void* p = dlsym(handle, #func); \
  10. // if (p == NULL) { \
  11. // FERROR("loading entry point %s error: %s", #func, dlerror()); \
  12. // } else { \
  13. // this->func = reinterpret_cast<decltype(this->func)>(p); \
  14. // } \
  15. // } while(0)
  16. #ifdef _WIN32
  17. #define LOAD_PROC_ADDRESS(handle,func) \
  18. if ((func = reinterpret_cast<API_##func>(GetProcAddress(handle, #func))) == NULL) { \
  19. FERROR("loading entry point {$} error!!!", #func); \
  20. }
  21. #else
  22. #define LOAD_PROC_ADDRESS(handle,func) \
  23. if ((func = reinterpret_cast<API_##func>(dlsym(handle, #func))) == NULL) { \
  24. FERROR("loading entry point {$} error!!!", #func); \
  25. }
  26. #endif
  27. #define BREAK_UINT32(var, ByteNum) \
  28. (unsigned char)((unsigned int)(((var) >> ((ByteNum)* 8)) & 0x00FF))
  29. #define BUILD_UINT32(Byte0, Byte1, Byte2, Byte3) \
  30. ((unsigned int)((unsigned int)((Byte0)& 0x00FF) \
  31. + ((unsigned int)((Byte1)& 0x00FF) << 8) \
  32. + ((unsigned int)((Byte2)& 0x00FF) << 16) \
  33. + ((unsigned int)((Byte3)& 0x00FF) << 24)))
  34. Detector_HaoBoRF::Detector_HaoBoRF()
  35. {
  36. m_pDPC2PanelID = new map<void*, int>();
  37. m_pPanelID2DPC = new map<int, void*>();
  38. m_nPanelCount = 0;
  39. m_nCurrentPanelID = 0;
  40. m_bConnected = false;
  41. m_nCurrentLogicMode = -1;
  42. m_pRawImgBuffer = nullptr;
  43. m_pFullImgBuffer = nullptr;
  44. m_nSaveRaw = 0;
  45. m_strCtrlWorkPath = "";
  46. m_eCaliType = CCOS_CALIBRATION_TYPE::CCOS_CALIBRATION_TYPE_NONE;
  47. m_eStatus = eDetStatus::DetStatus_NotIni;
  48. m_nMaxImgWidth = 0;
  49. m_nMaxImgHeight = 0;
  50. m_nRawImageWidth = 0;
  51. m_nRawImageHeight = 0;
  52. m_nCropLeft = 0;
  53. m_nCropRight = 0;
  54. m_nCropTop = 0;
  55. m_nCropBottom = 0;
  56. m_nImageWidth = 0;
  57. m_nImageHeight = 0;
  58. m_nFrameID = 0;
  59. m_nDropImgNum = 0;
  60. m_nDropImgCount = 0;
  61. m_nExamMode = APP_STATUS::APP_STATUS_MAX;
  62. m_hFPDScanThread = 0;
  63. m_fFrameRate = 10.0f; //缺省值,10帧每秒
  64. m_nDelayTime = 0;
  65. m_nExiThreshold = 200;//新增配置
  66. m_nImageBits = 16;
  67. m_bFirstImage = true;
  68. m_nModeID = 1;
  69. m_nGainLevel = 3;
  70. m_hSDKModule = nullptr;
  71. m_nFpdHandle = nullptr;
  72. m_bLoaded = false;
  73. nPGALevel = 0;
  74. nBinning = 0;
  75. nPrepareTime = 0;
  76. nLiveAcqTime = 0;
  77. m_hStopScanEvent = LinuxEvent::CreateEvent(LinuxEvent::AUTO_RESET, FALSE);
  78. m_hAcqEvent = LinuxEvent::CreateEvent(LinuxEvent::AUTO_RESET, FALSE);
  79. m_hGainEvent = LinuxEvent::CreateEvent(LinuxEvent::AUTO_RESET, FALSE);
  80. m_hDarkEvent = LinuxEvent::CreateEvent(LinuxEvent::AUTO_RESET, FALSE);
  81. m_hProcessImgEvent = LinuxEvent::CreateEvent(LinuxEvent::AUTO_RESET, FALSE);
  82. m_hArrayEvent.push_back(m_hStopScanEvent);
  83. m_hArrayEvent.push_back(m_hAcqEvent);
  84. m_hArrayEvent.push_back(m_hGainEvent);
  85. m_hArrayEvent.push_back(m_hDarkEvent);
  86. m_hArrayEvent.push_back(m_hProcessImgEvent);
  87. m_hStopOffsetEvent = LinuxEvent::CreateEvent(LinuxEvent::AUTO_RESET, FALSE);
  88. m_hStartAllOffset = LinuxEvent::CreateEvent(LinuxEvent::AUTO_RESET, FALSE);
  89. m_hStartOffset = LinuxEvent::CreateEvent(LinuxEvent::AUTO_RESET, FALSE);
  90. m_hAbortOffset = LinuxEvent::CreateEvent(LinuxEvent::AUTO_RESET, FALSE);
  91. m_hOffsetEvent.push_back(m_hStopOffsetEvent);
  92. m_hOffsetEvent.push_back(m_hStartAllOffset);
  93. m_hOffsetEvent.push_back(m_hStartOffset);
  94. m_hOffsetEvent.push_back(m_hAbortOffset);
  95. }
  96. Detector_HaoBoRF::~Detector_HaoBoRF()
  97. {
  98. FINFO("~Detector_HaoBoRF");
  99. if (m_pRawImgBuffer)
  100. {
  101. delete[]m_pRawImgBuffer;
  102. m_pRawImgBuffer = nullptr;
  103. }
  104. if (m_pFullImgBuffer != nullptr)
  105. {
  106. delete[]m_pFullImgBuffer;
  107. m_pFullImgBuffer = nullptr;
  108. }
  109. FINFO("Call HBI_Destroy");
  110. HBI_Destroy(m_nFpdHandle);
  111. }
  112. //参数说明:
  113. // @USER_CALLBACK_HANDLE_ENVENT
  114. // @pContext:参数 1,上位机对象指针,可以为空(NULL)
  115. // @ufpdId:参数 2,例如平板id
  116. // @byteEventid:参数 3,事件 ID,参考HbiType.h 中 enumeCallbackEventCommType
  117. // @PVEventParam1:参数 4,配置指针或图像结构体指针
  118. // @nEventParam2:参数 5,例如datasize 或状态
  119. // @nEventParam3:参数 6,例如帧号 frameid
  120. // @nEventParam4:参数 7,例如帧率 framerate 或状态等
  121. int CallBackFunc(void* _contex, int nDevId, unsigned char byteEventId, void* param1, int param2, int param3, int param4)
  122. {
  123. FINFO("CallBackFunc nDevId:{$},byteEventId:{$},param2:{$},param3:{$},param4:{$}", nDevId, (int)byteEventId, param2, param3, param4);
  124. int ret = 0;
  125. ImageData* imagedata = NULL;
  126. RegCfgInfo* pRegCfg = NULL;
  127. int status = -1;
  128. switch (byteEventId)
  129. {
  130. case ECALLBACK_TYPE_INVALVE:
  131. {
  132. FINFO("Callback Type: ECALLBACK_TYPE_INVALVE");
  133. break;
  134. }
  135. case ECALLBACK_TYPE_COMM_RIGHT:
  136. {
  137. FINFO("Callback Type: ECALLBACK_TYPE_COMM_RIGHT");
  138. break;
  139. }
  140. case ECALLBACK_TYPE_COMM_WRONG:
  141. {
  142. FINFO("Callback Type: ECALLBACK_TYPE_COMM_WRONG");
  143. break;
  144. }
  145. case ECALLBACK_TYPE_DUMMPLING:
  146. {
  147. FINFO("Callback Type: ECALLBACK_TYPE_DUMMPLING");
  148. break;
  149. }
  150. case ECALLBACK_TYPE_ACQ_END:
  151. {
  152. FINFO("Callback Type: ECALLBACK_TYPE_ACQ_END");
  153. break;
  154. }
  155. case ECALLBACK_TYPE_UPDATE_FIRMWARE:
  156. {
  157. FINFO("Callback Type: ECALLBACK_TYPE_UPDATE_FIRMWARE");
  158. break;
  159. }
  160. case ECALLBACK_TYPE_ERASE_FIRMWARE:
  161. {
  162. FINFO("Callback Type: ECALLBACK_TYPE_ERASE_FIRMWARE");
  163. break;
  164. }
  165. case ECALLBACK_TYPE_FPD_STATUS: // detetor status:connect/disconnect/ready/busy and so on
  166. {
  167. FINFO("Callback Type: ECALLBACK_TYPE_FPD_STATUS");
  168. if (param2 <= 0 && param2 >= -11)
  169. {
  170. if (param2 == 0)
  171. FERROR("ECALLBACK_TYPE_FPD_STATUS,Err:Network not connected!");
  172. else if (param2 == -1)
  173. FERROR("ECALLBACK_TYPE_FPD_STATUS,Err:Parameter exception!");
  174. else if (param2 == -2)
  175. FERROR("ECALLBACK_TYPE_FPD_STATUS,Err:Failed to return the number of ready descriptors!");
  176. else if (param2 == -3)
  177. FERROR("ECALLBACK_TYPE_FPD_STATUS,Err:Receive timeout!");
  178. else if (param2 == -4)
  179. FERROR("ECALLBACK_TYPE_FPD_STATUS,Err:Receive failed!");
  180. else if (param2 == -5)
  181. FERROR("ECALLBACK_TYPE_FPD_STATUS,Err:Socket-Port unreadable!");
  182. else if (param2 == -6)
  183. FERROR("ECALLBACK_TYPE_FPD_STATUS,Network card unusual!");
  184. else if (param2 == -7)
  185. FERROR("ECALLBACK_TYPE_FPD_STATUS,Network card ok!");
  186. else if (param2 == -8)
  187. FERROR("ECALLBACK_TYPE_FPD_STATUS:Update Firmware end!");
  188. else if (param2 == -9)
  189. FERROR("ECALLBACK_TYPE_FPD_STATUS:Light Fiber disconnected!");
  190. else if (param2 == -10)
  191. FERROR("ECALLBACK_TYPE_FPD_STATUS:Read ddr failed,try restarting the PCIe driver!");
  192. else /*if (param2 == -11)*/
  193. FERROR("ECALLBACK_TYPE_FPD_STATUS:is not jumb!");
  194. status = (int)FPD_DISCONN_STATUS;
  195. FERROR("Detector not connected!");
  196. g_pDetector->StatusFeedback(EVT_STATUS_PANEL, PANEL_CLOSE);
  197. }
  198. else if (param2 == FPD_CONN_SUCCESS) // connect
  199. {
  200. FINFO("Detector connected!");
  201. status = (int)FPD_CONN_SUCCESS;
  202. g_pDetector->StatusFeedback(EVT_STATUS_PANEL, PANEL_CONNECT);
  203. g_pDetector->GetPanelInfo();
  204. }
  205. else if (param2 == FPD_PREPARE_STATUS) // prepare
  206. {
  207. FINFO("Detector prepare!");
  208. status = (int)FPD_PREPARE_STATUS;
  209. g_pDetector->StatusFeedback(EVT_STATUS_PANEL, PANEL_STANDBY);
  210. }
  211. else if (param2 == FPD_READY_STATUS) // ready
  212. {
  213. FINFO("Detector ready!");
  214. status = (int)FPD_READY_STATUS;
  215. g_pDetector->StatusFeedback(EVT_STATUS_PANEL, PANEL_READY_EXP);
  216. }
  217. else if (param2 == FPD_DOOFFSET_TEMPLATE)// do offset template
  218. {
  219. FINFO("Detector do offset template!");
  220. status = (int)FPD_DOOFFSET_TEMPLATE;
  221. }
  222. else if (param2 == FPD_EXPOSE_STATUS) // busy expose
  223. {
  224. FINFO("Detector Exposing!");
  225. status = FPD_EXPOSE_STATUS;
  226. }
  227. else if (param2 == FPD_CONTINUE_READY) // continue ready
  228. {
  229. FINFO("Detector Continue ready!");
  230. status = FPD_CONTINUE_READY;
  231. g_pDetector->StatusFeedback(EVT_STATUS_PANEL, PANEL_READY_EXP);
  232. }
  233. else if (param2 == FPD_DWONLOAD_GAIN) // download gain template
  234. {
  235. FINFO("Detector Download gain template!");
  236. status = FPD_DWONLOAD_GAIN;
  237. }
  238. else if (param2 == FPD_DWONLOAD_DEFECT) // download defect template
  239. {
  240. FINFO("Detector Download defect template!");
  241. status = FPD_DWONLOAD_DEFECT;
  242. }
  243. else if (param2 == FPD_DWONLOAD_OFFSET)// download offset template
  244. {
  245. FINFO("Detector Download offset template!");
  246. status = FPD_DWONLOAD_OFFSET;
  247. }
  248. else if (param2 == FPD_UPDATE_FIRMARE)// update firmware
  249. {
  250. FINFO("Detector Update firmware!");
  251. status = FPD_UPDATE_FIRMARE;
  252. }
  253. else if (param2 == FPD_RETRANS_MISS)// Retrans mission
  254. {
  255. FINFO("Detector Retrans mission!");
  256. status = FPD_RETRANS_MISS;
  257. }
  258. else
  259. FERROR("Detector Other error:{$}", param2);
  260. if (status != -1)// error, diconnect detector
  261. {
  262. if (param2 <= 0 && param2 >= -10)
  263. {
  264. g_pDetector->HBI_DisConnectDetector(g_pDetector->m_nFpdHandle);
  265. }
  266. }
  267. break;
  268. }
  269. case ECALLBACK_TYPE_ROM_UPLOAD:
  270. {
  271. FINFO("Callback Type: ECALLBACK_TYPE_ROM_UPLOAD");
  272. if (param1 == NULL || param2 == 0)
  273. {
  274. FERROR("Parameter exception!");
  275. return ret;
  276. }
  277. pRegCfg = new RegCfgInfo;
  278. if (pRegCfg != NULL)
  279. {
  280. memset(pRegCfg, 0x00, sizeof(RegCfgInfo));
  281. memcpy(pRegCfg, (unsigned char*)param1, sizeof(RegCfgInfo));
  282. g_pDetector->PrintDetectorCfg(pRegCfg);
  283. }
  284. break;
  285. }
  286. case ECALLBACK_TYPE_RAM_UPLOAD:
  287. {
  288. FINFO("Callback Type: ECALLBACK_TYPE_RAM_UPLOAD");
  289. if (param1 == NULL || param2 == 0)
  290. {
  291. FERROR("Parameter exception!");
  292. return ret;
  293. }
  294. break;
  295. }
  296. case ECALLBACK_TYPE_FACTORY_UPLOAD:
  297. {
  298. FINFO("Callback Type: ECALLBACK_TYPE_FACTORY_UPLOAD");
  299. if (param1 == NULL || param2 == 0)
  300. {
  301. printf("err:Parameter exception!\n");
  302. return ret;
  303. }
  304. break;
  305. }
  306. case ECALLBACK_TYPE_WLAN_BATTERY:
  307. {
  308. FINFO("Callback Type: ECALLBACK_TYPE_WLAN_BATTERY");
  309. break;
  310. }
  311. case ECALLBACK_TYPE_BUFFER_WARNING:
  312. {
  313. FINFO("Callback Type: ECALLBACK_TYPE_BUFFER_WARNING");
  314. break;
  315. }
  316. case ECALLBACK_TYPE_ILLEGAL_PACKAGE_NUM:
  317. {
  318. FINFO("Callback Type: ECALLBACK_TYPE_ILLEGAL_PACKAGE_NUM");
  319. break;
  320. }
  321. case ECALLBACK_TYPE_USER_DDR_UPLOAD:
  322. {
  323. FINFO("Callback Type: ECALLBACK_TYPE_USER_DDR_UPLOAD");
  324. break;
  325. }
  326. case ECALLBACK_TYPE_WIRELESS_NETWORK:
  327. {
  328. FINFO("Callback Type: ECALLBACK_TYPE_WIRELESS_NETWORK");
  329. break;
  330. }
  331. case ECALLBACK_TYPE_WIRELESS_SETKEYOK:
  332. {
  333. FINFO("Callback Type: ECALLBACK_TYPE_WIRELESS_SETKEYOK");
  334. break;
  335. }
  336. case ECALLBACK_FIRMWARE_TEMPLATE_STATUS:
  337. {
  338. FINFO("Callback Type: ECALLBACK_FIRMWARE_TEMPLATE_STATUS");
  339. break;
  340. }
  341. case ECALLBACK_TYPE_SINGLE_IMAGE:
  342. case ECALLBACK_TYPE_MULTIPLE_IMAGE:
  343. FINFO("Callback Type: ECALLBACK_TYPE_SINGLE_IMAGE or ECALLBACK_TYPE_MULTIPLE_IMAGE");
  344. if (param1 == NULL || param2 == 0)
  345. {
  346. FERROR("Parameter exception!");
  347. return ret;
  348. }
  349. imagedata = (ImageData*)param1;
  350. g_pDetector->m_nRawImageWidth = imagedata->uwidth;
  351. g_pDetector->m_nRawImageHeight = imagedata->uheight;
  352. g_pDetector->m_nFrameID = imagedata->uframeid;
  353. FINFO("Callback ImageWidth:{$},ImageHeight:{$},FrameID:{$},datalen:{$}", imagedata->uwidth, imagedata->uheight, imagedata->uframeid, imagedata->datalen);
  354. memcpy(g_pDetector->m_pRawImgBuffer, (unsigned char*)imagedata->databuff, imagedata->datalen);
  355. //获取图像后需要调用停止采集,所以改到子线程处理
  356. g_pDetector->m_hProcessImgEvent->SetEvent();
  357. break;
  358. case ECALLBACK_TYPE_PREVIEW_IMAGE:
  359. {
  360. FINFO("Callback Type: ECALLBACK_TYPE_PREVIEW_IMAGE");
  361. if (param1 == NULL || param2 == 0)
  362. {
  363. FERROR("Parameter exception!");
  364. return ret;
  365. }
  366. imagedata = (ImageData*)param1;
  367. break;
  368. }
  369. case ECALLBACK_TYPE_LIVE_IMAGE:
  370. {
  371. FINFO("Callback Type: ECALLBACK_TYPE_LIVE_IMAGE");
  372. if (param1 == NULL || param2 == 0)
  373. {
  374. FERROR("Parameter exception!");
  375. return ret;
  376. }
  377. imagedata = (ImageData*)param1;
  378. g_pDetector->m_nRawImageWidth = imagedata->uwidth;
  379. g_pDetector->m_nRawImageHeight = imagedata->uheight;
  380. g_pDetector->m_nFrameID = imagedata->uframeid;
  381. FINFO("Callback ImageWidth:{$},ImageHeight:{$},FrameID:{$},datalen:{$}", imagedata->uwidth, imagedata->uheight, imagedata->uframeid, imagedata->datalen);
  382. memcpy(g_pDetector->m_pRawImgBuffer, (unsigned char*)imagedata->databuff, imagedata->datalen);
  383. //获取图像后需要调用停止采集,所以改到子线程处理
  384. g_pDetector->m_hProcessImgEvent->SetEvent();
  385. break;
  386. }
  387. case ECALLBACK_TYPE_PACKET_MISS:
  388. {
  389. FINFO("Callback Type: ECALLBACK_TYPE_PACKET_MISS");
  390. break;
  391. }
  392. case ECALLBACK_TYPE_OFFSET_TMP:
  393. {
  394. FINFO("Callback Type: ECALLBACK_TYPE_OFFSET_TMP");
  395. if (param1 == NULL || param2 == 0)
  396. {
  397. FERROR("Parameter exception!");
  398. return ret;
  399. }
  400. imagedata = (ImageData*)param1;
  401. break;
  402. }
  403. case ECALLBACK_OVERLAY_NUMBER:
  404. {
  405. FINFO("Callback Type: ECALLBACK_OVERLAY_NUMBER");
  406. break;
  407. }
  408. case ECALLBACK_OVERLAY_16BIT_IMAGE:
  409. {
  410. FINFO("Callback Type: ECALLBACK_OVERLAY_16BIT_IMAGE");
  411. if (param1 == NULL || param2 == 0)
  412. {
  413. FERROR("Parameter exception!");
  414. return ret;
  415. }
  416. imagedata = (ImageData*)param1;
  417. break;
  418. }
  419. case ECALLBACK_OVERLAY_32BIT_IMAGE:
  420. {
  421. FINFO("Callback Type: ECALLBACK_OVERLAY_32BIT_IMAGE");
  422. if (param1 == NULL || param2 == 0)
  423. {
  424. FERROR("Parameter exception!");
  425. return ret;
  426. }
  427. imagedata = (ImageData*)param1;
  428. break;
  429. }
  430. case ECALLBACK_TYPE_8BIT_IMAGE:
  431. {
  432. FINFO("Callback Type: ECALLBACK_TYPE_8BIT_IMAGE");
  433. break;
  434. }
  435. case ECALLBACK_TYPE_SENDTO_WIZARD:
  436. {
  437. FINFO("Callback Type: ECALLBACK_TYPE_SENDTO_WIZARD");
  438. break;
  439. }
  440. case ECALLBACK_TYPE_PACKET_MISS_MSG:
  441. {
  442. FINFO("Callback Type: ECALLBACK_TYPE_PACKET_MISS_MSG");
  443. break;
  444. }
  445. case ECALLBACK_TYPE_THREAD_EVENT:
  446. {
  447. FINFO("Callback Type: ECALLBACK_TYPE_THREAD_EVENT");
  448. if (param2 == 100)
  449. FINFO("ECALLBACK_TYPE_THREAD_EVENT,start recv data!");
  450. else if (param2 == 101)
  451. FINFO("ECALLBACK_TYPE_THREAD_EVENT,end recv data!");
  452. else if (param2 == 104)
  453. FINFO("ECALLBACK_TYPE_THREAD_EVENT,Packet Retransmission:start recv data!");
  454. else if (param2 == 105)
  455. FINFO("ECALLBACK_TYPE_THREAD_EVENT,Frame Retransmission:start recv data!");
  456. else if (param2 == 106)
  457. FINFO("ECALLBACK_TYPE_THREAD_EVENT,Frame loss retransmission over,end recv data!");
  458. else if (param2 == 107)
  459. FINFO("ECALLBACK_TYPE_THREAD_EVENT,image buff is null:end recv data!");
  460. else if (param2 == 108)
  461. FINFO("ECALLBACK_TYPE_THREAD_EVENT,Generate Offset Template:start thread!");
  462. else if (param2 == 109)
  463. FINFO("ECALLBACK_TYPE_THREAD_EVENT,Generate Offset Template:end thread!");
  464. else if (param2 == 110)
  465. FINFO("ECALLBACK_TYPE_THREAD_EVENT,Generate Gain Template:start thread!");
  466. else if (param2 == 111)
  467. FINFO("ECALLBACK_TYPE_THREAD_EVENT,Generate Gain Template:end thread!");
  468. else if (param2 == 112)
  469. FINFO("ECALLBACK_TYPE_THREAD_EVENT,offset calibrate:success!");
  470. else if (param2 == 113)
  471. FINFO("ECALLBACK_TYPE_THREAD_EVENT,offset calibrate:failed!");
  472. else if (param2 == 114)
  473. FINFO("ECALLBACK_TYPE_THREAD_EVENT,gain calibrate:success!");
  474. else if (param2 == 115)
  475. FINFO("ECALLBACK_TYPE_THREAD_EVENT,gain calibrate:failed!");
  476. else if (param2 == 116)
  477. FINFO("ECALLBACK_TYPE_THREAD_EVENT,defect calibrate:success!");
  478. else if (param2 == 117)
  479. FINFO("ECALLBACK_TYPE_THREAD_EVENT,defect calibrate:failed!");
  480. else if (param2 == 118)
  481. FINFO("ECALLBACK_TYPE_THREAD_EVENT,InitGainTemplate:failed!");
  482. else if (param2 == 119)
  483. FINFO("ECALLBACK_TYPE_THREAD_EVENT,firmare offset calibrate:success!");
  484. else if (param2 == 120)
  485. FINFO("ECALLBACK_TYPE_THREAD_EVENT,Generate Defect Template:start thread!");
  486. else if (param2 == 121)
  487. FINFO("ECALLBACK_TYPE_THREAD_EVENT,Generate Defect Template:end thread!");
  488. else
  489. FERROR("ECALLBACK_TYPE_THREAD_EVENT,Err:other error [{$}]", param2);
  490. break;
  491. }
  492. case ECALLBACK_TYPE_ACQ_DISCARDDED:
  493. {
  494. FINFO("Callback Type: ECALLBACK_TYPE_ACQ_DISCARDDED");
  495. break;
  496. }
  497. case ECALLBACK_TYPE_OFFSET_ERR_MSG:
  498. {
  499. FINFO("Callback Type: ECALLBACK_TYPE_OFFSET_ERR_MSG");
  500. break;
  501. }
  502. case ECALLBACK_TYPE_GAIN_ERR_MSG:
  503. {
  504. FINFO("Callback Type: ECALLBACK_TYPE_GAIN_ERR_MSG");
  505. break;
  506. }
  507. case ECALLBACK_TYPE_DEFECT_ERR_MSG:
  508. {
  509. FINFO("Callback Type: ECALLBACK_TYPE_DEFECT_ERR_MSG");
  510. break;
  511. }
  512. case ECALLBACK_TYPE_NET_ERR_MSG:
  513. {
  514. FINFO("Callback Type: ECALLBACK_TYPE_NET_ERR_MSG");
  515. break;
  516. }
  517. case ECALLBACK_TYPE_SET_CFG_OK:
  518. {
  519. FINFO("Callback Type: ECALLBACK_TYPE_SET_CFG_OK set rom param succuss!");
  520. break;
  521. }
  522. case ECALLBACK_TYPE_SAVE_SUCCESS:
  523. {
  524. FINFO("Callback Type: ECALLBACK_TYPE_SAVE_SUCCESS");
  525. break;
  526. }
  527. case ECALLBACK_TYPE_GENERATE_TEMPLATE:
  528. {
  529. FINFO("Callback Type: ECALLBACK_TYPE_GENERATE_TEMPLATE");
  530. if (param2 == ECALLBACK_TEMPLATE_BEGIN)
  531. {
  532. FINFO("ECALLBACK_TEMPLATE_BEGIN");
  533. }
  534. else if (param2 == ECALLBACK_TEMPLATE_INVALVE_PARAM)
  535. {
  536. FINFO("ECALLBACK_TEMPLATE_INVALVE_PARAM:{$}", param3);
  537. }
  538. else if (param2 == ECALLBACK_TEMPLATE_MALLOC_FAILED)
  539. {
  540. FINFO("ECALLBACK_TEMPLATE_MALLOC_FAILED:{$}", param3);
  541. }
  542. else if (param2 == ECALLBACK_TEMPLATE_SEND_FAILED)
  543. {
  544. FINFO("ECALLBACK_TEMPLATE_SEND_FAILED:{$}", param3);
  545. }
  546. else if (param2 == ECALLBACK_TEMPLATE_STATUS_ABORMAL)
  547. {
  548. FINFO("ECALLBACK_TEMPLATE_STATUS_ABORMAL:{$}", param3);
  549. }
  550. else if (param2 == ECALLBACK_TEMPLATE_FRAME_NUM)
  551. {
  552. FINFO("ECALLBACK_TEMPLATE_FRAME_NUM:{$}", param3);
  553. }
  554. else if (param2 == ECALLBACK_TEMPLATE_TIMEOUT)
  555. {
  556. FINFO("ECALLBACK_TEMPLATE_TIMEOUT:{$}", param3);
  557. }
  558. else if (param2 == ECALLBACK_TEMPLATE_MEAN)
  559. {
  560. ECALLBACK_RAW_INFO* ptr = (ECALLBACK_RAW_INFO*)param1;
  561. if (ptr != NULL)
  562. {
  563. FINFO("ECALLBACK_TEMPLATE_MEAN:{$},dMean={$.2f}", ptr->szRawName, ptr->dMean);
  564. }
  565. }
  566. else if (param2 == ECALLBACK_TEMPLATE_GENERATE)
  567. {
  568. if (param3 == OFFSET_TMP)
  569. FINFO("ECALLBACK_TEMPLATE_GENERATE:OFFSET_TMP");
  570. else if (param3 == GAIN_TMP)
  571. FINFO("ECALLBACK_TEMPLATE_GENERATE:GAIN_TMP");
  572. else if (param3 == DEFECT_TMP)
  573. FINFO("ECALLBACK_TEMPLATE_GENERATE:DEFECT_TMP,bad point={$}", param2);
  574. else
  575. FINFO("ECALLBACK_TEMPLATE_GENERATE:nid={$}", param3);
  576. }
  577. else if (param2 == ECALLBACK_TEMPLATE_RESULT)
  578. {
  579. FINFO("ECALLBACK_TEMPLATE_RESULT:{$}", param3);
  580. }
  581. else
  582. {
  583. FINFO("other:len={$},nid={$}", param2, param3);
  584. }
  585. break;
  586. }
  587. case ECALLBACK_TYPE_FILE_NOTEXIST:
  588. {
  589. FINFO("Callback Type: ECALLBACK_TYPE_FILE_NOTEXIST");
  590. if (param1 != NULL)
  591. FERROR("err:{$} not exist!", (char*)param1);
  592. break;
  593. }
  594. case ECALLBACK_TYPE_WORK_STATUS:
  595. {
  596. FINFO("Callback Type: ECALLBACK_TYPE_WORK_STATUS");
  597. break;
  598. }
  599. default:
  600. FINFO("default Callback Type[{$}]", (int)byteEventId);
  601. break;
  602. }
  603. if (pRegCfg)
  604. {
  605. delete pRegCfg;
  606. pRegCfg = nullptr;
  607. }
  608. return 0;
  609. }
  610. bool Detector_HaoBoRF::DriverEntry(void* pDrvDPC, ResDataObject& Configuration, const char* szWorkPath)
  611. {
  612. printf("========DriverEntry %p\n", pDrvDPC);
  613. FINFO("========DriverEntry {$}", pDrvDPC);
  614. map<void*, int>::iterator iter = m_pDPC2PanelID->find(pDrvDPC);
  615. if (iter != m_pDPC2PanelID->end())
  616. {
  617. FERROR("This DPC already exist");
  618. return false;
  619. }
  620. m_pDPC2PanelID->insert(pair<void*, int>(pDrvDPC, m_nPanelCount));
  621. m_pPanelID2DPC->insert(pair<int, void*>(m_nPanelCount, pDrvDPC));
  622. m_nPanelCount++;
  623. m_ModeConfig = Configuration; //记录配置 --目前只有一个平板,多板时应该分别存储
  624. FINFO("m_ModeConfig:{$}", m_ModeConfig.encode());
  625. if (m_hFPDScanThread == 0)
  626. {
  627. int ret = pthread_create(&m_hFPDScanThread, NULL, onFPDScanThread, this);
  628. if (ret != 0)
  629. {
  630. FERROR("Create FPD scan thread failed!");
  631. return false;
  632. }
  633. }
  634. if (m_hOffsetThread == 0)
  635. {
  636. int ret = pthread_create(&m_hOffsetThread, NULL, RefreshOffsetThread, this);
  637. if (ret != 0)
  638. {
  639. FERROR("Create offset thread failed!");
  640. return false;
  641. }
  642. }
  643. m_strCtrlWorkPath = szWorkPath;
  644. if (!LoadDll(szWorkPath))
  645. {
  646. FERROR("Load dll failed!");
  647. return false;
  648. }
  649. if (m_bLoaded)
  650. {
  651. FINFO("Calling HBI_Init");
  652. m_nFpdHandle = HBI_Init(0);
  653. if (!m_nFpdHandle)
  654. {
  655. FERROR("HBI_Init error!");
  656. return false;
  657. }
  658. FINFO("Calling HBI_RegEventCallBackFun");
  659. int nRet = HBI_RegEventCallBackFun(m_nFpdHandle, CallBackFunc, NULL);
  660. if (TestError(nRet, "HBI_RegEventCallBackFun"))
  661. {
  662. FERROR("Register call back fun fail!");
  663. return false;
  664. }
  665. }
  666. return true;
  667. }
  668. bool Detector_HaoBoRF::Connect(void* pDrvDPC)
  669. {
  670. printf("========Connect detector begin \r\n");
  671. FINFO("========Connect detector begin \n");
  672. if ((*m_pDPC2PanelID)[pDrvDPC] != m_nCurrentPanelID)
  673. {
  674. FERROR("Not current DPC, return true");
  675. return true;
  676. }
  677. if (m_bLoaded)
  678. {
  679. if (!ConnectDetector())
  680. {
  681. return false;
  682. }
  683. }
  684. else
  685. {
  686. FERROR("DLL is not loaded!");
  687. return false;
  688. }
  689. FINFO("Connect over");
  690. printf("Connect over \n");
  691. return true;
  692. }
  693. void Detector_HaoBoRF::DisConnect()
  694. {
  695. printf("========DisConnect with detector \n");
  696. FINFO("========DisConnect");
  697. int nRet = HBI_SUCCSS;
  698. }
  699. void Detector_HaoBoRF::EnterExamMode(int nExamMode)
  700. {
  701. switch (nExamMode)
  702. {
  703. case APP_STATUS_WORK_BEGIN:
  704. FINFO("Enter into Exam Windows");
  705. m_nExamMode = APP_STATUS_WORK_BEGIN;
  706. break;
  707. case APP_STATUS_WORK_END:
  708. FINFO("Quit Exam Windows");
  709. m_nExamMode = APP_STATUS_WORK_END;
  710. break;
  711. case APP_STATUS_DETSHARE_BEGIN:
  712. FINFO("Enter into Detector Share Windows");
  713. m_nExamMode = APP_STATUS_DETSHARE_BEGIN;
  714. break;
  715. case APP_STATUS_DETSHAR_END:
  716. m_nExamMode = APP_STATUS_IDLE;
  717. FINFO("Quit Detector Share Windows");
  718. m_nExamMode = APP_STATUS_DETSHAR_END;
  719. break;
  720. case APP_STATUS_CAL_BEGIN:
  721. FINFO("Enter into Calibration Windows");
  722. m_nExamMode = APP_STATUS_CAL_BEGIN;
  723. break;
  724. case APP_STATUS_CAL_END:
  725. FINFO("Quit Calibration Windows");
  726. m_nExamMode = APP_STATUS_CAL_END;
  727. break;
  728. case APP_STATUS_WORK_IN_SENSITIVITY:
  729. FINFO("Enter into sensitivity test interface");
  730. m_nExamMode = APP_STATUS_WORK_IN_SENSITIVITY;
  731. break;
  732. default:
  733. break;
  734. }
  735. if (APP_STATUS_WORK_END == m_nExamMode)
  736. {
  737. if (DetStatus_Acquire == GetDpcStatus())
  738. {
  739. FINFO("quit exam but detector status is acquire,so stop acquire");
  740. StopAcquisition(nullptr);
  741. }
  742. }
  743. }
  744. bool Detector_HaoBoRF::SetAcqMode(int nMode)
  745. {
  746. printf("========SetAcqMode nMode:%d \n", nMode);
  747. FINFO("========SetAcqMode nMode:{$}",nMode);
  748. if (m_nCurrentLogicMode == nMode)
  749. {
  750. FINFO("Same mode, return");
  751. return true;
  752. }
  753. //只有RAD模式读取全部的配置文件,CF和PF不读取全部配置,只读取部分
  754. try
  755. {
  756. int nModeCount = (int)m_ModeConfig["ModeTable"].size();
  757. for (int i = 0; i < nModeCount; i++)
  758. {
  759. int logicMode = (int)m_ModeConfig["ModeTable"][i]["LogicMode"];//SetAcqMode
  760. if (logicMode == nMode)
  761. {
  762. //FINFO("ModeTable22 {$}, {$}", i, m_ModeConfig["ModeTable"][i].encode());
  763. FINFO("find LogicMode == nMode");
  764. if (logicMode == RAD)
  765. {
  766. m_nModeID = (int)m_ModeConfig["ModeTable"][i]["OperationMode"];//RAD 固定用1*1的Binning
  767. }
  768. m_nDelayTime = (int)m_ModeConfig["ModeTable"][i]["DelayTime"];
  769. m_nDropImgCount = (int)m_ModeConfig["ModeTable"][i]["DropImgCount"];
  770. m_nSaveRaw = (int)m_ModeConfig["ModeTable"][i]["IsSaveRaw"];
  771. m_nTriggerMode = (int)m_ModeConfig["ModeTable"][i]["ExamType"];
  772. FINFO("m_nDelayTime:{$}, m_nDropImgCount:{$}, m_nSaveRaw:{$}, m_nModeID:{$}, m_nTriggerMode:{$}",
  773. m_nDelayTime, m_nDropImgCount, m_nSaveRaw, m_nModeID, m_nTriggerMode);
  774. m_nCropLeft = (int)m_ModeConfig["ModeTable"][i]["CropLeft"];
  775. m_nCropRight = (int)m_ModeConfig["ModeTable"][i]["CropRight"];
  776. m_nCropTop = (int)m_ModeConfig["ModeTable"][i]["CropTop"];
  777. m_nCropBottom = (int)m_ModeConfig["ModeTable"][i]["CropBottom"];
  778. FINFO("Crop left:{$}, top:{$}, right:{$}, bottom:{$}", m_nCropLeft, m_nCropTop, m_nCropRight, m_nCropBottom);
  779. m_nImageBits = (int)m_ModeConfig["ModeTable"][i]["PhySizeInfoBit"];
  780. m_nGainLevel = (int)m_ModeConfig["ModeTable"][i]["GainValue"];
  781. FINFO("m_nImageBits:{$}, m_nGainLevel:{$}", m_nImageBits, m_nGainLevel);
  782. break;
  783. }
  784. }
  785. }
  786. catch (ResDataObjectExption& e)
  787. {
  788. FERROR("Read configuration failed, Error code: {$}", e.what());
  789. return false;
  790. }
  791. m_nCurrentLogicMode = nMode;
  792. return true;
  793. }
  794. bool Detector_HaoBoRF::SetExposureTimes(int nTimes)
  795. {
  796. FINFO("========SetExposureTimes({$})", nTimes);
  797. return true;
  798. }
  799. bool Detector_HaoBoRF::PrepareAcquisition(void* pDrvDPC)
  800. {
  801. printf("========PrepareAcquisition \n");
  802. FINFO("========PrepareAcquisition ");
  803. int nRet = HBI_SUCCSS;
  804. if ((*m_pDPC2PanelID)[pDrvDPC] != m_nCurrentPanelID)
  805. {
  806. FERROR("Not current DPC, return");
  807. return false;
  808. }
  809. if (-1 == m_nCurrentLogicMode)
  810. {
  811. FERROR("Illegal exam mode");
  812. return false;
  813. }
  814. eDetStatus detectorStatus = GetDpcStatus();
  815. if (detectorStatus == DetStatus_Acquire)
  816. {
  817. bool ret = StopAcquisition(pDrvDPC);
  818. if (!ret)
  819. {
  820. return false;
  821. }
  822. }
  823. //modeID 1->1*1 2->2*2
  824. FINFO("m_nTriggerMode:{$}, m_nModeID:{$}, acqTime:{$}", m_nTriggerMode, m_nModeID, 1 / (int)m_fFrameRate);
  825. FINFO("Call HBI_TriggerBinningAcqTime");
  826. nRet = HBI_TriggerBinningAcqTime(m_nFpdHandle, m_nTriggerMode, m_nModeID, 1 / (int)m_fFrameRate, 0);
  827. if (TestError(nRet,"HBI_TriggerBinningAcqTime"))
  828. {
  829. FERROR("HBI_TriggerBinningAcqTime fail!");
  830. return false;
  831. }
  832. return true;
  833. }
  834. bool Detector_HaoBoRF::StartAcquisition(void* pDrvDPC)
  835. {
  836. printf("========StartAcquisition \n");
  837. FINFO("========StartAcquisition ");
  838. int nRet = HBI_SUCCSS;
  839. if ((*m_pDPC2PanelID)[pDrvDPC] != m_nCurrentPanelID)
  840. {
  841. FERROR("Not current DPC, return");
  842. return false;
  843. }
  844. if (-1 == m_nCurrentLogicMode)
  845. {
  846. FERROR("Illegal exam mode");
  847. return false;
  848. }
  849. else
  850. {
  851. m_bValidImage = false; //开始采集,恢复初值
  852. m_nDropImgNum = 0; //开始采集,恢复初值
  853. m_bFirstImage = true;
  854. struct timespec ts;
  855. clock_gettime(CLOCK_MONOTONIC, &ts); // 使用CLOCK_MONOTONIC时钟,不受系统时间调整影响
  856. m_dwBeginTime = (unsigned long long)ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
  857. }
  858. FPD_AQC_MODE aqc_mode;
  859. FINFO("m_nCurrentLogicMode:{$}", m_nCurrentLogicMode);
  860. if (m_nCurrentLogicMode == RAD)
  861. {
  862. aqc_mode.nAcqnumber = 1;
  863. }
  864. else if (m_nCurrentLogicMode == CF || m_nCurrentLogicMode == PF)
  865. {
  866. aqc_mode.eAqccmd = EnumIMAGE_ACQ_CMD::LIVE_ACQ_DEFAULT_TYPE;
  867. aqc_mode.eLivetype = EnumLIVE_ACQUISITION::ONLY_IMAGE;
  868. }
  869. FINFO("Call HBI_LiveAcquisition");
  870. nRet = HBI_LiveAcquisition(m_nFpdHandle, aqc_mode);
  871. if (TestError(nRet,"HBI_LiveAcquisition"))
  872. {
  873. FERROR("HBI_LiveAcquisition fail!");
  874. return false;
  875. }
  876. SetDpcStatus(DetStatus_Acquire); //动态模式激活采集,设置状态
  877. return true;
  878. }
  879. bool Detector_HaoBoRF::StopAcquisition(void* pDrvDPC)
  880. {
  881. printf("========StopAcquisition \n");
  882. FINFO("========StopAcquisition ");
  883. int nRet = HBI_SUCCSS;
  884. if ((*m_pDPC2PanelID)[pDrvDPC] != m_nCurrentPanelID)
  885. {
  886. FERROR("Not current DPC, return");
  887. return false;
  888. }
  889. FINFO("Call HBI_StopAcquisition");
  890. nRet = HBI_StopAcquisition(m_nFpdHandle);
  891. if (TestError(nRet,"HBI_StopAcquisition"))
  892. {
  893. FERROR("Stop Acqusition fail!");
  894. return false;
  895. }
  896. return true;
  897. }
  898. bool Detector_HaoBoRF::ActiveCalibration(void* pDrvDPC, CCOS_CALIBRATION_TYPE eType)
  899. {
  900. printf("========ActiveCalibration \n");
  901. FINFO("========ActiveCalibration ");
  902. if ((*m_pDPC2PanelID)[pDrvDPC] != m_nCurrentPanelID)
  903. {
  904. //printf("Not current DPC, return\n");
  905. FERROR("Not current DPC, return");
  906. return false;
  907. }
  908. if (-1 == m_nCurrentLogicMode)
  909. {
  910. FERROR("Illegal exam mode");
  911. return false;
  912. }
  913. if (CCOS_CALIBRATION_TYPE_XRAY == eType)
  914. {
  915. SetDpcStatus(DetStatus_GainCalibration);
  916. }
  917. m_eCaliType = eType;
  918. return true;
  919. }
  920. bool Detector_HaoBoRF::PrepareCalibration(void* pDrvDPC)
  921. {
  922. printf("========PrepareCalibration \n");
  923. FINFO("========PrepareCalibration ");
  924. bool bRet = false;
  925. if ((*m_pDPC2PanelID)[pDrvDPC] != m_nCurrentPanelID)
  926. {
  927. FERROR("Not current DPC, return");
  928. return bRet;
  929. }
  930. if (GetDpcStatus() != DetStatus_GainCalibration)
  931. {
  932. FINFO("Current status is not XrayCalibration, return succeed");
  933. return true;
  934. }
  935. return bRet;
  936. }
  937. bool Detector_HaoBoRF::StartCalibration(void* pDrvDPC)
  938. {
  939. printf("========StartCalibration \n");
  940. FINFO("========StartCalibration ");
  941. bool bRet = false;
  942. if ((*m_pDPC2PanelID)[pDrvDPC] != m_nCurrentPanelID)
  943. {
  944. FERROR("Not current DPC, return");
  945. return bRet;
  946. }
  947. if (CCOS_CALIBRATION_TYPE_DARK == m_eCaliType)
  948. {
  949. m_hDarkEvent->SetEvent();
  950. bRet = true;
  951. }
  952. else if (CCOS_CALIBRATION_TYPE_XRAY == m_eCaliType)
  953. {
  954. bRet = StartGainCalibration();
  955. }
  956. return bRet;
  957. }
  958. bool Detector_HaoBoRF::StopCalibration(void* pDrvDPC)
  959. {
  960. printf("========StopCalibration \n");
  961. FINFO("========StopCalibration ");
  962. bool bRet = false;
  963. int nRet = HBI_SUCCSS;
  964. if ((*m_pDPC2PanelID)[pDrvDPC] != m_nCurrentPanelID)
  965. {
  966. //printf("Not current DPC, return\n");
  967. FERROR("Not current DPC, return");
  968. return bRet;
  969. }
  970. FINFO("StopCalibration Calling StopCalibration");
  971. return bRet;
  972. }
  973. bool Detector_HaoBoRF::LoadDll(string strWorkPath)
  974. {
  975. printf("========LoadDll \n");
  976. FINFO("========LoadDll start");
  977. string strSDKPath = "";
  978. try
  979. {
  980. strSDKPath = (string)m_ModeConfig["SDKPath"];
  981. }
  982. catch (ResDataObjectExption& e)
  983. {
  984. FERROR("Read configuration failed, Error code: {$}", e.what());
  985. return false;
  986. }
  987. string workpath = strWorkPath + strSDKPath;
  988. FINFO("workpath:{$}", workpath);
  989. string drvpath = workpath + "/libHBISDKApi.so";
  990. m_hSDKModule = dlopen(drvpath.c_str(), RTLD_GLOBAL | RTLD_LAZY);
  991. if (m_hSDKModule == nullptr)
  992. {
  993. FERROR("Load {$} failed! FERROR: {$}", drvpath.c_str(), dlerror());
  994. return false;
  995. }
  996. LOAD_PROC_ADDRESS(m_hSDKModule, HBI_Init);
  997. LOAD_PROC_ADDRESS(m_hSDKModule, HBI_Destroy);
  998. LOAD_PROC_ADDRESS(m_hSDKModule, HBI_ConnectDetector);
  999. LOAD_PROC_ADDRESS(m_hSDKModule, HBI_DisConnectDetector);
  1000. LOAD_PROC_ADDRESS(m_hSDKModule, HBI_RegEventCallBackFun);
  1001. LOAD_PROC_ADDRESS(m_hSDKModule, HBI_GetSDKVerion);
  1002. LOAD_PROC_ADDRESS(m_hSDKModule, HBI_GetFirmareVerion);
  1003. LOAD_PROC_ADDRESS(m_hSDKModule, HBI_GetError);
  1004. LOAD_PROC_ADDRESS(m_hSDKModule, HBI_GetFpdCfgInfo);
  1005. LOAD_PROC_ADDRESS(m_hSDKModule, HBI_GetImageProperty);
  1006. LOAD_PROC_ADDRESS(m_hSDKModule, HBI_LiveAcquisition);
  1007. LOAD_PROC_ADDRESS(m_hSDKModule, HBI_StopAcquisition);
  1008. LOAD_PROC_ADDRESS(m_hSDKModule, HBI_SetSelfDumpingTime);
  1009. LOAD_PROC_ADDRESS(m_hSDKModule, HBI_TriggerAndCorrectApplay);
  1010. LOAD_PROC_ADDRESS(m_hSDKModule, HBI_UpdateTriggerMode);
  1011. LOAD_PROC_ADDRESS(m_hSDKModule, HBI_UpdateCorrectEnable);
  1012. LOAD_PROC_ADDRESS(m_hSDKModule, HBI_SetBinning);
  1013. LOAD_PROC_ADDRESS(m_hSDKModule, HBI_GenerateTemplate);
  1014. LOAD_PROC_ADDRESS(m_hSDKModule, HBI_TriggerBinningAcqTime);
  1015. LOAD_PROC_ADDRESS(m_hSDKModule, HBI_IsMutilMode);
  1016. LOAD_PROC_ADDRESS(m_hSDKModule, HBI_SinglePrepare);
  1017. LOAD_PROC_ADDRESS(m_hSDKModule, HBI_SingleAcquisition);
  1018. LOAD_PROC_ADDRESS(m_hSDKModule, HBI_RegProgressCallBack);
  1019. LOAD_PROC_ADDRESS(m_hSDKModule, HBI_WaitForReadyStatus);
  1020. LOAD_PROC_ADDRESS(m_hSDKModule, HBI_SetPGALevel);
  1021. m_bLoaded = true;
  1022. FINFO("LoadDll end");
  1023. return true;
  1024. }
  1025. int Detector_HaoBoRF::GetPGA(unsigned short usValue)
  1026. {
  1027. unsigned short gainMode = ((usValue & 0xff) << 8) | ((usValue >> 8) & 0xff);
  1028. int nPGA = (gainMode >> 10) & 0x3f;
  1029. if (nPGA == 0x02) return 1;
  1030. else if (nPGA == 0x04) return 2;
  1031. else if (nPGA == 0x08) return 3;
  1032. else if (nPGA == 0x0c) return 4;
  1033. else if (nPGA == 0x10) return 5;
  1034. else if (nPGA == 0x18) return 6;
  1035. else if (nPGA == 0x3e) return 7;
  1036. else return 0;
  1037. }
  1038. void Detector_HaoBoRF::PrintDetectorCfg(RegCfgInfo* pCfg)
  1039. {
  1040. FINFO("========PrintDetectorCfg");
  1041. if (pCfg == NULL)
  1042. {
  1043. FERROR("pCfg is NULL!");
  1044. return;
  1045. }
  1046. FINFO("Xray sensor type:{$}", (int)pCfg->m_SysBaseInfo.m_byXRaySensorType);
  1047. // detector type,width and hight
  1048. if (pCfg->m_SysBaseInfo.m_byPanelSize == 0x01)
  1049. FINFO("PanelSize:{$},fpd type:4343-140um", (int)pCfg->m_SysBaseInfo.m_byPanelSize);
  1050. else if (pCfg->m_SysBaseInfo.m_byPanelSize == 0x02)
  1051. FINFO("PanelSize:{$},fpd type:3543-140um", (int)pCfg->m_SysBaseInfo.m_byPanelSize);
  1052. else if (pCfg->m_SysBaseInfo.m_byPanelSize == 0x03)
  1053. FINFO("PanelSize:{$},fpd type:1613-125um", (int)pCfg->m_SysBaseInfo.m_byPanelSize);
  1054. else if (pCfg->m_SysBaseInfo.m_byPanelSize == 0x04)
  1055. FINFO("PanelSize:{$},fpd type:3030-140um", (int)pCfg->m_SysBaseInfo.m_byPanelSize);
  1056. else if (pCfg->m_SysBaseInfo.m_byPanelSize == 0x05)
  1057. FINFO("PanelSize:{$},fpd type:2530-85um", (int)pCfg->m_SysBaseInfo.m_byPanelSize);
  1058. else if (pCfg->m_SysBaseInfo.m_byPanelSize == 0x06)
  1059. FINFO("PanelSize:{$},fpd type:3025-140um", (int)pCfg->m_SysBaseInfo.m_byPanelSize);
  1060. else if (pCfg->m_SysBaseInfo.m_byPanelSize == 0x07)
  1061. FINFO("PanelSize:{$},fpd type:4343-100um", (int)pCfg->m_SysBaseInfo.m_byPanelSize);
  1062. else if (pCfg->m_SysBaseInfo.m_byPanelSize == 0x08)
  1063. FINFO("PanelSize:{$},fpd type:2530-75um", (int)pCfg->m_SysBaseInfo.m_byPanelSize);
  1064. else if (pCfg->m_SysBaseInfo.m_byPanelSize == 0x09)
  1065. FINFO("PanelSize:{$},fpd type:2121-200um", (int)pCfg->m_SysBaseInfo.m_byPanelSize);
  1066. else if (pCfg->m_SysBaseInfo.m_byPanelSize == 0x0a)
  1067. FINFO("PanelSize:{$},fpd type:1412-50um", (int)pCfg->m_SysBaseInfo.m_byPanelSize);
  1068. else if (pCfg->m_SysBaseInfo.m_byPanelSize == 0x0b)
  1069. FINFO("PanelSize:{$},fpd type:0606-50um", (int)pCfg->m_SysBaseInfo.m_byPanelSize);
  1070. else
  1071. FERROR("PanelSize:{$},invalid fpd type!", (int)pCfg->m_SysBaseInfo.m_byPanelSize);
  1072. FINFO("PixelPitch:{$}", (int)pCfg->m_SysBaseInfo.m_byPixelPitch);
  1073. m_nMaxImgWidth = pCfg->m_SysBaseInfo.m_sImageWidth;
  1074. m_nMaxImgHeight = pCfg->m_SysBaseInfo.m_sImageHeight;
  1075. FINFO("Detector Width={$},Hight={$}", m_nMaxImgWidth, m_nMaxImgHeight);
  1076. ConfFeedback(EVT_CONF_MAX_IMAGESIZE, m_nCurrentPanelID, "", m_nMaxImgWidth * m_nMaxImgHeight);
  1077. if (m_pRawImgBuffer)
  1078. {
  1079. delete[] m_pRawImgBuffer;
  1080. m_pRawImgBuffer = nullptr;
  1081. }
  1082. m_pRawImgBuffer = new WORD[(size_t)m_nMaxImgWidth * (size_t)m_nMaxImgHeight];
  1083. if (m_pFullImgBuffer)
  1084. {
  1085. delete[] m_pFullImgBuffer;
  1086. m_pFullImgBuffer = nullptr;
  1087. }
  1088. m_pFullImgBuffer = new WORD[(size_t)m_nMaxImgWidth * (size_t)m_nMaxImgHeight];
  1089. FINFO("Serial Number:{$}", pCfg->m_SysBaseInfo.m_cSnNumber);
  1090. FINFO("MaxFPS:{$}", (int)pCfg->m_SysBaseInfo.m_cMaxFps);
  1091. //ip and port
  1092. unsigned short usValue = ((pCfg->m_EtherInfo.m_sDestUDPPort & 0xff) << 8) | ((pCfg->m_EtherInfo.m_sDestUDPPort >> 8) & 0xff);
  1093. FINFO("SourceIP:{$}.{$}.{$}.{$}:{$}",
  1094. pCfg->m_EtherInfo.m_byDestIP[0],
  1095. pCfg->m_EtherInfo.m_byDestIP[1],
  1096. pCfg->m_EtherInfo.m_byDestIP[2],
  1097. pCfg->m_EtherInfo.m_byDestIP[3],
  1098. usValue);
  1099. usValue = ((pCfg->m_EtherInfo.m_sSourceUDPPort & 0xff) << 8) | ((pCfg->m_EtherInfo.m_sSourceUDPPort >> 8) & 0xff);
  1100. FINFO("DestIP:{$}.{$}.{$}.{$}:{$}",
  1101. pCfg->m_EtherInfo.m_bySourceIP[0],
  1102. pCfg->m_EtherInfo.m_bySourceIP[1],
  1103. pCfg->m_EtherInfo.m_bySourceIP[2],
  1104. pCfg->m_EtherInfo.m_bySourceIP[3],
  1105. usValue);
  1106. //trigger mode
  1107. if (pCfg->m_SysCfgInfo.m_byTriggerMode == 0x01)
  1108. FINFO("static software trigger.[0x{$02X}]", pCfg->m_SysCfgInfo.m_byTriggerMode);
  1109. else if (pCfg->m_SysCfgInfo.m_byTriggerMode == 0x03)
  1110. FINFO("static hvg trigger.[0x{$02X}]", pCfg->m_SysCfgInfo.m_byTriggerMode);
  1111. else if (pCfg->m_SysCfgInfo.m_byTriggerMode == 0x04)
  1112. FINFO("Free AED trigger mode.[0x{$02X}]", pCfg->m_SysCfgInfo.m_byTriggerMode);
  1113. else if (pCfg->m_SysCfgInfo.m_byTriggerMode == 0x05)
  1114. FINFO("Dynamic:Hvg Sync mode.[0x{$02X}]", pCfg->m_SysCfgInfo.m_byTriggerMode);
  1115. else if (pCfg->m_SysCfgInfo.m_byTriggerMode == 0x06)
  1116. FINFO("Dynamic:Fpd Sync mode.[0x{$02X}]", pCfg->m_SysCfgInfo.m_byTriggerMode);
  1117. else if (pCfg->m_SysCfgInfo.m_byTriggerMode == 0x07)
  1118. FINFO("Dynamic:Fpd Continue mode.[0x{$02X}]", pCfg->m_SysCfgInfo.m_byTriggerMode);
  1119. else if (pCfg->m_SysCfgInfo.m_byTriggerMode == 0x08)
  1120. FINFO("Static:SAEC mode.[0x{$02X}]", pCfg->m_SysCfgInfo.m_byTriggerMode);
  1121. else
  1122. FINFO("other trigger mode.[0x{$02X}]", pCfg->m_SysCfgInfo.m_byTriggerMode);
  1123. // Binning type
  1124. nBinning = (int)pCfg->m_SysCfgInfo.m_byBinning;
  1125. if (nBinning < 1 || nBinning > 4)
  1126. {
  1127. nBinning = 1;
  1128. }
  1129. FINFO("Binning type:{$}*{$}", nBinning, nBinning);
  1130. // acq delay time
  1131. FINFO("Pre Acquisition Delay Time:{$}ms", pCfg->m_SysCfgInfo.m_unPreAcquisitionDelayTime);
  1132. // prepare delayed
  1133. BYTE byte1 = 0, byte2 = 0, byte3 = 0, byte4 = 0;
  1134. byte1 = BREAK_UINT32(pCfg->m_SysCfgInfo.m_unPreAcquisitionDelayTime, 0);
  1135. byte2 = BREAK_UINT32(pCfg->m_SysCfgInfo.m_unPreAcquisitionDelayTime, 1);
  1136. byte3 = BREAK_UINT32(pCfg->m_SysCfgInfo.m_unPreAcquisitionDelayTime, 2);
  1137. byte4 = BREAK_UINT32(pCfg->m_SysCfgInfo.m_unPreAcquisitionDelayTime, 3);
  1138. nPrepareTime = BUILD_UINT32(byte4, byte3, byte2, byte1);
  1139. FINFO("nPrepareTime:{$}ms", nPrepareTime);
  1140. FINFO("commCfg._type:{$}", (int)commCfg._type);
  1141. // Continuous acquisition time interval and frame rate calculation
  1142. if (commCfg._type == UDP_COMM_TYPE || commCfg._type == WALN_COMM_TYPE)
  1143. {
  1144. byte1 = BREAK_UINT32(pCfg->m_SysCfgInfo.m_unContinuousAcquisitionSpanTime, 0);
  1145. byte2 = BREAK_UINT32(pCfg->m_SysCfgInfo.m_unContinuousAcquisitionSpanTime, 1);
  1146. byte3 = BREAK_UINT32(pCfg->m_SysCfgInfo.m_unContinuousAcquisitionSpanTime, 2);
  1147. byte4 = BREAK_UINT32(pCfg->m_SysCfgInfo.m_unContinuousAcquisitionSpanTime, 3);
  1148. }
  1149. else
  1150. {
  1151. byte1 = BREAK_UINT32(pCfg->m_SysCfgInfo.m_unSelfDumpingSpanTime, 0);
  1152. byte2 = BREAK_UINT32(pCfg->m_SysCfgInfo.m_unSelfDumpingSpanTime, 1);
  1153. byte3 = BREAK_UINT32(pCfg->m_SysCfgInfo.m_unSelfDumpingSpanTime, 2);
  1154. byte4 = BREAK_UINT32(pCfg->m_SysCfgInfo.m_unSelfDumpingSpanTime, 3);
  1155. }
  1156. nLiveAcqTime = BUILD_UINT32(byte4, byte3, byte2, byte1);
  1157. if (nLiveAcqTime <= 0)
  1158. {
  1159. FWARN("live aqc time:{$}ms", nLiveAcqTime);
  1160. nLiveAcqTime = 1000;
  1161. }
  1162. FINFO("nLiveAcqTime:{$}ms", nLiveAcqTime);
  1163. unsigned int nMaxFps = (unsigned int)(1000 / nLiveAcqTime);
  1164. if (nMaxFps <= 0) nMaxFps = 1;
  1165. FINFO("nMaxFps:{$}", nMaxFps);
  1166. //correction enable
  1167. if (pCfg->m_ImgCaliCfg.m_byOffsetCorrection == 0x01)
  1168. FINFO("Firmware offset correction disenable.[0x{$02X}]", pCfg->m_ImgCaliCfg.m_byOffsetCorrection);
  1169. else if (pCfg->m_ImgCaliCfg.m_byOffsetCorrection == 0x02)
  1170. FINFO("Firmware offset correction enable.[0x{$02X}]", pCfg->m_ImgCaliCfg.m_byOffsetCorrection);
  1171. else
  1172. FINFO("Firmware other offset correction enable.[0x{$02X}]", pCfg->m_ImgCaliCfg.m_byOffsetCorrection);
  1173. if (pCfg->m_ImgCaliCfg.m_byGainCorrection == 0x01)
  1174. FINFO("Firmware gain correction disenable.[0x{$02X}]", pCfg->m_ImgCaliCfg.m_byGainCorrection);
  1175. else if (pCfg->m_ImgCaliCfg.m_byGainCorrection == 0x02)
  1176. FINFO("Firmware gain correction enable.[0x{$02X}]", pCfg->m_ImgCaliCfg.m_byGainCorrection);
  1177. else
  1178. FINFO("Firmware gain offset correction enable.[0x{$02X}]", pCfg->m_ImgCaliCfg.m_byGainCorrection);
  1179. if (pCfg->m_ImgCaliCfg.m_byDefectCorrection == 0x01)
  1180. FINFO("Firmware defect correction disenable.[0x{$02X}]", pCfg->m_ImgCaliCfg.m_byDefectCorrection);
  1181. else if (pCfg->m_ImgCaliCfg.m_byDefectCorrection == 0x02)
  1182. FINFO("Firmware defect correction enable.[0x{$02X}]", pCfg->m_ImgCaliCfg.m_byDefectCorrection);
  1183. else
  1184. FINFO("Firmware defect offset correction enable.[0x{$02X}]", pCfg->m_ImgCaliCfg.m_byDefectCorrection);
  1185. if (pCfg->m_ImgCaliCfg.m_byDummyCorrection == 0x01)
  1186. FINFO("Firmware Dummy correction disenable.[0x{$02X}]", pCfg->m_ImgCaliCfg.m_byDummyCorrection);
  1187. else if (pCfg->m_ImgCaliCfg.m_byDummyCorrection == 0x02)
  1188. FINFO("Firmware Dummy correction enable.[0x{$02X}]", pCfg->m_ImgCaliCfg.m_byDummyCorrection);
  1189. else
  1190. FINFO("Firmware Dummy offset correction enable.[0x{$02X}]", pCfg->m_ImgCaliCfg.m_byDummyCorrection);
  1191. // PGA level
  1192. nPGALevel = GetPGA(pCfg->m_TICOFCfg.m_sTICOFRegister[26]);
  1193. FINFO("nPGALevel:{$}", nPGALevel);
  1194. }
  1195. bool Detector_HaoBoRF::ConnectDetector()
  1196. {
  1197. printf("========ConnectDetector \n");
  1198. FINFO("========ConnectDetector ");
  1199. int nRet = HBI_SUCCSS;
  1200. int j = 0;
  1201. commCfg._type = UDP_JUMBO_COMM_TYPE;//标准网口,支持jumbo类型
  1202. commCfg._loacalPort = 32896;
  1203. commCfg._remotePort = 32897;
  1204. memset(commCfg._localip, 0, sizeof(commCfg._localip));
  1205. memset(commCfg._remoteip,0, sizeof(commCfg._remoteip));
  1206. j = sprintf(commCfg._localip, "%s", "192.168.10.20");//本机IP
  1207. commCfg._localip[j] = '\0';
  1208. j = sprintf(commCfg._remoteip, "%s", "192.168.10.40");//探测器IP
  1209. commCfg._remoteip[j] = '\0';
  1210. commCfg._pcieId = 7024;
  1211. int dooffsetTemplate = 0;
  1212. FINFO("Calling HBI_ConnectDetector");
  1213. nRet = HBI_ConnectDetector(m_nFpdHandle, commCfg, dooffsetTemplate);
  1214. if (TestError(nRet,"HBI_ConnectDetector"))
  1215. {
  1216. FERROR("Connect detector fail!");
  1217. return false;
  1218. }
  1219. return true;
  1220. }
  1221. //True 有错 false 没错
  1222. bool Detector_HaoBoRF::TestError(int nErrorCode, const char* szFuncName)
  1223. {
  1224. if (nErrorCode)
  1225. {
  1226. string strErrorInfo = HBI_GetError(CrErrStrList, 51, nErrorCode);
  1227. if (strErrorInfo.size() == 0)
  1228. {
  1229. FERROR("HBI_GetError fail! error code:{$}", nErrorCode);
  1230. }
  1231. else
  1232. {
  1233. FERROR("{$} return error {$}, reason: {$}", szFuncName, nErrorCode, strErrorInfo);
  1234. }
  1235. return true;
  1236. }
  1237. else
  1238. {
  1239. FINFO("{$} executed successfully", szFuncName);
  1240. return false;
  1241. }
  1242. }
  1243. bool Detector_HaoBoRF::GetPanelInfo()
  1244. {
  1245. FINFO("======GetPanelInfo");
  1246. int nRet = HBI_SUCCSS;
  1247. char serialNumber[20] = { 0 };
  1248. FINFO("Call HBI_GetFPDSerialNumber");
  1249. nRet = HBI_GetFPDSerialNumber(m_nFpdHandle, serialNumber);
  1250. if (TestError(nRet,"HBI_GetFPDSerialNumber"))
  1251. {
  1252. return false;
  1253. }
  1254. FINFO("Detector SN:{$}",serialNumber);
  1255. char sdkVersion[100] = { 0 };
  1256. FINFO("Call HBI_GetSDKVerion");
  1257. nRet = HBI_GetSDKVerion(m_nFpdHandle, sdkVersion);
  1258. if (TestError(nRet, "HBI_GetSDKVerion"))
  1259. {
  1260. return false;
  1261. }
  1262. FINFO("Detector sdk version:{$}", sdkVersion);
  1263. char firmwareVersion[100] = { 0 };
  1264. FINFO("Call HBI_GetFirmareVerion");
  1265. nRet = HBI_GetFirmareVerion(m_nFpdHandle, firmwareVersion);
  1266. if (TestError(nRet, "HBI_GetFirmareVerion"))
  1267. {
  1268. return false;
  1269. }
  1270. FINFO("Detector firmware version:{$}", firmwareVersion);
  1271. RegCfgInfo* pCfgInfo = new RegCfgInfo;
  1272. memset(pCfgInfo, 0x00, sizeof(RegCfgInfo));
  1273. FINFO("Call HBI_GetFirmareVerion");
  1274. nRet = HBI_GetFpdCfgInfo(m_nFpdHandle, pCfgInfo);
  1275. if (TestError(nRet, "HBI_GetFirmareVerion"))
  1276. {
  1277. return false;
  1278. }
  1279. PrintDetectorCfg(pCfgInfo);
  1280. if (pCfgInfo)
  1281. {
  1282. delete pCfgInfo;
  1283. pCfgInfo = nullptr;
  1284. }
  1285. IMAGE_PROPERTY* imageProperty = new ImageProperty();
  1286. FINFO("Call HBI_GetImageProperty");
  1287. nRet = HBI_GetImageProperty(m_nFpdHandle, imageProperty);
  1288. if (TestError(nRet, "HBI_GetImageProperty"))
  1289. {
  1290. return false;
  1291. }
  1292. FINFO("FPD num:{$}, ImageWidth:{$}, ImageHeight:{$}, DataType:{$}, ImageBit:{$}, Endian:{$}, PacketSize:{$}, \
  1293. FrameSize:{$}, TailPacketSize:{$}, CapacitySize:{$}",
  1294. imageProperty->nFpdNum, imageProperty->nwidth, imageProperty->nheight, imageProperty->datatype, imageProperty->ndatabit,
  1295. imageProperty->nendian, imageProperty->packet_size, imageProperty->frame_size, imageProperty->tailPacketSize, imageProperty->frame_number);
  1296. if (imageProperty)
  1297. {
  1298. delete imageProperty;
  1299. imageProperty = nullptr;
  1300. }
  1301. FINFO("Call HBI_IsMutilMode");
  1302. bool bRet = HBI_IsMutilMode(m_nFpdHandle);
  1303. if (bRet)
  1304. {
  1305. FINFO("Detector support multi mode!");
  1306. }
  1307. else
  1308. {
  1309. FINFO("Detector doesn't support multi mode!");
  1310. }
  1311. return true;
  1312. }
  1313. bool Detector_HaoBoRF::CalculateEXI(WORD* pImgData, int nImgWidth, int nImgHeight, int nImageBit, int nThreshold)
  1314. {
  1315. int nROIXL = static_cast<int>(0.3 * nImgWidth);
  1316. int nROIXR = static_cast<int>(0.7 * nImgWidth);
  1317. int nROIYL = static_cast<int>(0.3 * nImgHeight);
  1318. int nROIYR = static_cast<int>(0.7 * nImgHeight);
  1319. WORD* pSrc = NULL;
  1320. long nCount = 0;
  1321. uint64_t nSum = 0;
  1322. int nEXI = 0;
  1323. try
  1324. {
  1325. for (int i = nROIYL; i < nROIYR; i++)
  1326. {
  1327. pSrc = pImgData + (i * nImgWidth);
  1328. for (int j = nROIXL; j < nROIXR; j++)
  1329. {
  1330. nSum += *(pSrc + j);
  1331. nCount++;
  1332. }
  1333. }
  1334. if (nCount > 0) {
  1335. nEXI = static_cast<int>(nSum / nCount);
  1336. }
  1337. else {
  1338. // 处理除零错误
  1339. return false;
  1340. }
  1341. }
  1342. catch (...)
  1343. {
  1344. return false;
  1345. }
  1346. FINFO("Image EXI:{$}, Threshold:{$}", nEXI, nThreshold);
  1347. if (nEXI >= nThreshold)
  1348. {
  1349. FINFO("Image has xray!");
  1350. return true;//有x射线
  1351. }
  1352. return false;
  1353. }
  1354. bool Detector_HaoBoRF::CheckImageExi(WORD* pImgData, int nImgWidth, int nImgHeight, WORD dwExiThrethold)
  1355. {
  1356. if (dwExiThrethold <= 0)
  1357. {
  1358. return true;
  1359. }
  1360. FINFO("Check image exi...");
  1361. bool bResult = CalculateEXI(pImgData, nImgWidth, nImgHeight, 16, dwExiThrethold);
  1362. if (bResult)
  1363. {
  1364. return true;
  1365. }
  1366. FINFO("Check image exi---black Image");
  1367. return false;
  1368. }
  1369. //nIndex 缺省-1
  1370. void Detector_HaoBoRF::OnProcessImage(int nWidth, int nHeight, int nFrameID)
  1371. {
  1372. printf("========OnProcessImage \n");
  1373. FINFO("========OnProcessImage nWidth:{$},nHeight:{$},nIndex:{$}", nWidth, nHeight, nFrameID);
  1374. int ret = 0;
  1375. bool bImageCrop = false;
  1376. if (m_nCropLeft != 0 || m_nCropTop != 0 || m_nCropRight != 0 || m_nCropBottom != 0)
  1377. {
  1378. ret = CropImageMargin(m_pFullImgBuffer, m_nImageWidth, m_nImageHeight,
  1379. m_pRawImgBuffer, m_nRawImageWidth, m_nRawImageHeight, m_nImageBits,
  1380. m_nCropLeft, m_nCropTop, m_nCropRight, m_nCropBottom);
  1381. if (ret)
  1382. {
  1383. FERROR("CropImageMargin fail!!");
  1384. }
  1385. else
  1386. {
  1387. FINFO("CropImageMargin success!");
  1388. bImageCrop = true;
  1389. }
  1390. }
  1391. //上图之前回调图像的宽高,使得上层在拷贝内存时是正确的图像大小
  1392. size_t processedSize = m_nImageWidth * m_nImageHeight * sizeof(unsigned short);
  1393. if (bImageCrop)
  1394. {
  1395. ConfFeedback(EVT_CONF_RAW_WIDTH, m_nCurrentPanelID, "", m_nImageWidth);
  1396. ConfFeedback(EVT_CONF_RAW_HIGHT, m_nCurrentPanelID, "", m_nImageHeight);
  1397. }
  1398. else
  1399. {
  1400. processedSize = m_nRawImageWidth * m_nRawImageHeight * sizeof(unsigned short);
  1401. ConfFeedback(EVT_CONF_RAW_WIDTH, m_nCurrentPanelID, "", m_nRawImageWidth);
  1402. ConfFeedback(EVT_CONF_RAW_HIGHT, m_nCurrentPanelID, "", m_nRawImageHeight);
  1403. }
  1404. if (!m_bValidImage && m_nDelayTime > 0)
  1405. {
  1406. struct timespec ts;
  1407. clock_gettime(CLOCK_MONOTONIC, &ts); // 使用CLOCK_MONOTONIC时钟,不受系统时间调整影响
  1408. m_dwEndTime = (unsigned long long)ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
  1409. }
  1410. FINFO("OnProcessImage m_nDropImgNum:{$} m_nDropImgCount:{$}", m_nDropImgNum, m_nDropImgCount);
  1411. if (m_nDropImgNum < m_nDropImgCount)//配置为0时表示不丢图
  1412. {
  1413. m_nDropImgNum++;
  1414. FINFO("Drop {$} image", m_nDropImgNum);
  1415. }
  1416. else if (!CheckTimeLimit(m_dwBeginTime, m_dwEndTime))//m_nDelayTime 配置为0时表示不延时
  1417. {
  1418. m_nDropImgNum++;
  1419. FERROR("CheckTimeLimit Drop {$} image", m_nDropImgNum);
  1420. }
  1421. else
  1422. {
  1423. if (m_bFirstImage)
  1424. {
  1425. FINFO("m_bFirstImage is true");
  1426. StatusFeedback(EVT_STATUS_PANEL, PANEL_XWINDOW_ON);
  1427. m_bFirstImage = false;
  1428. }
  1429. FINFO("OnProcessImage m_nCurrentLogicMode:{$}", m_nCurrentLogicMode);
  1430. //560RF动态模式做点片
  1431. //if (m_nCurrentLogicMode == AcqMode::RAD)
  1432. {
  1433. //FINFO("RAD mode");
  1434. if (m_bValidImage)
  1435. {
  1436. //RAD模式已经推过图了就不在推图了
  1437. FINFO("RAD has send one image! return");
  1438. return;
  1439. }
  1440. if (bImageCrop)
  1441. {
  1442. //计算图像EXI 不满足的不往上推
  1443. if (!CheckImageExi(m_pFullImgBuffer, m_nImageWidth, m_nImageHeight, m_nExiThreshold)) //true 有射线 false 没射线
  1444. {
  1445. return;
  1446. }
  1447. }
  1448. else
  1449. {
  1450. //计算图像EXI 不满足的不往上推
  1451. if (!CheckImageExi(m_pRawImgBuffer, m_nRawImageWidth, m_nRawImageHeight, m_nExiThreshold)) //true 有射线 false 没射线
  1452. {
  1453. return;
  1454. }
  1455. }
  1456. }
  1457. const std::string baseTimestamp = std::to_string(
  1458. std::chrono::duration_cast<std::chrono::milliseconds>(
  1459. std::chrono::system_clock::now().time_since_epoch()
  1460. ).count()
  1461. );
  1462. std::string processedImagePath = "";
  1463. if (bImageCrop)
  1464. {
  1465. processedImagePath = saveProcessedImage(
  1466. m_pFullImgBuffer,
  1467. processedSize,
  1468. baseTimestamp
  1469. );
  1470. }
  1471. else
  1472. {
  1473. processedImagePath = saveProcessedImage(
  1474. m_pRawImgBuffer,
  1475. processedSize,
  1476. baseTimestamp
  1477. );
  1478. }
  1479. InfoFeedback(EVT_DATA_RAW_IMAGE, -1, 0, 0, processedImagePath.c_str());
  1480. m_bValidImage = true; //合法图像,不再判断时间间隔
  1481. }
  1482. }
  1483. void Detector_HaoBoRF::SaveImage(int nIndex, bool bImageCrop)
  1484. {
  1485. FINFO("========Begin save image");
  1486. char szTemp[30] = { 0 };
  1487. FILE* fp;
  1488. string strFileName = m_strCtrlWorkPath + "/rawdata";
  1489. if (-1 == nIndex)
  1490. {
  1491. strFileName += "/Image_Rad.raw";
  1492. }
  1493. else
  1494. {
  1495. sprintf(szTemp, "/Image_%d.raw", nIndex);
  1496. strFileName += szTemp;
  1497. }
  1498. if ((fp = fopen(strFileName.c_str(), "wb+")) == NULL)
  1499. {
  1500. int err = errno;
  1501. FERROR("fopen {$} failed, {$}", strFileName, err);
  1502. return;
  1503. }
  1504. if (bImageCrop)
  1505. {
  1506. fwrite(m_pFullImgBuffer, sizeof(WORD),
  1507. (unsigned long long)m_nImageWidth * m_nImageHeight, fp);
  1508. }
  1509. else
  1510. {
  1511. fwrite(m_pRawImgBuffer, sizeof(WORD),
  1512. (unsigned long long)m_nRawImageWidth * m_nRawImageHeight, fp);
  1513. }
  1514. fclose(fp);
  1515. FINFO("Save {$} image over", strFileName);
  1516. }
  1517. // 说明:调用SDK接口,执行offset校正流程 dark校正、闲时offset刷新都会调用这个函数
  1518. bool Detector_HaoBoRF::StartDarkCalibration()
  1519. {
  1520. printf("========StartDarkCalibration \n");
  1521. FINFO("========StartDarkCalibration");
  1522. FINFO("Call HBI_GenerateTemplate");
  1523. int ret = HBI_GenerateTemplate(m_nFpdHandle, OFFSET_TEMPLATE_TYPE, 0);
  1524. if (TestError(ret,"HBI_GenerateTemplate"))
  1525. {
  1526. return false;
  1527. }
  1528. return true;
  1529. }
  1530. bool Detector_HaoBoRF::StartGainCalibration()
  1531. {
  1532. printf("========StartGainCalibration \n");
  1533. FINFO("========StartGainCalibration ");
  1534. if (-1 == m_nCurrentLogicMode)
  1535. {
  1536. FERROR("Illegal exam mode");
  1537. return false;
  1538. }
  1539. return true;
  1540. }
  1541. void* Detector_HaoBoRF::onFPDScanThread(void* pvoid)
  1542. {
  1543. Detector_HaoBoRF* pOpr = (Detector_HaoBoRF*)pvoid;
  1544. FINFO("Enter scan thread");
  1545. bool bExit = false;
  1546. while (!bExit)
  1547. {
  1548. DWORD dwRet = LinuxEvent::WaitForMultipleEvents(pOpr->m_hArrayEvent,INFINITE);
  1549. if (WAIT_OBJECT_0 == dwRet) //m_hStopScanEvent
  1550. {
  1551. bExit = true;
  1552. }
  1553. else if (WAIT_OBJECT_0 + 1 == dwRet) //m_hAcqEvent
  1554. {
  1555. pOpr->OnAcquireImage();
  1556. }
  1557. else if (WAIT_OBJECT_0 + 2 == dwRet) //m_hGainEvent
  1558. {
  1559. pOpr->OnAcquireGainImage();
  1560. }
  1561. else if (WAIT_OBJECT_0 + 3 == dwRet) //m_hDarkEvent
  1562. {
  1563. pOpr->OnStartDarkCalibration();
  1564. }
  1565. else if (WAIT_OBJECT_0 + 4 == dwRet) //m_hProcessImgEvent
  1566. {
  1567. pOpr->OnProcessImage(pOpr->m_nRawImageWidth, pOpr->m_nRawImageHeight, pOpr->m_nFrameID);
  1568. }
  1569. }
  1570. FINFO("Level scan thread");
  1571. return 0;
  1572. }
  1573. void* Detector_HaoBoRF::RefreshOffsetThread(void* pvoid)
  1574. {
  1575. Detector_HaoBoRF* pOpr = (Detector_HaoBoRF*)pvoid;
  1576. FINFO("========Enter fresh offset thread");
  1577. bool bExit = false;
  1578. while (!bExit)
  1579. {
  1580. DWORD dwRet = LinuxEvent::WaitForMultipleEvents(pOpr->m_hOffsetEvent, INFINITE);
  1581. if (WAIT_OBJECT_0 == dwRet) //m_hStopOffsetEvent
  1582. {
  1583. bExit = true;
  1584. }
  1585. else if (WAIT_OBJECT_0 + 1 == dwRet) //m_hStartAllOffset
  1586. {
  1587. pOpr->RefreshAllOffset();
  1588. }
  1589. else if (WAIT_OBJECT_0 + 2 == dwRet) //m_hStartOffset
  1590. {
  1591. pOpr->OnRefreshOffset();
  1592. }
  1593. else if (WAIT_OBJECT_0 + 3 == dwRet) //m_hAbortOffset
  1594. {
  1595. pOpr->AbortFreshOffset();
  1596. }
  1597. }
  1598. FINFO("Level fresh offset thread");
  1599. return 0;
  1600. }
  1601. //1800RF支持单独的RAD模式
  1602. void Detector_HaoBoRF::OnAcquireImage()
  1603. {
  1604. printf("========OnAcquireImage \n");
  1605. FINFO("========OnAcquireImage ");
  1606. SetDpcStatus(DetStatus_Acquire); //动态模式激活采集,设置状态
  1607. SetDpcStatus(DetStatus_Standby); //rad采集失败,设置状态
  1608. }
  1609. //SDK Rad模式增益校正流程
  1610. void Detector_HaoBoRF::OnAcquireGainImage()
  1611. {
  1612. printf("========OnAcquireGainImage \n");
  1613. FINFO("========OnAcquireGainImage ");
  1614. SetDpcStatus(DetStatus_Standby);
  1615. StatusFeedback(EVT_STATUS_CALIBRATIOIN, PANEL_EVENT_END_OK);
  1616. }
  1617. //说明:开始暗场校正
  1618. void Detector_HaoBoRF::OnStartDarkCalibration()
  1619. {
  1620. printf("========OnStartDarkCalibration \n");
  1621. FINFO("========OnStartDarkCalibration ");
  1622. SetDpcStatus(DetStatus_OffsetCalibration); //开始offset校正
  1623. if (StartDarkCalibration())
  1624. {
  1625. StatusFeedback(EVT_STATUS_CALIBRATIOIN, PANEL_EVENT_END_OK);
  1626. }
  1627. else
  1628. {
  1629. StatusFeedback(EVT_STATUS_CALIBRATIOIN, PANEL_EVENT_END_ERROR);
  1630. }
  1631. SetDpcStatus(DetStatus_Standby); //offset校正完成,置回Standby
  1632. }
  1633. //获取只用于本DPC记录的探测器状态
  1634. eDetStatus Detector_HaoBoRF::GetDpcStatus()
  1635. {
  1636. string strStatus = "Unknown";
  1637. switch (m_eStatus)
  1638. {
  1639. case DetStatus_NotIni:
  1640. strStatus = "NotIni";
  1641. break;
  1642. case DetStatus_NotConn:
  1643. strStatus = "NotConn";
  1644. break;
  1645. case DetStatus_Sleep:
  1646. strStatus = "Sleep";
  1647. break;
  1648. case DetStatus_Standby:
  1649. strStatus = "Standby";
  1650. break;
  1651. case DetStatus_Acquire:
  1652. strStatus = "Acquire";
  1653. break;
  1654. case DetStatus_OffsetCalibration:
  1655. strStatus = "OffsetCalibration";
  1656. break;
  1657. case DetStatus_GainCalibration:
  1658. strStatus = "GainCalibration";
  1659. break;
  1660. default:
  1661. break;
  1662. }
  1663. FINFO("Driver status: {$}", strStatus.c_str());
  1664. return m_eStatus;
  1665. }
  1666. //设置只用于本DPC的探测器状态
  1667. bool Detector_HaoBoRF::SetDpcStatus(eDetStatus status)
  1668. {
  1669. string strStatus = "Unknown";
  1670. bool bSetStatus = true;
  1671. switch (status)
  1672. {
  1673. case DetStatus_NotIni:
  1674. strStatus = "NotIni";
  1675. break;
  1676. case DetStatus_NotConn:
  1677. strStatus = "NotConn";
  1678. break;
  1679. case DetStatus_Sleep:
  1680. strStatus = "Sleep";
  1681. break;
  1682. case DetStatus_Standby:
  1683. strStatus = "Standby";
  1684. break;
  1685. case DetStatus_Acquire:
  1686. strStatus = "Acquire";
  1687. break;
  1688. case DetStatus_OffsetCalibration:
  1689. strStatus = "OffsetCalibration";
  1690. break;
  1691. case DetStatus_GainCalibration:
  1692. strStatus = "GainCalibration";
  1693. break;
  1694. default:
  1695. bSetStatus = false;
  1696. break;
  1697. }
  1698. if (bSetStatus)
  1699. {
  1700. m_eStatus = status;
  1701. FINFO("Set driver status: {$}", strStatus.c_str());
  1702. }
  1703. else
  1704. {
  1705. FERROR("{$} {$} is a illegal status", strStatus.c_str(), (int)status);
  1706. }
  1707. return bSetStatus;
  1708. }
  1709. bool Detector_HaoBoRF::LoadCalibrationFiles()
  1710. {
  1711. FINFO("========LoadCalibrationFiles");
  1712. return true;
  1713. }
  1714. //参照RFOC康众动态代码整理的图像裁剪功能
  1715. int Detector_HaoBoRF::CropImageMargin(void* pDstData, int& nDstWidth, int& nDstHeight,
  1716. void* pScrData, int nSrcWidth, int nSrcHeight, int nBits,
  1717. int nLeftMargin, int nTopMargin, int nRightMargin, int nBottomMargin)
  1718. {
  1719. printf("========CropImageMargin \n");
  1720. FINFO("========CropImageMargin ");
  1721. if ((pDstData == NULL) || (pScrData == NULL) || (nSrcWidth <= 0) || (nSrcHeight <= 0) || (nBits <= 0))
  1722. return -1;
  1723. if ((nLeftMargin >= nSrcWidth) || (nTopMargin >= nSrcHeight))
  1724. return -1;
  1725. int nBitsToBYTE = (nBits + 7) / 8;
  1726. if (nBitsToBYTE < 1)
  1727. return -1;
  1728. int nXL, nXR, nYL, nYR;
  1729. nXL = nLeftMargin;//32
  1730. nYL = nTopMargin;
  1731. if (nSrcWidth - nRightMargin < 0)
  1732. return -1;
  1733. nXR = nSrcWidth - nRightMargin - 1; //2783
  1734. if (nXR < nXL)
  1735. return -1;
  1736. if (nSrcHeight - nBottomMargin < 0)
  1737. return -1;
  1738. nYR = nSrcHeight - nBottomMargin - 1;
  1739. if (nYR < nYL)
  1740. return -1;
  1741. nDstWidth = nXR - nXL + 1;
  1742. nDstHeight = nYR - nYL + 1;
  1743. FINFO("TopCrop:{$};Bottom:{$},nDstWidth:{$},nDstHeight:{$},Bits:{$}", nYL, nYR, nDstWidth, nDstHeight, nBitsToBYTE);
  1744. // 检查目标缓冲区是否足够大
  1745. size_t dstBufferSize = nDstWidth * nDstHeight * nBitsToBYTE;
  1746. // 这里应该添加对目标缓冲区大小的检查,但需要知道缓冲区大小信息
  1747. // 使用标准memcpy而不是::memcpy
  1748. for (int i = nYL; i <= nYR; i++)
  1749. {
  1750. // 计算源和目标的偏移量
  1751. size_t srcOffset = (i * nSrcWidth + nXL) * nBitsToBYTE;
  1752. size_t dstOffset = (i - nYL) * nDstWidth * nBitsToBYTE;
  1753. // 使用memcpy复制数据
  1754. memcpy(
  1755. static_cast<char*>(pDstData) + dstOffset,
  1756. static_cast<char*>(pScrData) + srcOffset,
  1757. nDstWidth * nBitsToBYTE
  1758. );
  1759. }
  1760. return 0;
  1761. }
  1762. std::string Detector_HaoBoRF::saveProcessedImage(void* data, size_t size, const std::string& baseTimestamp)
  1763. {
  1764. const size_t MAX_PROC_FILE_COUNT = 10;
  1765. const std::string TMPFS_DIR = "/mnt/tmpfs/";
  1766. const std::string ORIGINAL_DIR = m_strCtrlWorkPath + "/RawData/";
  1767. std::string procDir;
  1768. bool useTmpfs = false;
  1769. // 确定存储目录
  1770. if (std::filesystem::exists(TMPFS_DIR) && std::filesystem::is_directory(TMPFS_DIR))
  1771. {
  1772. procDir = TMPFS_DIR;
  1773. useTmpfs = true;
  1774. std::cout << "[Detector_HaoBoRF::saveProcessedImage] Using tmpfs directory: " << procDir << std::endl;
  1775. }
  1776. else
  1777. {
  1778. procDir = ORIGINAL_DIR;
  1779. std::cout << "[Detector_HaoBoRF::saveProcessedImage] /mnt/tmpfs not found, using original directory: " << procDir << std::endl;
  1780. }
  1781. const std::string FILE_PREFIX = "Proc_";
  1782. std::string filePath;
  1783. try
  1784. {
  1785. std::filesystem::create_directories(procDir);
  1786. filePath = procDir + FILE_PREFIX + baseTimestamp + ".raw";
  1787. std::ofstream file(filePath, std::ios::binary);
  1788. if (file.is_open())
  1789. {
  1790. file.write(reinterpret_cast<const char*>(data), size);
  1791. std::cout << "[Detector_HaoBoRF::saveProcessedImage] Saved image: " << filePath << std::endl;
  1792. }
  1793. else
  1794. {
  1795. std::cout << "[Detector_HaoBoRF::saveProcessedImage] Failed to open file: " << filePath << std::endl;
  1796. return "";
  1797. }
  1798. cleanOldFiles(procDir, FILE_PREFIX, MAX_PROC_FILE_COUNT, useTmpfs);
  1799. }
  1800. catch (const std::filesystem::filesystem_error& e)
  1801. {
  1802. std::cout << "[Detector_HaoBoRF::saveProcessedImage] File system error: " << e.what() << std::endl;
  1803. return "";
  1804. }
  1805. catch (const std::exception& e)
  1806. {
  1807. std::cout << "[Detector_HaoBoRF::saveProcessedImage] Unexpected error: " << e.what() << std::endl;
  1808. return "";
  1809. }
  1810. return filePath;
  1811. }
  1812. std::filesystem::space_info getFileSystemInfo(const std::string& dirPath) {
  1813. try {
  1814. return std::filesystem::space(dirPath);
  1815. }
  1816. catch (const std::filesystem::filesystem_error& e) {
  1817. std::cout << "[getFileSystemInfo] Failed to get filesystem info for "
  1818. << dirPath << ": " << e.what() << std::endl;
  1819. return {};
  1820. }
  1821. }
  1822. void Detector_HaoBoRF::cleanOldFiles(const std::string& dirPath, const std::string& filePrefix,
  1823. size_t maxCount, bool checkSize)
  1824. {
  1825. const std::string funcTag = "[Detector_HaoBoRF::cleanOldFiles] ";
  1826. try
  1827. {
  1828. // 检查目录是否存在
  1829. if (!std::filesystem::exists(dirPath) || !std::filesystem::is_directory(dirPath))
  1830. {
  1831. std::cout << funcTag << "Directory not found: " << dirPath << std::endl;
  1832. return;
  1833. }
  1834. std::vector<std::filesystem::directory_entry> targetFiles;
  1835. for (const auto& entry : std::filesystem::directory_iterator(dirPath))
  1836. {
  1837. // 只处理常规文件
  1838. if (!entry.is_regular_file())
  1839. continue;
  1840. // 检查文件扩展名是否为.raw
  1841. const std::string extension = entry.path().extension().string();
  1842. if (extension != ".raw")
  1843. continue;
  1844. // 检查文件名是否以指定前缀开头
  1845. const std::string filename = entry.path().filename().string();
  1846. if (filename.length() < filePrefix.length())
  1847. continue;
  1848. if (filename.substr(0, filePrefix.length()) == filePrefix)
  1849. {
  1850. targetFiles.push_back(entry);
  1851. }
  1852. }
  1853. // 按文件最后修改时间排序(最旧的在前)
  1854. std::sort(targetFiles.begin(), targetFiles.end(),
  1855. [](const std::filesystem::directory_entry& a, const std::filesystem::directory_entry& b)
  1856. {
  1857. return std::filesystem::last_write_time(a) < std::filesystem::last_write_time(b);
  1858. });
  1859. // 1. 先按数量限制清理
  1860. size_t filesToDelete = 0;
  1861. if (targetFiles.size() > maxCount)
  1862. {
  1863. filesToDelete = targetFiles.size() - maxCount;
  1864. // 删除超出数量限制的最旧文件
  1865. for (size_t i = 0; i < filesToDelete; ++i)
  1866. {
  1867. const auto& fileToDelete = targetFiles[i];
  1868. if (std::filesystem::remove(fileToDelete.path()))
  1869. {
  1870. std::cout << funcTag << "Deleted old file (count limit): " << fileToDelete.path() << std::endl;
  1871. }
  1872. else
  1873. {
  1874. std::cout << funcTag << "Failed to delete file: " << fileToDelete.path() << std::endl;
  1875. }
  1876. }
  1877. // 更新目标文件列表(移除已删除的文件)
  1878. targetFiles.erase(targetFiles.begin(), targetFiles.begin() + filesToDelete);
  1879. filesToDelete = 0; // 重置计数器用于大小检查
  1880. }
  1881. // 2. 如果需要,按大小限制清理(仅对tmpfs目录)
  1882. if (checkSize && !targetFiles.empty())
  1883. {
  1884. // 获取文件系统信息
  1885. auto fsInfo = getFileSystemInfo(dirPath);
  1886. if (fsInfo.capacity == 0) // 获取信息失败
  1887. {
  1888. std::cout << funcTag << "Cannot check filesystem size, skipping size check" << std::endl;
  1889. return;
  1890. }
  1891. // 计算最大允许使用空间(预留10%作为缓冲)
  1892. const uintmax_t reservedSpace = fsInfo.capacity / 10; // 10%预留空间
  1893. const uintmax_t maxAllowedSize = fsInfo.capacity - reservedSpace;
  1894. // 计算当前文件总大小
  1895. auto calculateTotalSize = [](const std::vector<std::filesystem::directory_entry>& files) {
  1896. uintmax_t total = 0;
  1897. for (const auto& file : files) {
  1898. total += file.file_size();
  1899. }
  1900. return total;
  1901. };
  1902. uintmax_t currentTotalSize = calculateTotalSize(targetFiles);
  1903. std::cout << funcTag << "Current files total size: " << currentTotalSize
  1904. << ", Max allowed size: " << maxAllowedSize << std::endl;
  1905. // 如果当前总大小超过限制,继续删除最旧的文件
  1906. while (currentTotalSize > maxAllowedSize && !targetFiles.empty())
  1907. {
  1908. const auto& fileToDelete = targetFiles[0];
  1909. uintmax_t fileSize = fileToDelete.file_size();
  1910. if (std::filesystem::remove(fileToDelete.path()))
  1911. {
  1912. std::cout << funcTag << "Deleted old file (size limit): " << fileToDelete.path()
  1913. << " (" << fileSize << " bytes)" << std::endl;
  1914. // 更新总量和文件列表
  1915. currentTotalSize -= fileSize;
  1916. targetFiles.erase(targetFiles.begin());
  1917. filesToDelete++;
  1918. }
  1919. else
  1920. {
  1921. std::cout << funcTag << "Failed to delete file: " << fileToDelete.path() << std::endl;
  1922. break; // 删除失败时停止处理
  1923. }
  1924. }
  1925. }
  1926. std::cout << funcTag << "Cleanup completed. Deleted " << filesToDelete
  1927. << " files. Remaining: " << targetFiles.size() << std::endl;
  1928. }
  1929. catch (const std::filesystem::filesystem_error& e)
  1930. {
  1931. std::cout << funcTag << "File system error: " << e.what() << std::endl;
  1932. }
  1933. catch (const std::exception& e)
  1934. {
  1935. std::cout << funcTag << "Unexpected error: " << e.what() << std::endl;
  1936. }
  1937. }
  1938. //说明:初始化时刷新所有模式的offset 配置文件的OffsetInterval配置为0时则不会运行此段代码
  1939. bool Detector_HaoBoRF::RefreshAllOffset()
  1940. {
  1941. printf("========Refresh all mode offset begin \n");
  1942. FINFO("========Refresh all mode offset begin");
  1943. bool bRet = false;
  1944. SetDpcStatus(DetStatus_OffsetCalibration); //开始全模式offset刷新
  1945. StatusFeedback(EVT_STATUS_OFFSET, PANEL_EVENT_START);
  1946. StatusFeedback(EVT_STATUS_OFFSET_PROGRESS, 0);
  1947. bRet = StartDarkCalibration(); //当前mode失败,仍可以进行下一个mode的刷新
  1948. if (!bRet)
  1949. {
  1950. FERROR("RefreshAllOffset StartDarkCalibration failed!");
  1951. StatusFeedback(EVT_STATUS_OFFSET, PANEL_EVENT_END_ERROR);
  1952. }
  1953. StatusFeedback(EVT_STATUS_OFFSET_PROGRESS, 1);
  1954. SetDpcStatus(DetStatus_Standby); //全模式offset刷新结束,置回Standby
  1955. StatusFeedback(EVT_STATUS_OFFSET, PANEL_EVENT_END_OK);
  1956. FINFO("Refresh all mode offset over");
  1957. return bRet;
  1958. }
  1959. //说明:刷当前模式的offset,什么时候刷由前端控制
  1960. void Detector_HaoBoRF::OnRefreshOffset()
  1961. {
  1962. FINFO("========OnRefreshOffset begin");
  1963. bool bRet = false;
  1964. SetDpcStatus(DetStatus_OffsetCalibration);
  1965. StatusFeedback(EVT_STATUS_OFFSET_PROGRESS, 0);
  1966. StatusFeedback(EVT_STATUS_OFFSET, PANEL_EVENT_START);
  1967. bRet = StartDarkCalibration();
  1968. if (bRet)
  1969. {
  1970. StatusFeedback(EVT_STATUS_OFFSET_PROGRESS, 1);
  1971. }
  1972. else
  1973. {
  1974. FERROR("OnRefreshOffset StartDarkCalibration failed!");
  1975. StatusFeedback(EVT_STATUS_OFFSET, PANEL_EVENT_END_ERROR);
  1976. }
  1977. SetDpcStatus(DetStatus_Standby);
  1978. StatusFeedback(EVT_STATUS_OFFSET, PANEL_EVENT_END_OK);
  1979. FINFO("OnRefreshOffset over");
  1980. }
  1981. bool Detector_HaoBoRF::CheckTimeLimit(unsigned long long dwBeginTime, unsigned long long dwEndTime)
  1982. {
  1983. if (m_bValidImage) //合法图像,不再判断时间间隔,直接返回true
  1984. {
  1985. FINFO("CheckTimeLimit m_bValidImage is true!");
  1986. return true;
  1987. }
  1988. if (m_nDelayTime == 0) //没有开启延时机制
  1989. {
  1990. FINFO("The delay time is invalid, return");
  1991. return true;
  1992. }
  1993. if ((int)(dwEndTime - dwBeginTime) > m_nDelayTime)
  1994. {
  1995. FINFO("The interval({$}) is ok", dwEndTime - dwBeginTime);
  1996. return true;
  1997. }
  1998. FERROR("The interval({$}) is shorter than the delay({$})",dwEndTime - dwBeginTime, m_nDelayTime);
  1999. return false;
  2000. }
  2001. void Detector_HaoBoRF::ConfFeedback(int nEventID, int nDetectorID, const char* pszMsg, int nParam1, float fParam2, int nPtrParamLen, void* pParam)
  2002. {
  2003. if (-1 == nDetectorID)
  2004. {
  2005. nDetectorID = m_nCurrentPanelID;
  2006. }
  2007. ((FPDDeviceHaoBo*)(*m_pPanelID2DPC)[nDetectorID])->OnFPDCallback(nDetectorID,
  2008. nEventID, EVT_LEVEL_CONFIGURATION, pszMsg, nParam1, fParam2, nPtrParamLen, pParam);
  2009. }
  2010. void Detector_HaoBoRF::InfoFeedback(int nEventID, int nDetectorID, int nParam1, float fParam2, const char* pszMsg, int nPtrParamLen, void* pParam)
  2011. {
  2012. if (-1 == nDetectorID)
  2013. {
  2014. nDetectorID = m_nCurrentPanelID;
  2015. }
  2016. ((FPDDeviceHaoBo*)(*m_pPanelID2DPC)[nDetectorID])->OnFPDCallback(nDetectorID,
  2017. nEventID, EVT_LEVEL_INFORMATOION, pszMsg, nParam1, fParam2, nPtrParamLen, pParam);
  2018. }
  2019. void Detector_HaoBoRF::StatusFeedback(int nEventID, int nParam1, const char* pszMsg, int nDetectorID, float fParam2, int nPtrParamLen, void* pParam)
  2020. {
  2021. if (-1 == nDetectorID)
  2022. {
  2023. nDetectorID = m_nCurrentPanelID;
  2024. }
  2025. ((FPDDeviceHaoBo*)(*m_pPanelID2DPC)[nDetectorID])->OnFPDCallback(nDetectorID,
  2026. nEventID, EVT_LEVEL_STATUS, pszMsg, nParam1, fParam2, nPtrParamLen, pParam);
  2027. }
  2028. void Detector_HaoBoRF::DataFeedback(int nEventID, void* pParam, int nParam1, float fParam2, const char* pszMsg, int nPtrParamLen, int nDetectorID)
  2029. {
  2030. if (-1 == nDetectorID)
  2031. {
  2032. nDetectorID = m_nCurrentPanelID;
  2033. }
  2034. ((FPDDeviceHaoBo*)(*m_pPanelID2DPC)[nDetectorID])->OnFPDCallback(nDetectorID,
  2035. nEventID, EVT_LEVEL_DATA, pszMsg, nParam1, fParam2, nPtrParamLen, pParam);
  2036. }
  2037. void Detector_HaoBoRF::WarnFeedback(int nEventID, const char* pszMsg, int nParam1, float fParam2, int nPtrParamLen, void* pParam, int nDetectorID)
  2038. {
  2039. if (-1 == nDetectorID)
  2040. {
  2041. nDetectorID = m_nCurrentPanelID;
  2042. }
  2043. ((FPDDeviceHaoBo*)(*m_pPanelID2DPC)[nDetectorID])->OnFPDCallback(nDetectorID,
  2044. nEventID, EVT_LEVEL_WARNING, pszMsg, nParam1, fParam2, nPtrParamLen, pParam);
  2045. }
  2046. void Detector_HaoBoRF::ErrorFeedback(int nEventID, const char* pszMsg, int nDetectorID, int nParam1, float fParam2, int nPtrParamLen, void* pParam)
  2047. {
  2048. if (-1 == nDetectorID)
  2049. {
  2050. nDetectorID = m_nCurrentPanelID;
  2051. }
  2052. ((FPDDeviceHaoBo*)(*m_pPanelID2DPC)[nDetectorID])->OnFPDCallback(nDetectorID,
  2053. nEventID, EVT_LEVEL_ERROR, pszMsg, nParam1, fParam2, nPtrParamLen, pParam);
  2054. }
  2055. float Detector_HaoBoRF::SetFluPPS(float fPPS)
  2056. {
  2057. FINFO("========SetFluPPS {$}", fPPS);
  2058. m_fFrameRate = fPPS;
  2059. return m_fFrameRate;
  2060. }
  2061. void Detector_HaoBoRF::GetFluPPS(float& fFluPPS)
  2062. {
  2063. fFluPPS = m_fFrameRate;
  2064. FINFO("========GetFluPPS {$}", fFluPPS);
  2065. }
  2066. void Detector_HaoBoRF::AbortFreshOffset()
  2067. {
  2068. FINFO("========AbortFreshOffset");
  2069. }
  2070. void Detector_HaoBoRF::SetAbortOffsetEvent()
  2071. {
  2072. FINFO("========SetAbortOffsetEvent");
  2073. m_hAbortOffset->SetEvent();
  2074. }
  2075. void Detector_HaoBoRF::SetFreshAllOffsetEvent()
  2076. {
  2077. FINFO("========SetFreshAllOffsetEvent");
  2078. m_hStartAllOffset->SetEvent();
  2079. }
  2080. void Detector_HaoBoRF::SetfreshOffsetEvent()
  2081. {
  2082. FINFO("========SetfreshOffsetEvent");
  2083. m_hStartOffset->SetEvent();
  2084. }
  2085. void Detector_HaoBoRF::UpdateModeInRunning(int nMode, float fFrequency)
  2086. {
  2087. FINFO("========UpdateModeInRunning ModeID:{$},Frequency:{$}", nMode, fFrequency);
  2088. m_nModeID = nMode;
  2089. m_fFrameRate = fFrequency;
  2090. }