UVCDeviceManager.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #include "UVCDeviceManager.h"
  2. #include "../DataManager/DataManager.h"
  3. #include "../DataManager/DataPackage.h"
  4. void UVCManager::init(int nWaitingTime)
  5. {
  6. // get device list
  7. uvc_device_t **deviceList;
  8. uvc_get_device_list(ctx, &deviceList);
  9. // get device number
  10. deviceNum = 0;
  11. while (deviceList[deviceNum] != NULL)
  12. {
  13. deviceNum++;
  14. }
  15. std::cout << "Device Number: " << deviceNum << std::endl;
  16. uvc_device_t **OrderedDeviceList = new uvc_device_t *[deviceNum];
  17. for (int i = 0; i < deviceNum; i++)
  18. {
  19. int minBus = 255;
  20. int minAddress = 255;
  21. int minBusIndex = -1;
  22. int minAddressIndex = -1;
  23. for (int j = 0; j < deviceNum; j++)
  24. {
  25. if (deviceList[j] == nullptr)
  26. {
  27. continue;
  28. }
  29. int bus = uvc_get_bus_number(deviceList[j]);
  30. if (bus < minBus)
  31. {
  32. minBus = bus;
  33. minBusIndex = j;
  34. minAddress = uvc_get_device_address(deviceList[j]);
  35. minAddressIndex = j;
  36. }
  37. else if (bus == minBus && uvc_get_device_address(deviceList[j]) < minAddress)
  38. {
  39. minAddress = uvc_get_device_address(deviceList[j]);
  40. minAddressIndex = j;
  41. }
  42. }
  43. OrderedDeviceList[i] = deviceList[minBusIndex];
  44. deviceList[minBusIndex] = nullptr;
  45. }
  46. // init the Uvc device by deviceNum
  47. for (int i = 0; i < deviceNum; i++)
  48. {
  49. UVCDevice *uvcDevice = new UVCDevice();
  50. uvcDevice->init(i, ctx, OrderedDeviceList[i]);
  51. uvcDevice->setCallBack(cbSaveToLocal);
  52. uvcDeviceList.push_back(uvcDevice);
  53. int bus = uvc_get_bus_number(OrderedDeviceList[i]);
  54. int address = uvc_get_device_address(OrderedDeviceList[i]);
  55. std::cout << "Device " << i << " Bus: " << bus << " Address: " << address << std::endl;
  56. // init the data manager
  57. DataManager::getInstance()
  58. .addDataPipe<DataPackage>("uvc" + std::to_string(i));
  59. }
  60. m_GpioExplorer = std::make_shared<GPIOExplorer>("gpiochip0", 16);
  61. m_GpioExplorer->setWaitingTime(nWaitingTime);
  62. }
  63. int UVCManager::deliverFrameInCallBack(uvc_device_handle_t *devh)
  64. {
  65. int i = 0;
  66. std::list<UVCDevice *>::iterator it = uvcDeviceList.begin();
  67. for (; it != uvcDeviceList.end(); it++, i++)
  68. {
  69. if ((*it)->getDevh() == devh)
  70. {
  71. return i;
  72. }
  73. }
  74. return -1;
  75. }
  76. void UVCManager::startAllStreaming()
  77. {
  78. for (auto uvcDevice : uvcDeviceList)
  79. {
  80. uvcDevice->startStreaming();
  81. }
  82. }
  83. void UVCManager::stopAllStreaming()
  84. {
  85. for (auto uvcDevice : uvcDeviceList)
  86. {
  87. uvcDevice->stopStreaming();
  88. }
  89. }