当前位置:   article > 正文

C语言面向对象编程,linux同时控制TOF摄像头和RGB摄像头

C语言面向对象编程,linux同时控制TOF摄像头和RGB摄像头

linux应用层同时控制TOF和RGB摄像头,为了方便操作,统一接口,这里将TOF和RGB摄像头看成两个对象,对它们分别实现,初始化,去初始化,读取视频流,停止视频流,启动视频流,读取配置文件,保存配置,删除配置,获取参数,设置参数等接口。

1、头文件

  1. /* 对象接口 */
  2. typedef struct {
  3. char device_name[LV_DEVICE_LENGTH];
  4. int context_size;
  5. void *context;
  6. int (*init)(const char *device_name, void *context);
  7. int (*deinit)(void *context);
  8. int (*start_stream)(void *context);
  9. int (*stop_stream)(void *context, int flag);
  10. int (*read_stream)(void *context);
  11. int (*read_config)(void *context);
  12. int (*save_config)(void *context);
  13. int (*delete_config)(void *context);
  14. int (*get_parameter)(void *context, uint16_t command_type, char *data_get, int *data_len);
  15. int (*set_parameter)(void *context, uint16_t command_type, char *data_set, int data_len);
  16. } LV_STREAM_T;
  17. /* 全局数据,包含TOF对象和RGB对象 */
  18. typedef struct {
  19. uint8_t lv_start_flag; /* 启动采集标志位,0:停止采集;1:开始采集 */
  20. uint8_t lv_tof_support; /* TOF设备支持 */
  21. uint8_t lv_rgb_support; /* RGB设备支持 */
  22. char device_name_tof[LV_DEVICE_LENGTH]; /* TOF设备名称 */
  23. char device_name_rgb[LV_DEVICE_LENGTH]; /* RGB设备名称 */
  24. LV_STREAM_T *lv_stream_tof_p; /* TOF设备对象 */
  25. LV_STREAM_T *lv_stream_rgb_p; /* RGB设备对象 */
  26. } LV_DATA_T;

2.对象实现

  1. LV_STREAM_T g_lv_tof_stream = {
  2. "/dev/vide0",
  3. sizeof(LV_IMX_STREAM_T),
  4. NULL,
  5. lv_tof_stream_init,
  6. lv_tof_stream_deinit,
  7. lv_tof_stream_start,
  8. lv_tof_stream_stop,
  9. lv_tof_read_stream,
  10. lv_tof_read_config,
  11. lv_tof_save_config,
  12. lv_tof_delete_config,
  13. lv_tof_get_parameter,
  14. lv_tof_set_parameter,
  15. };
  16. LV_STREAM_T g_lv_rgb_stream = {
  17. "/dev/vide1",
  18. sizeof(LV_RGB_STREAM_T),
  19. NULL,
  20. lv_rgb_stream_init,
  21. lv_rgb_stream_deinit,
  22. lv_rgb_stream_start,
  23. lv_rgb_stream_stop,
  24. lv_rgb_read_stream,
  25. lv_rgb_read_config,
  26. lv_rgb_save_config,
  27. lv_rgb_delete_config,
  28. lv_rgb_get_parameter,
  29. lv_rgb_set_parameter,
  30. };
  31. /* TOF对象接口实现 */
  32. int lv_tof_stream_init(const char *video_device_name, void *context) {
  33. LV_IMX_STREAM_T *lv_imx_stream_p = (LV_IMX_STREAM_T *)context;
  34. return 0;
  35. }
  36. int lv_tof_stream_deinit(void *context) {
  37. LV_IMX_STREAM_T *lv_imx_stream_p = (LV_IMX_STREAM_T *)context;
  38. return 0;
  39. }
  40. int lv_tof_stream_start(void *context) {
  41. LV_IMX_STREAM_T *lv_imx_stream_p = (LV_IMX_STREAM_T *)context;
  42. return 0;
  43. }
  44. int lv_tof_stream_stop(void *context) {
  45. LV_IMX_STREAM_T *lv_imx_stream_p = (LV_IMX_STREAM_T *)context;
  46. return 0;
  47. }
  48. int lv_tof_read_stream(void *context) {
  49. LV_IMX_STREAM_T *lv_imx_stream_p = (LV_IMX_STREAM_T *)context;
  50. return 0;
  51. }
  52. int lv_tof_read_config(void *context) {
  53. LV_IMX_STREAM_T *lv_imx_stream_p = (LV_IMX_STREAM_T *)context;
  54. return 0;
  55. }
  56. int lv_tof_save_config(void *context) {
  57. LV_IMX_STREAM_T *lv_imx_stream_p = (LV_IMX_STREAM_T *)context;
  58. return 0;
  59. }
  60. int lv_tof_delete_config(void *context) {
  61. LV_IMX_STREAM_T *lv_imx_stream_p = (LV_IMX_STREAM_T *)context;
  62. return 0;
  63. }
  64. int lv_tof_get_parameter(void *context, uint16_t command_type, char *data_get, int *data_len) {
  65. LV_IMX_STREAM_T *lv_imx_stream_p = (LV_IMX_STREAM_T *)context;
  66. return 0;
  67. }
  68. int lv_tof_set_parameter(void *context, uint16_t command_type, char *data_set, int data_len) {
  69. LV_IMX_STREAM_T *lv_imx_stream_p = (LV_IMX_STREAM_T *)context;
  70. return 0;
  71. }

