Espia.cpp 7.95 KB
Newer Older
1
#include "Espia.h"
2
#include "MemUtils.h"
3
4
5
6
7
8

using namespace lima;
using namespace std;

#define CHECK_CALL(ret)		ESPIA_CHECK_CALL(ret)

9
10
11
#define ESPIA_MIN_BUFFER_SIZE	(128 * 1024)


12
Espia::Espia(int dev_nb)
13
{
14
	m_dev_nb = Invalid;
15
16
17
18
19
	m_dev = ESPIA_DEV_INVAL;

	m_nb_buffers = m_buffer_frames = 0;
	m_real_frame_factor = m_real_frame_size = 0;
	
20
21
22
23
24
25
26
27
	m_nb_frames = 0;
	m_started = false;

	resetFrameInfo(m_last_frame_info);
	m_last_frame_cb_nr = Invalid;

	open(dev_nb);
	registerLastFrameCb();
28
29
30
31
32
33
34
}

Espia::~Espia()
{
	close();
}

35
void Espia::open(int dev_nb)
36
{
37
	if (dev_nb == m_dev_nb)
38
39
40
41
		return;

	close();

42
43
	CHECK_CALL(espia_open(dev_nb, &m_dev));
	m_dev_nb = dev_nb;
44
45
46
47
}

void Espia::close()
{
48
	if (m_dev_nb == Invalid)
49
50
		return;

51
	bufferFree();
52
	unregisterLastFrameCb();
53
	CHECK_CALL(espia_close(m_dev));
54

55
	m_dev = ESPIA_DEV_INVAL;
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
	m_dev_nb = Invalid;
}

int Espia::cbDispatch(struct espia_cb_data *cb_data)
{
	Espia *espia = (Espia *) cb_data->data;

	void (Espia::*method)(struct espia_cb_data *cb_data) = NULL;

	int& cb_nr = cb_data->cb_nr;
	if (cb_nr == espia->m_last_frame_cb_nr)
		method = &Espia::lastFrameCb;
	
	if (method) {
		try {
			(espia->*method)(cb_data);
		} catch (...) {

		}
	}

	return ESPIA_OK;
}

void Espia::registerLastFrameCb()
{
	if (m_last_frame_cb_nr != Invalid)
		return;

	struct espia_cb_data cb_data;
	cb_data.type    = ESPIA_CB_ACQ;
	cb_data.cb      = cbDispatch;
	cb_data.data    = this;
	cb_data.timeout = BlockForever;

	struct img_frame_info& req_finfo = cb_data.info.acq.req_finfo;
	req_finfo.buffer_nr    = ESPIA_ACQ_ANY;
	req_finfo.frame_nr     = ESPIA_ACQ_ANY;
	req_finfo.round_count  = ESPIA_ACQ_ANY;
	req_finfo.acq_frame_nr = ESPIA_ACQ_EACH;

	int& cb_nr = m_last_frame_cb_nr;
	CHECK_CALL(espia_register_callback(m_dev, &cb_data, &cb_nr));

	try {
		CHECK_CALL(espia_callback_active(m_dev, cb_nr, true));
	} catch (...) {
		unregisterLastFrameCb();
		throw;
	}
}

void Espia::unregisterLastFrameCb()
{
	if (m_last_frame_cb_nr == Invalid)
		return;

	CHECK_CALL(espia_unregister_callback(m_dev, m_last_frame_cb_nr));

	m_last_frame_cb_nr = Invalid;
}

