File: CamUsbIo.cpp

package info (click to toggle)
libapogee3 3.2%2B20221221183454-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 9,284 kB
  • sloc: cpp: 26,737; sh: 8; makefile: 3
file content (381 lines) | stat: -rw-r--r-- 9,447 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/*! 
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Copyright(c) 2009 Apogee Instruments, Inc. 
* \class CamUsbIo 
* \brief Usb implementation of the ICamIo interface
* 
*/ 

#include "CamUsbIo.h" 

#include <sstream>
#include <algorithm>


#include <iostream>

#include "apgHelper.h" 
#include "IUsb.h"
#include "helpers.h"
#include "ApnUsbSys.h"


#ifdef WIN_OS
   #include "GenTwoWinUSB.h" 
#else
    #include "linux/GenOneLinuxUSB.h"
#endif



//////////////////////////// 
// CTOR 
CamUsbIo::CamUsbIo( const std::string & DeviceEnum, const uint32_t MaxBufSize,
                                       const bool ApplyPad ) : 
                                                   m_fileName( __BASE_FILE__ ),
                                                   m_ApplyPadding( ApplyPad ),
                                                   m_MaxBufSize( MaxBufSize )

{ 

    const uint16_t deviceNum = help::Str2uShort( DeviceEnum );

#ifdef WIN_OS
        m_Usb =  std::shared_ptr<IUsb>(new GenTwoWinUSB( deviceNum ) );
#else
    m_Usb =  std::shared_ptr<IUsb>(new GenOneLinuxUSB( deviceNum ) );
#endif

  
}

//////////////////////////// 
// DTOR 
CamUsbIo::~CamUsbIo() 
{ 

} 

//////////////////////////// 
// READ     REG 
uint16_t CamUsbIo::ReadReg( uint16_t reg ) const 
{
 #ifdef DEBUGGING_CAMERA
    const uint16_t val = m_Usb->ReadReg( reg );
    apgHelper::DebugMsg( "CamUsbIo::ReadReg -> reg = 0x%X, val = 0x%X", reg, val );
    return val;
#else
    return m_Usb->ReadReg( reg );
#endif    

    
}

//////////////////////////// 
// WRITE        REG 
void CamUsbIo::WriteReg( uint16_t reg, uint16_t val ) 
{
#ifdef DEBUGGING_CAMERA
    apgHelper::DebugMsg( "CamUsbIo::WriteReg -> reg = 0x%X, val = 0x%X", reg, val );
#endif

    m_Usb->WriteReg( reg, val );
}

//////////////////////////// 
// WRITE        SRMD 
void CamUsbIo::WriteSRMD( const uint16_t reg, const std::vector<uint16_t> & data )
{
    std::vector<uint16_t>::const_iterator iter;
    for( iter = data.begin(); iter != data.end(); ++iter )
    {
        WriteReg( reg, *iter );
    }
}

//////////////////////////// 
// WRITE        MRMD 
void CamUsbIo::WriteMRMD(const uint16_t reg, const std::vector<uint16_t> & data )
{
    std::vector<uint16_t>::const_iterator iter;
    uint16_t offset = 0;
    for( iter = data.begin(); iter != data.end(); ++iter, ++offset )
    {
        WriteReg( reg+offset, *iter );
    }
}

//////////////////////////// 
// GET      USB     VENDOR      INFO
void CamUsbIo::GetUsbVendorInfo( uint16_t & VendorId,
            uint16_t & ProductId, uint16_t  & DeviceId)
{
    m_Usb->GetVendorInfo(VendorId, ProductId, DeviceId);
}


//////////////////////////// 
// SETUP     IMG     XFER
void CamUsbIo::SetupImgXfer(const uint16_t Rows, 
            const uint16_t Cols,
            const uint16_t NumOfImages, 
            const bool IsBulkSeq)
{
    if( IsBulkSeq )
    {
        //we are going to fake out the hardware by "rolling"
        //the number of images we want into the image height
        //thus the hw will wait for one really big image
        m_Usb->SetupSingleImgXfer(Rows, Cols*NumOfImages );

    }
    else if( NumOfImages > 1 )
    {
        //request a set of images
        m_Usb->SetupSequenceImgXfer(Rows, Cols, NumOfImages );
    }
    else
    {
        //just get one image with this h and w
        m_Usb->SetupSingleImgXfer( Rows, Cols );
    }
    
}

//////////////////////////// 
// CANCEL      IMG         XFER
void CamUsbIo::CancelImgXfer()
{
    m_Usb->CancelImgXfer();

}

//////////////////////////// 
// GET  IMAGE   DATA
void CamUsbIo::GetImageData( std::vector<uint16_t> & data )
{
     //allocate the proper buffer for the camera type
    const int32_t PadSize = GetPadding( apgHelper::SizeT2Int32(data.size()) );

    if( PadSize )
    {
        data.resize( data.size() + PadSize );
    }
   
    uint32_t NumBytesExpected = 
        apgHelper::SizeT2Uint32( data.size() ) * sizeof(uint16_t);
    std::vector<uint16_t>::iterator iter = data.begin();

    while( NumBytesExpected > 0 )
    {
        uint32_t SizeToRead = std::min<uint32_t>(NumBytesExpected,
            m_MaxBufSize );

        uint32_t ReceivedSize = 0;

        m_Usb->ReadImage(&(*iter),SizeToRead,ReceivedSize);

        NumBytesExpected -= ReceivedSize;
        
        if( ReceivedSize != SizeToRead )
        {
            break;
        }
        
        iter += ReceivedSize / sizeof(uint16_t);
    }

    if( NumBytesExpected )
    {
        const uint32_t TotalBytes = 
             apgHelper::SizeT2Uint32( data.size() ) * sizeof(uint16_t);
        const uint32_t  DownloadedBytes = TotalBytes - NumBytesExpected;
        std::stringstream msg;
        msg << "GetImageData error - Expected " << data.size()*sizeof(uint16_t) << " bytes.";
        msg << "  Downloaded " <<  DownloadedBytes << " bytes.";
        msg << "  " << NumBytesExpected << " bytes remaining.";
        
        apgHelper::throwRuntimeException( m_fileName, msg.str(), 
            __LINE__, Apg::ErrorType_Critical );
    }

    //remove the padded data if it exists
    if( 0 < PadSize )
    {
        int32_t offset =  
            apgHelper::SizeT2Int32( data.size() ) - PadSize;
        std::vector<uint16_t>::iterator iter = data.begin()+offset;
        data.erase( iter, data.end() );
    }
    
}

