1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2019 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See http://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * IDS uEye interface.
33  *
34  *****************************************************************************/
35 
36 #ifndef DOXYGEN_SHOULD_SKIP_THIS
37 
38 class CameraList
39 {
40 public:
CameraList()41   CameraList()
42     : m_pCamList(NULL), m_CamInfo()
43   {
44     // init the internal camera info structure
45     ZeroMemory(&m_CamInfo, sizeof(UEYE_CAMERA_INFO));
46     // get the cameralist from SDK
47     initCameraList();
48 
49     m_CamInfo.dwDeviceID = -1;
50   }
51 
~CameraList()52   ~CameraList()
53   {
54     deleteCameraList();
55   }
56 
initCameraList()57   bool initCameraList()
58   {
59     /*!
60      * \par Retrieve the camera list from libueye_api.so
61      *
62      * According to the libueye_api documentation, camera list query is performed in two steps:
63      * \li Query the number of available cameras by calling is_GetCameralist() with one
64      * UEYE_CAMERA_LIST structure as argument whose member dwCount ist zero. The number of cameras
65      * may then be retrieved from the data returned by is_GetCameralist() by reading
66      * m_pCamList->dwCount.
67      * \li Allocate the number of elements of type UEYE_CAMERA_LIST and call is_GetCameraList()
68      * again according to the uEye SDK documentation. The number of elements to retrieve must be
69      * given in member m_pCamList->dwCount of the first array element.
70      */
71     bool ret;
72     deleteCameraList ();
73     m_pCamList = new UEYE_CAMERA_LIST;
74 
75     m_pCamList->dwCount = 0;
76 
77     if (is_GetCameraList (m_pCamList) == IS_SUCCESS) {
78       DWORD dw = m_pCamList->dwCount;
79       delete m_pCamList;
80       m_pCamList = NULL;
81 
82       if (dw) {
83         // Reallocate the required camera list size
84         m_pCamList = (PUEYE_CAMERA_LIST)new char[sizeof(DWORD) + dw * sizeof(UEYE_CAMERA_INFO)];
85         m_pCamList->dwCount = dw;
86 
87         // Get CameraList and store it ...
88         if (is_GetCameraList (m_pCamList) == IS_SUCCESS) {
89           //SelectCamera (0);
90           ret = true;
91         }
92         else {
93           ret = false;
94         }
95       }
96       else {
97         ret = false;
98       }
99     }
100     else {
101       ret = false;
102     }
103 
104     return ret;
105   }
106 
deleteCameraList()107   void deleteCameraList()
108   {
109     if (m_pCamList)
110       delete m_pCamList;
111     m_pCamList = NULL;
112 
113     ZeroMemory (&m_CamInfo, sizeof(UEYE_CAMERA_INFO));
114   }
115 
getCameraIDList()116   std::vector<unsigned int> getCameraIDList()
117   {
118     std::vector<unsigned int> ids;
119     for (unsigned int i = 0; i < size(); i ++) {
120       ids.push_back(m_pCamList->uci[i].dwDeviceID);
121     }
122     return ids;
123   }
124 
getCameraModelList()125   std::vector<std::string> getCameraModelList()
126   {
127     std::vector<std::string> models;
128     for (unsigned int i = 0; i < size(); i ++) {
129       models.push_back(m_pCamList->uci[i].Model);
130     }
131     return models;
132   }
133 
getCameraSerialNumberList()134   std::vector<std::string> getCameraSerialNumberList()
135   {
136     std::vector<std::string> serials;
137     for (unsigned int i = 0; i < size(); i ++) {
138       serials.push_back(m_pCamList->uci[i].SerNo);
139     }
140     return serials;
141   }
142 
getCameraInfo()143   UEYE_CAMERA_INFO getCameraInfo()
144   {
145     return m_CamInfo;
146   }
147 
148   /*!
149    * Select a camera from the camera list.
150    * \param cam_index : Camera index.
151    * \return Zero if successful, nonzero otherwise.
152    */
setActiveCamera(unsigned int cam_index)153   int setActiveCamera(unsigned int cam_index)
154   {
155     if (!m_pCamList) {
156       return -1;
157     }
158     if (cam_index >= m_pCamList->dwCount)
159       return -1;
160     // copy current camera info
161     memcpy(&m_CamInfo, &m_pCamList->uci[cam_index], sizeof(UEYE_CAMERA_INFO));
162     return 0;
163   }
164 
165   /*!
166    * Select a camera from the camera list by her ID.
167    * \param cam_id :  Camera ID.
168    * \return Zero if successful and camera exist, nonzero otherwise
169    */
selectCameraByID(unsigned int cam_id)170   int selectCameraByID(unsigned int cam_id)
171   {
172     for(unsigned int i=0; i < size(); i++) {
173       if(m_pCamList->uci[i].dwDeviceID == cam_id) {
174         // copy current camera info
175         memcpy(&m_CamInfo, &m_pCamList->uci[i], sizeof(UEYE_CAMERA_INFO));
176         return 0;
177       }
178     }
179     return -1;
180   }
181 
size()182   unsigned int size()
183   {
184     if (m_pCamList) {
185       return (unsigned int)m_pCamList->dwCount;
186     }
187     else {
188       return 0;
189     }
190   }
191 
192 private:
193   PUEYE_CAMERA_LIST m_pCamList;
194   UEYE_CAMERA_INFO m_CamInfo;
195 };
196 
197 /*
198  **********************************************************************************************
199  */
200 
201 namespace helper
202 {
203   class LockUnlockSeqBuffer
204   {
205   public:
206     LockUnlockSeqBuffer(HIDS hCam, INT nSeqNum, char* pcMem);
207     ~LockUnlockSeqBuffer(void);
208 
OwnsLock(void)209     bool OwnsLock(void) const   { return m_bOwnsLock; }
210     void Unlock(void);
211 
212   private:
213     LockUnlockSeqBuffer(void);
214     LockUnlockSeqBuffer(const LockUnlockSeqBuffer&);
215     void operator=(LockUnlockSeqBuffer);
216 
217   private:
218     HIDS m_hCam;
219     INT m_nSeqNum;
220     char* m_pcMem;
221     bool m_bOwnsLock;
222   };
223 
LockUnlockSeqBuffer(HIDS hCam,INT nSeqNum,char * pcMem)224   LockUnlockSeqBuffer::LockUnlockSeqBuffer(HIDS hCam, INT nSeqNum, char* pcMem)
225     : m_hCam(hCam)
226     , m_nSeqNum(nSeqNum)
227     , m_pcMem(pcMem)
228     , m_bOwnsLock(false)
229   {
230     INT nRet = is_LockSeqBuf(m_hCam, m_nSeqNum, m_pcMem);
231 
232     m_bOwnsLock = (IS_SUCCESS == nRet);
233   }
234 
~LockUnlockSeqBuffer(void)235   LockUnlockSeqBuffer::~LockUnlockSeqBuffer(void)
236   {
237     Unlock();
238   }
239 
Unlock(void)240   void LockUnlockSeqBuffer::Unlock(void)
241   {
242     if (m_bOwnsLock)
243     {
244       is_UnlockSeqBuf(m_hCam, m_nSeqNum, m_pcMem);
245       m_bOwnsLock = false;
246     }
247   }
248 }
249 
250 #endif // #ifndef DOXYGEN_SHOULD_SKIP_THIS
251