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

#pragma once
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <mutex>
#include <set>
#include <sstream>
#include <string>
#include <vector>
//
#include "i_xsync_udp.hpp"
#include "xsync_errcode.hpp"
#include "xsync_packet.hpp"
#include "xsync_regs.hpp"
#include "xsync_v2_sig_type.hpp"
namespace xsync {
using namespace std;
typedef function<void(XsyncTimecode_t *timecode_msg)> xsync_on_timecode_msg_t;
typedef function<void(uint32_t workstate)> xsync_on_workstate_change_msg_t;
typedef function<void(xysnc_camera_sync_data_t *timecode_msg)> xsync_on_camera_sync_msg_t;
/*******************************************************************************
* 信号发生器模块相关枚举 *
*******************************************************************************/
namespace sig_generator_module {
typedef enum {
kControlMode_manualTrigger, // 0.手动,启动停止
kControlMode_externalTimecodeTrigger, // 1.外部TIMECODE触发启动
kControlMode_externalTTL1Trigger, // 2.外部TTL输入1_高电平触发,低电平停止
kControlMode_externalTTL2Trigger, // 3.外部TTL输入2_高电平触发,低电平停止
kControlMode_externalTTL3Trigger, // 4.外部TTL输入3_高电平触发,低电平停止
kControlMode_externalTTL4Trigger, // 5.外部TTL输入4_高电平触发,低电平停止
} ControlMode_t;
string ControlMode2Str(ControlMode_t mode);
ControlMode_t Str2ControlMode(string mode);
list<string> ControlModeStrSet();
} // namespace sig_generator_module
/*******************************************************************************
* 时码输出模块 *
*******************************************************************************/
namespace timecode_output_module {
typedef enum {
koff = 0, // 关闭输出
kinternal_timecode_sig = 1, // 外部timecode信号
kext_timecode_sig = 2, // 内部timecode信号
} TriggerSigType_t;
typedef enum {
kline,
kmic,
} OutputSigLevelType_t;
string TriggerSigType2Str(TriggerSigType_t type);
TriggerSigType_t Str2TriggerSigType(string type);
list<string> TriggerSigTypeStrSet();
string OutputSigLevelType2Str(OutputSigLevelType_t type);
OutputSigLevelType_t Str2OutputSigLevelType(string type);
list<string> OutputSigLevelTypeStrSet();
} // namespace timecode_output_module
/*******************************************************************************
* 相机同步信号发生器模块 *
*******************************************************************************/
namespace camera_sync_packet_generator_module {
typedef enum {
koff = 0, // 关闭输出
kinternal_genlock_sig = 1, // 外部genlock信号
kext_genlock_sig = 2, // 内部genlock信号
} TriggerSigType_t;
string TriggerSigType2Str(TriggerSigType_t type);
TriggerSigType_t Str2TriggerSigType(string type);
list<string> TriggerSigTypeStrSet();
} // namespace camera_sync_packet_generator_module
/*******************************************************************************
* 时码输入 *
*******************************************************************************/
namespace timecode_input_module {
typedef enum {
koff = 0,
kbnc_timecode,
kheadphone_timecode,
} TriggerSigType_t;
string TriggerSigType2Str(TriggerSigType_t type);
TriggerSigType_t Str2TriggerSigType(string type);
list<string> TriggerSigTypeStrSet();
} // namespace timecode_input_module
class Xsync {
public:
private:
/* data */
I_XSUDPFactory *m_xsync_udp_factory = nullptr;
shared_ptr<I_XSUDP> m_xsync_reg_udp = nullptr;
shared_ptr<I_XSUDP> m_xsync_timecode_udp_listener = nullptr;
shared_ptr<I_XSUDP> m_xsync_camera_sync_udp_listener = nullptr;
string m_xsync_ip;
bool m_is_connected = false;
xsync_on_camera_sync_msg_t m_on_camera_sync_msg_cb = nullptr;
xsync_on_timecode_msg_t m_on_timecode_msg_cb = nullptr;
xsync_on_workstate_change_msg_t m_on_workstate_change_msg_cb = nullptr;
int txpacket_index = 0;
uint8_t m_xync_cmd_rxdata_cache[2560];
xsync_net_state_t m_net_state = kxsync_net_state_disconnect;
std::recursive_mutex lock_;
Xsync(/* args */);
public:
static Xsync &Ins();
void initialize(I_XSUDPFactory *xsync_udp_factory);
/*******************************************************************************
* 设备连接操作 *
*******************************************************************************/
bool ping(string xsync_ip);
xs_error_code_t connect(string xsync_ip);
xs_error_code_t disConnect();
xsync_net_state_t getNetState();
/*******************************************************************************
* 上报消息监听 *
*******************************************************************************/
void Basic_registerOnTimecodeMsgCallback(xsync_on_timecode_msg_t on_timecode_msg_cb);
void Basic_registerOnCameraSyncMsgCallback(xsync_on_camera_sync_msg_t on_camera_sync_msg_cb);
void Basic_registerOnWorkstateChangeMsgCallback(xsync_on_workstate_change_msg_t on_workstate_change_msg_cb);
/*******************************************************************************
* 寄存器直接读写操作 *
*******************************************************************************/
xs_error_code_t reg_write(uint32_t regadd, uint32_t regvalue, uint32_t &regbackvalue, int32_t overtime_ms = 100);
xs_error_code_t reg_write(uint32_t regadd, uint32_t regvalue, int32_t overtime_ms = 100);
xs_error_code_t reg_read(uint32_t regadd, uint32_t &regvalue, int32_t overtime_ms = 100);
xs_error_code_t reg_read_muti(uint32_t regadd, uint32_t nreg, vector<uint32_t> &regvalues, int32_t overtime_ms = 100);
/*******************************************************************************
* 设备基础操作 *
*******************************************************************************/
xs_error_code_t Basic_generatorNewMac();
xs_error_code_t Basic_factoryReset();
xs_error_code_t Basic_reboot();
xs_error_code_t Basic_changeNetworkConfig(string ip, string mask, string gateway);
xs_error_code_t Basic_clearXsyncCameraSyncIndexCount();
// xs_error_code_t Basic_setGenlockFormat(GenlockFormat_t format);
// xs_error_code_t Basic_getGenlockFormat(GenlockFormat_t &format);
// xs_error_code_t Basic_setTimecodeFormat(TimecodeFormat_t format);
public:
/*******************************************************************************
* 相机同步信号上报控制模块 *
*******************************************************************************/
/**
* @brief 选择相机同步信号上报的信号源
*
* @param sig
* @return xs_error_code_t
*/
xs_error_code_t CameraSyncPacketGeneratorModule_setTriggerSig(camera_sync_packet_generator_module::TriggerSigType_t sig);
xs_error_code_t CameraSyncPacketGeneratorModule_getTriggerSig(camera_sync_packet_generator_module::TriggerSigType_t &sig);
/**
* @brief 设置相机同步信号上报的报告周期
*
* @param packetNum 例如:30 表示每30个包上报一次
* @return xs_error_code_t
*/
xs_error_code_t CameraSyncPacketGeneratorModule_setReportPeriod(uint32_t packetNum);
xs_error_code_t CameraSyncPacketGeneratorModule_getReportPeriod(uint32_t &packetNum);
/**
* @brief 获取当前相机同步信号上报的包序号
*
* @param index
* @return xs_error_code_t
*/
xs_error_code_t CameraSyncPacketGeneratorModule_getPacketIndex(uint32_t &index);
/**
* @brief 清除当前相机同步信号上报的包序号
*
* @return xs_error_code_t
*/
xs_error_code_t CameraSyncPacketGeneratorModule_clearPacketIndex();
public:
/*******************************************************************************
* 新接口 *
*******************************************************************************/
/*******************************************************************************
* 内部时码发生器 *
*******************************************************************************/
/**
* @brief 内部Timecode信号发生器,设置Timecode格式
*
* @param format
* 支持列表
* TIMECODE_FPS2398
* TIMECODE_FPS2400
* TIMECODE_FPS2500
* TIMECODE_FPS2997
* TIMECODE_FPS2997Drop
* TIMECODE_FPS3000
*
* @return xs_error_code_t
*/
xs_error_code_t InternalTimecode_setFormat(TimecodeFormat_t format);
xs_error_code_t InternalTimecode_getFormat(TimecodeFormat_t &format);
/**
* @brief 内部Timecode信号发生器,设置Timecode值
*
* @param timecode
* @return xs_error_code_t
*/
xs_error_code_t InternalTimecode_setCode(XsyncTimecode_t timecode);
xs_error_code_t InternalTimecode_getCode(XsyncTimecode_t &timecode);
/*******************************************************************************
* 时码输入 *
*******************************************************************************/
/**
* @brief 选择时码输入源
*
* @param src
* INPUT_IF_TIMECODE_BNC
* INPUT_IF_TIMECODE_HEADPHONE
*
* @return xs_error_code_t
*/
xs_error_code_t ExternalTimecode_setSource(InputInterface_t src);
xs_error_code_t ExternalTimecode_getSource(InputInterface_t &timecode_select);
/**
* @brief 设置时码格式
*
* @param format
* @return xs_error_code_t
*/
xs_error_code_t ExternalTimecode_setFormat(TimecodeFormat_t format);
xs_error_code_t ExternalTimecode_getFormat(TimecodeFormat_t &format);
// ExternalTimecode_readCode
xs_error_code_t ExternalTimecode_readCode(XsyncTimecode_t &timecode);
/*******************************************************************************
* SysTimecode *
*******************************************************************************/
/**
* @brief 系统Timecode选择时钟源
*
* @param internal
* 0: 内部Timecode
* 1: 外部Timecode
* @return xs_error_code_t
*/
xs_error_code_t SysTimecode_setSource(uint32_t sig);
xs_error_code_t SysTimecode_getSource(uint32_t &sig);
/**
* @brief 读取系统Timecode格式
*
* @param format
* @return xs_error_code_t
*/
xs_error_code_t SysTimecode_readFormat(TimecodeFormat_t &format);
/**
* @brief 读取系统Timecode值
*
* @param timecode
* @return xs_error_code_t
*/
xs_error_code_t SysTimecode_readCode(XsyncTimecode_t &timecode);
/*******************************************************************************
* Timecode输出口配置 *
*******************************************************************************/
/**
* @brief TIMECODE输出模块
*
* 信号源: SYS_TIMECODE
*/
/**
* @brief 设置时码输出模块的BNC输出电平
*
* @param level
* @return xs_error_code_t
*/
xs_error_code_t TimecodeOutputModule_setBncOutputLevel(int level); // 0:line,1:mic
xs_error_code_t TimecodeOutputModule_getBncOutputLevel(int &level);
/**
* @brief 设置时码输出模块的耳机输出电平
*
* @param level
* @return xs_error_code_t
*/
xs_error_code_t TimecodeOutputModule_setHeadphoneOutputLevel(int level); // 0:line,1:mic
xs_error_code_t TimecodeOutputModule_getHeadphoneOutputLevel(int &level);
/*******************************************************************************
* TTL输入模块 *
*******************************************************************************/
/**
* @brief 读取TTL输入模块的频率
*
* @param index
* @param freq
* @return xs_error_code_t
*/
xs_error_code_t TTLInputModule1_detectFreq(uint32_t &freq);
xs_error_code_t TTLInputModule2_detectFreq(uint32_t &freq);
xs_error_code_t TTLInputModule3_detectFreq(uint32_t &freq);
xs_error_code_t TTLInputModule4_detectFreq(uint32_t &freq);
/*******************************************************************************
* TTL输出模块 *
*******************************************************************************/
/**
* @brief TTLOutputModuleX_setSrcSigType TTL输出口设置信号源
*
* @param source
* 支持列表:
* SIGNAL_TTLIN1
* SIGNAL_TTLIN2
* SIGNAL_TTLIN3
* SIGNAL_TTLIN4
* SIGNAL_SYS_CLK_OUTPUT
* SIGNAL_SYS_GENLOCK_OUTPUT
* SIGNAL_SYS_TIMECODE_FREQ_OUTPUT
* SIGNAL_BUSINESS_RECORD_SIG
* SIGNAL_BUSINESS_RECORD_EXPOSURE_SIG
*
* @return xs_error_code_t
*/
xs_error_code_t TTLOutputModule1_setSrcSigType(SignalType_t source);
xs_error_code_t TTLOutputModule2_setSrcSigType(SignalType_t source);
xs_error_code_t TTLOutputModule3_setSrcSigType(SignalType_t source);
xs_error_code_t TTLOutputModule4_setSrcSigType(SignalType_t source);
xs_error_code_t TTLOutputModule1_getSrcSigType(SignalType_t &source);
xs_error_code_t TTLOutputModule2_getSrcSigType(SignalType_t &source);
xs_error_code_t TTLOutputModule3_getSrcSigType(SignalType_t &source);
xs_error_code_t TTLOutputModule4_getSrcSigType(SignalType_t &source);
xs_error_code_t TTLOutputModule1_setFreqDivision(uint32_t div);
xs_error_code_t TTLOutputModule2_setFreqDivision(uint32_t div);
xs_error_code_t TTLOutputModule3_setFreqDivision(uint32_t div);
xs_error_code_t TTLOutputModule4_setFreqDivision(uint32_t div);
xs_error_code_t TTLOutputModule1_getFreqDivision(uint32_t &div);
xs_error_code_t TTLOutputModule2_getFreqDivision(uint32_t &div);
xs_error_code_t TTLOutputModule3_getFreqDivision(uint32_t &div);
xs_error_code_t TTLOutputModule4_getFreqDivision(uint32_t &div);
xs_error_code_t TTLOutputModule1_setFreqMultiplication(uint32_t multi);
xs_error_code_t TTLOutputModule2_setFreqMultiplication(uint32_t multi);
xs_error_code_t TTLOutputModule3_setFreqMultiplication(uint32_t multi);
xs_error_code_t TTLOutputModule4_setFreqMultiplication(uint32_t multi);
xs_error_code_t TTLOutputModule1_getFreqMultiplication(uint32_t &multi);
xs_error_code_t TTLOutputModule2_getFreqMultiplication(uint32_t &multi);
xs_error_code_t TTLOutputModule3_getFreqMultiplication(uint32_t &multi);
xs_error_code_t TTLOutputModule4_getFreqMultiplication(uint32_t &multi);
xs_error_code_t TTLOutputModule1_readInFreq(float &freq);
xs_error_code_t TTLOutputModule2_readInFreq(float &freq);
xs_error_code_t TTLOutputModule3_readInFreq(float &freq);
xs_error_code_t TTLOutputModule4_readInFreq(float &freq);
xs_error_code_t TTLOutputModule1_readOutFreq(float &freq);
xs_error_code_t TTLOutputModule2_readOutFreq(float &freq);
xs_error_code_t TTLOutputModule3_readOutFreq(float &freq);
xs_error_code_t TTLOutputModule4_readOutFreq(float &freq);
/*******************************************************************************
* GENLOCK *
*******************************************************************************/
/**
* @brief 读取Genlock输入模块的频率
*
* @param freq
* @return xs_error_code_t
*/
xs_error_code_t ExternalGenlock_detectFreq(float &freq);
/**
* @brief 内部Genlock信号发生器,设置格式
*
* @param format
* 支持列表:
* GENLOCK_FPS2397
* GENLOCK_FPS2398
* GENLOCK_FPS2400
* GENLOCK_FPS2500
* GENLOCK_FPS2997
* GENLOCK_FPS3000
* GENLOCK_FPS5000
* GENLOCK_FPS5994
* GENLOCK_FPS6000
* @return xs_error_code_t
*/
xs_error_code_t InternalGenlock_setFormat(GenlockFormat_t format);
xs_error_code_t InternalGenlock_getFormat(GenlockFormat_t &format);
/**
* @brief 系统Genlock选择时钟源
*
* @param sig
* 支持列表:
* 0: 内部
* 1: 外部
*
* @return xs_error_code_t
*/
xs_error_code_t SysGenlock_setSrc(uint32_t source);
xs_error_code_t SysGenlock_getSrc(uint32_t &source);
/**
* @brief 读取系统Genlock频率
*
* @param freq
* @return xs_error_code_t
*/
xs_error_code_t SysGenlock_readFreq(float &freq);
/*******************************************************************************
* 内部频率发生器 *
*******************************************************************************/
/**
* @brief 设备内部时钟发生器频率
*
* @param freq 单位为HZ,最小频率变化支持0.1HZ
* @return xs_error_code_t
*/
xs_error_code_t InternalClock_setFreq(float freq);
xs_error_code_t InternalClock_getFreq(float &freq);
/*******************************************************************************
* 系统时钟 *
*******************************************************************************/
/**
* @brief 系统时钟选择时钟源
*
* @param mode
* 支持列表:
* SIGNAL_TTLIN1
* SIGNAL_TTLIN1
* SIGNAL_TTLIN2
* SIGNAL_TTLIN3
* SIGNAL_TTLIN4
* SIGNAL_INTERNAL_CLOCK_SIG
* SIGNAL_SYS_GENLOCK_OUTPUT
*
* @return xs_error_code_t
*/
xs_error_code_t SysClock_setSrc(SignalType_t sig);
xs_error_code_t SysClock_getSrc(SignalType_t &sig);
/**
* @brief 设置触发边沿
*
* @param edge
* TRIGGER_EDGE_RISING
* TRIGGER_EDGE_FALLING
* @return xs_error_code_t
*/
xs_error_code_t SysClock_setTriggerEdge(TriggerEdge_t edge);
xs_error_code_t SysClock_getTriggerEdge(TriggerEdge_t &edge);
/**
* @brief 设置主时钟分频系数
*
* @param div
* @return xs_error_code_t
*/
xs_error_code_t SysClock_setFreqDivision(uint32_t div);
xs_error_code_t SysClock_geFreqtDivision(uint32_t &div);
/**
* @brief 设置主时钟倍频系数
*
* @param muti
* @return xs_error_code_t
*/
xs_error_code_t SysClock_setFreqMultiplication(uint32_t muti);
xs_error_code_t SysClock_getFreqMultiplication(uint32_t &muti);
/**
* @brief 读取主时钟频率
*
* @param freq
* @return xs_error_code_t
*/
xs_error_code_t SysClock_readOutSigFreq(float &freq);
/**
* @brief 读取系统时钟输入频率
*
* @param freq
* @return xs_error_code_t
*/
xs_error_code_t SysClock_readInSigFreq(float &freq);
private:
xs_error_code_t doaction(uint32_t action, uint32_t actionval, uint32_t *ackreturn, int32_t overtime_ms = 100);
xs_error_code_t storageConfig();
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);
xs_error_code_t readtimecode(uint32_t reg0, uint32_t reg1, XsyncTimecode_t &timecode);
xs_error_code_t writetimecode(uint32_t reg0, uint32_t reg1, XsyncTimecode_t timecode);
xs_error_code_t readfreq(uint32_t reg, float &freq);
template <typename T>
xs_error_code_t _reg_read(uint32_t regadd, T &regvalue, int32_t overtime_ms = 100) {
uint32_t regvalue_u32;
xs_error_code_t ret = reg_read(regadd, regvalue_u32, overtime_ms);
if (ret == kxs_ec_success) {
regvalue = (T)regvalue_u32;
}
return ret;
}
void parseTimecodeMsgAndReport(XsyncNetAdd &from, uint8_t *data, size_t length);
void parseCameraSyncMsgAndReport(XsyncNetAdd &from, uint8_t *data, size_t length);
};
} // namespace xsync