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.

890 lines
28 KiB

#include "xsync.hpp"
#include <string.h>
#include <map>
#define ENABLE_LOG
#ifdef ENABLE_LOG
#include "../src/logger.hpp"
#endif
#define TAG "XSYNC"
using namespace xsync;
using namespace std;
/**
* @brief XSYNC协议端口
*/
#define IFLYTOP_XSYNC_SERVICE_XSYNC_PORT 19900 // xsync端端口
#define IFLYTOP_XSYNC_SERVICE_PC_PORT 19901 // pc 端端口
#define IFLYTOP_XSYNC_TIMECODE_REPORT_XSYNC_PORT 19902 // xsync端端口
#define IFLYTOP_XSYNC_TIMECODE_REPORT_PC_PORT 19903 // pc端端口
#define IFLYTOP_XSYNC_CAMERA_SYNC_PACKET_XSYNC_PORT 13013 // xsync端端口
#define IFLYTOP_XSYNC_CAMERA_SYNC_PACKET_PC_PORT 13014 // pc端端口
#define DO_XSYNC(exptr) \
{ \
xs_error_code_t ecode = exptr; \
if (ecode != kxs_ec_success) return ecode; \
}
static uint32_t ipToUint32(const std::string &ipAddress, bool &suc) {
uint32_t result = 0;
std::istringstream iss(ipAddress);
std::string segment;
int i = 0;
while (std::getline(iss, segment, '.')) {
uint32_t octet = std::stoi(segment);
if (octet > 255) {
suc = false;
return 0;
}
result |= (octet << ((3 - i) * 8));
i++;
}
if (i != 4) {
suc = false;
return 0;
}
suc = true;
uint32_t result_n = 0;
result_n |= ((result & 0xff000000) >> 24);
result_n |= ((result & 0x00ff0000) >> 8);
result_n |= ((result & 0x0000ff00) << 8);
result_n |= ((result & 0x000000ff) << 24);
return result_n;
}
namespace xsync {
namespace ttlout_module {
static map<string, TriggerSigType_t> TriggerSigType2StrMap = {
{"logic0", tri_logic0},
{"logic1", tri_logic1},
{"ttlin1_module_ext", tri_ttlin1_module_ext},
{"ttlin1_module_divide", tri_ttlin1_module_divide},
{"ttlin2_module_ext", tri_ttlin2_module_ext},
{"ttlin2_module_divide", tri_ttlin2_module_divide},
{"ttlin3_module_ext", tri_ttlin3_module_ext},
{"ttlin3_module_divide", tri_ttlin3_module_divide},
{"ttlin4_module_ext", tri_ttlin4_module_ext},
{"ttlin4_module_divide", tri_ttlin4_module_divide},
{"internal_en_flag", tri_internal_en_flag},
{"genlock_frame_sync_ext", tri_genlock_frame_sync_ext},
{"genlock_frame_sync_internal", tri_genlock_frame_sync_internal},
{"timecode_frame_sync_ext", tri_timecode_frame_sync_ext},
{"timecode_frame_sync_internal", tri_timecode_frame_sync_internal},
{"timecode_serial_data_ext", tri_timecode_serial_data_ext},
{"timecode_serial_data_internal", tri_timecode_serial_data_internal},
{"internal_100hz", tri_internal_100hz},
};
static map<string, OutputSigType_t> Str2TriggerSigTypeMap = {
{"OutSigType_logic0", OutSigType_logic0}, //
{"OutSigType_logic1", OutSigType_logic1}, //
{"OutSigType_test_signal", OutSigType_test_signal}, //
{"OutSigType_input_signal", OutSigType_input_signal}, //
{"OutSigType_input_signal_mirror", OutSigType_input_signal_mirror}, //
{"OutSigType_trigger_mode_signal", OutSigType_trigger_mode_signal}, //
{"OutSigType_trigger_mode_signal_mirror", OutSigType_trigger_mode_signal_mirror},
};
string TriggerSigType2Str(TriggerSigType_t type) {
for (auto &item : TriggerSigType2StrMap) {
if (item.second == type) return item.first;
}
return "unkown";
}
TriggerSigType_t Str2TriggerSigType(string type) {
auto it = TriggerSigType2StrMap.find(type);
if (it != TriggerSigType2StrMap.end()) {
return it->second;
}
return tri_logic0;
}
string OutputSigType2Str(OutputSigType_t type) {
for (auto &item : Str2TriggerSigTypeMap) {
if (item.second == type) return item.first;
}
return "unkown";
}
OutputSigType_t Str2OutputSigType(string type) {
auto it = Str2TriggerSigTypeMap.find(type);
if (it != Str2TriggerSigTypeMap.end()) {
return it->second;
}
return OutSigType_logic0;
}
list<string> TriggerSigTypeStrSet() {
list<string> ret;
for (auto &item : TriggerSigType2StrMap) {
ret.push_back(item.first);
}
return ret;
}
list<string> OutputSigTypeStrSet() {
list<string> ret;
for (auto &item : Str2TriggerSigTypeMap) {
ret.push_back(item.first);
}
return ret;
}
} // namespace ttlout_module
namespace sig_generator_module {
static map<string, ControlMode_t> Str2ControlModeMap = {
{"manualTrigger", kControlMode_manualTrigger}, //
{"extTimecodeTrigger", kControlMode_externalTimecodeTrigger},
{"ttl1Trigger", kControlMode_externalTTL1Trigger},
{"ttl2Trigger", kControlMode_externalTTL2Trigger},
{"ttl3Trigger", kControlMode_externalTTL3Trigger},
{"ttl4Trigger", kControlMode_externalTTL4Trigger},
};
string ControlMode2Str(ControlMode_t mode) {
for (auto &item : Str2ControlModeMap) {
if (item.second == mode) return item.first;
}
return "unkown";
}
ControlMode_t Str2ControlMode(string mode) {
auto it = Str2ControlModeMap.find(mode);
if (it != Str2ControlModeMap.end()) {
return it->second;
}
return kControlMode_manualTrigger;
}
list<string> ControlModeStrSet() {
list<string> ret;
for (auto &item : Str2ControlModeMap) {
ret.push_back(item.first);
}
return ret;
}
} // namespace sig_generator_module
static map<string, TimecodeFormat_t> Str2TimecodeFormatMap = {
{"fps2398", TIMECODE_FPS2398}, //
{"fps2400", TIMECODE_FPS2400}, //
{"fps2500", TIMECODE_FPS2500}, //
{"fps2997", TIMECODE_FPS2997}, //
{"fps2997Drop", TIMECODE_FPS2997Drop}, {"fps3000", TIMECODE_FPS3000}, //
};
static map<string, GenlockFormat_t> Str2GenlockFormatMap = {
{"fps2397", GENLOCK_FPS2397}, //
{"fps2398", GENLOCK_FPS2398}, //
{"fps2400", GENLOCK_FPS2400}, //
{"fps2500", GENLOCK_FPS2500}, //
{"fps2997", GENLOCK_FPS2997}, //
{"fps3000", GENLOCK_FPS3000}, //
{"fps5000", GENLOCK_FPS5000}, //
{"fps5994", GENLOCK_FPS5994}, //
{"fps6000", GENLOCK_FPS6000}, //
};
string GenlockFormatToStr(GenlockFormat_t fomrat) {
for (auto &item : Str2GenlockFormatMap) {
if (item.second == fomrat) return item.first;
}
return "unkown";
}
string TimecodeFormatToStr(TimecodeFormat_t fomrat) {
for (auto &item : Str2TimecodeFormatMap) {
if (item.second == fomrat) return item.first;
}
return "unkown";
}
GenlockFormat_t Str2GenlockFormat(string format) {
auto it = Str2GenlockFormatMap.find(format);
if (it != Str2GenlockFormatMap.end()) {
return it->second;
}
return GENLOCK_FPS2397;
}
TimecodeFormat_t Str2TimecodeFormat(string format) {
auto it = Str2TimecodeFormatMap.find(format);
if (it != Str2TimecodeFormatMap.end()) {
return it->second;
}
return TIMECODE_FPS2398;
}
list<string> GenlockFormatStrSet() {
list<string> ret;
for (auto &item : Str2GenlockFormatMap) {
ret.push_back(item.first);
}
return ret;
}
list<string> TimecodeFormatStrSet() {
list<string> ret;
for (auto &item : Str2TimecodeFormatMap) {
ret.push_back(item.first);
}
return ret;
}
string XsyncTimecodeToStr(XsyncTimecode_t timecode) {
char buf[32] = {0};
sprintf(buf, "%02d:%02d:%02d:%02d", timecode.hour, timecode.minute, timecode.second, timecode.frame);
return string(buf);
}
XsyncTimecode_t Str2XsyncTimecode(string timecode) {
XsyncTimecode_t ret;
char buf[128] = {0};
strncpy(buf, timecode.c_str(), 127);
sscanf(buf, "%02d:%02d:%02d:%02d", &ret.hour, &ret.minute, &ret.second, &ret.frame);
return ret;
}
} // namespace xsync
static XsyncTimecode_t timecode64ToXsyncTimeCode(Timecode64_t tc64) {
uint8_t frameuints = tc64.tc0 & 0x0f;
uint8_t frame10s = (tc64.tc0 >> 8) & 0x3;
uint8_t seconduints = (tc64.tc0 >> 16) & 0x0f;
uint8_t second10s = (tc64.tc0 >> 24) & 0x07;
uint8_t minuteuints = tc64.tc1 & 0x0f;
uint8_t minute10s = (tc64.tc1 >> 8) & 0x07;
uint8_t houruints = (tc64.tc1 >> 16) & 0x0f;
uint8_t hour10s = (tc64.tc1 >> 24) & 0x03;
XsyncTimecode_t timecode;
timecode.hour = hour10s * 10 + houruints;
timecode.minute = minute10s * 10 + minuteuints;
timecode.second = second10s * 10 + seconduints;
timecode.frame = frame10s * 10 + frameuints;
return timecode;
}
static Timecode64_t timecodeTo64(XsyncTimecode_t tc) {
Timecode64_t tc64;
uint32_t frameuints = tc.frame % 10;
uint32_t frame10s = tc.frame / 10;
uint32_t seconduints = tc.second % 10;
uint32_t second10s = tc.second / 10;
uint32_t minuteuints = tc.minute % 10;
uint32_t minute10s = tc.minute / 10;
uint32_t houruints = tc.hour % 10;
uint32_t hour10s = tc.hour / 10;
tc64.tc0 = frameuints + (frame10s << 8) + (seconduints << 16) + (second10s << 24);
tc64.tc1 = minuteuints + (minute10s << 8) + (houruints << 16) + (hour10s << 24);
return tc64;
}
/*******************************************************************************
* Xsync *
*******************************************************************************/
Xsync::Xsync(/* args */) {}
Xsync &Xsync::Ins() {
static Xsync xsync;
return xsync;
}
void Xsync::initialize(I_XSUDPFactory *xsync_udp_factory) { m_xsync_udp_factory = xsync_udp_factory; }
xs_error_code_t Xsync::connect(string xsync_ip) {
lock_guard<recursive_mutex> lock(lock_);
m_xsync_ip = xsync_ip;
disConnect();
/**
* @brief 创建 m_xsync_reg_udp
*/
xs_error_code_t ecode = kxs_ec_success;
auto xsync_reg_udp = m_xsync_udp_factory->createXSUDP();
ecode = xsync_reg_udp->initialize("0.0.0.0", IFLYTOP_XSYNC_SERVICE_PC_PORT);
if (ecode != kxs_ec_success) {
return ecode;
}
/**
* @brief 创建 m_xsync_timecode_udp_listener
*/
auto xsync_timecode_udp_listener = m_xsync_udp_factory->createXSUDP();
ecode = xsync_timecode_udp_listener->initialize("0.0.0.0", IFLYTOP_XSYNC_TIMECODE_REPORT_PC_PORT);
if (ecode != kxs_ec_success) {
return ecode;
}
ecode = xsync_timecode_udp_listener->startReceive([this](XsyncNetAdd &from, uint8_t *data, size_t length) { parseTimecodeMsgAndReport(from, data, length); });
if (ecode != kxs_ec_success) {
return ecode;
}
/**
* @brief 创建 m_xsync_camera_sync_udp_listener
*/
auto xsync_camera_sync_udp_listener = m_xsync_udp_factory->createXSUDP();
ecode = xsync_camera_sync_udp_listener->initialize("0.0.0.0", IFLYTOP_XSYNC_CAMERA_SYNC_PACKET_PC_PORT);
if (ecode != kxs_ec_success) {
return ecode;
}
ecode = xsync_camera_sync_udp_listener->startReceive([this](XsyncNetAdd &from, uint8_t *data, size_t length) { parseCameraSyncMsgAndReport(from, data, length); });
if (ecode != kxs_ec_success) {
return ecode;
}
m_xsync_reg_udp = xsync_reg_udp;
m_xsync_timecode_udp_listener = xsync_timecode_udp_listener;
m_xsync_camera_sync_udp_listener = xsync_camera_sync_udp_listener;
m_net_state = kxsync_net_state_connected;
return ecode;
}
xs_error_code_t Xsync::disConnect() {
lock_guard<recursive_mutex> lock(lock_);
if (m_xsync_reg_udp != nullptr) {
m_xsync_reg_udp->stopReceive();
m_xsync_reg_udp = nullptr;
}
if (m_xsync_timecode_udp_listener != nullptr) {
m_xsync_timecode_udp_listener->stopReceive();
m_xsync_timecode_udp_listener = nullptr;
}
if (m_xsync_camera_sync_udp_listener != nullptr) {
m_xsync_camera_sync_udp_listener->stopReceive();
m_xsync_camera_sync_udp_listener = nullptr;
}
m_net_state = kxsync_net_state_disconnect;
return kxs_ec_success;
}
xsync_net_state_t Xsync::getNetState() { return m_net_state; }
void Xsync::Basic_registerOnTimecodeMsgCallback(xsync_on_timecode_msg_t on_timecode_msg_cb) { m_on_timecode_msg_cb = on_timecode_msg_cb; }
void Xsync::Basic_registerOnCameraSyncMsgCallback(xsync_on_camera_sync_msg_t on_camera_sync_msg_cb) { m_on_camera_sync_msg_cb = on_camera_sync_msg_cb; }
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) {
lock_guard<recursive_mutex> lock(lock_);
if (!m_xsync_reg_udp) return kxs_ec_lose_connect;
m_xsync_reg_udp->clearRxBuffer();
cmd->index = txpacket_index++;
XsyncNetAdd toadd = {m_xsync_ip, IFLYTOP_XSYNC_SERVICE_XSYNC_PORT};
xs_error_code_t ecode = //
m_xsync_reg_udp->sendto(toadd, (const char *)cmd, sizeof(iflytop_xsync_packet_header_t) + cmd->ndata * 4, nullptr);
if (ecode != kxs_ec_success) {
return ecode;
}
XsyncNetAdd fromadd;
while (true) {
// ZLOGI(TAG, "start rx wait for rxdata");
ecode = m_xsync_reg_udp->receive((char *)rx_data, buffersize, fromadd, overtime_ms);
// ZLOGI(TAG, "end rx wait for rxdata");
if (ecode != kxs_ec_success) {
return ecode;
}
if (rx_data->index != cmd->index) {
// ZLOGI(TAG, "packet index error %d %d", cmd->index, rx_data->index);
continue;
}
break;
}
return (xs_error_code_t)rx_data->data[0];
}
xs_error_code_t Xsync::reg_write(uint32_t regadd, uint32_t regvalue, uint32_t &regbackvalue, int32_t overtime_ms) {
/**
* @brief
* 协议说明
*
* kxsync_packet_type_reg_write
* tx: regadd,regdata
* rx: ecode,regdata
*/
uint8_t txdata[128] = {0};
uint8_t rxdata[128] = {0};
iflytop_xsync_packet_header_t *txpacket = (iflytop_xsync_packet_header_t *)txdata;
iflytop_xsync_packet_header_t *rxpacket = (iflytop_xsync_packet_header_t *)rxdata;
txpacket->type = kxsync_packet_type_cmd;
txpacket->index = txpacket_index++;
txpacket->cmd = kxsync_packet_type_reg_write;
txpacket->ndata = 2;
txpacket->data[0] = regadd;
txpacket->data[1] = regvalue;
auto ecode = xsync_send_cmd_block(txpacket, rxpacket, sizeof(rxdata), overtime_ms);
if (ecode != kxs_ec_success) {
return ecode;
}
regbackvalue = rxpacket->data[1];
return ecode;
}
xs_error_code_t Xsync::reg_read(uint32_t regadd, uint32_t &regvalue, int32_t overtime_ms) {
/**
* @brief
* 协议说明
*
* kxsync_packet_type_reg_write
* tx: regadd,regdata
* rx: ecode,regdata
*/
uint8_t txdata[128] = {0};
uint8_t rxdata[128] = {0};
iflytop_xsync_packet_header_t *txpacket = (iflytop_xsync_packet_header_t *)txdata;
iflytop_xsync_packet_header_t *rxpacket = (iflytop_xsync_packet_header_t *)rxdata;
txpacket->type = kxsync_packet_type_cmd;
txpacket->index = txpacket_index++;
txpacket->cmd = kxsync_packet_type_reg_read;
txpacket->ndata = 2;
txpacket->data[0] = regadd;
txpacket->data[1] = regvalue;
auto ecode = xsync_send_cmd_block(txpacket, rxpacket, sizeof(rxdata), overtime_ms);
if (ecode != kxs_ec_success) {
return ecode;
}
regvalue = rxpacket->data[1];
return ecode;
}
xs_error_code_t Xsync::reg_read_muti(uint32_t regadd, uint32_t nreg, vector<uint32_t> &regvalues, int32_t overtime_ms) {
/**
* @brief
* 协议说明
*
* kxsync_packet_type_reg_read_regs
* tx: regstartadd,nreg
* rx: ecode,regdatas
*/
uint8_t txdata[128] = {0};
uint8_t rxdata[1280] = {0};
iflytop_xsync_packet_header_t *txpacket = (iflytop_xsync_packet_header_t *)txdata;
iflytop_xsync_packet_header_t *rxpacket = (iflytop_xsync_packet_header_t *)rxdata;
txpacket->type = kxsync_packet_type_cmd;
txpacket->index = txpacket_index++;
txpacket->cmd = kxsync_packet_type_reg_read_regs;
txpacket->ndata = 2;
txpacket->data[0] = regadd;
txpacket->data[1] = nreg;
auto ecode = xsync_send_cmd_block(txpacket, rxpacket, sizeof(rxdata), overtime_ms);
if (ecode != kxs_ec_success) {
return ecode;
}
if (rxpacket->ndata > 0) {
for (int i = 0; i < rxpacket->ndata - 1; i++) {
regvalues.push_back(rxpacket->data[i + 1]);
}
}
return ecode;
}
void Xsync::parseTimecodeMsgAndReport(XsyncNetAdd &from, uint8_t *data, size_t length) {
//
iflytop_timecode_report_packet_t *packet = (iflytop_timecode_report_packet_t *)data;
Timecode64_t tc64;
tc64.tc0 = packet->timecode0;
tc64.tc1 = packet->timecode1;
XsyncTimecode_t timecode = timecode64ToXsyncTimeCode(tc64);
if (m_on_timecode_msg_cb) m_on_timecode_msg_cb(&timecode);
}
void Xsync::parseCameraSyncMsgAndReport(XsyncNetAdd &from, uint8_t *data, size_t length) {
uint32_t count = 0;
uint32_t data0 = data[7];
uint32_t data1 = data[6];
uint32_t data2 = data[5];
uint32_t data3 = data[4];
count = data0 + (data1 << 8) + (data2 << 16) + (data3 << 24);
xysnc_camera_sync_data_t camera_sync_data;
camera_sync_data.frameIndex = count;
if (m_on_camera_sync_msg_cb) m_on_camera_sync_msg_cb(&camera_sync_data);
}
xs_error_code_t Xsync::Basic_generatorNewMac() { return doaction(xsync_stm32_action_generator_new_mac, 0, nullptr, 2000); }
xs_error_code_t Xsync::Basic_factoryReset() { return doaction(xsync_stm32_action_factory_reset, 0, nullptr, 1000); }
xs_error_code_t Xsync::Basic_reboot() { return doaction(xsync_stm32_action_Basic_reboot, 0, nullptr); }
xs_error_code_t Xsync::storageConfig() { return doaction(xsync_stm32_action_storage_cfg, 0, nullptr, 1000); }
xs_error_code_t Xsync::Basic_changeNetworkConfig(string ip, string mask, string gateway) {
uint32_t ip32 = 0;
uint32_t mask32 = 0;
uint32_t gateway32 = 0;
xs_error_code_t ecode;
bool suc = false;
ip32 = (uint32_t)ipToUint32(ip.c_str(), suc);
if (!suc) return kxs_ec_param_error;
mask32 = (uint32_t)ipToUint32(mask.c_str(), suc);
if (!suc) return kxs_ec_param_error;
gateway32 = (uint32_t)ipToUint32(gateway.c_str(), suc);
if (!suc) return kxs_ec_param_error;
uint32_t readbak = 0;
ecode = reg_write(reg::kstm32_ip, ip32, readbak);
if (ecode != kxs_ec_success) return ecode;
ecode = reg_write(reg::kstm32_netmask, mask32, readbak);
if (ecode != kxs_ec_success) return ecode;
ecode = reg_write(reg::kstm32_gw, gateway32, readbak);
if (ecode != kxs_ec_success) return ecode;
ecode = storageConfig();
if (ecode != kxs_ec_success) return ecode;
return kxs_ec_success;
}
xs_error_code_t Xsync::Basic_clearXsyncCameraSyncIndexCount() {
uint32_t readbak = 0;
return reg_write(reg::kstm32_camera_sync_signal_count, 0, readbak);
}
xs_error_code_t Xsync::doaction(uint32_t action, uint32_t actionval, uint32_t *ackreturn, int32_t overtime_ms) {
//
uint32_t readbak = 0;
xs_error_code_t ecode;
ecode = reg_write(reg::kstm32_action_val0, actionval, readbak);
if (ecode != kxs_ec_success) return ecode;
ecode = reg_write(reg::kstm32_action0, action, readbak, overtime_ms);
if (ecode != kxs_ec_success) return ecode;
if (ackreturn) *ackreturn = readbak;
return ecode;
}
xs_error_code_t Xsync::Basic_setGenlockFormat(GenlockFormat_t format) {
DO_XSYNC(SigGenerator_setGenlockFormat(format));
return kxs_ec_success;
}
xs_error_code_t Xsync::Basic_getGenlockFormat(GenlockFormat_t &format) {
DO_XSYNC(SigGenerator_getGenlockFormat(format));
return kxs_ec_success;
}
xs_error_code_t Xsync::Basic_setTimecodeFormat(TimecodeFormat_t format) {
DO_XSYNC(SigGenerator_setTimecodeFormat(format));
return kxs_ec_success;
}
xs_error_code_t Xsync::Basic_getTimecodeFormat(TimecodeFormat_t &format) {
DO_XSYNC(SigGenerator_getTimecodeFormat(format));
return kxs_ec_success;
}
/*******************************************************************************
* TTLOutputModule *
*******************************************************************************/
xs_error_code_t Xsync::TTLOutputModule_setInputSigType(int32_t index, ttlout_module::TriggerSigType_t source) {
xs_error_code_t ecode = kxs_ec_success;
uint32_t readbak = 0;
uint32_t regAdd = 0;
if (index == 1) {
regAdd = reg::kttlout1_input_sig_slt;
} else if (index == 2) {
regAdd = reg::kttlout2_input_sig_slt;
} else if (index == 3) {
regAdd = reg::kttlout3_input_sig_slt;
} else if (index == 4) {
regAdd = reg::kttlout4_input_sig_slt;
} else {
return kxs_ec_param_error;
}
return reg_write(regAdd, source, readbak, 10);
}
xs_error_code_t Xsync::TTLOutputModule_getInputSigType(int32_t index, ttlout_module::TriggerSigType_t &source) {
xs_error_code_t ecode = kxs_ec_success;
uint32_t readbak = 0;
uint32_t regAdd = 0;
if (index == 1) {
regAdd = reg::kttlout1_input_sig_slt;
} else if (index == 2) {
regAdd = reg::kttlout2_input_sig_slt;
} else if (index == 3) {
regAdd = reg::kttlout3_input_sig_slt;
} else if (index == 4) {
regAdd = reg::kttlout4_input_sig_slt;
} else {
return kxs_ec_param_error;
}
ecode = reg_read(regAdd, readbak, 10);
if (ecode != kxs_ec_success) return ecode;
source = (ttlout_module::TriggerSigType_t)readbak;
return ecode;
}
xs_error_code_t Xsync::TTLOutputModule_setOutputSigType(int32_t index, ttlout_module::OutputSigType_t output_type) {
xs_error_code_t ecode = kxs_ec_success;
uint32_t readbak = 0;
uint32_t regAdd = 0;
if (index == 1) {
regAdd = reg::kttlout1_output_sig_slt;
} else if (index == 2) {
regAdd = reg::kttlout2_output_sig_slt;
} else if (index == 3) {
regAdd = reg::kttlout3_output_sig_slt;
} else if (index == 4) {
regAdd = reg::kttlout4_output_sig_slt;
} else {
return kxs_ec_param_error;
}
return reg_write(regAdd, output_type, readbak, 10);
}
xs_error_code_t Xsync::TTLOutputModule_getOutputSigType(int32_t index, ttlout_module::OutputSigType_t &output_type) {
xs_error_code_t ecode = kxs_ec_success;
uint32_t readbak = 0;
uint32_t regAdd = 0;
if (index == 1) {
regAdd = reg::kttlout1_output_sig_slt;
} else if (index == 2) {
regAdd = reg::kttlout2_output_sig_slt;
} else if (index == 3) {
regAdd = reg::kttlout3_output_sig_slt;
} else if (index == 4) {
regAdd = reg::kttlout4_output_sig_slt;
} else {
return kxs_ec_param_error;
}
ecode = reg_read(regAdd, readbak, 10);
if (ecode != kxs_ec_success) return ecode;
output_type = (ttlout_module::OutputSigType_t)readbak;
return ecode;
}
xs_error_code_t Xsync::TTLOutputModule_setTriggerModePulseWidth(int32_t index, uint32_t pulse_width_ms) {
xs_error_code_t ecode = kxs_ec_success;
uint32_t readbak = 0;
uint32_t regAdd = 0;
if (index == 1) {
regAdd = reg::kttlout1_pulse_mode_duration;
} else if (index == 2) {
regAdd = reg::kttlout2_pulse_mode_duration;
} else if (index == 3) {
regAdd = reg::kttlout3_pulse_mode_duration;
} else if (index == 4) {
regAdd = reg::kttlout4_pulse_mode_duration;
} else {
return kxs_ec_param_error;
}
uint32_t pluse_width = pulse_width_ms * 1000; // 1MHZ计数
return reg_write(regAdd, pluse_width, readbak, 10);
}
xs_error_code_t Xsync::TTLOutputModule_getTriggerModePulseWidth(int32_t index, uint32_t &pulse_width_ms) {
xs_error_code_t ecode = kxs_ec_success;
uint32_t regAdd = 0;
uint32_t readbak = 0;
if (index == 1) {
regAdd = reg::kttlout1_pulse_mode_duration;
} else if (index == 2) {
regAdd = reg::kttlout2_pulse_mode_duration;
} else if (index == 3) {
regAdd = reg::kttlout3_pulse_mode_duration;
} else if (index == 4) {
regAdd = reg::kttlout4_pulse_mode_duration;
} else {
return kxs_ec_param_error;
}
ecode = reg_read(regAdd, regAdd, 10);
if (ecode != kxs_ec_success) return ecode;
pulse_width_ms = readbak / 1000;
return ecode;
}
xs_error_code_t Xsync::TTLOutputModule_setTriggerModePulseDelay(int32_t index, uint32_t pulse_delay_ms) {
xs_error_code_t ecode = kxs_ec_success;
uint32_t regAdd = 0;
uint32_t readbak = 0;
if (index == 1) {
regAdd = reg::kttlout1_pulse_mode_delay;
} else if (index == 2) {
regAdd = reg::kttlout2_pulse_mode_delay;
} else if (index == 3) {
regAdd = reg::kttlout3_pulse_mode_delay;
} else if (index == 4) {
regAdd = reg::kttlout4_pulse_mode_delay;
} else {
return kxs_ec_param_error;
}
uint32_t pulse_delay = pulse_delay_ms * 1000; // 1MHZ计数
return reg_write(regAdd, pulse_delay, readbak, 10);
}
xs_error_code_t Xsync::TTLOutputModule_getTriggerModePulseDelay(int32_t index, uint32_t &pulse_delay_ms) {
xs_error_code_t ecode = kxs_ec_success;
uint32_t regAdd = 0;
uint32_t readbak = 0;
if (index == 1) {
regAdd = reg::kttlout1_pulse_mode_delay;
} else if (index == 2) {
regAdd = reg::kttlout2_pulse_mode_delay;
} else if (index == 3) {
regAdd = reg::kttlout3_pulse_mode_delay;
} else if (index == 4) {
regAdd = reg::kttlout4_pulse_mode_delay;
} else {
return kxs_ec_param_error;
}
ecode = reg_read(regAdd, readbak, 10);
if (ecode != kxs_ec_success) return ecode;
pulse_delay_ms = readbak / 1000;
return ecode;
}
xs_error_code_t Xsync::SigGenerator_setControlMode(sig_generator_module::ControlMode_t mode) {
uint32_t readbak = 0;
return reg_write(reg::kSigGenerator_ctl, mode, readbak, 10);
}
xs_error_code_t Xsync::SigGenerator_getControlMode(sig_generator_module::ControlMode_t &mode) {
uint32_t readbak = 0;
auto ecode = reg_read(reg::kSigGenerator_ctl, readbak, 10);
if (ecode != kxs_ec_success) return ecode;
mode = (sig_generator_module::ControlMode_t)readbak;
return ecode;
}
xs_error_code_t Xsync::SigGenerator_manualStart() {
uint32_t readbak = 0;
return reg_write(reg::kSigGenerator_control_trigger_reg, 1, readbak, 10);
}
xs_error_code_t Xsync::SigGenerator_manualStop() {
uint32_t readbak = 0;
return reg_write(reg::kSigGenerator_control_trigger_reg, 0, readbak, 10);
}
xs_error_code_t Xsync::SigGenerator_setTimecode(XsyncTimecode_t timecode) {
uint32_t readbak = 0;
xs_error_code_t ecode = kxs_ec_success;
Timecode64_t tc64 = timecodeTo64(timecode);
ecode = reg_write(reg::kSigGenerator_timecode0, tc64.tc0, readbak, 10);
if (ecode != kxs_ec_success) return ecode;
ecode = reg_write(reg::kSigGenerator_timecode1, tc64.tc1, readbak, 10);
if (ecode != kxs_ec_success) return ecode;
return ecode;
}
xs_error_code_t Xsync::SigGenerator_getTimecode(XsyncTimecode_t &timecode) {
uint32_t readbak = 0;
xs_error_code_t ecode = kxs_ec_success;
uint32_t tc0 = 0;
uint32_t tc1 = 0;
ecode = reg_read(reg::kSigGenerator_timecode0, tc0, 10);
if (ecode != kxs_ec_success) return ecode;
ecode = reg_read(reg::kSigGenerator_timecode1, tc1, 10);
if (ecode != kxs_ec_success) return ecode;
Timecode64_t tc64;
tc64.tc0 = tc0;
tc64.tc1 = tc1;
timecode = timecode64ToXsyncTimeCode(tc64);
return ecode;
}
xs_error_code_t Xsync::SigGenerator_setAutoStartTimecode(XsyncTimecode_t timecode) {
uint32_t readbak = 0;
xs_error_code_t ecode = kxs_ec_success;
Timecode64_t tc64 = timecodeTo64(timecode);
ecode = reg_write(reg::kSigGenerator_timecode_start0, tc64.tc0, readbak, 10);
if (ecode != kxs_ec_success) return ecode;
ecode = reg_write(reg::kSigGenerator_timecode_start1, tc64.tc1, readbak, 10);
if (ecode != kxs_ec_success) return ecode;
return ecode;
}
xs_error_code_t Xsync::SigGenerator_getAutoStartTimecode(XsyncTimecode_t &timecode) {
uint32_t readbak = 0;
xs_error_code_t ecode = kxs_ec_success;
uint32_t tc0 = 0;
uint32_t tc1 = 0;
ecode = reg_read(reg::kSigGenerator_timecode_start0, tc0, 10);
if (ecode != kxs_ec_success) return ecode;
ecode = reg_read(reg::kSigGenerator_timecode_start1, tc1, 10);
if (ecode != kxs_ec_success) return ecode;
Timecode64_t tc64;
tc64.tc0 = tc0;
tc64.tc1 = tc1;
timecode = timecode64ToXsyncTimeCode(tc64);
return ecode;
}
xs_error_code_t Xsync::SigGenerator_getWorkState(uint32_t &work_state) {
uint32_t readbak = 0;
auto ecode = reg_read(reg::kSigGenerator_work_state, readbak, 10);
if (ecode != kxs_ec_success) return ecode;
work_state = readbak;
return ecode;
}
xs_error_code_t Xsync::SigGenerator_setGenlockFormat(GenlockFormat_t format) {
uint32_t readbak = 0;
return reg_write(reg::kSigGenerator_genlock_format, format, readbak, 10);
}
xs_error_code_t Xsync::SigGenerator_getGenlockFormat(GenlockFormat_t &format) {
uint32_t readbak = 0;
auto ecode = reg_read(reg::kSigGenerator_genlock_format, readbak, 10);
if (ecode != kxs_ec_success) return ecode;
format = (GenlockFormat_t)readbak;
return ecode;
}
xs_error_code_t Xsync::SigGenerator_setTimecodeFormat(TimecodeFormat_t format) {
uint32_t readbak = 0;
return reg_write(reg::kSigGenerator_timecode_format, format, readbak, 10);
}
xs_error_code_t Xsync::SigGenerator_getTimecodeFormat(TimecodeFormat_t &format) {
uint32_t readbak = 0;
auto ecode = reg_read(reg::kSigGenerator_timecode_format, readbak, 10);
if (ecode != kxs_ec_success) return ecode;
format = (TimecodeFormat_t)readbak;
return ecode;
}