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.

961 lines
46 KiB

1 year ago
2 years ago
2 years ago
2 years 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
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
1 year ago
1 year 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
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
2 years ago
2 years ago
2 years 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
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
1 year 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
2 years ago
1 year 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
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
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
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. #include "xsync_v2.hpp"
  2. #include <string.h>
  3. #include <map>
  4. #include "xsync_errcode.hpp"
  5. #include "xsync_regs.hpp"
  6. #include "xsync_utils.hpp"
  7. #include "xsync_v2_sig_type.hpp"
  8. #define TAG "XSYNC"
  9. using namespace xsync;
  10. using namespace std;
  11. /**
  12. * @brief XSYNC协议端口
  13. */
  14. #define IFLYTOP_XSYNC_SERVICE_XSYNC_PORT 19900 // xsync端端口
  15. #define IFLYTOP_XSYNC_SERVICE_PC_PORT 19901 // pc 端端口
  16. #define IFLYTOP_XSYNC_TIMECODE_REPORT_XSYNC_PORT 19902 // xsync端端口
  17. #define IFLYTOP_XSYNC_TIMECODE_REPORT_PC_PORT 19903 // pc端端口
  18. #define IFLYTOP_XSYNC_CAMERA_SYNC_PACKET_XSYNC_PORT 13013 // xsync端端口
  19. #define IFLYTOP_XSYNC_CAMERA_SYNC_PACKET_PC_PORT 13014 // pc端端口
  20. #define DO_XSYNC(exptr) \
  21. { \
  22. xs_error_code_t ecode = exptr; \
  23. if (ecode != kxs_ec_success) return ecode; \
  24. }
  25. #define REG_WRITE(reg, value) \
  26. { \
  27. DO_XSYNC(reg_write(reg, value, 10)); \
  28. return kxs_ec_success; \
  29. }
  30. #define REG_READ(reg, value) \
  31. { \
  32. uint32_t readbak = 0; \
  33. DO_XSYNC(reg_read(reg, readbak, 10)); \
  34. value = (decltype(value))readbak; \
  35. return kxs_ec_success; \
  36. }
  37. class Xsync : public IXsync {
  38. private:
  39. /* data */
  40. I_XSUDPFactory *m_xsync_udp_factory = nullptr;
  41. shared_ptr<I_XSUDP> m_xsync_reg_udp = nullptr;
  42. shared_ptr<I_XSUDP> m_xsync_timecode_udp_listener = nullptr;
  43. shared_ptr<I_XSUDP> m_xsync_camera_sync_udp_listener = nullptr;
  44. string m_xsync_ip;
  45. bool m_is_connected = false;
  46. xsync_on_camera_sync_msg_t m_on_camera_sync_msg_cb = nullptr;
  47. xsync_on_timecode_msg_t m_on_timecode_msg_cb = nullptr;
  48. xsync_on_record_sig_change_msg_t m_on_record_sig_change_msg_cb = nullptr;
  49. int txpacket_index = 0;
  50. uint8_t m_xync_cmd_rxdata_cache[2560];
  51. std::recursive_mutex lock_;
  52. public:
  53. static Xsync &Ins();
  54. Xsync(/* args */);
  55. virtual ~Xsync() {}
  56. void initialize(I_XSUDPFactory *xsync_udp_factory);
  57. xs_error_code_t connect(string xsync_ip);
  58. xs_error_code_t disConnect();
  59. virtual xs_error_code_t changeXsyncIp(string xsync_ip) override;
  60. virtual bool ping() override;
  61. virtual void registerOnTimecodeMsgCallback(xsync_on_timecode_msg_t cb) override;
  62. virtual void registerOnCameraSyncMsgCallback(xsync_on_camera_sync_msg_t cb) override;
  63. virtual void registerOnRecordSigChangeMsgCallback(xsync_on_record_sig_change_msg_t cb) override;
  64. virtual xs_error_code_t generatorNewMac() override;
  65. virtual xs_error_code_t factoryReset() override;
  66. virtual xs_error_code_t reboot() override;
  67. virtual xs_error_code_t changeNetworkConfig(string ip, string mask, string gateway) override;
  68. virtual xs_error_code_t readSn(string &sn) override;
  69. virtual xs_error_code_t readMac(string &mac) override;
  70. public:
  71. virtual xs_error_code_t reg_write(uint32_t regadd, uint32_t regvalue, uint32_t &regbackvalue, int32_t overtime_ms = 100) override;
  72. virtual xs_error_code_t reg_read(uint32_t regadd, uint32_t &regvalue, int32_t overtime_ms = 100) override;
  73. public:
  74. virtual xs_error_code_t ExternalTimecode_setSource(InputInterface_t src) override;
  75. virtual xs_error_code_t ExternalTimecode_getSource(InputInterface_t &timecode_select) override;
  76. virtual xs_error_code_t ExternalTimecode_setFormat(TimecodeFormat_t format) override;
  77. virtual xs_error_code_t ExternalTimecode_getFormat(TimecodeFormat_t &format) override;
  78. virtual xs_error_code_t ExternalTimecode_readCode(XsyncTimecode_t &timecode) override;
  79. virtual xs_error_code_t TTLInputModule1_detectFreq(float &freq) override;
  80. virtual xs_error_code_t TTLInputModule2_detectFreq(float &freq) override;
  81. virtual xs_error_code_t TTLInputModule3_detectFreq(float &freq) override;
  82. virtual xs_error_code_t TTLInputModule4_detectFreq(float &freq) override;
  83. virtual xs_error_code_t ExternalGenlock_detectFreq(float &freq) override;
  84. virtual xs_error_code_t InternalTimecode_setFormat(TimecodeFormat_t format) override;
  85. virtual xs_error_code_t InternalTimecode_getFormat(TimecodeFormat_t &format) override;
  86. virtual xs_error_code_t InternalTimecode_setCode(XsyncTimecode_t timecode) override;
  87. virtual xs_error_code_t InternalTimecode_getCode(XsyncTimecode_t &timecode) override;
  88. virtual xs_error_code_t InternalGenlock_setFormat(GenlockFormat_t format) override;
  89. virtual xs_error_code_t InternalGenlock_getFormat(GenlockFormat_t &format) override;
  90. virtual xs_error_code_t InternalClock_setFreq(float freq) override;
  91. virtual xs_error_code_t InternalClock_getFreq(float &freq) override;
  92. virtual xs_error_code_t SysTimecode_setSource(uint32_t sig) override;
  93. virtual xs_error_code_t SysTimecode_getSource(uint32_t &sig) override;
  94. virtual xs_error_code_t SysTimecode_readFormat(TimecodeFormat_t &format) override;
  95. virtual xs_error_code_t SysTimecode_readCode(XsyncTimecode_t &timecode) override;
  96. virtual xs_error_code_t SysGenlock_setSrc(uint32_t source) override;
  97. virtual xs_error_code_t SysGenlock_getSrc(uint32_t &source) override;
  98. virtual xs_error_code_t SysGenlock_readFreq(float &freq) override;
  99. virtual xs_error_code_t SysClock_setSrc(SignalType_t sig) override;
  100. virtual xs_error_code_t SysClock_getSrc(SignalType_t &sig) override;
  101. virtual xs_error_code_t SysClock_setTriggerEdge(TriggerEdge_t edge) override;
  102. virtual xs_error_code_t SysClock_getTriggerEdge(TriggerEdge_t &edge) override;
  103. virtual xs_error_code_t SysClock_setFreqDivision(uint32_t div) override;
  104. virtual xs_error_code_t SysClock_geFreqtDivision(uint32_t &div) override;
  105. virtual xs_error_code_t SysClock_setFreqMultiplication(uint32_t muti) override;
  106. virtual xs_error_code_t SysClock_getFreqMultiplication(uint32_t &muti) override;
  107. virtual xs_error_code_t SysClock_readOutSigFreq(float &freq) override;
  108. virtual xs_error_code_t SysClock_readInSigFreq(float &freq) override;
  109. virtual xs_error_code_t RecordSigGenerator_setContrlMode(ControlMode_t mode) override;
  110. virtual xs_error_code_t RecordSigGenerator_getContrlMode(ControlMode_t &mode) override;
  111. virtual xs_error_code_t RecordSigGenerator_manualStart() override;
  112. virtual xs_error_code_t RecordSigGenerator_manualStop() override;
  113. virtual xs_error_code_t RecordSigGenerator_setAutoStartTimecode(XsyncTimecode_t timecode) override;
  114. virtual xs_error_code_t RecordSigGenerator_setAutoStopTimecode(XsyncTimecode_t timecode) override;
  115. virtual xs_error_code_t RecordSigGenerator_getAutoStartTimecode(XsyncTimecode_t &timecode) override;
  116. virtual xs_error_code_t RecordSigGenerator_getAutoStopTimecode(XsyncTimecode_t &timecode) override;
  117. virtual xs_error_code_t RecordSigGenerator_setTimecodeCtrlFlag(uint32_t autoStart, uint32_t autoStop) override;
  118. virtual xs_error_code_t RecordSigGenerator_getTimecodeCtrlFlag(uint32_t &autoStart, uint32_t &autoStop) override;
  119. virtual xs_error_code_t RecordSigGenerator_setExternalTTLTriggerSrc(InputInterface_t ttlPortNum); // 1- override4
  120. virtual xs_error_code_t RecordSigGenerator_getExternalTTLTriggerSrc(InputInterface_t &ttlPortNum) override;
  121. virtual xs_error_code_t RecordSigGenerator_setExternalTTLTriggerPolarity(uint32_t polarity) override;
  122. virtual xs_error_code_t RecordSigGenerator_getExternalTTLTriggerPolarity(uint32_t &polarity) override;
  123. virtual xs_error_code_t RecordSigGenerator_setRecordExposureTime(uint32_t us) override;
  124. virtual xs_error_code_t RecordSigGenerator_getRecordExposureTime(uint32_t &us) override;
  125. virtual xs_error_code_t RecordSigGenerator_setRecordExposureOffsetTime(uint32_t us) override;
  126. virtual xs_error_code_t RecordSigGenerator_getRecordExposureOffsetTime(uint32_t &us) override;
  127. virtual xs_error_code_t RecordSigGenerator_getRecordState(uint32_t &state) override;
  128. virtual xs_error_code_t RecordSigGenerator_readTimecodeSnapshot(XsyncTimecode_t &timecode) override;
  129. xs_error_code_t TimecodeOutputModule_setBncOutputLevel(int level); // 0:line,1:mi overridec
  130. xs_error_code_t TimecodeOutputModule_getBncOutputLevel(int &level);
  131. xs_error_code_t TimecodeOutputModule_setHeadphoneOutputLevel(int level); // 0:line,1:mi overridec
  132. xs_error_code_t TimecodeOutputModule_getHeadphoneOutputLevel(int &level);
  133. virtual xs_error_code_t TTLOutputModule1_setSrcSigType(SignalType_t source) override;
  134. virtual xs_error_code_t TTLOutputModule1_getSrcSigType(SignalType_t &source) override;
  135. virtual xs_error_code_t TTLOutputModule1_setFreqDivision(uint32_t div) override;
  136. virtual xs_error_code_t TTLOutputModule1_getFreqDivision(uint32_t &div) override;
  137. virtual xs_error_code_t TTLOutputModule1_setFreqMultiplication(uint32_t multi) override;
  138. virtual xs_error_code_t TTLOutputModule1_getFreqMultiplication(uint32_t &multi) override;
  139. virtual xs_error_code_t TTLOutputModule1_readInFreq(float &freq) override;
  140. virtual xs_error_code_t TTLOutputModule1_readOutFreq(float &freq) override;
  141. virtual xs_error_code_t TTLOutputModule2_setSrcSigType(SignalType_t source) override;
  142. virtual xs_error_code_t TTLOutputModule2_getSrcSigType(SignalType_t &source) override;
  143. virtual xs_error_code_t TTLOutputModule2_setFreqDivision(uint32_t div) override;
  144. virtual xs_error_code_t TTLOutputModule2_getFreqDivision(uint32_t &div) override;
  145. virtual xs_error_code_t TTLOutputModule2_setFreqMultiplication(uint32_t multi) override;
  146. virtual xs_error_code_t TTLOutputModule2_getFreqMultiplication(uint32_t &multi) override;
  147. virtual xs_error_code_t TTLOutputModule2_readInFreq(float &freq) override;
  148. virtual xs_error_code_t TTLOutputModule2_readOutFreq(float &freq) override;
  149. virtual xs_error_code_t TTLOutputModule3_setSrcSigType(SignalType_t source) override;
  150. virtual xs_error_code_t TTLOutputModule3_getSrcSigType(SignalType_t &source) override;
  151. virtual xs_error_code_t TTLOutputModule3_setFreqDivision(uint32_t div) override;
  152. virtual xs_error_code_t TTLOutputModule3_getFreqDivision(uint32_t &div) override;
  153. virtual xs_error_code_t TTLOutputModule3_setFreqMultiplication(uint32_t multi) override;
  154. virtual xs_error_code_t TTLOutputModule3_getFreqMultiplication(uint32_t &multi) override;
  155. virtual xs_error_code_t TTLOutputModule3_readInFreq(float &freq) override;
  156. virtual xs_error_code_t TTLOutputModule3_readOutFreq(float &freq) override;
  157. virtual xs_error_code_t TTLOutputModule4_setSrcSigType(SignalType_t source) override;
  158. virtual xs_error_code_t TTLOutputModule4_getSrcSigType(SignalType_t &source) override;
  159. virtual xs_error_code_t TTLOutputModule4_setFreqDivision(uint32_t div) override;
  160. virtual xs_error_code_t TTLOutputModule4_getFreqDivision(uint32_t &div) override;
  161. virtual xs_error_code_t TTLOutputModule4_setFreqMultiplication(uint32_t multi) override;
  162. virtual xs_error_code_t TTLOutputModule4_getFreqMultiplication(uint32_t &multi) override;
  163. virtual xs_error_code_t TTLOutputModule4_readInFreq(float &freq) override;
  164. virtual xs_error_code_t TTLOutputModule4_readOutFreq(float &freq) override;
  165. private:
  166. xs_error_code_t doaction(uint32_t action, uint32_t actionval, uint32_t *ackreturn, int32_t overtime_ms = 100);
  167. xs_error_code_t storageConfig();
  168. 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);
  169. xs_error_code_t readtimecode(uint32_t reg0, uint32_t reg1, XsyncTimecode_t &timecode);
  170. xs_error_code_t writetimecode(uint32_t reg0, uint32_t reg1, XsyncTimecode_t timecode);
  171. xs_error_code_t readfreq(uint32_t reg, float &freq);
  172. xs_error_code_t reg_write(uint32_t regadd, uint32_t regvalue, int32_t overtime_ms = 100);
  173. xs_error_code_t reg_read_muti(uint32_t regadd, uint32_t nreg, vector<uint32_t> &regvalues, int32_t overtime_ms = 100);
  174. template <typename T>
  175. xs_error_code_t _reg_read(uint32_t regadd, T &regvalue, int32_t overtime_ms = 100) {
  176. uint32_t regvalue_u32;
  177. xs_error_code_t ret = reg_read(regadd, regvalue_u32, overtime_ms);
  178. if (ret == kxs_ec_success) {
  179. regvalue = (T)regvalue_u32;
  180. }
  181. return ret;
  182. }
  183. void parseTimecodeMsgAndReport(XsyncNetAdd &from, uint8_t *data, size_t length);
  184. void parseCameraSyncMsgAndReport(XsyncNetAdd &from, uint8_t *data, size_t length);
  185. };
  186. /*******************************************************************************
  187. * Xsync *
  188. *******************************************************************************/
  189. Xsync::Xsync(/* args */) {}
  190. Xsync &Xsync::Ins() {
  191. static Xsync xsync;
  192. return xsync;
  193. }
  194. void Xsync::initialize(I_XSUDPFactory *xsync_udp_factory) { m_xsync_udp_factory = xsync_udp_factory; }
  195. xs_error_code_t Xsync::changeXsyncIp(string xsync_ip) {
  196. disConnect();
  197. return connect(xsync_ip);
  198. }
  199. bool Xsync::ping() {
  200. uint32_t readbak;
  201. xs_error_code_t ecode = reg_read(reg::kproduct_type_id, readbak, 10);
  202. if (ecode != kxs_ec_success) {
  203. return false;
  204. }
  205. return true;
  206. }
  207. xs_error_code_t Xsync::connect(string xsync_ip) {
  208. lock_guard<recursive_mutex> lock(lock_);
  209. m_xsync_ip = xsync_ip;
  210. disConnect();
  211. /**
  212. * @brief m_xsync_reg_udp
  213. */
  214. xs_error_code_t ecode = kxs_ec_success;
  215. auto xsync_reg_udp = m_xsync_udp_factory->createXSUDP();
  216. ecode = xsync_reg_udp->initialize("0.0.0.0", IFLYTOP_XSYNC_SERVICE_PC_PORT);
  217. if (ecode != kxs_ec_success) {
  218. return ecode;
  219. }
  220. /**
  221. * @brief m_xsync_timecode_udp_listener
  222. */
  223. auto xsync_timecode_udp_listener = m_xsync_udp_factory->createXSUDP();
  224. ecode = xsync_timecode_udp_listener->initialize("0.0.0.0", IFLYTOP_XSYNC_TIMECODE_REPORT_PC_PORT);
  225. if (ecode != kxs_ec_success) {
  226. return ecode;
  227. }
  228. ecode = xsync_timecode_udp_listener->startReceive([this](XsyncNetAdd &from, uint8_t *data, size_t length) { parseTimecodeMsgAndReport(from, data, length); });
  229. if (ecode != kxs_ec_success) {
  230. return ecode;
  231. }
  232. /**
  233. * @brief m_xsync_camera_sync_udp_listener
  234. */
  235. auto xsync_camera_sync_udp_listener = m_xsync_udp_factory->createXSUDP();
  236. ecode = xsync_camera_sync_udp_listener->initialize("0.0.0.0", IFLYTOP_XSYNC_CAMERA_SYNC_PACKET_PC_PORT);
  237. if (ecode != kxs_ec_success) {
  238. return ecode;
  239. }
  240. ecode = xsync_camera_sync_udp_listener->startReceive([this](XsyncNetAdd &from, uint8_t *data, size_t length) { parseCameraSyncMsgAndReport(from, data, length); });
  241. if (ecode != kxs_ec_success) {
  242. return ecode;
  243. }
  244. m_xsync_reg_udp = xsync_reg_udp;
  245. m_xsync_timecode_udp_listener = xsync_timecode_udp_listener;
  246. m_xsync_camera_sync_udp_listener = xsync_camera_sync_udp_listener;
  247. return ecode;
  248. }
  249. xs_error_code_t Xsync::disConnect() {
  250. lock_guard<recursive_mutex> lock(lock_);
  251. if (m_xsync_reg_udp != nullptr) {
  252. m_xsync_reg_udp->stopReceive();
  253. m_xsync_reg_udp = nullptr;
  254. }
  255. if (m_xsync_timecode_udp_listener != nullptr) {
  256. m_xsync_timecode_udp_listener->stopReceive();
  257. m_xsync_timecode_udp_listener = nullptr;
  258. }
  259. if (m_xsync_camera_sync_udp_listener != nullptr) {
  260. m_xsync_camera_sync_udp_listener->stopReceive();
  261. m_xsync_camera_sync_udp_listener = nullptr;
  262. }
  263. return kxs_ec_success;
  264. }
  265. void Xsync::registerOnTimecodeMsgCallback(xsync_on_timecode_msg_t cb) { m_on_timecode_msg_cb = cb; }
  266. void Xsync::registerOnCameraSyncMsgCallback(xsync_on_camera_sync_msg_t cb) { m_on_camera_sync_msg_cb = cb; }
  267. void Xsync::registerOnRecordSigChangeMsgCallback(xsync_on_record_sig_change_msg_t cb) { m_on_record_sig_change_msg_cb = cb; }
  268. xs_error_code_t Xsync::readSn(string &sn) {
  269. sn = "X1001000000000";
  270. return kxs_ec_success;
  271. }
  272. xs_error_code_t Xsync::readMac(string &mac) {
  273. // mac = "";
  274. uint32_t mac0;
  275. uint32_t mac1;
  276. DO_XSYNC(reg_read(reg::kmac0, mac0));
  277. DO_XSYNC(reg_read(reg::kmac1, mac1));
  278. char buf[128] = {0};
  279. sprintf(buf, "%02x-%02x-%02x-%02x-%02x-%02x", (mac0 >> 0) & 0xff, (mac0 >> 8) & 0xff, (mac0 >> 16) & 0xff, (mac0 >> 24) & 0xff, (mac1 >> 0) & 0xff, (mac1 >> 8) & 0xff);
  280. mac = buf;
  281. return kxs_ec_success;
  282. }
  283. xs_error_code_t Xsync::xsync_send_cmd_block(iflytop_xsync_packet_header_t *cmd, iflytop_xsync_packet_header_t *rx_data, int32_t buffersize, int32_t overtime_ms) {
  284. lock_guard<recursive_mutex> lock(lock_);
  285. if (!m_xsync_reg_udp) return kxs_ec_lose_connect;
  286. m_xsync_reg_udp->clearRxBuffer();
  287. cmd->index = txpacket_index++;
  288. XsyncNetAdd toadd = {m_xsync_ip, IFLYTOP_XSYNC_SERVICE_XSYNC_PORT};
  289. xs_error_code_t ecode = //
  290. m_xsync_reg_udp->sendto(toadd, (const char *)cmd, sizeof(iflytop_xsync_packet_header_t) + cmd->ndata * 4, nullptr);
  291. if (ecode != kxs_ec_success) {
  292. return ecode;
  293. }
  294. XsyncNetAdd fromadd;
  295. while (true) {
  296. ecode = m_xsync_reg_udp->receive((char *)rx_data, buffersize, fromadd, overtime_ms);
  297. if (ecode != kxs_ec_success) {
  298. return ecode;
  299. }
  300. if (rx_data->index != cmd->index) {
  301. continue;
  302. }
  303. break;
  304. }
  305. return (xs_error_code_t)rx_data->data[0];
  306. }
  307. xs_error_code_t Xsync::reg_write(uint32_t regadd, uint32_t regvalue, int32_t overtime_ms) { //
  308. uint32_t readbak = 0;
  309. return reg_write(regadd, regvalue, readbak, overtime_ms);
  310. }
  311. xs_error_code_t Xsync::reg_write(uint32_t regadd, uint32_t regvalue, uint32_t &regbackvalue, int32_t overtime_ms) {
  312. /**
  313. * @brief
  314. *
  315. *
  316. * kxsync_packet_type_reg_write
  317. * tx: regadd,regdata
  318. * rx: ecode,regdata
  319. */
  320. uint8_t txdata[128] = {0};
  321. uint8_t rxdata[128] = {0};
  322. iflytop_xsync_packet_header_t *txpacket = (iflytop_xsync_packet_header_t *)txdata;
  323. iflytop_xsync_packet_header_t *rxpacket = (iflytop_xsync_packet_header_t *)rxdata;
  324. txpacket->type = kxsync_packet_type_cmd;
  325. txpacket->index = txpacket_index++;
  326. txpacket->cmd = kxsync_packet_type_reg_write;
  327. txpacket->ndata = 2;
  328. txpacket->data[0] = regadd;
  329. txpacket->data[1] = regvalue;
  330. auto ecode = xsync_send_cmd_block(txpacket, rxpacket, sizeof(rxdata), overtime_ms);
  331. if (ecode != kxs_ec_success) {
  332. return ecode;
  333. }
  334. regbackvalue = rxpacket->data[1];
  335. return ecode;
  336. }
  337. xs_error_code_t Xsync::reg_read(uint32_t regadd, uint32_t &regvalue, int32_t overtime_ms) {
  338. /**
  339. * @brief
  340. *
  341. *
  342. * kxsync_packet_type_reg_write
  343. * tx: regadd,regdata
  344. * rx: ecode,regdata
  345. */
  346. uint8_t txdata[128] = {0};
  347. uint8_t rxdata[128] = {0};
  348. iflytop_xsync_packet_header_t *txpacket = (iflytop_xsync_packet_header_t *)txdata;
  349. iflytop_xsync_packet_header_t *rxpacket = (iflytop_xsync_packet_header_t *)rxdata;
  350. txpacket->type = kxsync_packet_type_cmd;
  351. txpacket->index = txpacket_index++;
  352. txpacket->cmd = kxsync_packet_type_reg_read;
  353. txpacket->ndata = 2;
  354. txpacket->data[0] = regadd;
  355. txpacket->data[1] = regvalue;
  356. auto ecode = xsync_send_cmd_block(txpacket, rxpacket, sizeof(rxdata), overtime_ms);
  357. if (ecode != kxs_ec_success) {
  358. return ecode;
  359. }
  360. regvalue = rxpacket->data[1];
  361. return ecode;
  362. }
  363. xs_error_code_t Xsync::reg_read_muti(uint32_t regadd, uint32_t nreg, vector<uint32_t> &regvalues, int32_t overtime_ms) {
  364. /**
  365. * @brief
  366. *
  367. *
  368. * kxsync_packet_type_reg_read_regs
  369. * tx: regstartadd,nreg
  370. * rx: ecode,regdatas
  371. */
  372. uint8_t txdata[128] = {0};
  373. uint8_t rxdata[1280] = {0};
  374. iflytop_xsync_packet_header_t *txpacket = (iflytop_xsync_packet_header_t *)txdata;
  375. iflytop_xsync_packet_header_t *rxpacket = (iflytop_xsync_packet_header_t *)rxdata;
  376. txpacket->type = kxsync_packet_type_cmd;
  377. txpacket->index = txpacket_index++;
  378. txpacket->cmd = kxsync_packet_type_reg_read_regs;
  379. txpacket->ndata = 2;
  380. txpacket->data[0] = regadd;
  381. txpacket->data[1] = nreg;
  382. auto ecode = xsync_send_cmd_block(txpacket, rxpacket, sizeof(rxdata), overtime_ms);
  383. if (ecode != kxs_ec_success) {
  384. return ecode;
  385. }
  386. if (rxpacket->ndata > 0) {
  387. for (int i = 0; i < rxpacket->ndata - 1; i++) {
  388. regvalues.push_back(rxpacket->data[i + 1]);
  389. }
  390. }
  391. return ecode;
  392. }
  393. xs_error_code_t Xsync::readtimecode(uint32_t reg0, uint32_t reg1, XsyncTimecode_t &timecode) {
  394. uint32_t readbak = 0;
  395. xs_error_code_t ecode = kxs_ec_success;
  396. uint32_t tc0 = 0;
  397. uint32_t tc1 = 0;
  398. ecode = reg_read(reg0, tc0, 10);
  399. if (ecode != kxs_ec_success) return ecode;
  400. ecode = reg_read(reg1, tc1, 10);
  401. if (ecode != kxs_ec_success) return ecode;
  402. Timecode64_t tc64;
  403. tc64.tc0 = tc0;
  404. tc64.tc1 = tc1;
  405. timecode = timecode64ToXsyncTimeCode(tc64);
  406. return ecode;
  407. }
  408. xs_error_code_t Xsync::writetimecode(uint32_t reg0, uint32_t reg1, XsyncTimecode_t timecode) {
  409. uint32_t readbak = 0;
  410. xs_error_code_t ecode = kxs_ec_success;
  411. Timecode64_t tc64 = timecodeTo64(timecode);
  412. ecode = reg_write(reg0, tc64.tc0, readbak, 10);
  413. if (ecode != kxs_ec_success) return ecode;
  414. ecode = reg_write(reg1, tc64.tc1, readbak, 10);
  415. if (ecode != kxs_ec_success) return ecode;
  416. return ecode;
  417. }
  418. xs_error_code_t Xsync::readfreq(uint32_t reg, float &freqfloat) {
  419. uint32_t freq_cnt = 0;
  420. DO_XSYNC(reg_read(reg, freq_cnt));
  421. if (freq_cnt == 0) {
  422. freqfloat = 0;
  423. }
  424. if (freq_cnt != 0) {
  425. uint32_t freq_1000x = ((1.0 / (freq_cnt * 1.0 / (10 * 1000 * 1000))) * 1000 + 0.5); //+0.5是因为c++ 小数强转成整数时是取整,而非四舍五入
  426. // ZLOGI(TAG, "freq_10x %f", freq_10x);
  427. freqfloat = freq_1000x / 1000.0;
  428. } else {
  429. freqfloat = 0;
  430. }
  431. return kxs_ec_success;
  432. }
  433. void Xsync::parseTimecodeMsgAndReport(XsyncNetAdd &from, uint8_t *data, size_t length) {
  434. iflytop_xsync_event_report_packet_t *packet = (iflytop_xsync_event_report_packet_t *)data;
  435. if (packet->eventid == ktimecode_report_event) {
  436. Timecode64_t tc64;
  437. tc64.tc0 = packet->data[0];
  438. tc64.tc1 = packet->data[1];
  439. XsyncTimecode_t timecode = timecode64ToXsyncTimeCode(tc64);
  440. if (m_on_timecode_msg_cb) m_on_timecode_msg_cb(&timecode);
  441. } else if (packet->eventid == kxsync_work_state_report_event) {
  442. // 信号发生器状态改变
  443. Timecode64_t tc64;
  444. tc64.tc0 = packet->data[1];
  445. tc64.tc1 = packet->data[2];
  446. XsyncTimecode_t timecode = timecode64ToXsyncTimeCode(tc64);
  447. if (m_on_record_sig_change_msg_cb) m_on_record_sig_change_msg_cb(packet->data[0], &timecode);
  448. }
  449. }
  450. void Xsync::parseCameraSyncMsgAndReport(XsyncNetAdd &from, uint8_t *data, size_t length) {
  451. uint32_t count = 0;
  452. uint32_t data0 = data[7];
  453. uint32_t data1 = data[6];
  454. uint32_t data2 = data[5];
  455. uint32_t data3 = data[4];
  456. count = data0 + (data1 << 8) + (data2 << 16) + (data3 << 24);
  457. xysnc_camera_sync_data_t camera_sync_data;
  458. camera_sync_data.frameIndex = count;
  459. if (m_on_camera_sync_msg_cb) m_on_camera_sync_msg_cb(&camera_sync_data);
  460. }
  461. xs_error_code_t Xsync::generatorNewMac() { return doaction(xsync_stm32_action_generator_new_mac, 0, nullptr, 2000); }
  462. xs_error_code_t Xsync::factoryReset() { return doaction(xsync_stm32_action_factory_reset, 0, nullptr, 1000); }
  463. xs_error_code_t Xsync::reboot() { return doaction(xsync_stm32_action_reboot, 0, nullptr); }
  464. xs_error_code_t Xsync::storageConfig() { return doaction(xsync_stm32_action_storage_cfg, 0, nullptr, 1000); }
  465. xs_error_code_t Xsync::changeNetworkConfig(string ip, string mask, string gateway) {
  466. uint32_t ip32 = 0;
  467. uint32_t mask32 = 0;
  468. uint32_t gateway32 = 0;
  469. xs_error_code_t ecode;
  470. bool suc = false;
  471. ip32 = (uint32_t)ipToUint32(ip.c_str(), suc);
  472. if (!suc) return kxs_ec_param_error;
  473. mask32 = (uint32_t)ipToUint32(mask.c_str(), suc);
  474. if (!suc) return kxs_ec_param_error;
  475. gateway32 = (uint32_t)ipToUint32(gateway.c_str(), suc);
  476. if (!suc) return kxs_ec_param_error;
  477. uint32_t readbak = 0;
  478. ecode = reg_write(reg::kstm32_ip, ip32, readbak);
  479. if (ecode != kxs_ec_success) return ecode;
  480. ecode = reg_write(reg::kstm32_netmask, mask32, readbak);
  481. if (ecode != kxs_ec_success) return ecode;
  482. ecode = reg_write(reg::kstm32_gw, gateway32, readbak);
  483. if (ecode != kxs_ec_success) return ecode;
  484. ecode = storageConfig();
  485. if (ecode != kxs_ec_success) return ecode;
  486. return kxs_ec_success;
  487. }
  488. xs_error_code_t Xsync::doaction(uint32_t action, uint32_t actionval, uint32_t *ackreturn, int32_t overtime_ms) {
  489. //
  490. uint32_t readbak = 0;
  491. xs_error_code_t ecode;
  492. ecode = reg_write(reg::kstm32_action_val0, actionval, readbak);
  493. if (ecode != kxs_ec_success) return ecode;
  494. ecode = reg_write(reg::kstm32_action0, action, readbak, overtime_ms);
  495. if (ecode != kxs_ec_success) return ecode;
  496. if (ackreturn) *ackreturn = readbak;
  497. return ecode;
  498. }
  499. /*******************************************************************************
  500. * TTLInputModule *
  501. *******************************************************************************/
  502. #define FREQ_CNT_TO_FREQ(cnt) ((cnt != 0) ? (uint32_t)(1.0 / (cnt * 1.0 / (10 * 1000 * 1000)) + 0.5) : 0) //+0.5是因为c++ 小数强转成整数时是取整,而非四舍五入
  503. xs_error_code_t Xsync::TTLInputModule1_detectFreq(float &freq) { return readfreq(reg::k_ttlin1_freq_detector_reg, freq); }
  504. xs_error_code_t Xsync::TTLInputModule2_detectFreq(float &freq) { return readfreq(reg::k_ttlin2_freq_detector_reg, freq); }
  505. xs_error_code_t Xsync::TTLInputModule3_detectFreq(float &freq) { return readfreq(reg::k_ttlin3_freq_detector_reg, freq); }
  506. xs_error_code_t Xsync::TTLInputModule4_detectFreq(float &freq) { return readfreq(reg::k_ttlin4_freq_detector_reg, freq); }
  507. /*******************************************************************************
  508. * TTLOutputModule *
  509. *******************************************************************************/
  510. // 0:固定输出低电平,1:固定输出高电平,2:分频倍频模式,3:转发模式,4:测试模式
  511. xs_error_code_t Xsync::TTLOutputModule1_setSrcSigType(SignalType_t source) {
  512. if (source != SIGNAL_LOGIC0 && //
  513. source != SIGNAL_LOGIC1 && //
  514. source != SIGNAL_TTLIN1 && //
  515. source != SIGNAL_TTLIN2 && //
  516. source != SIGNAL_TTLIN3 && //
  517. source != SIGNAL_TTLIN4 && //
  518. source != SIGNAL_SYS_CLK_OUTPUT && //
  519. source != SIGNAL_SYS_GENLOCK_OUTPUT && //
  520. source != SIGNAL_SYS_TIMECODE_FREQ_OUTPUT && //
  521. source != SIGNAL_BUSINESS_RECORD_SIG && //
  522. source != SIGNAL_BUSINESS_RECORD_EXPOSURE_SIG //
  523. ) {
  524. return kxs_ec_param_error;
  525. }
  526. if (source == SIGNAL_TTLIN1 || //
  527. source == SIGNAL_TTLIN2 || //
  528. source == SIGNAL_TTLIN3 || //
  529. source == SIGNAL_TTLIN4 || //
  530. source == SIGNAL_SYS_CLK_OUTPUT || //
  531. source == SIGNAL_SYS_GENLOCK_OUTPUT || //
  532. source == SIGNAL_SYS_TIMECODE_FREQ_OUTPUT //
  533. ) {
  534. DO_XSYNC(reg_write(reg::kreg_ttlout1_signal_process_mode, 2, 10)); // 分频倍频模式
  535. } else {
  536. DO_XSYNC(reg_write(reg::kreg_ttlout1_signal_process_mode, 3, 10)); // 转发模式
  537. }
  538. REG_WRITE(reg::kreg_ttlout1_input_signal_select, source);
  539. }
  540. xs_error_code_t Xsync::TTLOutputModule2_setSrcSigType(SignalType_t source) {
  541. if (source != SIGNAL_LOGIC0 && //
  542. source != SIGNAL_LOGIC1 && //
  543. source != SIGNAL_TTLIN1 && //
  544. source != SIGNAL_TTLIN2 && //
  545. source != SIGNAL_TTLIN3 && //
  546. source != SIGNAL_TTLIN4 && //
  547. source != SIGNAL_SYS_CLK_OUTPUT && //
  548. source != SIGNAL_SYS_GENLOCK_OUTPUT && //
  549. source != SIGNAL_SYS_TIMECODE_FREQ_OUTPUT && //
  550. source != SIGNAL_BUSINESS_RECORD_SIG && //
  551. source != SIGNAL_BUSINESS_RECORD_EXPOSURE_SIG //
  552. ) {
  553. return kxs_ec_param_error;
  554. }
  555. if (source == SIGNAL_TTLIN1 || //
  556. source == SIGNAL_TTLIN2 || //
  557. source == SIGNAL_TTLIN3 || //
  558. source == SIGNAL_TTLIN4 || //
  559. source == SIGNAL_SYS_CLK_OUTPUT || //
  560. source == SIGNAL_SYS_GENLOCK_OUTPUT || //
  561. source == SIGNAL_SYS_TIMECODE_FREQ_OUTPUT //
  562. ) {
  563. DO_XSYNC(reg_write(reg::kreg_ttlout2_signal_process_mode, 2, 10)); // 分频倍频模式
  564. } else {
  565. DO_XSYNC(reg_write(reg::kreg_ttlout2_signal_process_mode, 3, 10)); // 转发模式
  566. }
  567. REG_WRITE(reg::kreg_ttlout2_input_signal_select, source);
  568. }
  569. xs_error_code_t Xsync::TTLOutputModule3_setSrcSigType(SignalType_t source) {
  570. if (source != SIGNAL_LOGIC0 && //
  571. source != SIGNAL_LOGIC1 && //
  572. source != SIGNAL_TTLIN1 && //
  573. source != SIGNAL_TTLIN2 && //
  574. source != SIGNAL_TTLIN3 && //
  575. source != SIGNAL_TTLIN4 && //
  576. source != SIGNAL_SYS_CLK_OUTPUT && //
  577. source != SIGNAL_SYS_GENLOCK_OUTPUT && //
  578. source != SIGNAL_SYS_TIMECODE_FREQ_OUTPUT && //
  579. source != SIGNAL_BUSINESS_RECORD_SIG && //
  580. source != SIGNAL_BUSINESS_RECORD_EXPOSURE_SIG //
  581. ) {
  582. return kxs_ec_param_error;
  583. }
  584. if (source == SIGNAL_TTLIN1 || //
  585. source == SIGNAL_TTLIN2 || //
  586. source == SIGNAL_TTLIN3 || //
  587. source == SIGNAL_TTLIN4 || //
  588. source == SIGNAL_SYS_CLK_OUTPUT || //
  589. source == SIGNAL_SYS_GENLOCK_OUTPUT || //
  590. source == SIGNAL_SYS_TIMECODE_FREQ_OUTPUT //
  591. ) {
  592. DO_XSYNC(reg_write(reg::kreg_ttlout3_signal_process_mode, 2, 10)); // 分频倍频模式
  593. } else {
  594. DO_XSYNC(reg_write(reg::kreg_ttlout3_signal_process_mode, 3, 10)); // 转发模式
  595. }
  596. REG_WRITE(reg::kreg_ttlout3_input_signal_select, source);
  597. }
  598. xs_error_code_t Xsync::TTLOutputModule4_setSrcSigType(SignalType_t source) {
  599. if (source != SIGNAL_LOGIC0 && //
  600. source != SIGNAL_LOGIC1 && //
  601. source != SIGNAL_TTLIN1 && //
  602. source != SIGNAL_TTLIN2 && //
  603. source != SIGNAL_TTLIN3 && //
  604. source != SIGNAL_TTLIN4 && //
  605. source != SIGNAL_SYS_CLK_OUTPUT && //
  606. source != SIGNAL_SYS_GENLOCK_OUTPUT && //
  607. source != SIGNAL_SYS_TIMECODE_FREQ_OUTPUT && //
  608. source != SIGNAL_BUSINESS_RECORD_SIG && //
  609. source != SIGNAL_BUSINESS_RECORD_EXPOSURE_SIG //
  610. ) {
  611. return kxs_ec_param_error;
  612. }
  613. if (source == SIGNAL_TTLIN1 || source == SIGNAL_TTLIN2 || source == SIGNAL_TTLIN3 || source == SIGNAL_TTLIN4) {
  614. DO_XSYNC(reg_write(reg::kreg_ttlout4_signal_process_mode, 2, 10)); // 分频倍频模式
  615. } else {
  616. DO_XSYNC(reg_write(reg::kreg_ttlout4_signal_process_mode, 3, 10)); // 转发模式
  617. }
  618. REG_WRITE(reg::kreg_ttlout4_input_signal_select, source);
  619. }
  620. xs_error_code_t Xsync::TTLOutputModule1_getSrcSigType(SignalType_t &source) { REG_READ(reg::kreg_ttlout1_input_signal_select, source); }
  621. xs_error_code_t Xsync::TTLOutputModule2_getSrcSigType(SignalType_t &source) { REG_READ(reg::kreg_ttlout2_input_signal_select, source); }
  622. xs_error_code_t Xsync::TTLOutputModule3_getSrcSigType(SignalType_t &source) { REG_READ(reg::kreg_ttlout3_input_signal_select, source); }
  623. xs_error_code_t Xsync::TTLOutputModule4_getSrcSigType(SignalType_t &source) { REG_READ(reg::kreg_ttlout4_input_signal_select, source); }
  624. xs_error_code_t Xsync::TTLOutputModule1_setFreqDivision(uint32_t div) { REG_WRITE(reg::kreg_ttlout1_pllout_freq_division_ctrl, div); }
  625. xs_error_code_t Xsync::TTLOutputModule2_setFreqDivision(uint32_t div) { REG_WRITE(reg::kreg_ttlout2_pllout_freq_division_ctrl, div); }
  626. xs_error_code_t Xsync::TTLOutputModule3_setFreqDivision(uint32_t div) { REG_WRITE(reg::kreg_ttlout3_pllout_freq_division_ctrl, div); }
  627. xs_error_code_t Xsync::TTLOutputModule4_setFreqDivision(uint32_t div) { REG_WRITE(reg::kreg_ttlout4_pllout_freq_division_ctrl, div); }
  628. xs_error_code_t Xsync::TTLOutputModule1_getFreqDivision(uint32_t &div) { REG_READ(reg::kreg_ttlout1_pllout_freq_division_ctrl, div); }
  629. xs_error_code_t Xsync::TTLOutputModule2_getFreqDivision(uint32_t &div) { REG_READ(reg::kreg_ttlout2_pllout_freq_division_ctrl, div); }
  630. xs_error_code_t Xsync::TTLOutputModule3_getFreqDivision(uint32_t &div) { REG_READ(reg::kreg_ttlout3_pllout_freq_division_ctrl, div); }
  631. xs_error_code_t Xsync::TTLOutputModule4_getFreqDivision(uint32_t &div) { REG_READ(reg::kreg_ttlout4_pllout_freq_division_ctrl, div); }
  632. xs_error_code_t Xsync::TTLOutputModule1_setFreqMultiplication(uint32_t multi) { REG_WRITE(reg::kreg_ttlout1_pllout_freq_multiplication_ctrl, multi); }
  633. xs_error_code_t Xsync::TTLOutputModule2_setFreqMultiplication(uint32_t multi) { REG_WRITE(reg::kreg_ttlout2_pllout_freq_multiplication_ctrl, multi); }
  634. xs_error_code_t Xsync::TTLOutputModule3_setFreqMultiplication(uint32_t multi) { REG_WRITE(reg::kreg_ttlout3_pllout_freq_multiplication_ctrl, multi); }
  635. xs_error_code_t Xsync::TTLOutputModule4_setFreqMultiplication(uint32_t multi) { REG_WRITE(reg::kreg_ttlout4_pllout_freq_multiplication_ctrl, multi); }
  636. xs_error_code_t Xsync::TTLOutputModule1_getFreqMultiplication(uint32_t &multi) { REG_READ(reg::kreg_ttlout1_pllout_freq_multiplication_ctrl, multi); }
  637. xs_error_code_t Xsync::TTLOutputModule2_getFreqMultiplication(uint32_t &multi) { REG_READ(reg::kreg_ttlout2_pllout_freq_multiplication_ctrl, multi); }
  638. xs_error_code_t Xsync::TTLOutputModule3_getFreqMultiplication(uint32_t &multi) { REG_READ(reg::kreg_ttlout3_pllout_freq_multiplication_ctrl, multi); }
  639. xs_error_code_t Xsync::TTLOutputModule4_getFreqMultiplication(uint32_t &multi) { REG_READ(reg::kreg_ttlout4_pllout_freq_multiplication_ctrl, multi); }
  640. xs_error_code_t Xsync::TTLOutputModule1_readInFreq(float &freq) { return readfreq(reg::kreg_ttlout1_sig_in_freq_detect, freq); }
  641. xs_error_code_t Xsync::TTLOutputModule2_readInFreq(float &freq) { return readfreq(reg::kreg_ttlout2_sig_in_freq_detect, freq); }
  642. xs_error_code_t Xsync::TTLOutputModule3_readInFreq(float &freq) { return readfreq(reg::kreg_ttlout3_sig_in_freq_detect, freq); }
  643. xs_error_code_t Xsync::TTLOutputModule4_readInFreq(float &freq) { return readfreq(reg::kreg_ttlout4_sig_in_freq_detect, freq); }
  644. xs_error_code_t Xsync::TTLOutputModule1_readOutFreq(float &freq) { return readfreq(reg::kreg_ttlout1_sig_out_freq_detect, freq); }
  645. xs_error_code_t Xsync::TTLOutputModule2_readOutFreq(float &freq) { return readfreq(reg::kreg_ttlout2_sig_out_freq_detect, freq); }
  646. xs_error_code_t Xsync::TTLOutputModule3_readOutFreq(float &freq) { return readfreq(reg::kreg_ttlout3_sig_out_freq_detect, freq); }
  647. xs_error_code_t Xsync::TTLOutputModule4_readOutFreq(float &freq) { return readfreq(reg::kreg_ttlout4_sig_out_freq_detect, freq); }
  648. /*******************************************************************************
  649. * TimecodeInputModule *
  650. *******************************************************************************/
  651. xs_error_code_t Xsync::ExternalTimecode_setSource(InputInterface_t src) {
  652. if (src == INPUT_IF_TIMECODE_BNC) {
  653. DO_XSYNC(reg_write(reg::external_timecode_sig_selt, 1, 10)); // 0:off,1:bnc,2:headphone
  654. } else if (src == INPUT_IF_TIMECODE_HEADPHONE) {
  655. DO_XSYNC(reg_write(reg::external_timecode_sig_selt, 2, 10)); // 0:off,1:bnc,2:headphone
  656. } else if (src == INPUT_IF_OFF) {
  657. DO_XSYNC(reg_write(reg::external_timecode_sig_selt, 0, 10)); // 0:off,1:bnc,2:headphone
  658. } else {
  659. return kxs_ec_param_error;
  660. }
  661. return kxs_ec_success;
  662. }
  663. xs_error_code_t Xsync::ExternalTimecode_getSource(InputInterface_t &timecode_select) {
  664. uint32_t readbak = 0;
  665. DO_XSYNC(reg_read(reg::external_timecode_sig_selt, readbak, 10));
  666. if (readbak == 1) {
  667. timecode_select = INPUT_IF_TIMECODE_BNC;
  668. } else if (readbak == 2) {
  669. timecode_select = INPUT_IF_TIMECODE_HEADPHONE;
  670. } else if (readbak == 0) {
  671. timecode_select = INPUT_IF_OFF;
  672. } else {
  673. timecode_select = INPUT_IF_OFF;
  674. }
  675. return kxs_ec_success;
  676. }
  677. xs_error_code_t Xsync::ExternalTimecode_setFormat(TimecodeFormat_t format) { REG_WRITE(reg::external_timecode_format, format); }
  678. xs_error_code_t Xsync::ExternalTimecode_getFormat(TimecodeFormat_t &format) { REG_READ(reg::external_timecode_format, format); }
  679. xs_error_code_t Xsync::ExternalTimecode_readCode(XsyncTimecode_t &timecode) { return readtimecode(reg::external_timecode_code0, reg::external_timecode_code1, timecode); }
  680. /*******************************************************************************
  681. * InternalTimecode *
  682. *******************************************************************************/
  683. xs_error_code_t Xsync::InternalTimecode_setFormat(TimecodeFormat_t format) { REG_WRITE(reg::internal_timecode_format, format); }
  684. xs_error_code_t Xsync::InternalTimecode_getFormat(TimecodeFormat_t &format) { REG_READ(reg::internal_timecode_format, format); }
  685. xs_error_code_t Xsync::InternalTimecode_setCode(XsyncTimecode_t timecode) {
  686. DO_XSYNC(reg_write(reg::internal_timecode_en, 0));
  687. DO_XSYNC(writetimecode(reg::internal_timecode_data0, reg::internal_timecode_data1, timecode));
  688. DO_XSYNC(reg_write(reg::internal_timecode_en, 1));
  689. return kxs_ec_success;
  690. }
  691. xs_error_code_t Xsync::InternalTimecode_getCode(XsyncTimecode_t &timecode) { return readtimecode(reg::internal_timecode_data0, reg::internal_timecode_data1, timecode); }
  692. /*******************************************************************************
  693. * SysTimecode *
  694. *******************************************************************************/
  695. xs_error_code_t Xsync::SysTimecode_setSource(uint32_t sig) { REG_WRITE(reg::sys_timecode_select, sig); }
  696. xs_error_code_t Xsync::SysTimecode_getSource(uint32_t &sig) { REG_READ(reg::sys_timecode_select, sig); }
  697. xs_error_code_t Xsync::SysTimecode_readFormat(TimecodeFormat_t &format) { REG_READ(reg::sys_timecode_format, format); }
  698. xs_error_code_t Xsync::SysTimecode_readCode(XsyncTimecode_t &timecode) { return readtimecode(reg::sys_timecode_data0, reg::sys_timecode_data1, timecode); }
  699. /*******************************************************************************
  700. * TimecodeOutputModule *
  701. *******************************************************************************/
  702. xs_error_code_t Xsync::TimecodeOutputModule_setBncOutputLevel(int level) { REG_WRITE(reg::timecode_output_bnc_outut_level_select, level); }
  703. xs_error_code_t Xsync::TimecodeOutputModule_getBncOutputLevel(int &level) { REG_READ(reg::timecode_output_bnc_outut_level_select, level); }
  704. xs_error_code_t Xsync::TimecodeOutputModule_setHeadphoneOutputLevel(int level) { REG_WRITE(reg::timecode_output_headphone_outut_level_select, level); }
  705. xs_error_code_t Xsync::TimecodeOutputModule_getHeadphoneOutputLevel(int &level) { REG_READ(reg::timecode_output_headphone_outut_level_select, level); }
  706. /*******************************************************************************
  707. * GENLOCK *
  708. *******************************************************************************/
  709. xs_error_code_t Xsync::ExternalGenlock_detectFreq(float &freq) { return readfreq(reg::external_genlock_freq, freq); }
  710. xs_error_code_t Xsync::InternalGenlock_setFormat(GenlockFormat_t format) { return reg_write(reg::internal_genlock_format, format); }
  711. xs_error_code_t Xsync::InternalGenlock_getFormat(GenlockFormat_t &format) { REG_READ(reg::internal_genlock_format, format); }
  712. xs_error_code_t Xsync::SysGenlock_setSrc(uint32_t source) { return reg_write(reg::sys_genlock_source, source); }
  713. xs_error_code_t Xsync::SysGenlock_getSrc(uint32_t &extern_or_internal) {
  714. REG_READ(reg::sys_genlock_source, extern_or_internal);
  715. return kxs_ec_success;
  716. }
  717. xs_error_code_t Xsync::SysGenlock_readFreq(float &freq) { return readfreq(reg::sys_genlock_freq, freq); }
  718. /*******************************************************************************
  719. * INTERNAL_CLOCK *
  720. *******************************************************************************/
  721. xs_error_code_t Xsync::InternalClock_setFreq(float freq) {
  722. double T = 1.0 / freq;
  723. double T_ns = T * 1000 * 1000 * 1000;
  724. double cnt = T_ns / 100 + 0.5; // 10MHZ <=> 100ns
  725. uint32_t cnt_u32 = uint32_t(cnt);
  726. return reg_write(reg::internal_clock_freq, cnt_u32);
  727. return kxs_ec_success;
  728. }
  729. xs_error_code_t Xsync::InternalClock_getFreq(float &freq) { return readfreq(reg::internal_clock_freq, freq); }
  730. /*******************************************************************************
  731. * SysClock *
  732. *******************************************************************************/
  733. xs_error_code_t Xsync::SysClock_setSrc(SignalType_t sig) {
  734. if (sig != SIGNAL_TTLIN1 && //
  735. sig != SIGNAL_TTLIN2 && //
  736. sig != SIGNAL_TTLIN3 && //
  737. sig != SIGNAL_TTLIN4 && //
  738. sig != SIGNAL_SYS_CLK_OUTPUT && //
  739. sig != SIGNAL_SYS_GENLOCK_OUTPUT && //
  740. sig != SIGNAL_SYS_TIMECODE_FREQ_OUTPUT) {
  741. return kxs_ec_param_error;
  742. }
  743. return reg_write(reg::sys_clock_source, sig);
  744. }
  745. xs_error_code_t Xsync::SysClock_getSrc(SignalType_t &sig) { REG_READ(reg::sys_clock_source, sig); }
  746. xs_error_code_t Xsync::SysClock_setTriggerEdge(TriggerEdge_t edge) {
  747. if (edge != TRIGGER_EDGE_RISING && edge != TRIGGER_EDGE_FALLING) {
  748. return kxs_ec_param_error;
  749. }
  750. return reg_write(reg::sys_clock_trigger_edge_select, edge);
  751. }
  752. xs_error_code_t Xsync::SysClock_getTriggerEdge(TriggerEdge_t &edge) { return _reg_read(reg::sys_clock_trigger_edge_select, edge); }
  753. xs_error_code_t Xsync::SysClock_setFreqDivision(uint32_t div) { return reg_write(reg::sys_clock_freq_division_ctrl, div); }
  754. xs_error_code_t Xsync::SysClock_geFreqtDivision(uint32_t &div) { return _reg_read(reg::sys_clock_freq_division_ctrl, div); }
  755. xs_error_code_t Xsync::SysClock_setFreqMultiplication(uint32_t muti) { return reg_write(reg::sys_clock_freq_multiplication_ctrl, muti); }
  756. xs_error_code_t Xsync::SysClock_getFreqMultiplication(uint32_t &muti) { return _reg_read(reg::sys_clock_freq_multiplication_ctrl, muti); }
  757. xs_error_code_t Xsync::SysClock_readOutSigFreq(float &freq) { return readfreq(reg::sys_clock_outfreq_detect, freq); }
  758. xs_error_code_t Xsync::SysClock_readInSigFreq(float &freq) { return readfreq(reg::sys_clock_infreq_detect, freq); }
  759. xs_error_code_t Xsync::RecordSigGenerator_setContrlMode(ControlMode_t mode) {
  760. if (mode != CONTROLMODE_MANUAL_TRIGGER && mode != CONTROLMODE_TIMECODE_TRIGGER && mode != CONTROLMODE_EXTERNALTTL_TRIGGER) {
  761. return kxs_ec_param_error;
  762. }
  763. return reg_write(reg::record_sig_gen_ctrl_control_mode, mode);
  764. }
  765. xs_error_code_t Xsync::RecordSigGenerator_getContrlMode(ControlMode_t &mode) { //
  766. return _reg_read(reg::record_sig_gen_ctrl_control_mode, mode);
  767. }
  768. xs_error_code_t Xsync::RecordSigGenerator_manualStart() { return reg_write(reg::record_sig_gen_manual_ctrl, 1, 10); }
  769. xs_error_code_t Xsync::RecordSigGenerator_manualStop() { return reg_write(reg::record_sig_gen_manual_ctrl, 0, 10); }
  770. xs_error_code_t Xsync::RecordSigGenerator_setAutoStartTimecode(XsyncTimecode_t timecode) { //
  771. return writetimecode(reg::record_sig_gen_timecode_start0, reg::record_sig_gen_timecode_start1, timecode);
  772. }
  773. xs_error_code_t Xsync::RecordSigGenerator_setAutoStopTimecode(XsyncTimecode_t timecode) { //
  774. return writetimecode(reg::record_sig_gen_timecode_stop0, reg::record_sig_gen_timecode_stop1, timecode);
  775. }
  776. xs_error_code_t Xsync::RecordSigGenerator_getAutoStartTimecode(XsyncTimecode_t &timecode) { //
  777. return readtimecode(reg::record_sig_gen_timecode_start0, reg::record_sig_gen_timecode_start1, timecode);
  778. }
  779. xs_error_code_t Xsync::RecordSigGenerator_getAutoStopTimecode(XsyncTimecode_t &timecode) { return readtimecode(reg::record_sig_gen_timecode_stop0, reg::record_sig_gen_timecode_stop1, timecode); }
  780. xs_error_code_t Xsync::RecordSigGenerator_setExternalTTLTriggerSrc(InputInterface_t ttlPortNum) {
  781. if (ttlPortNum != INPUT_IF_TTL1 && ttlPortNum != INPUT_IF_TTL2 && ttlPortNum != INPUT_IF_TTL3 && ttlPortNum != INPUT_IF_TTL4) {
  782. return kxs_ec_param_error;
  783. }
  784. return reg_write(reg::record_sig_gen_ttlin_trigger_sig_source, ttlPortNum);
  785. }
  786. xs_error_code_t Xsync::RecordSigGenerator_getExternalTTLTriggerSrc(InputInterface_t &ttlPortNum) { //
  787. return _reg_read(reg::record_sig_gen_ttlin_trigger_sig_source, ttlPortNum);
  788. }
  789. xs_error_code_t Xsync::RecordSigGenerator_setExternalTTLTriggerPolarity(uint32_t polarity) { //
  790. return reg_write(reg::record_sig_gen_ttlin_trigger_level, polarity);
  791. }
  792. xs_error_code_t Xsync::RecordSigGenerator_getExternalTTLTriggerPolarity(uint32_t &polarity) { //
  793. return _reg_read(reg::record_sig_gen_ttlin_trigger_level, polarity);
  794. }
  795. xs_error_code_t Xsync::RecordSigGenerator_setRecordExposureTime(uint32_t us) { //
  796. return reg_write(reg::record_sig_gen_exposure_time, us);
  797. }
  798. xs_error_code_t Xsync::RecordSigGenerator_getRecordExposureTime(uint32_t &us) {
  799. auto ret = reg_read(reg::record_sig_gen_exposure_time, us);
  800. // us = us / 10;1
  801. return ret;
  802. }
  803. xs_error_code_t Xsync::RecordSigGenerator_setRecordExposureOffsetTime(uint32_t us) { return reg_write(reg::record_sig_gen_exposure_offset_time, us); }
  804. xs_error_code_t Xsync::RecordSigGenerator_getRecordExposureOffsetTime(uint32_t &us) {
  805. auto ret = reg_read(reg::record_sig_gen_exposure_offset_time, us);
  806. // us = us / 10;
  807. return ret;
  808. }
  809. xs_error_code_t Xsync::RecordSigGenerator_setTimecodeCtrlFlag(uint32_t autoStart, uint32_t autoStop) { //
  810. uint32_t flag = (autoStart & 0x01) | ((autoStop & 0x01) << 1);
  811. return reg_write(reg::record_sig_gen_timecode_control_flag, flag);
  812. }
  813. xs_error_code_t Xsync::RecordSigGenerator_getTimecodeCtrlFlag(uint32_t &autoStart, uint32_t &autoStop) {
  814. uint32_t flag = 0;
  815. auto ret = reg_read(reg::record_sig_gen_timecode_control_flag, flag);
  816. autoStart = flag & 0x01;
  817. autoStop = (flag >> 1) & 0x01;
  818. return ret;
  819. }
  820. xs_error_code_t Xsync::RecordSigGenerator_getRecordState(uint32_t &state) { return _reg_read(reg::record_sig_gen_record_state, state); }
  821. xs_error_code_t Xsync::RecordSigGenerator_readTimecodeSnapshot(XsyncTimecode_t &timecode) { //
  822. return readtimecode(reg::record_sig_gen_timecode_snapshot0, reg::record_sig_gen_timecode_snapshot1, timecode);
  823. }
  824. namespace xsync {
  825. void XsyncInit(I_XSUDPFactory *xsync_udp_factory, string xsyncIp) {
  826. Xsync::Ins().initialize(xsync_udp_factory);
  827. Xsync::Ins().changeXsyncIp(xsyncIp);
  828. }
  829. IXsync *XsyncIns() { return &Xsync::Ins(); }
  830. } // namespace xsync