//////////////////////////// 
// GET  STATUS
void CamUsbIo::GetStatus(CameraStatusRegs::BasicStatus & status)
{
    m_Usb->GetStatus( reinterpret_cast<uint8_t*>(&status),
        sizeof(status) );
}
        
//////////////////////////// 
// GET  STATUS
void CamUsbIo::GetStatus(CameraStatusRegs::AdvStatus & status)
{
	int MaxRetries = 2;
    for (int RetryNumber = 0; RetryNumber <= MaxRetries; RetryNumber++)
	{
		try
		{
			m_Usb->GetStatus( reinterpret_cast<uint8_t*>(&status),
				sizeof(status) );
			break;
		}
		catch(std::exception & err )
		{
			if ( RetryNumber == MaxRetries )
			{
                std::stringstream msg;
				msg << "CamUsbIo::GetStatus (adv) failed after ";
				msg << RetryNumber << " retries with error ";
				msg <<  err.what();
				apgHelper::throwRuntimeException( m_fileName, msg.str(), 
					__LINE__, Apg::ErrorType_Critical );
			}
		}
	}
}

//////////////////////////// 
// GET       FIRMWARE    REV
uint16_t CamUsbIo::GetFirmwareRev()
{
    return ReadReg( CameraRegs::FIRMWARE_REV );
}

//////////////////////////// 
// GET  USB        FIRMWARE        VERSION
std::string CamUsbIo::GetUsbFirmwareVersion()
 {
     std::vector<char> data(UsbFrmwr::REV_LENGTH+1, 0);

     m_Usb->GetUsbFirmwareVersion( reinterpret_cast<int8_t*>(&data.at(0)), UsbFrmwr::REV_LENGTH );

    std::string version( &data.at(0) );

    return version;
 }

//////////////////////////// 
//  GET  INFO
std::string CamUsbIo::GetInfo()
{
     uint16_t vid, pid, did = 0;
    GetUsbVendorInfo( vid, pid, did );

    std::stringstream result;

    result << "Interface: USB\n";
    result << "Camera Firmware: " << GetFirmwareRev() << "\n";
    result << "USB Firmware: " << GetUsbFirmwareVersion() << "\n";
    result << "USB Vendor ID: " << vid << "\n";
    result << "USB Prodcut ID: " << pid << "\n";
    result << "USB Device ID: " << did << "\n";
    result << "USB Driver Version: " << GetDriverVersion() << "\n";
    result << "USB Device Number: " << m_Usb->GetDeviceNum() << "\n";
      
    return result.str();
}

//////////////////////////// 
// READ       BUFCON          REG 
uint8_t CamUsbIo::ReadBufConReg( uint16_t reg )
{
    uint8_t value = 0;
    m_Usb->UsbRequestIn( VND_APOGEE_BUFCON_REG, reg, 0,
        &value, sizeof(uint8_t) );
    return value;
}
	    

//////////////////////////// 
// WRITE       BUFCON          REG 
void CamUsbIo::WriteBufConReg( const uint16_t reg, 
                                uint8_t val )
{
    m_Usb->UsbRequestOut( VND_APOGEE_BUFCON_REG, reg, 0, 
        reinterpret_cast<uint8_t*>(&val), sizeof(uint8_t) );
}
        
//////////////////////////// 
// READ       FX2          REG 
uint8_t CamUsbIo::ReadFx2Reg( uint16_t reg )
{
     uint8_t value = 0;
    m_Usb->UsbRequestIn( VND_APOGEE_MEMRW, reg, 0,
        &value, sizeof(uint8_t) );
    return value;
}

//////////////////////////// 
// WRITE       FX2         REG 
void CamUsbIo::WriteFx2Reg( uint16_t reg, uint8_t val )
{
    m_Usb->UsbRequestOut( VND_APOGEE_MEMRW, reg, 0, 
        &val, sizeof(uint8_t) );

}


//////////////////////////// 
//  GET    PADDING
int32_t CamUsbIo::GetPadding( const int32_t Num )
{

    // if we don't need to pad the data
    // bail here
    if( !m_ApplyPadding )
    {
        return 0;
    }

    //if the output buffer is not divisible by
    //8 we can get an error in the usb data
    //so we pad the buffer
    int32_t PadSize = 8 - (Num % 8);

    if( 8 == PadSize )
    {
        PadSize = 0;
    }

    return PadSize;
}

//////////////////////////// 
//      GET    DRIVER   VERSION
std::string CamUsbIo::GetDriverVersion()
{
    return m_Usb->GetDriverVersion();
}

//////////////////////////// 
// IS      ERROR
bool CamUsbIo::IsError()
{
    return m_Usb->IsError();
}


//////////////////////////// 
// PROGRESS        2          STDOUT
void CamUsbIo::Progress2StdOut(const int32_t percentComplete)
{
    if( m_Print2StdOut )
    {
        std::cout << "Precent:" << percentComplete << std::endl;
    }
}