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.

555 lines
20 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
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
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
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
2 years ago
2 years ago
2 years ago
1 year ago
2 years ago
1 year ago
2 years ago
2 years ago
2 years 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
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
1 year ago
2 years ago
1 year 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
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 workstate)> xsync_on_workstate_change_msg_t;
  23. typedef function<void(xysnc_camera_sync_data_t *timecode_msg)> xsync_on_camera_sync_msg_t;
  24. /*******************************************************************************
  25. * *
  26. *******************************************************************************/
  27. namespace sig_generator_module {
  28. typedef enum {
  29. kControlMode_manualTrigger, // 0.手动,启动停止
  30. kControlMode_externalTimecodeTrigger, // 1.外部TIMECODE触发启动
  31. kControlMode_externalTTL1Trigger, // 2.外部TTL输入1_高电平触发,低电平停止
  32. kControlMode_externalTTL2Trigger, // 3.外部TTL输入2_高电平触发,低电平停止
  33. kControlMode_externalTTL3Trigger, // 4.外部TTL输入3_高电平触发,低电平停止
  34. kControlMode_externalTTL4Trigger, // 5.外部TTL输入4_高电平触发,低电平停止
  35. } ControlMode_t;
  36. string ControlMode2Str(ControlMode_t mode);
  37. ControlMode_t Str2ControlMode(string mode);
  38. list<string> ControlModeStrSet();
  39. } // namespace sig_generator_module
  40. /*******************************************************************************
  41. * *
  42. *******************************************************************************/
  43. namespace timecode_output_module {
  44. typedef enum {
  45. koff = 0, // 关闭输出
  46. kinternal_timecode_sig = 1, // 外部timecode信号
  47. kext_timecode_sig = 2, // 内部timecode信号
  48. } TriggerSigType_t;
  49. typedef enum {
  50. kline,
  51. kmic,
  52. } OutputSigLevelType_t;
  53. string TriggerSigType2Str(TriggerSigType_t type);
  54. TriggerSigType_t Str2TriggerSigType(string type);
  55. list<string> TriggerSigTypeStrSet();
  56. string OutputSigLevelType2Str(OutputSigLevelType_t type);
  57. OutputSigLevelType_t Str2OutputSigLevelType(string type);
  58. list<string> OutputSigLevelTypeStrSet();
  59. } // namespace timecode_output_module
  60. /*******************************************************************************
  61. * *
  62. *******************************************************************************/
  63. namespace camera_sync_packet_generator_module {
  64. typedef enum {
  65. koff = 0, // 关闭输出
  66. kinternal_genlock_sig = 1, // 外部genlock信号
  67. kext_genlock_sig = 2, // 内部genlock信号
  68. } TriggerSigType_t;
  69. string TriggerSigType2Str(TriggerSigType_t type);
  70. TriggerSigType_t Str2TriggerSigType(string type);
  71. list<string> TriggerSigTypeStrSet();
  72. } // namespace camera_sync_packet_generator_module
  73. /*******************************************************************************
  74. * *
  75. *******************************************************************************/
  76. namespace timecode_input_module {
  77. typedef enum {
  78. koff = 0,
  79. kbnc_timecode,
  80. kheadphone_timecode,
  81. } TriggerSigType_t;
  82. string TriggerSigType2Str(TriggerSigType_t type);
  83. TriggerSigType_t Str2TriggerSigType(string type);
  84. list<string> TriggerSigTypeStrSet();
  85. } // namespace timecode_input_module
  86. class Xsync {
  87. public:
  88. private:
  89. /* data */
  90. I_XSUDPFactory *m_xsync_udp_factory = nullptr;
  91. shared_ptr<I_XSUDP> m_xsync_reg_udp = nullptr;
  92. shared_ptr<I_XSUDP> m_xsync_timecode_udp_listener = nullptr;
  93. shared_ptr<I_XSUDP> m_xsync_camera_sync_udp_listener = nullptr;
  94. string m_xsync_ip;
  95. bool m_is_connected = false;
  96. xsync_on_camera_sync_msg_t m_on_camera_sync_msg_cb = nullptr;
  97. xsync_on_timecode_msg_t m_on_timecode_msg_cb = nullptr;
  98. xsync_on_workstate_change_msg_t m_on_workstate_change_msg_cb = nullptr;
  99. int txpacket_index = 0;
  100. uint8_t m_xync_cmd_rxdata_cache[2560];
  101. xsync_net_state_t m_net_state = kxsync_net_state_disconnect;
  102. std::recursive_mutex lock_;
  103. Xsync(/* args */);
  104. public:
  105. static Xsync &Ins();
  106. void initialize(I_XSUDPFactory *xsync_udp_factory);
  107. /*******************************************************************************
  108. * *
  109. *******************************************************************************/
  110. bool ping(string xsync_ip);
  111. xs_error_code_t connect(string xsync_ip);
  112. xs_error_code_t disConnect();
  113. xsync_net_state_t getNetState();
  114. /*******************************************************************************
  115. * *
  116. *******************************************************************************/
  117. void Basic_registerOnTimecodeMsgCallback(xsync_on_timecode_msg_t on_timecode_msg_cb);
  118. void Basic_registerOnCameraSyncMsgCallback(xsync_on_camera_sync_msg_t on_camera_sync_msg_cb);
  119. void Basic_registerOnWorkstateChangeMsgCallback(xsync_on_workstate_change_msg_t on_workstate_change_msg_cb);
  120. /*******************************************************************************
  121. * *
  122. *******************************************************************************/
  123. xs_error_code_t reg_write(uint32_t regadd, uint32_t regvalue, uint32_t &regbackvalue, int32_t overtime_ms = 100);
  124. xs_error_code_t reg_write(uint32_t regadd, uint32_t regvalue, int32_t overtime_ms = 100);
  125. xs_error_code_t reg_read(uint32_t regadd, uint32_t &regvalue, int32_t overtime_ms = 100);
  126. xs_error_code_t reg_read_muti(uint32_t regadd, uint32_t nreg, vector<uint32_t> &regvalues, int32_t overtime_ms = 100);
  127. /*******************************************************************************
  128. * *
  129. *******************************************************************************/
  130. xs_error_code_t Basic_generatorNewMac();
  131. xs_error_code_t Basic_factoryReset();
  132. xs_error_code_t Basic_reboot();
  133. xs_error_code_t Basic_changeNetworkConfig(string ip, string mask, string gateway);
  134. xs_error_code_t Basic_clearXsyncCameraSyncIndexCount();
  135. // xs_error_code_t Basic_setGenlockFormat(GenlockFormat_t format);
  136. // xs_error_code_t Basic_getGenlockFormat(GenlockFormat_t &format);
  137. // xs_error_code_t Basic_setTimecodeFormat(TimecodeFormat_t format);
  138. public:
  139. /*******************************************************************************
  140. * *
  141. *******************************************************************************/
  142. /**
  143. * @brief
  144. *
  145. * @param sig
  146. * @return xs_error_code_t
  147. */
  148. xs_error_code_t CameraSyncPacketGeneratorModule_setTriggerSig(camera_sync_packet_generator_module::TriggerSigType_t sig);
  149. xs_error_code_t CameraSyncPacketGeneratorModule_getTriggerSig(camera_sync_packet_generator_module::TriggerSigType_t &sig);
  150. /**
  151. * @brief
  152. *
  153. * @param packetNum :30 30
  154. * @return xs_error_code_t
  155. */
  156. xs_error_code_t CameraSyncPacketGeneratorModule_setReportPeriod(uint32_t packetNum);
  157. xs_error_code_t CameraSyncPacketGeneratorModule_getReportPeriod(uint32_t &packetNum);
  158. /**
  159. * @brief
  160. *
  161. * @param index
  162. * @return xs_error_code_t
  163. */
  164. xs_error_code_t CameraSyncPacketGeneratorModule_getPacketIndex(uint32_t &index);
  165. /**
  166. * @brief
  167. *
  168. * @return xs_error_code_t
  169. */
  170. xs_error_code_t CameraSyncPacketGeneratorModule_clearPacketIndex();
  171. public:
  172. /*******************************************************************************
  173. * *
  174. *******************************************************************************/
  175. /*******************************************************************************
  176. * *
  177. *******************************************************************************/
  178. /**
  179. * @brief Timecode信号发生器Timecode格式
  180. *
  181. * @param format
  182. *
  183. * TIMECODE_FPS2398
  184. * TIMECODE_FPS2400
  185. * TIMECODE_FPS2500
  186. * TIMECODE_FPS2997
  187. * TIMECODE_FPS2997Drop
  188. * TIMECODE_FPS3000
  189. *
  190. * @return xs_error_code_t
  191. */
  192. xs_error_code_t InternalTimecode_setFormat(TimecodeFormat_t format);
  193. xs_error_code_t InternalTimecode_getFormat(TimecodeFormat_t &format);
  194. /**
  195. * @brief Timecode信号发生器Timecode值
  196. *
  197. * @param timecode
  198. * @return xs_error_code_t
  199. */
  200. xs_error_code_t InternalTimecode_setCode(XsyncTimecode_t timecode);
  201. xs_error_code_t InternalTimecode_getCode(XsyncTimecode_t &timecode);
  202. /*******************************************************************************
  203. * *
  204. *******************************************************************************/
  205. /**
  206. * @brief
  207. *
  208. * @param src
  209. * INPUT_IF_TIMECODE_BNC
  210. * INPUT_IF_TIMECODE_HEADPHONE
  211. *
  212. * @return xs_error_code_t
  213. */
  214. xs_error_code_t ExternalTimecode_setSource(InputInterface_t src);
  215. xs_error_code_t ExternalTimecode_getSource(InputInterface_t &timecode_select);
  216. /**
  217. * @brief
  218. *
  219. * @param format
  220. * @return xs_error_code_t
  221. */
  222. xs_error_code_t ExternalTimecode_setFormat(TimecodeFormat_t format);
  223. xs_error_code_t ExternalTimecode_getFormat(TimecodeFormat_t &format);
  224. // ExternalTimecode_readCode
  225. xs_error_code_t ExternalTimecode_readCode(XsyncTimecode_t &timecode);
  226. /*******************************************************************************
  227. * SysTimecode *
  228. *******************************************************************************/
  229. /**
  230. * @brief Timecode选择时钟源
  231. *
  232. * @param internal
  233. * 0: Timecode
  234. * 1: Timecode
  235. * @return xs_error_code_t
  236. */
  237. xs_error_code_t SysTimecode_setSource(uint32_t sig);
  238. xs_error_code_t SysTimecode_getSource(uint32_t &sig);
  239. /**
  240. * @brief Timecode格式
  241. *
  242. * @param format
  243. * @return xs_error_code_t
  244. */
  245. xs_error_code_t SysTimecode_readFormat(TimecodeFormat_t &format);
  246. /**
  247. * @brief Timecode值
  248. *
  249. * @param timecode
  250. * @return xs_error_code_t
  251. */
  252. xs_error_code_t SysTimecode_readCode(XsyncTimecode_t &timecode);
  253. /*******************************************************************************
  254. * Timecode输出口配置 *
  255. *******************************************************************************/
  256. /**
  257. * @brief TIMECODE输出模块
  258. *
  259. * : SYS_TIMECODE
  260. */
  261. /**
  262. * @brief BNC输出电平
  263. *
  264. * @param level
  265. * @return xs_error_code_t
  266. */
  267. xs_error_code_t TimecodeOutputModule_setBncOutputLevel(int level); // 0:line,1:mic
  268. xs_error_code_t TimecodeOutputModule_getBncOutputLevel(int &level);
  269. /**
  270. * @brief
  271. *
  272. * @param level
  273. * @return xs_error_code_t
  274. */
  275. xs_error_code_t TimecodeOutputModule_setHeadphoneOutputLevel(int level); // 0:line,1:mic
  276. xs_error_code_t TimecodeOutputModule_getHeadphoneOutputLevel(int &level);
  277. /*******************************************************************************
  278. * TTL输入模块 *
  279. *******************************************************************************/
  280. /**
  281. * @brief TTL输入模块的频率
  282. *
  283. * @param index
  284. * @param freq
  285. * @return xs_error_code_t
  286. */
  287. xs_error_code_t TTLInputModule1_detectFreq(uint32_t &freq);
  288. xs_error_code_t TTLInputModule2_detectFreq(uint32_t &freq);
  289. xs_error_code_t TTLInputModule3_detectFreq(uint32_t &freq);
  290. xs_error_code_t TTLInputModule4_detectFreq(uint32_t &freq);
  291. /*******************************************************************************
  292. * TTL输出模块 *
  293. *******************************************************************************/
  294. /**
  295. * @brief TTLOutputModuleX_setSrcSigType TTL输出口设置信号源
  296. *
  297. * @param source
  298. * :
  299. * SIGNAL_TTLIN1
  300. * SIGNAL_TTLIN2
  301. * SIGNAL_TTLIN3
  302. * SIGNAL_TTLIN4
  303. * SIGNAL_SYS_CLK_OUTPUT
  304. * SIGNAL_SYS_GENLOCK_OUTPUT
  305. * SIGNAL_SYS_TIMECODE_FREQ_OUTPUT
  306. * SIGNAL_BUSINESS_RECORD_SIG
  307. * SIGNAL_BUSINESS_RECORD_EXPOSURE_SIG
  308. *
  309. * @return xs_error_code_t
  310. */
  311. xs_error_code_t TTLOutputModule1_setSrcSigType(SignalType_t source);
  312. xs_error_code_t TTLOutputModule2_setSrcSigType(SignalType_t source);
  313. xs_error_code_t TTLOutputModule3_setSrcSigType(SignalType_t source);
  314. xs_error_code_t TTLOutputModule4_setSrcSigType(SignalType_t source);
  315. xs_error_code_t TTLOutputModule1_getSrcSigType(SignalType_t &source);
  316. xs_error_code_t TTLOutputModule2_getSrcSigType(SignalType_t &source);
  317. xs_error_code_t TTLOutputModule3_getSrcSigType(SignalType_t &source);
  318. xs_error_code_t TTLOutputModule4_getSrcSigType(SignalType_t &source);
  319. xs_error_code_t TTLOutputModule1_setFreqDivision(uint32_t div);
  320. xs_error_code_t TTLOutputModule2_setFreqDivision(uint32_t div);
  321. xs_error_code_t TTLOutputModule3_setFreqDivision(uint32_t div);
  322. xs_error_code_t TTLOutputModule4_setFreqDivision(uint32_t div);
  323. xs_error_code_t TTLOutputModule1_getFreqDivision(uint32_t &div);
  324. xs_error_code_t TTLOutputModule2_getFreqDivision(uint32_t &div);
  325. xs_error_code_t TTLOutputModule3_getFreqDivision(uint32_t &div);
  326. xs_error_code_t TTLOutputModule4_getFreqDivision(uint32_t &div);
  327. xs_error_code_t TTLOutputModule1_setFreqMultiplication(uint32_t multi);
  328. xs_error_code_t TTLOutputModule2_setFreqMultiplication(uint32_t multi);
  329. xs_error_code_t TTLOutputModule3_setFreqMultiplication(uint32_t multi);
  330. xs_error_code_t TTLOutputModule4_setFreqMultiplication(uint32_t multi);
  331. xs_error_code_t TTLOutputModule1_getFreqMultiplication(uint32_t &multi);
  332. xs_error_code_t TTLOutputModule2_getFreqMultiplication(uint32_t &multi);
  333. xs_error_code_t TTLOutputModule3_getFreqMultiplication(uint32_t &multi);
  334. xs_error_code_t TTLOutputModule4_getFreqMultiplication(uint32_t &multi);
  335. xs_error_code_t TTLOutputModule1_readInFreq(float &freq);
  336. xs_error_code_t TTLOutputModule2_readInFreq(float &freq);
  337. xs_error_code_t TTLOutputModule3_readInFreq(float &freq);
  338. xs_error_code_t TTLOutputModule4_readInFreq(float &freq);
  339. xs_error_code_t TTLOutputModule1_readOutFreq(float &freq);
  340. xs_error_code_t TTLOutputModule2_readOutFreq(float &freq);
  341. xs_error_code_t TTLOutputModule3_readOutFreq(float &freq);
  342. xs_error_code_t TTLOutputModule4_readOutFreq(float &freq);
  343. /*******************************************************************************
  344. * GENLOCK *
  345. *******************************************************************************/
  346. /**
  347. * @brief Genlock输入模块的频率
  348. *
  349. * @param freq
  350. * @return xs_error_code_t
  351. */
  352. xs_error_code_t ExternalGenlock_detectFreq(float &freq);
  353. /**
  354. * @brief Genlock信号发生器
  355. *
  356. * @param format
  357. * :
  358. * GENLOCK_FPS2397
  359. * GENLOCK_FPS2398
  360. * GENLOCK_FPS2400
  361. * GENLOCK_FPS2500
  362. * GENLOCK_FPS2997
  363. * GENLOCK_FPS3000
  364. * GENLOCK_FPS5000
  365. * GENLOCK_FPS5994
  366. * GENLOCK_FPS6000
  367. * @return xs_error_code_t
  368. */
  369. xs_error_code_t InternalGenlock_setFormat(GenlockFormat_t format);
  370. xs_error_code_t InternalGenlock_getFormat(GenlockFormat_t &format);
  371. /**
  372. * @brief Genlock选择时钟源
  373. *
  374. * @param sig
  375. * :
  376. * 0:
  377. * 1:
  378. *
  379. * @return xs_error_code_t
  380. */
  381. xs_error_code_t SysGenlock_setSrc(uint32_t source);
  382. xs_error_code_t SysGenlock_getSrc(uint32_t &source);
  383. /**
  384. * @brief Genlock频率
  385. *
  386. * @param freq
  387. * @return xs_error_code_t
  388. */
  389. xs_error_code_t SysGenlock_readFreq(float &freq);
  390. /*******************************************************************************
  391. * *
  392. *******************************************************************************/
  393. /**
  394. * @brief
  395. *
  396. * @param freq HZ,0.1HZ
  397. * @return xs_error_code_t
  398. */
  399. xs_error_code_t InternalClock_setFreq(float freq);
  400. xs_error_code_t InternalClock_getFreq(float &freq);
  401. /*******************************************************************************
  402. * *
  403. *******************************************************************************/
  404. /**
  405. * @brief
  406. *
  407. * @param mode
  408. * :
  409. * SIGNAL_TTLIN1
  410. * SIGNAL_TTLIN1
  411. * SIGNAL_TTLIN2
  412. * SIGNAL_TTLIN3
  413. * SIGNAL_TTLIN4
  414. * SIGNAL_INTERNAL_CLOCK_SIG
  415. * SIGNAL_SYS_GENLOCK_OUTPUT
  416. *
  417. * @return xs_error_code_t
  418. */
  419. xs_error_code_t SysClock_setSrc(SignalType_t sig);
  420. xs_error_code_t SysClock_getSrc(SignalType_t &sig);
  421. /**
  422. * @brief 沿
  423. *
  424. * @param edge
  425. * TRIGGER_EDGE_RISING
  426. * TRIGGER_EDGE_FALLING
  427. * @return xs_error_code_t
  428. */
  429. xs_error_code_t SysClock_setTriggerEdge(TriggerEdge_t edge);
  430. xs_error_code_t SysClock_getTriggerEdge(TriggerEdge_t &edge);
  431. /**
  432. * @brief
  433. *
  434. * @param div
  435. * @return xs_error_code_t
  436. */
  437. xs_error_code_t SysClock_setFreqDivision(uint32_t div);
  438. xs_error_code_t SysClock_geFreqtDivision(uint32_t &div);
  439. /**
  440. * @brief
  441. *
  442. * @param muti
  443. * @return xs_error_code_t
  444. */
  445. xs_error_code_t SysClock_setFreqMultiplication(uint32_t muti);
  446. xs_error_code_t SysClock_getFreqMultiplication(uint32_t &muti);
  447. /**
  448. * @brief
  449. *
  450. * @param freq
  451. * @return xs_error_code_t
  452. */
  453. xs_error_code_t SysClock_readFreq(float &freq);
  454. /**
  455. * @brief
  456. *
  457. * @param freq
  458. * @return xs_error_code_t
  459. */
  460. xs_error_code_t SysClock_readInputFreq(float &freq);
  461. private:
  462. xs_error_code_t doaction(uint32_t action, uint32_t actionval, uint32_t *ackreturn, int32_t overtime_ms = 100);
  463. xs_error_code_t storageConfig();
  464. 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);
  465. xs_error_code_t readtimecode(uint32_t reg0, uint32_t reg1, XsyncTimecode_t &timecode);
  466. xs_error_code_t writetimecode(uint32_t reg0, uint32_t reg1, XsyncTimecode_t timecode);
  467. xs_error_code_t readfreq(uint32_t reg, float &freq);
  468. template <typename T>
  469. xs_error_code_t _reg_read(uint32_t regadd, T &regvalue, int32_t overtime_ms = 100) {
  470. uint32_t regvalue_u32;
  471. xs_error_code_t ret = reg_read(regadd, regvalue_u32, overtime_ms);
  472. if (ret == kxs_ec_success) {
  473. regvalue = (T)regvalue_u32;
  474. }
  475. return ret;
  476. }
  477. void parseTimecodeMsgAndReport(XsyncNetAdd &from, uint8_t *data, size_t length);
  478. void parseCameraSyncMsgAndReport(XsyncNetAdd &from, uint8_t *data, size_t length);
  479. };
  480. } // namespace xsync