main.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #include "../common/common.hpp"
  2. void eventCallback(TY_EVENT_INFO *event_info, void *userdata)
  3. {
  4. if (event_info->eventId == TY_EVENT_DEVICE_OFFLINE) {
  5. LOGD("=== Event Callback: Device Offline!");
  6. // Note:
  7. // Please set TY_BOOL_KEEP_ALIVE_ONOFF feature to false if you need to debug with breakpoint!
  8. }
  9. else if (event_info->eventId == TY_EVENT_LICENSE_ERROR) {
  10. LOGD("=== Event Callback: License Error!");
  11. }
  12. }
  13. int main(int argc, char* argv[])
  14. {
  15. std::string ID, IP;
  16. TY_INTERFACE_HANDLE hIface = NULL;
  17. TY_DEV_HANDLE hDevice;
  18. for(int i = 1; i < argc; i++) {
  19. if(strcmp(argv[i], "-id") == 0){
  20. ID = argv[++i];
  21. } else if(strcmp(argv[i], "-ip") == 0) {
  22. IP = argv[++i];
  23. } else if(strcmp(argv[i], "-h") == 0) {
  24. LOGI("Usage: %s [-h]", argv[0]);
  25. return 0;
  26. }
  27. }
  28. LOGD("=== Init lib");
  29. ASSERT_OK( TYInitLib() );
  30. TY_VERSION_INFO ver;
  31. ASSERT_OK( TYLibVersion(&ver) );
  32. LOGD(" - lib version: %d.%d.%d", ver.major, ver.minor, ver.patch);
  33. std::vector<TY_DEVICE_BASE_INFO> selected;
  34. ASSERT_OK( selectDevice(TY_INTERFACE_ALL, ID, IP, 1, selected) );
  35. ASSERT(selected.size() > 0);
  36. TY_DEVICE_BASE_INFO& selectedDev = selected[0];
  37. ASSERT_OK( TYOpenInterface(selectedDev.iface.id, &hIface) );
  38. ASSERT_OK( TYOpenDevice(hIface, selectedDev.id, &hDevice) );
  39. TY_COMPONENT_ID allComps, componentIDs = 0;
  40. ASSERT_OK( TYGetComponentIDs(hDevice, &allComps) );
  41. if(allComps & TY_COMPONENT_BRIGHT_HISTO) {
  42. LOGD("=== Has bright histo component");
  43. componentIDs |= TY_COMPONENT_BRIGHT_HISTO;
  44. }
  45. LOGD("=== Configure components, open depth cam");
  46. componentIDs |= TY_COMPONENT_IR_CAM_RIGHT| TY_COMPONENT_IR_CAM_LEFT;
  47. ASSERT_OK( TYEnableComponents(hDevice, componentIDs) );
  48. //try to enable depth map
  49. LOGD("Configure components, open depth cam");
  50. if (allComps & TY_COMPONENT_DEPTH_CAM) {
  51. TY_IMAGE_MODE image_mode;
  52. ASSERT_OK(get_default_image_mode(hDevice, TY_COMPONENT_DEPTH_CAM, image_mode));
  53. LOGD("Select Depth Image Mode: %dx%d", TYImageWidth(image_mode), TYImageHeight(image_mode));
  54. ASSERT_OK(TYSetEnum(hDevice, TY_COMPONENT_DEPTH_CAM, TY_ENUM_IMAGE_MODE, image_mode));
  55. ASSERT_OK(TYEnableComponents(hDevice, TY_COMPONENT_DEPTH_CAM));
  56. }
  57. LOGD("=== Prepare image buffer");
  58. uint32_t frameSize;
  59. ASSERT_OK( TYGetFrameBufferSize(hDevice, &frameSize) );
  60. LOGD(" - Get size of framebuffer, %d", frameSize);
  61. LOGD(" - Allocate & enqueue buffers");
  62. char* frameBuffer[2];
  63. frameBuffer[0] = new char[frameSize];
  64. frameBuffer[1] = new char[frameSize];
  65. LOGD(" - Enqueue buffer (%p, %d)", frameBuffer[0], frameSize);
  66. ASSERT_OK( TYEnqueueBuffer(hDevice, frameBuffer[0], frameSize) );
  67. LOGD(" - Enqueue buffer (%p, %d)", frameBuffer[1], frameSize);
  68. ASSERT_OK( TYEnqueueBuffer(hDevice, frameBuffer[1], frameSize) );
  69. LOGD("=== Register event callback");
  70. ASSERT_OK(TYRegisterEventCallback(hDevice, eventCallback, NULL));
  71. LOGD("=== Disable trigger mode");
  72. TY_TRIGGER_PARAM trigger;
  73. trigger.mode = TY_TRIGGER_MODE_OFF;
  74. ASSERT_OK(TYSetStruct(hDevice, TY_COMPONENT_DEVICE, TY_STRUCT_TRIGGER_PARAM, &trigger, sizeof(trigger)));
  75. LOGD("=== Start capture");
  76. ASSERT_OK( TYStartCapture(hDevice) );
  77. LOGD("=== While loop to fetch frame");
  78. bool exit_main = false;
  79. TY_FRAME_DATA frame;
  80. int index = 0;
  81. while(!exit_main) {
  82. int err = TYFetchFrame(hDevice, &frame, -1);
  83. if( err == TY_STATUS_OK ) {
  84. LOGD("=== Get frame %d", ++index);
  85. int fps = get_fps();
  86. if (fps > 0){
  87. LOGI("fps: %d", fps);
  88. }
  89. for( int i = 0; i < frame.validCount; i++ ){
  90. if(frame.image[i].componentID == TY_COMPONENT_BRIGHT_HISTO) {
  91. int32_t *ir_left_his, *ir_right_his;
  92. ir_left_his = (int32_t *)frame.image[i].buffer;
  93. ir_right_his = (int32_t *)frame.image[i].buffer + 256;
  94. for(int i=0; i<256; i++) {
  95. LOGI("ir_left_his[%d] = %u, ir_right_his[%d] = %u"
  96. , i, ir_left_his[i], i, ir_right_his[i]);
  97. }
  98. }
  99. if(frame.image[i].componentID == TY_COMPONENT_IR_CAM_LEFT) {
  100. cv::Mat irl(frame.image[i].height, frame.image[i].width, CV_8U, frame.image[i].buffer);
  101. imshow("irl", irl);
  102. }
  103. if(frame.image[i].componentID == TY_COMPONENT_IR_CAM_RIGHT) {
  104. cv::Mat irr(frame.image[i].height, frame.image[i].width, CV_8U, frame.image[i].buffer);
  105. imshow("irr", irr);
  106. }
  107. }
  108. int key = cv::waitKey(1);
  109. switch(key & 0xff) {
  110. case 0xff:
  111. break;
  112. case 'q':
  113. exit_main = true;
  114. break;
  115. default:
  116. LOGD("Unmapped key %d", key);
  117. }
  118. LOGD("=== Re-enqueue buffer(%p, %d)"
  119. , frame.userBuffer, frame.bufferSize);
  120. ASSERT_OK( TYEnqueueBuffer(hDevice, frame.userBuffer, frame.bufferSize) );
  121. }
  122. }
  123. ASSERT_OK( TYStopCapture(hDevice) );
  124. ASSERT_OK( TYCloseDevice(hDevice) );
  125. ASSERT_OK( TYCloseInterface(hIface) );
  126. ASSERT_OK( TYDeinitLib() );
  127. delete frameBuffer[0];
  128. delete frameBuffer[1];
  129. LOGD("=== Main done!");
  130. return 0;
  131. }