3.对象调用

  1. /* 对象列表 */
  2. extern LV_STREAM_T g_lv_tof_stream;
  3. extern LV_STREAM_T g_lv_rgb_stream;
  4. static LV_STREAM_T *lv_stream_list[] = {
  5. &g_lv_tof_stream,
  6. &g_lv_rgb_stream,
  7. };
  8. /* 根据设备名称来找到对应的相机对象
  9. tof : /dev/video0
  10. rgb : /dev/video1
  11. */
  12. static LV_STREAM_T *lv_find_stream(const char *device_name) {
  13. int i = 0;
  14. LV_STREAM_T *lv_stream_p = NULL;
  15. int list_size = ARRAY_SIZE(lv_stream_list);
  16. for (i = 0; i < list_size; i++) {
  17. if (strncmp(lv_stream_list[i]->device_name, device_name, strlen(lv_stream_list[i]->device_name)) == 0) {
  18. lv_stream_p = lv_stream_list[i];
  19. return lv_stream_p;
  20. }
  21. }
  22. LV_WARN("Not find lv_stream, device_name:%s\n", device_name);
  23. return NULL;
  24. }
  25. /* 创建对象 */
  26. LV_STREAM_T *lv_stream_new(const char *device_name) {
  27. int ret = 0;
  28. LV_STREAM_T *lv_stream_p = NULL;
  29. if (NULL == device_name) {
  30. LV_ERROR("device_name is NULL!\n");
  31. return NULL;
  32. }
  33. lv_stream_p = lv_find_stream(device_name);
  34. if (lv_stream_p) {
  35. if(lv_stream_p->init == NULL) {
  36. LV_ERROR("init is NULL!\n");
  37. return NULL;
  38. }
  39. LV_DEBUG("find stream, device_name=%s, context_size=%d\n", device_name, lv_stream_p->context_size);
  40. lv_stream_p->context = calloc(1, lv_stream_p->context_size);
  41. if (NULL == lv_stream_p->context) {
  42. LV_ERROR("context calloc failed!\n");
  43. }
  44. ret = lv_stream_p->init(device_name, lv_stream_p->context);
  45. if(ret) {
  46. LV_ERROR("stream init failed!\n");
  47. free(lv_stream_p->context);
  48. return NULL;
  49. }
  50. lv_stream_p->read_config(lv_stream_p->context);
  51. } else {
  52. LV_ERROR("find stream failed!\n");
  53. }
  54. return lv_stream_p;
  55. }
  56. /* 释放对象 */
  57. int lv_stream_free(LV_STREAM_T *lv_stream_p) {
  58. if (NULL == lv_stream_p) {
  59. return -1;
  60. }
  61. if (NULL == lv_stream_p->deinit) {
  62. return -2;
  63. }
  64. lv_stream_p->stop_stream(lv_stream_p->context);
  65. if (lv_stream_p->context) {
  66. lv_stream_p->deinit(lv_stream_p->context);
  67. free(lv_stream_p->context);
  68. }
  69. lv_stream_p = NULL;
  70. return 0;
  71. }
  72. /* 读取TOF数据流 */
  73. void *start_read_tof_pthread(void *arg) {
  74. int ret = 0;
  75. LV_DATA_T *lv_data_p = (LV_DATA_T *)arg;
  76. while (1) {
  77. ret = lv_data_p->lv_stream_tof_p->read_stream(lv_data_p->lv_stream_tof_p->context);
  78. if (0 != ret) {
  79. sleep_ms(10);
  80. }
  81. }
  82. lv_stream_free(lv_data_p->lv_stream_tof_p);
  83. return 0;
  84. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/635125
推荐阅读
相关标签
  

闽ICP备14008679号