You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

472 lines
15 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
1 year ago
2 years ago
2 years ago
1 year ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
1 year ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
1 year ago
2 years ago
1 year ago
2 years ago
2 years ago
1 year ago
2 years ago
1 year ago
2 years ago
1 year ago
2 years ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
2 years ago
1 year ago
2 years ago
1 year ago
2 years ago
1 year ago
2 years ago
1 year ago
2 years ago
1 year ago
2 years ago
1 year ago
2 years ago
1 year ago
2 years ago
1 year ago
2 years ago
1 year ago
2 years ago
1 year ago
2 years ago
1 year ago
2 years ago
1 year ago
2 years ago
2 years ago
1 year ago
2 years ago
2 years ago
2 years ago
1 year ago
2 years ago
1 year ago
2 years ago
1 year ago
2 years ago
1 year ago
2 years ago
1 year ago
2 years ago
1 year ago
2 years ago
1 year ago
2 years ago
1 year ago
2 years ago
1 year ago
2 years ago
1 year ago
2 years ago
1 year ago
2 years ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
2 years ago
2 years ago
2 years ago
2 years ago
1 year ago
1 year ago
2 years ago
2 years ago
2 years ago
  1. #pragma once
  2. #include <fstream>
  3. #include <functional>
  4. #include <iostream>
  5. #include <list>
  6. #include <map>
  7. #include <memory>
  8. #include <mutex>
  9. #include <set>
  10. #include <sstream>
  11. #include <string>
  12. #include <vector>
  13. //
  14. #include "i_xsync_udp.hpp"
  15. #include "xsync_errcode.hpp"
  16. #include "xsync_packet.hpp"
  17. #include "xsync_regs.hpp"
  18. #include "xsync_v2_sig_type.hpp"
  19. namespace xsync {
  20. using namespace std;
  21. typedef function<void(XsyncTimecode_t *timecode_msg)> xsync_on_timecode_msg_t;
  22. typedef function<void(uint32_t recordSig, XsyncTimecode_t *timecode_msg)> xsync_on_record_sig_change_msg_t;
  23. typedef function<void(xysnc_camera_sync_data_t *timecode_msg)> xsync_on_camera_sync_msg_t;
  24. class Xsync {
  25. public:
  26. private:
  27. /* data */
  28. I_XSUDPFactory *m_xsync_udp_factory = nullptr;
  29. shared_ptr<I_XSUDP> m_xsync_reg_udp = nullptr;
  30. shared_ptr<I_XSUDP> m_xsync_timecode_udp_listener = nullptr;
  31. shared_ptr<I_XSUDP> m_xsync_camera_sync_udp_listener = nullptr;
  32. string m_xsync_ip;
  33. bool m_is_connected = false;
  34. xsync_on_camera_sync_msg_t m_on_camera_sync_msg_cb = nullptr;
  35. xsync_on_timecode_msg_t m_on_timecode_msg_cb = nullptr;
  36. xsync_on_record_sig_change_msg_t m_on_record_sig_change_msg_cb = nullptr;
  37. int txpacket_index = 0;
  38. uint8_t m_xync_cmd_rxdata_cache[2560];
  39. xsync_net_state_t m_net_state = kxsync_net_state_disconnect;
  40. std::recursive_mutex lock_;
  41. Xsync(/* args */);
  42. public:
  43. static Xsync &Ins();
  44. void initialize(I_XSUDPFactory *xsync_udp_factory);
  45. xs_error_code_t connect(string xsync_ip);
  46. xs_error_code_t disConnect();
  47. xsync_net_state_t getNetState();
  48. bool ping(string xsync_ip);
  49. void registerOnTimecodeMsgCallback(xsync_on_timecode_msg_t cb);
  50. void registerOnCameraSyncMsgCallback(xsync_on_camera_sync_msg_t cb);
  51. void registerOnRecordSigChangeMsgCallback(xsync_on_record_sig_change_msg_t cb);
  52. xs_error_code_t reg_write(uint32_t regadd, uint32_t regvalue, uint32_t &regbackvalue, int32_t overtime_ms = 100);
  53. xs_error_code_t reg_write(uint32_t regadd, uint32_t regvalue, int32_t overtime_ms = 100);
  54. xs_error_code_t reg_read(uint32_t regadd, uint32_t &regvalue, int32_t overtime_ms = 100);
  55. xs_error_code_t reg_read_muti(uint32_t regadd, uint32_t nreg, vector<uint32_t> &regvalues, int32_t overtime_ms = 100);
  56. xs_error_code_t readSN(string &sn);
  57. xs_error_code_t readMAC(string &mac);
  58. xs_error_code_t generatorNewMac();
  59. xs_error_code_t factoryReset();
  60. xs_error_code_t reboot();
  61. xs_error_code_t changeNetworkConfig(string ip, string mask, string gateway);
  62. public:
  63. /*******************************************************************************
  64. * *
  65. *******************************************************************************/
  66. /**
  67. * @brief Timecode信号发生器Timecode格式
  68. *
  69. * @param format
  70. *
  71. * TIMECODE_FPS2398
  72. * TIMECODE_FPS2400
  73. * TIMECODE_FPS2500
  74. * TIMECODE_FPS2997
  75. * TIMECODE_FPS2997Drop
  76. * TIMECODE_FPS3000
  77. *
  78. * @return xs_error_code_t
  79. */
  80. xs_error_code_t InternalTimecode_setFormat(TimecodeFormat_t format);
  81. xs_error_code_t InternalTimecode_getFormat(TimecodeFormat_t &format);
  82. /**
  83. * @brief Timecode信号发生器Timecode值
  84. *
  85. * @param timecode
  86. * @return xs_error_code_t
  87. */
  88. xs_error_code_t InternalTimecode_setCode(XsyncTimecode_t timecode);
  89. xs_error_code_t InternalTimecode_getCode(XsyncTimecode_t &timecode);
  90. /*******************************************************************************
  91. * *
  92. *******************************************************************************/
  93. /**
  94. * @brief
  95. *
  96. * @param src
  97. * INPUT_IF_TIMECODE_BNC
  98. * INPUT_IF_TIMECODE_HEADPHONE
  99. *
  100. * @return xs_error_code_t
  101. */
  102. xs_error_code_t ExternalTimecode_setSource(InputInterface_t src);
  103. xs_error_code_t ExternalTimecode_getSource(InputInterface_t &timecode_select);
  104. /**
  105. * @brief
  106. *
  107. * @param format
  108. * @return xs_error_code_t
  109. */
  110. xs_error_code_t ExternalTimecode_setFormat(TimecodeFormat_t format);
  111. xs_error_code_t ExternalTimecode_getFormat(TimecodeFormat_t &format);
  112. // ExternalTimecode_readCode
  113. xs_error_code_t ExternalTimecode_readCode(XsyncTimecode_t &timecode);
  114. /**
  115. * @brief Timecode选择时钟源
  116. *
  117. * @param internal
  118. * 0: Timecode
  119. * 1: Timecode
  120. * @return xs_error_code_t
  121. */
  122. xs_error_code_t SysTimecode_setSource(uint32_t sig);
  123. xs_error_code_t SysTimecode_getSource(uint32_t &sig);
  124. /**
  125. * @brief Timecode格式
  126. *
  127. * @param format
  128. * @return xs_error_code_t
  129. */
  130. xs_error_code_t SysTimecode_readFormat(TimecodeFormat_t &format);
  131. /**
  132. * @brief Timecode值
  133. *
  134. * @param timecode
  135. * @return xs_error_code_t
  136. */
  137. xs_error_code_t SysTimecode_readCode(XsyncTimecode_t &timecode);
  138. /**
  139. * @brief TIMECODE输出模块
  140. *
  141. * : SYS_TIMECODE
  142. */
  143. /**
  144. * @brief BNC输出电平
  145. *
  146. * @param level
  147. * @return xs_error_code_t
  148. */
  149. xs_error_code_t TimecodeOutputModule_setBncOutputLevel(int level); // 0:line,1:mic
  150. xs_error_code_t TimecodeOutputModule_getBncOutputLevel(int &level);
  151. /**
  152. * @brief
  153. *
  154. * @param level
  155. * @return xs_error_code_t
  156. */
  157. xs_error_code_t TimecodeOutputModule_setHeadphoneOutputLevel(int level); // 0:line,1:mic
  158. xs_error_code_t TimecodeOutputModule_getHeadphoneOutputLevel(int &level);
  159. /**
  160. * @brief TTL输入模块的频率
  161. *
  162. * @param index
  163. * @param freq
  164. * @return xs_error_code_t
  165. */
  166. xs_error_code_t TTLInputModule1_detectFreq(uint32_t &freq);
  167. xs_error_code_t TTLInputModule2_detectFreq(uint32_t &freq);
  168. xs_error_code_t TTLInputModule3_detectFreq(uint32_t &freq);
  169. xs_error_code_t TTLInputModule4_detectFreq(uint32_t &freq);
  170. /**
  171. * @brief TTLOutputModuleX_setSrcSigType TTL输出口设置信号源
  172. *
  173. * @param source
  174. * :
  175. * SIGNAL_TTLIN1
  176. * SIGNAL_TTLIN2
  177. * SIGNAL_TTLIN3
  178. * SIGNAL_TTLIN4
  179. * SIGNAL_SYS_CLK_OUTPUT
  180. * SIGNAL_SYS_GENLOCK_OUTPUT
  181. * SIGNAL_SYS_TIMECODE_FREQ_OUTPUT
  182. * SIGNAL_BUSINESS_RECORD_SIG
  183. * SIGNAL_BUSINESS_RECORD_EXPOSURE_SIG
  184. *
  185. * @return xs_error_code_t
  186. */
  187. xs_error_code_t TTLOutputModule1_setSrcSigType(SignalType_t source);
  188. xs_error_code_t TTLOutputModule2_setSrcSigType(SignalType_t source);
  189. xs_error_code_t TTLOutputModule3_setSrcSigType(SignalType_t source);
  190. xs_error_code_t TTLOutputModule4_setSrcSigType(SignalType_t source);
  191. xs_error_code_t TTLOutputModule1_getSrcSigType(SignalType_t &source);
  192. xs_error_code_t TTLOutputModule2_getSrcSigType(SignalType_t &source);
  193. xs_error_code_t TTLOutputModule3_getSrcSigType(SignalType_t &source);
  194. xs_error_code_t TTLOutputModule4_getSrcSigType(SignalType_t &source);
  195. xs_error_code_t TTLOutputModule1_setFreqDivision(uint32_t div);
  196. xs_error_code_t TTLOutputModule2_setFreqDivision(uint32_t div);
  197. xs_error_code_t TTLOutputModule3_setFreqDivision(uint32_t div);
  198. xs_error_code_t TTLOutputModule4_setFreqDivision(uint32_t div);
  199. xs_error_code_t TTLOutputModule1_getFreqDivision(uint32_t &div);
  200. xs_error_code_t TTLOutputModule2_getFreqDivision(uint32_t &div);
  201. xs_error_code_t TTLOutputModule3_getFreqDivision(uint32_t &div);
  202. xs_error_code_t TTLOutputModule4_getFreqDivision(uint32_t &div);
  203. xs_error_code_t TTLOutputModule1_setFreqMultiplication(uint32_t multi);
  204. xs_error_code_t TTLOutputModule2_setFreqMultiplication(uint32_t multi);
  205. xs_error_code_t TTLOutputModule3_setFreqMultiplication(uint32_t multi);
  206. xs_error_code_t TTLOutputModule4_setFreqMultiplication(uint32_t multi);
  207. xs_error_code_t TTLOutputModule1_getFreqMultiplication(uint32_t &multi);
  208. xs_error_code_t TTLOutputModule2_getFreqMultiplication(uint32_t &multi);
  209. xs_error_code_t TTLOutputModule3_getFreqMultiplication(uint32_t &multi);
  210. xs_error_code_t TTLOutputModule4_getFreqMultiplication(uint32_t &multi);
  211. xs_error_code_t TTLOutputModule1_readInFreq(float &freq);
  212. xs_error_code_t TTLOutputModule2_readInFreq(float &freq);
  213. xs_error_code_t TTLOutputModule3_readInFreq(float &freq);
  214. xs_error_code_t TTLOutputModule4_readInFreq(float &freq);
  215. xs_error_code_t TTLOutputModule1_readOutFreq(float &freq);
  216. xs_error_code_t TTLOutputModule2_readOutFreq(float &freq);
  217. xs_error_code_t TTLOutputModule3_readOutFreq(float &freq);
  218. xs_error_code_t TTLOutputModule4_readOutFreq(float &freq);
  219. /**
  220. * @brief Genlock输入模块的频率
  221. *
  222. * @param freq
  223. * @return xs_error_code_t
  224. */
  225. xs_error_code_t ExternalGenlock_detectFreq(float &freq);
  226. /**
  227. * @brief Genlock信号发生器
  228. *
  229. * @param format
  230. * :
  231. * GENLOCK_FPS2397
  232. * GENLOCK_FPS2398
  233. * GENLOCK_FPS2400
  234. * GENLOCK_FPS2500
  235. * GENLOCK_FPS2997
  236. * GENLOCK_FPS3000
  237. * GENLOCK_FPS5000
  238. * GENLOCK_FPS5994
  239. * GENLOCK_FPS6000
  240. * @return xs_error_code_t
  241. */
  242. xs_error_code_t InternalGenlock_setFormat(GenlockFormat_t format);
  243. xs_error_code_t InternalGenlock_getFormat(GenlockFormat_t &format);
  244. /**
  245. * @brief Genlock选择时钟源
  246. *
  247. * @param sig
  248. * :
  249. * 0:
  250. * 1:
  251. *
  252. * @return xs_error_code_t
  253. */
  254. xs_error_code_t SysGenlock_setSrc(uint32_t source);
  255. xs_error_code_t SysGenlock_getSrc(uint32_t &source);
  256. /**
  257. * @brief Genlock频率
  258. *
  259. * @param freq
  260. * @return xs_error_code_t
  261. */
  262. xs_error_code_t SysGenlock_readFreq(float &freq);
  263. /**
  264. * @brief
  265. *
  266. * @param freq HZ,0.1HZ
  267. * @return xs_error_code_t
  268. */
  269. xs_error_code_t InternalClock_setFreq(float freq);
  270. xs_error_code_t InternalClock_getFreq(float &freq);
  271. /**
  272. * @brief
  273. *
  274. * @param mode
  275. * :
  276. * SIGNAL_TTLIN1
  277. * SIGNAL_TTLIN1
  278. * SIGNAL_TTLIN2
  279. * SIGNAL_TTLIN3
  280. * SIGNAL_TTLIN4
  281. * SIGNAL_INTERNAL_CLOCK_SIG
  282. * SIGNAL_SYS_GENLOCK_OUTPUT
  283. *
  284. * @return xs_error_code_t
  285. */
  286. xs_error_code_t SysClock_setSrc(SignalType_t sig);
  287. xs_error_code_t SysClock_getSrc(SignalType_t &sig);
  288. /**
  289. * @brief 沿
  290. *
  291. * @param edge
  292. * TRIGGER_EDGE_RISING
  293. * TRIGGER_EDGE_FALLING
  294. * @return xs_error_code_t
  295. */
  296. xs_error_code_t SysClock_setTriggerEdge(TriggerEdge_t edge);
  297. xs_error_code_t SysClock_getTriggerEdge(TriggerEdge_t &edge);
  298. /**
  299. * @brief
  300. *
  301. * @param div
  302. * @return xs_error_code_t
  303. */
  304. xs_error_code_t SysClock_setFreqDivision(uint32_t div);
  305. xs_error_code_t SysClock_geFreqtDivision(uint32_t &div);
  306. /**
  307. * @brief
  308. *
  309. * @param muti
  310. * @return xs_error_code_t
  311. */
  312. xs_error_code_t SysClock_setFreqMultiplication(uint32_t muti);
  313. xs_error_code_t SysClock_getFreqMultiplication(uint32_t &muti);
  314. /**
  315. * @brief
  316. *
  317. * @param freq
  318. * @return xs_error_code_t
  319. */
  320. xs_error_code_t SysClock_readOutSigFreq(float &freq);
  321. /**
  322. * @brief
  323. *
  324. * @param freq
  325. * @return xs_error_code_t
  326. */
  327. xs_error_code_t SysClock_readInSigFreq(float &freq);
  328. /**
  329. * @brief
  330. *
  331. * @param mode
  332. * :
  333. * CONTROLMODE_MANUAL_TRIGGER
  334. * CONTROLMODE_TIMECODE_TRIGGER
  335. * CONTROLMODE_EXTERNALTTL_TRIGGER
  336. * @return xs_error_code_t
  337. */
  338. xs_error_code_t RecordSigGenerator_setContrlMode(ControlMode_t mode);
  339. xs_error_code_t RecordSigGenerator_getContrlMode(ControlMode_t &mode);
  340. // 手动控制
  341. xs_error_code_t RecordSigGenerator_manualStart();
  342. xs_error_code_t RecordSigGenerator_manualStop();
  343. // timecode控制启停
  344. xs_error_code_t RecordSigGenerator_setAutoStartTimecode(XsyncTimecode_t timecode);
  345. xs_error_code_t RecordSigGenerator_setAutoStopTimecode(XsyncTimecode_t timecode);
  346. xs_error_code_t RecordSigGenerator_getAutoStartTimecode(XsyncTimecode_t &timecode);
  347. xs_error_code_t RecordSigGenerator_getAutoStopTimecode(XsyncTimecode_t &timecode);
  348. xs_error_code_t RecordSigGenerator_setTimecodeCtrlFlag(uint32_t autoStart, uint32_t autoStop);
  349. xs_error_code_t RecordSigGenerator_getTimecodeCtrlFlag(uint32_t &autoStart, uint32_t &autoStop);
  350. // 外部TTL触发控制
  351. /**
  352. * @brief
  353. *
  354. * @param ttlPortNum
  355. * INPUT_IF_TTL1
  356. * INPUT_IF_TTL2
  357. * INPUT_IF_TTL3
  358. * INPUT_IF_TTL4
  359. * @return xs_error_code_t
  360. */
  361. xs_error_code_t RecordSigGenerator_setExternalTTLTriggerSrc(InputInterface_t ttlPortNum); // 1-4
  362. xs_error_code_t RecordSigGenerator_getExternalTTLTriggerSrc(InputInterface_t &ttlPortNum);
  363. /**
  364. * @brief
  365. *
  366. * @param polarity 0: 1:
  367. * @return xs_error_code_t
  368. */
  369. xs_error_code_t RecordSigGenerator_setExternalTTLTriggerPolarity(uint32_t polarity);
  370. xs_error_code_t RecordSigGenerator_getExternalTTLTriggerPolarity(uint32_t &polarity);
  371. /**
  372. * @brief
  373. *
  374. * @param us
  375. * @return xs_error_code_t
  376. */
  377. xs_error_code_t RecordSigGenerator_setRecordExposureTime(uint32_t us);
  378. xs_error_code_t RecordSigGenerator_getRecordExposureTime(uint32_t &us);
  379. /**
  380. * @brief
  381. *
  382. * @param us
  383. * @return xs_error_code_t
  384. */
  385. xs_error_code_t RecordSigGenerator_setRecordExposureOffsetTime(uint32_t us);
  386. xs_error_code_t RecordSigGenerator_getRecordExposureOffsetTime(uint32_t &us);
  387. /**
  388. * @brief
  389. *
  390. * @param state 0:1:
  391. * @return xs_error_code_t
  392. */
  393. xs_error_code_t RecordSigGenerator_getRecordState(uint32_t &state);
  394. xs_error_code_t RecordSigGenerator_readTimecodeSnapshot(XsyncTimecode_t &timecode);
  395. private:
  396. xs_error_code_t doaction(uint32_t action, uint32_t actionval, uint32_t *ackreturn, int32_t overtime_ms = 100);
  397. xs_error_code_t storageConfig();
  398. xs_error_code_t xsync_send_cmd_block(iflytop_xsync_packet_header_t *cmd, iflytop_xsync_packet_header_t *rx_data, int32_t buffersize, int32_t overtime_ms);
  399. xs_error_code_t readtimecode(uint32_t reg0, uint32_t reg1, XsyncTimecode_t &timecode);
  400. xs_error_code_t writetimecode(uint32_t reg0, uint32_t reg1, XsyncTimecode_t timecode);
  401. xs_error_code_t readfreq(uint32_t reg, float &freq);
  402. template <typename T>
  403. xs_error_code_t _reg_read(uint32_t regadd, T &regvalue, int32_t overtime_ms = 100) {
  404. uint32_t regvalue_u32;
  405. xs_error_code_t ret = reg_read(regadd, regvalue_u32, overtime_ms);
  406. if (ret == kxs_ec_success) {
  407. regvalue = (T)regvalue_u32;
  408. }
  409. return ret;
  410. }
  411. void parseTimecodeMsgAndReport(XsyncNetAdd &from, uint8_t *data, size_t length);
  412. void parseCameraSyncMsgAndReport(XsyncNetAdd &from, uint8_t *data, size_t length);
  413. };
  414. } // namespace xsync