void Espia::lastFrameCb(struct espia_cb_data *cb_data)
{
	AutoMutex l = acqLock();

	struct img_frame_info& cb_finfo = cb_data->info.acq.cb_finfo;
	if (!finished_espia_frame_info(&cb_finfo, cb_data->ret))
		m_last_frame_info = cb_finfo;
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
void Espia::bufferAlloc(const FrameDim& frame_dim, int& nb_buffers,
			int buffer_frames)
{
	if (!frame_dim.isValid() || (nb_buffers <= 0) || (buffer_frames <= 0))
		throw LIMA_HW_EXC(InvalidValue, "Invalid frame_dim, "
				  "nb_buffers and/or buffer_frames");

	if ((frame_dim == m_frame_dim) && (nb_buffers == m_nb_buffers) &&
	    (buffer_frames == m_buffer_frames))
		return;

	bufferFree();

	int& virt_buffers   = nb_buffers;
	int& virt_frames    = buffer_frames;

	int real_buffers    = virt_buffers;
	int real_frames     = virt_frames;
	int real_frame_size = frame_dim.getMemSize();
	if (virt_frames == 1) {
		int page_size;
		GetPageSize(page_size);
		real_frame_size += page_size - 1;
		real_frame_size &= ~(page_size - 1);
	}

	int real_buffer_size = real_frame_size * real_frames;
	int frame_factor = 1;
	if (real_buffer_size < ESPIA_MIN_BUFFER_SIZE) {
		frame_factor  = ESPIA_MIN_BUFFER_SIZE / real_buffer_size;
		real_frames  *= frame_factor;
		real_buffers += frame_factor - 1;
		real_buffers /= frame_factor;
		virt_buffers  = real_buffers * frame_factor;
	}

	CHECK_CALL(espia_buffer_alloc(m_dev, real_buffers, real_frames,
				      real_frame_size));

	m_frame_dim         = frame_dim;
	m_nb_buffers        = virt_buffers;
	m_buffer_frames     = virt_frames;
	m_real_frame_factor = frame_factor;
	m_real_frame_size   = real_frame_size;
}

void Espia::bufferFree()
{
	if ((m_nb_buffers == 0) || (m_buffer_frames == 0))
		return;

178
179
	stopAcq();

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
	CHECK_CALL(espia_buffer_free(m_dev));

	m_frame_dim = FrameDim();
	m_nb_buffers = m_buffer_frames = 0;
	m_real_frame_factor = m_real_frame_size = 0;
}

void Espia::getFrameDim(FrameDim& frame_dim)
{
	frame_dim = m_frame_dim;
}

void Espia::getNbBuffers(int& nb_buffers)
{
	nb_buffers = m_nb_buffers;
}

void Espia::getBufferFrames(int& buffer_frames)
{
	buffer_frames = m_buffer_frames;
}

void *Espia::getBufferFramePtr(int buffer_nb, int frame_nb)
{
	int real_buffer = realBufferNb(buffer_nb, frame_nb);
	int real_frame  = realFrameNb(buffer_nb, frame_nb);
	void *ptr;
	CHECK_CALL(espia_frame_address(m_dev, real_buffer, real_frame, &ptr));
	return ptr;
}

void *Espia::getAcqFramePtr(int acq_frame_nb)
{
	int buffer_nb = ESPIA_ACQ_ANY;
	void *ptr;
	CHECK_CALL(espia_frame_address(m_dev, buffer_nb, acq_frame_nb, &ptr));
	return ptr;
}

void Espia::getFrameInfo(int acq_frame_nb, HwFrameInfoType& info)
{
	struct img_frame_info finfo;
	finfo.buffer_nr    = ESPIA_ACQ_ANY;
	finfo.frame_nr     = ESPIA_ACQ_ANY;
	finfo.round_count  = ESPIA_ACQ_ANY;
	finfo.acq_frame_nr = acq_frame_nb;
226
	CHECK_CALL(espia_get_frame(m_dev, &finfo, NoBlock));
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243

	real2virtFrameInfo(finfo, info);
}

void Espia::real2virtFrameInfo(const struct img_frame_info& real_info, 
			       HwFrameInfoType& virt_info)
{
	char *buffer_ptr = (char *) real_info.buffer_ptr;
	int frame_offset = real_info.frame_nr * m_real_frame_size;

	virt_info.acq_frame_nb    = real_info.acq_frame_nr;
	virt_info.frame_ptr       = buffer_ptr + frame_offset;
	virt_info.frame_dim       = &m_frame_dim;
	virt_info.frame_timestamp = usec2sec(real_info.time_us);
	virt_info.valid_pixels    = real_info.pixels;
}

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
void Espia::resetFrameInfo(struct img_frame_info& frame_info)
{
	frame_info.buffer_ptr   = NULL;
	frame_info.buffer_nr    = Invalid;
	frame_info.frame_nr     = Invalid;
	frame_info.round_count  = Invalid;
	frame_info.acq_frame_nr = Invalid;
	frame_info.time_us      = 0;
	frame_info.pixels       = 0;
}

void Espia::setNbFrames(int nb_frames)
{
	if (nb_frames < 0)
		throw LIMA_HW_EXC(InvalidValue, "Invalid nb of frames");

	m_nb_frames = nb_frames;
}

void Espia::getNbFrames(int& nb_frames)
{
	nb_frames = m_nb_frames;
}

void Espia::startAcq()
{
	AutoMutex l = acqLock();

	if (m_started)
		throw LIMA_HW_EXC(Error, "Acquisition already running");

	resetFrameInfo(m_last_frame_info);

	CHECK_CALL(espia_start_acq(m_dev, 0, m_nb_frames, NoBlock));

	m_start_ts = Timestamp::now();
	m_started = true;
}

void Espia::stopAcq()
{
	AutoMutex l = acqLock();

	if (!m_started)
		return;

	CHECK_CALL(espia_stop_acq(m_dev));
	
	m_started = false;
}

void Espia::getAcqStatus(AcqStatusType& acq_status)
{
	AutoMutex l = acqLock();

	unsigned long acq_run_nb;
	int acq_running = espia_acq_active(m_dev, &acq_run_nb);
	CHECK_CALL(acq_running);

	acq_status.acq_started = m_started;
	acq_status.acq_running = acq_running;
	acq_status.acq_run_nb = acq_run_nb;
	acq_status.last_acq_frame_nb = m_last_frame_info.acq_frame_nr;
}

ahoms's avatar
ahoms committed
309
310
311
312
313
314
void Espia::serWrite(const string& buffer, int block_size, double block_delay, 
		     bool no_wait)
{
	unsigned long len = buffer.length();
	char *ptr = len ? (char *) buffer.data() : NULL;
	CHECK_CALL(espia_ser_write(m_dev, ptr, &len, block_size, 
315
				   sec2usec(block_delay), !no_wait));
ahoms's avatar
ahoms committed
316
317
318
319
320
321
322
}

void Espia::serRead(string& buffer, int len, double timeout)
{
	buffer.resize(len);
	char *ptr = len ? (char *) buffer.data() : NULL;
	unsigned long ret_len = len;
323
	CHECK_CALL(espia_ser_read(m_dev, ptr, &ret_len, sec2usec(timeout)));
ahoms's avatar
ahoms committed
324
325
326
327
328
329
330
331
332
333
334
	buffer.resize(ret_len);
}

void Espia::serReadStr(string& buffer, int len, const string& term, 
		       double timeout)
{
	buffer.resize(len);
	char *ptr = len ? (char *) buffer.data() : NULL;
	char *term_ptr = (char *) term.data();
	unsigned long ret_len = len;
	CHECK_CALL(espia_ser_read_str(m_dev, ptr, &ret_len, term_ptr,
335
				      term.length(), sec2usec(timeout)));
ahoms's avatar
ahoms committed
336
337
	buffer.resize(ret_len);
}
338

ahoms's avatar
ahoms committed
339
340
341
342
void Espia::serFlush()
{
	CHECK_CALL(espia_ser_flush(m_dev));
}
ahoms's avatar
ahoms committed
343
344
345
346
347
348
349

void Espia::serGetAvailableBytes(int& available_bytes)
{
	unsigned long ret_bytes = 0;
	CHECK_CALL(espia_ser_read(m_dev, NULL, &ret_bytes, 0));
	available_bytes = ret_bytes;
}
ahoms's avatar
ahoms committed
350
	
351
352
353
354
355
void Espia::throwError(int ret, string file, string func, int line)
{
	string err_desc = string("Espia: ") + espia_strerror(ret);
	throw Exception(Hardware, Error, err_desc, file, func, line);
}