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.

361 lines
13 KiB

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
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
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. #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. namespace xsync {
  19. using namespace std;
  20. typedef enum {
  21. kxsync_net_state_disconnect,
  22. kxsync_net_state_connecting,
  23. kxsync_net_state_connected,
  24. } xsync_net_state_t;
  25. typedef enum {
  26. TIMECODE_FPS2398 = 0,
  27. TIMECODE_FPS2400 = 1,
  28. TIMECODE_FPS2500 = 2,
  29. TIMECODE_FPS2997 = 3,
  30. TIMECODE_FPS2997Drop = 4,
  31. TIMECODE_FPS3000 = 5,
  32. } TimecodeFormat_t;
  33. typedef enum {
  34. GENLOCK_FPS2397 = 0,
  35. GENLOCK_FPS2398 = 1,
  36. GENLOCK_FPS2400 = 2,
  37. GENLOCK_FPS2500 = 3,
  38. GENLOCK_FPS2997 = 4,
  39. GENLOCK_FPS3000 = 5,
  40. GENLOCK_FPS5000 = 6,
  41. GENLOCK_FPS5994 = 7,
  42. GENLOCK_FPS6000 = 8,
  43. } GenlockFormat_t;
  44. string GenlockFormatToStr(GenlockFormat_t fomrat);
  45. string TimecodeFormatToStr(TimecodeFormat_t fomrat);
  46. GenlockFormat_t Str2GenlockFormat(string format);
  47. TimecodeFormat_t Str2TimecodeFormat(string format);
  48. list<string> GenlockFormatStrSet();
  49. list<string> TimecodeFormatStrSet();
  50. typedef struct {
  51. uint8_t hour;
  52. uint8_t minute;
  53. uint8_t second;
  54. uint8_t frame;
  55. } XsyncTimecode_t;
  56. string XsyncTimecodeToStr(XsyncTimecode_t timecode);
  57. XsyncTimecode_t Str2XsyncTimecode(string timecode);
  58. typedef struct {
  59. uint32_t tc0;
  60. uint32_t tc1;
  61. } Timecode64_t;
  62. typedef struct {
  63. uint32_t frameIndex;
  64. } xysnc_camera_sync_data_t;
  65. typedef function<void(XsyncTimecode_t *timecode_msg)> xsync_on_timecode_msg_t;
  66. typedef function<void(xysnc_camera_sync_data_t *timecode_msg)> xsync_on_camera_sync_msg_t;
  67. /*******************************************************************************
  68. * TTL输出模块相关枚举 *
  69. *******************************************************************************/
  70. namespace ttlout_module {
  71. typedef enum {
  72. tri_logic0 = 0, // 逻辑0
  73. tri_logic1 = 1, // 逻辑1
  74. tri_ttlin1_module_ext = 2, // ttl1输入模块原始信号
  75. tri_ttlin1_module_divide = 3, // ttl1输入模块分频信号
  76. tri_ttlin2_module_ext = 4, // ttl2输入模块原始信号
  77. tri_ttlin2_module_divide = 5, // ttl2输入模块分频信号
  78. tri_ttlin3_module_ext = 6, // ttl3输入模块原始信号
  79. tri_ttlin3_module_divide = 7, // ttl3输入模块分频信号
  80. tri_ttlin4_module_ext = 8, // ttl4输入模块原始信号
  81. tri_ttlin4_module_divide = 9, // ttl4输入模块分频信号
  82. tri_internal_en_flag = 10, // 内部使能状态信号输出
  83. tri_genlock_frame_sync_ext = 11, // 外部genlock帧同步信号
  84. tri_genlock_frame_sync_internal = 12, // 内部genlock帧同步信号
  85. tri_timecode_frame_sync_ext = 13, // 外部timecode帧同步信号
  86. tri_timecode_frame_sync_internal = 14, // 内部timecode帧同步信号
  87. tri_timecode_serial_data_ext = 15, // 外部timecode串行数据输入
  88. tri_timecode_serial_data_internal = 16, // 内部timecode串行数据输入
  89. tri_internal_100hz = 31 // 100hz测试信号
  90. } TriggerSigType_t;
  91. string TriggerSigType2Str(TriggerSigType_t type);
  92. TriggerSigType_t Str2TriggerSigType(string type);
  93. list<string> TriggerSigTypeStrSet();
  94. typedef enum {
  95. OutSigType_logic0 = 0, // 0
  96. OutSigType_logic1 = 1, // 1
  97. OutSigType_test_signal = 2, // 测试信号,信号为ID*1000HZ方波信号
  98. OutSigType_input_signal = 3, // 输入信号
  99. OutSigType_input_signal_mirror = 4, // 翻转后的输入信号
  100. OutSigType_trigger_mode_signal = 5, // 触发模式下的触发信号
  101. OutSigType_trigger_mode_signal_mirror = 6, // 触发模式下的触发信号翻转
  102. } OutputSigType_t;
  103. string OutputSigType2Str(OutputSigType_t type);
  104. OutputSigType_t Str2OutputSigType(string type);
  105. list<string> OutputSigTypeStrSet();
  106. } // namespace ttlout_module
  107. /*******************************************************************************
  108. * *
  109. *******************************************************************************/
  110. namespace sig_generator_module {
  111. typedef enum {
  112. kControlMode_manualTrigger, // 0.手动,启动停止
  113. kControlMode_externalTimecodeTrigger, // 1.外部TIMECODE触发启动
  114. kControlMode_externalTTL1Trigger, // 2.外部TTL输入1_高电平触发,低电平停止
  115. kControlMode_externalTTL2Trigger, // 3.外部TTL输入2_高电平触发,低电平停止
  116. kControlMode_externalTTL3Trigger, // 4.外部TTL输入3_高电平触发,低电平停止
  117. kControlMode_externalTTL4Trigger, // 5.外部TTL输入4_高电平触发,低电平停止
  118. } ControlMode_t;
  119. string ControlMode2Str(ControlMode_t mode);
  120. ControlMode_t Str2ControlMode(string mode);
  121. list<string> ControlModeStrSet();
  122. } // namespace sig_generator_module
  123. class Xsync {
  124. public:
  125. private:
  126. /* data */
  127. I_XSUDPFactory *m_xsync_udp_factory = nullptr;
  128. shared_ptr<I_XSUDP> m_xsync_reg_udp = nullptr;
  129. shared_ptr<I_XSUDP> m_xsync_timecode_udp_listener = nullptr;
  130. shared_ptr<I_XSUDP> m_xsync_camera_sync_udp_listener = nullptr;
  131. string m_xsync_ip;
  132. bool m_is_connected = false;
  133. xsync_on_camera_sync_msg_t m_on_camera_sync_msg_cb = nullptr;
  134. xsync_on_timecode_msg_t m_on_timecode_msg_cb = nullptr;
  135. int txpacket_index = 0;
  136. uint8_t m_xync_cmd_rxdata_cache[2560];
  137. xsync_net_state_t m_net_state = kxsync_net_state_disconnect;
  138. std::recursive_mutex lock_;
  139. Xsync(/* args */);
  140. public:
  141. static Xsync &Ins();
  142. void initialize(I_XSUDPFactory *xsync_udp_factory);
  143. /*******************************************************************************
  144. * *
  145. *******************************************************************************/
  146. bool ping(string xsync_ip);
  147. xs_error_code_t connect(string xsync_ip);
  148. xs_error_code_t disConnect();
  149. xsync_net_state_t getNetState();
  150. /*******************************************************************************
  151. * *
  152. *******************************************************************************/
  153. void Basic_registerOnTimecodeMsgCallback(xsync_on_timecode_msg_t on_timecode_msg_cb);
  154. void Basic_registerOnCameraSyncMsgCallback(xsync_on_camera_sync_msg_t on_camera_sync_msg_cb);
  155. /*******************************************************************************
  156. * *
  157. *******************************************************************************/
  158. xs_error_code_t reg_write(uint32_t regadd, uint32_t regvalue, uint32_t &regbackvalue, int32_t overtime_ms = 100);
  159. xs_error_code_t reg_read(uint32_t regadd, uint32_t &regvalue, int32_t overtime_ms = 100);
  160. xs_error_code_t reg_read_muti(uint32_t regadd, uint32_t nreg, vector<uint32_t> &regvalues, int32_t overtime_ms = 100);
  161. /*******************************************************************************
  162. * *
  163. *******************************************************************************/
  164. xs_error_code_t Basic_generatorNewMac();
  165. xs_error_code_t Basic_factoryReset();
  166. xs_error_code_t Basic_reboot();
  167. xs_error_code_t Basic_changeNetworkConfig(string ip, string mask, string gateway);
  168. xs_error_code_t Basic_clearXsyncCameraSyncIndexCount();
  169. xs_error_code_t Basic_setGenlockFormat(GenlockFormat_t format);
  170. xs_error_code_t Basic_getGenlockFormat(GenlockFormat_t &format);
  171. xs_error_code_t Basic_setTimecodeFormat(TimecodeFormat_t format);
  172. xs_error_code_t Basic_getTimecodeFormat(TimecodeFormat_t &format);
  173. /*******************************************************************************
  174. * *
  175. *******************************************************************************/
  176. /**
  177. * :
  178. * 0.
  179. * 1. TIMECODE触发模式下
  180. */
  181. /**
  182. * @brief
  183. *
  184. * @param mode
  185. * @return xs_error_code_t
  186. */
  187. xs_error_code_t SigGenerator_setControlMode(sig_generator_module::ControlMode_t mode);
  188. /**
  189. * @brief
  190. *
  191. * @param mode
  192. * @return xs_error_code_t
  193. */
  194. xs_error_code_t SigGenerator_getControlMode(sig_generator_module::ControlMode_t &mode);
  195. /**
  196. * @brief
  197. *
  198. * @return xs_error_code_t
  199. */
  200. xs_error_code_t SigGenerator_manualStart();
  201. /**
  202. * @brief
  203. *
  204. * @return xs_error_code_t
  205. */
  206. xs_error_code_t SigGenerator_manualStop();
  207. /**
  208. * @brief
  209. *
  210. * @param timecode
  211. * @return xs_error_code_t
  212. */
  213. xs_error_code_t SigGenerator_setTimecode(XsyncTimecode_t timecode);
  214. /**
  215. * @brief
  216. *
  217. * @param timecode
  218. * @return xs_error_code_t
  219. */
  220. xs_error_code_t SigGenerator_getTimecode(XsyncTimecode_t &timecode);
  221. /**
  222. * @brief
  223. *
  224. * @param timecode
  225. * @return xs_error_code_t
  226. */
  227. xs_error_code_t SigGenerator_setAutoStartTimecode(XsyncTimecode_t timecode);
  228. /**
  229. * @brief
  230. *
  231. * @param timecode
  232. * @return xs_error_code_t
  233. */
  234. xs_error_code_t SigGenerator_getAutoStartTimecode(XsyncTimecode_t &timecode);
  235. /**
  236. * @brief
  237. *
  238. * @param work_state
  239. * @return xs_error_code_t
  240. */
  241. xs_error_code_t SigGenerator_getWorkState(uint32_t &work_state);
  242. /**
  243. * @brief genlock格式
  244. *
  245. * @param format
  246. * @return xs_error_code_t
  247. */
  248. xs_error_code_t SigGenerator_setGenlockFormat(GenlockFormat_t format); // 不建议使用这个方法,用Basic_setGenlockFormat
  249. xs_error_code_t SigGenerator_getGenlockFormat(GenlockFormat_t &format); // 不建议使用这个方法,用Basic_getGenlockFormat
  250. /**
  251. * @brief timecode格式
  252. *
  253. * @param format
  254. * @return xs_error_code_t
  255. */
  256. xs_error_code_t SigGenerator_setTimecodeFormat(TimecodeFormat_t format); // 不建议使用这个方法,用Basic_setTimecodeFormat
  257. xs_error_code_t SigGenerator_getTimecodeFormat(TimecodeFormat_t &format); // 不建议使用这个方法,用Basic_getTimecodeFormat
  258. public:
  259. /*******************************************************************************
  260. * TTL输出模块控制 *
  261. *******************************************************************************/
  262. /**
  263. * :
  264. * 1.
  265. * 2.
  266. * 3.
  267. * 4.
  268. *
  269. */
  270. /**
  271. * @brief TTL输出模块的输入信号
  272. *
  273. * @param index
  274. * @param source
  275. * @return xs_error_code_t
  276. */
  277. xs_error_code_t TTLOutputModule_setInputSigType(int32_t index, ttlout_module::TriggerSigType_t source);
  278. xs_error_code_t TTLOutputModule_getInputSigType(int32_t index, ttlout_module::TriggerSigType_t &source);
  279. /**
  280. * @brief TTL输出模块的输出信号
  281. *
  282. * @param index
  283. * @param output_type
  284. * @return xs_error_code_t
  285. */
  286. xs_error_code_t TTLOutputModule_setOutputSigType(int32_t index, ttlout_module::OutputSigType_t output_type);
  287. xs_error_code_t TTLOutputModule_getOutputSigType(int32_t index, ttlout_module::OutputSigType_t &output_type);
  288. /**
  289. * @brief
  290. *
  291. * @param index
  292. * @param pulse_width_ms
  293. * @return xs_error_code_t
  294. */
  295. xs_error_code_t TTLOutputModule_setTriggerModePulseWidth(int32_t index, uint32_t pulse_width_ms);
  296. xs_error_code_t TTLOutputModule_getTriggerModePulseWidth(int32_t index, uint32_t &pulse_width_ms);
  297. /**
  298. * @brief
  299. *
  300. * @param index
  301. * @param pulse_delay_ms
  302. * @return xs_error_code_t
  303. */
  304. xs_error_code_t TTLOutputModule_setTriggerModePulseDelay(int32_t index, uint32_t pulse_delay_ms);
  305. xs_error_code_t TTLOutputModule_getTriggerModePulseDelay(int32_t index, uint32_t &pulse_delay_ms);
  306. private:
  307. xs_error_code_t doaction(uint32_t action, uint32_t actionval, uint32_t *ackreturn, int32_t overtime_ms = 100);
  308. xs_error_code_t storageConfig();
  309. 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);
  310. void parseTimecodeMsgAndReport(XsyncNetAdd &from, uint8_t *data, size_t length);
  311. void parseCameraSyncMsgAndReport(XsyncNetAdd &from, uint8_t *data, size_t length);
  312. };
  313. } // namespace xsync