Explorar o código

change driver_camera_ioctl.

yuchuli hai 1 ano
pai
achega
43c9551370

+ 2 - 0
src/driver/driver_camera_ioctl/pythonusbcam.py

@@ -17,6 +17,7 @@ def showpic():
     nlen = nlenx()
     mylib.StartCam("/dev/video0",1920,1080,30);
     while i< 10000:
+#    	nRtn = mylib.GetJPEGDataWithWait("/dev/video0",y,nlen,100);
     	nRtn = mylib.GetJPEGData("/dev/video0",y,nlen);
         if(nRtn>0):
 		xdata = np.frombuffer(y,count = nlen[0])
@@ -25,6 +26,7 @@ def showpic():
 		cv.waitKey(1)
 		print nRtn
 	else :
+		print("no data");
 		time.sleep(0.001);
 		
     str="exit"

+ 65 - 1
src/driver/driver_camera_ioctl/usb_cam_python.cpp

@@ -92,7 +92,7 @@ void threadcam(iv::threadcam * pthreadcam)
             pthreadcam->mbUpdate = true;
             pthreadcam->mmutexdata.unlock();
             pthreadcam->mcv.notify_all();
-  //          std::cout<<std::chrono::system_clock::now().time_since_epoch().count()/1000<< " "<<nLen<<std::endl;
+ //           std::cout<<std::chrono::system_clock::now().time_since_epoch().count()/1000<< " "<<nLen<<std::endl;
         }
         else
         {
@@ -230,8 +230,72 @@ int StartCam(const char * strvideoname,int image_width,int image_height,int imag
 }
 
 
+int GetJPEGDataWithWait(char * strvideoname, char * str,int * x,int nwaitms)
+{
+    std::string strvideo = strvideoname;
+
+    int nsize;
+    int i;
+    iv::threadcam * pthreadcam = NULL;
+    for(i=0;i<nsize;i++)
+    {
+        if(gvectorthreadcam[i]->mstrvideoname == strvideo)
+        {
+            pthreadcam = gvectorthreadcam[i];
+            break;
+        }
+    }
+
+    if(pthreadcam == NULL)
+    {
+        std::cout<<" video device not open."<<std::endl;
+        return 0;
+    }
+
+    int nRtn = 0;
+    pthreadcam->mmutexdata.lock();
+    if(pthreadcam->mbUpdate)
+    {
+//        str.copy(gstrbuf,gnLen);
+        memcpy(str,pthreadcam->mpstr_data.get(),pthreadcam->mndatasize);
+        nRtn = pthreadcam->mndatasize;
+        *x = pthreadcam->mndatasize;
+        pthreadcam->mbUpdate= false;
+    }
+    pthreadcam->mmutexdata.unlock();
+
+    if(nRtn > 0)return nRtn;
+
+    if(nwaitms == 0)return nRtn;
+
+    std::unique_lock<std::mutex> lk(pthreadcam->mmutexcv);
+    if(pthreadcam->mcv.wait_for(lk,std::chrono::milliseconds(nwaitms)) == std::cv_status::timeout)
+    {
+        lk.unlock();
+    }
+    else
+    {
+        lk.unlock();
+    }
+    pthreadcam->mmutexdata.lock();
+    if(pthreadcam->mbUpdate)
+    {
+//        str.copy(gstrbuf,gnLen);
+        memcpy(str,pthreadcam->mpstr_data.get(),pthreadcam->mndatasize);
+        nRtn = pthreadcam->mndatasize;
+        *x = pthreadcam->mndatasize;
+        pthreadcam->mbUpdate= false;
+    }
+    pthreadcam->mmutexdata.unlock();
+
+    return nRtn;
+
+
+}
+
 int GetJPEGData(char * strvideoname, char * str,int * x)
 {
+    return GetJPEGDataWithWait(strvideoname,str,x,0);
     std::string strvideo = strvideoname;
 
     int nsize;