linux应用层同时控制TOF和RGB摄像头,为了方便操作,统一接口,这里将TOF和RGB摄像头看成两个对象,对它们分别实现,初始化,去初始化,读取视频流,停止视频流,启动视频流,读取配置文件,保存配置,删除配置,获取参数,设置参数等接口。
1、头文件
/* 对象接口 */
typedef struct {
char device_name[LV_DEVICE_LENGTH];
int context_size;
void *context;
int (*init)(const char *device_name, void *context);
int (*deinit)(void *context);
int (*start_stream)(void *context);
int (*stop_stream)(void *context, int flag);
int (*read_stream)(void *context);
int (*read_config)(void *context);
int (*save_config)(void *context);
int (*delete_config)(void *context);
int (*get_parameter)(void *context, uint16_t command_type, char *data_get, int *data_len);
int (*set_parameter)(void *context, uint16_t command_type, char *data_set, int data_len);
} LV_STREAM_T;
/* 全局数据,包含TOF对象和RGB对象 */
typedef struct {
uint8_t lv_start_flag; /* 启动采集标志位,0:停止采集;1:开始采集 */
uint8_t lv_tof_support; /* TOF设备支持 */
uint8_t lv_rgb_support; /* RGB设备支持 */
char device_name_tof[LV_DEVICE_LENGTH]; /* TOF设备名称 */
char device_name_rgb[LV_DEVICE_LENGTH]; /* RGB设备名称 */
LV_STREAM_T *lv_stream_tof_p; /* TOF设备对象 */
LV_STREAM_T *lv_stream_rgb_p; /* RGB设备对象 */
} LV_DATA_T;
2.对象实现
LV_STREAM_T g_lv_tof_stream = {
"/dev/vide0",
sizeof(LV_IMX_STREAM_T),
NULL,
lv_tof_stream_init,
lv_tof_stream_deinit,
lv_tof_stream_start,
lv_tof_stream_stop,
lv_tof_read_stream,
lv_tof_read_config,
lv_tof_save_config,
lv_tof_delete_config,
lv_tof_get_parameter,
lv_tof_set_parameter,
};
LV_STREAM_T g_lv_rgb_stream = {
"/dev/vide1",
sizeof(LV_RGB_STREAM_T),
NULL,
lv_rgb_stream_init,
lv_rgb_stream_deinit,
lv_rgb_stream_start,
lv_rgb_stream_stop,
lv_rgb_read_stream,
lv_rgb_read_config,
lv_rgb_save_config,
lv_rgb_delete_config,
lv_rgb_get_parameter,
lv_rgb_set_parameter,
};
/* TOF对象接口实现 */
int lv_tof_stream_init(const char *video_device_name, void *context) {
LV_IMX_STREAM_T *lv_imx_stream_p = (LV_IMX_STREAM_T *)context;
return 0;
}
int lv_tof_stream_deinit(void *context) {
LV_IMX_STREAM_T *lv_imx_stream_p = (LV_IMX_STREAM_T *)context;
return 0;
}
int lv_tof_stream_start(void *context) {
LV_IMX_STREAM_T *lv_imx_stream_p = (LV_IMX_STREAM_T *)context;
return 0;
}
int lv_tof_stream_stop(void *context) {
LV_IMX_STREAM_T *lv_imx_stream_p = (LV_IMX_STREAM_T *)context;
return 0;
}
int lv_tof_read_stream(void *context) {
LV_IMX_STREAM_T *lv_imx_stream_p = (LV_IMX_STREAM_T *)context;
return 0;
}
int lv_tof_read_config(void *context) {
LV_IMX_STREAM_T *lv_imx_stream_p = (LV_IMX_STREAM_T *)context;
return 0;
}
int lv_tof_save_config(void *context) {
LV_IMX_STREAM_T *lv_imx_stream_p = (LV_IMX_STREAM_T *)context;
return 0;
}
int lv_tof_delete_config(void *context) {
LV_IMX_STREAM_T *lv_imx_stream_p = (LV_IMX_STREAM_T *)context;
return 0;
}
int lv_tof_get_parameter(void *context, uint16_t command_type, char *data_get, int *data_len) {
LV_IMX_STREAM_T *lv_imx_stream_p = (LV_IMX_STREAM_T *)context;
return 0;
}
int lv_tof_set_parameter(void *context, uint16_t command_type, char *data_set, int data_len) {
LV_IMX_STREAM_T *lv_imx_stream_p = (LV_IMX_STREAM_T *)context;
return 0;
}
3.对象调用
/* 对象列表 */
extern LV_STREAM_T g_lv_tof_stream;
extern LV_STREAM_T g_lv_rgb_stream;
static LV_STREAM_T *lv_stream_list[] = {
&g_lv_tof_stream,
&g_lv_rgb_stream,
};
/* 根据设备名称来找到对应的相机对象
tof : /dev/video0
rgb : /dev/video1
*/
static LV_STREAM_T *lv_find_stream(const char *device_name) {
int i = 0;
LV_STREAM_T *lv_stream_p = NULL;
int list_size = ARRAY_SIZE(lv_stream_list);
for (i = 0; i < list_size; i++) {
if (strncmp(lv_stream_list[i]->device_name, device_name, strlen(lv_stream_list[i]->device_name)) == 0) {
lv_stream_p = lv_stream_list[i];
return lv_stream_p;
}
}
LV_WARN("Not find lv_stream, device_name:%s\n", device_name);
return NULL;
}
/* 创建对象 */
LV_STREAM_T *lv_stream_new(const char *device_name) {
int ret = 0;
LV_STREAM_T *lv_stream_p = NULL;
if (NULL == device_name) {
LV_ERROR("device_name is NULL!\n");
return NULL;
}
lv_stream_p = lv_find_stream(device_name);
if (lv_stream_p) {
if(lv_stream_p->init == NULL) {
LV_ERROR("init is NULL!\n");
return NULL;
}
LV_DEBUG("find stream, device_name=%s, context_size=%d\n", device_name, lv_stream_p->context_size);
lv_stream_p->context = calloc(1, lv_stream_p->context_size);
if (NULL == lv_stream_p->context) {
LV_ERROR("context calloc failed!\n");
}
ret = lv_stream_p->init(device_name, lv_stream_p->context);
if(ret) {
LV_ERROR("stream init failed!\n");
free(lv_stream_p->context);
return NULL;
}
lv_stream_p->read_config(lv_stream_p->context);
} else {
LV_ERROR("find stream failed!\n");
}
return lv_stream_p;
}
/* 释放对象 */
int lv_stream_free(LV_STREAM_T *lv_stream_p) {
if (NULL == lv_stream_p) {
return -1;
}
if (NULL == lv_stream_p->deinit) {
return -2;
}
lv_stream_p->stop_stream(lv_stream_p->context);
if (lv_stream_p->context) {
lv_stream_p->deinit(lv_stream_p->context);
free(lv_stream_p->context);
}
lv_stream_p = NULL;
return 0;
}
/* 读取TOF数据流 */
void *start_read_tof_pthread(void *arg) {
int ret = 0;
LV_DATA_T *lv_data_p = (LV_DATA_T *)arg;
while (1) {
ret = lv_data_p->lv_stream_tof_p->read_stream(lv_data_p->lv_stream_tof_p->context);
if (0 != ret) {
sleep_ms(10);
}
}
lv_stream_free(lv_data_p->lv_stream_tof_p);
return 0;
}