SimulatorCamera.cpp 5.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
//###########################################################################
// This file is part of LImA, a Library for Image Acquisition
//
// Copyright (C) : 2009-2011
// European Synchrotron Radiation Facility
// BP 220, Grenoble 38043
// FRANCE
//
// This is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// This software is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, see <http://www.gnu.org/licenses/>.
//###########################################################################
22
#include "SimulatorCamera.h"
ahoms's avatar
ahoms committed
23

24
#include <string>
25
#include <unistd.h>
26 27
#include <cmath>

ahoms's avatar
ahoms committed
28
using namespace lima;
29
using namespace lima::Simulator;
30
using namespace std;
ahoms's avatar
ahoms committed
31

32
Camera::SimuThread::SimuThread(Camera& simu)
33
	: m_simu(&simu)
ahoms's avatar
ahoms committed
34
{
35 36
    DEB_CONSTRUCTOR();

37
	m_acq_frame_nb = 0;
38
	m_force_stop = false;
39 40
}

41
void Camera::SimuThread::start()
42
{
43 44
    DEB_MEMBER_FUNCT();

45
	CmdThread::start();
ahoms's avatar
ahoms committed
46 47 48
	waitStatus(Ready);
}

49
void Camera::SimuThread::init()
ahoms's avatar
ahoms committed
50
{
51 52
    DEB_MEMBER_FUNCT();

ahoms's avatar
ahoms committed
53 54 55
	setStatus(Ready);
}

56
void Camera::SimuThread::execCmd(int cmd)
ahoms's avatar
ahoms committed
57
{
58 59
    DEB_MEMBER_FUNCT();

60 61 62 63
	int status = getStatus();
	switch (cmd) {
	case StartAcq:
		if (status != Ready)
64
			throw LIMA_HW_EXC(InvalidValue,  "Not Ready to StartAcq");
65 66 67 68 69
		execStartAcq();
		break;
	}
}

70
void Camera::SimuThread::execStartAcq()
71
{
72 73
    DEB_MEMBER_FUNCT();

74
	StdBufferCbMgr& buffer_mgr = m_simu->m_buffer_ctrl_obj.getBuffer();
75 76
	buffer_mgr.setStartTimestamp(Timestamp::now());

77
	FrameBuilder& frame_builder = m_simu->m_frame_builder;
78
	frame_builder.resetFrameNr(m_acq_frame_nb);
79

80
	int nb_frames = m_simu->m_trig_mode == IntTrig ? m_simu->m_nb_frames : m_acq_frame_nb + 1;
81
	int& frame_nb = m_acq_frame_nb;
82
	for (;(frame_nb < nb_frames)||(nb_frames==0); frame_nb++) {
83
		double req_time;
84 85 86 87 88 89
		if(m_force_stop)
		{
			m_force_stop = false;
			setStatus(Ready);
			return;
		}
90
		req_time = m_simu->m_exp_time;
91 92
		if (req_time > 0) {	
			setStatus(Exposure);
93
			usleep(long(req_time * 1e6));
94
		}
95

96
		setStatus(Readout);
97
		void *ptr = buffer_mgr.getFrameBufferPtr(frame_nb);
98
		typedef unsigned char *BufferPtr;
99
		frame_builder.getNextFrame(BufferPtr(ptr));
100 101 102 103

		HwFrameInfoType frame_info;
		frame_info.acq_frame_nb = frame_nb;
		buffer_mgr.newFrameReady(frame_info);
104

105
		req_time = m_simu->m_lat_time;
106 107
		if (req_time > 0) {
			setStatus(Latency);
108
			usleep(long(req_time * 1e6));
109
		}
110 111
	}
	setStatus(Ready);
ahoms's avatar
ahoms committed
112 113
}

114
Camera::Camera() : 
115
  m_thread(*this)
116
{
117 118
    DEB_CONSTRUCTOR();

119
	init();
120 121

	m_thread.start();
122 123
}

124
void Camera::init()
ahoms's avatar
ahoms committed
125
{
126 127
    DEB_MEMBER_FUNCT();

ahoms's avatar
ahoms committed
128
	m_exp_time = 1.0;
129
	m_lat_time = 0.0;
ahoms's avatar
ahoms committed
130 131 132
	m_nb_frames = 1;
}

133
Camera::~Camera()
ahoms's avatar
ahoms committed
134
{
135
    DEB_DESTRUCTOR();
ahoms's avatar
ahoms committed
136 137
}

138
HwBufferCtrlObj* Camera::getBufferCtrlObj()
ahoms's avatar
ahoms committed
139
{
140
	return &m_buffer_ctrl_obj;
141 142
}

143 144 145 146 147
FrameBuilder* Camera::getFrameBuilder()
{
	return &m_frame_builder;
}

148
void Camera::getMaxImageSize(Size& max_image_size)
149
{
150
	m_frame_builder.getMaxImageSize(max_image_size);
151 152
}

153
void Camera::setNbFrames(int nb_frames)
154 155 156 157 158 159 160
{
	if (nb_frames < 0)
		throw LIMA_HW_EXC(InvalidValue, "Invalid nb of frames");

	m_nb_frames = nb_frames;
}

161
void Camera::getNbFrames(int& nb_frames)
162 163 164 165
{
	nb_frames = m_nb_frames;
}

166
void Camera::setExpTime(double exp_time)
167 168 169 170 171
{
	if (exp_time <= 0)
		throw LIMA_HW_EXC(InvalidValue, "Invalid exposure time");
		
	m_exp_time = exp_time;
ahoms's avatar
ahoms committed
172 173
}

174
void Camera::getExpTime(double& exp_time)
175 176 177 178
{
	exp_time = m_exp_time;
}

179
void Camera::setLatTime(double lat_time)
180 181 182 183 184 185 186
{
	if (lat_time < 0)
		throw LIMA_HW_EXC(InvalidValue, "Invalid latency time");
		
	m_lat_time = lat_time;
}

187
void Camera::getLatTime(double& lat_time)
188 189 190 191
{
	lat_time = m_lat_time;
}

192
void Camera::setBin(const Bin& bin)
193
{
kirov's avatar
kirov committed
194
	m_frame_builder.setBin(bin);
195 196
}

197
void Camera::getBin(Bin& bin)
198
{
kirov's avatar
kirov committed
199
	m_frame_builder.getBin(bin);
200 201
}

202
void Camera::checkBin(Bin& bin)
203 204 205 206
{
	m_frame_builder.checkBin(bin);
}

207
void Camera::setFrameDim(const FrameDim& frame_dim)
208 209 210 211
{
	m_frame_builder.setFrameDim(frame_dim);
}

212
void Camera::getFrameDim(FrameDim& frame_dim)
213 214 215 216
{
	m_frame_builder.getFrameDim(frame_dim);
}

217
void Camera::reset()
218 219 220 221 222 223
{
	stopAcq();

	init();
}

224
HwInterface::StatusType::Basic Camera::getStatus()
225 226 227 228
{
	int thread_status = m_thread.getStatus();
	switch (thread_status) {
	case SimuThread::Ready:
229
		return HwInterface::StatusType::Ready;
230
	case SimuThread::Exposure:
231
		return HwInterface::StatusType::Exposure;
232
	case SimuThread::Readout:
233
		return HwInterface::StatusType::Readout;
234
	case SimuThread::Latency:
235
		return HwInterface::StatusType::Latency;
236 237 238 239
	default:
		throw LIMA_HW_EXC(Error, "Invalid thread status");
	}
}
240 241 242 243
void Camera::prepareAcq()
{
  m_thread.m_acq_frame_nb = 0;
}
244

245
void Camera::startAcq()
246
{
247 248
    DEB_MEMBER_FUNCT();

249 250
	m_thread.m_force_stop = false;//uggly but work	
	m_buffer_ctrl_obj.getBuffer().setStartTimestamp(Timestamp::now());
251

252 253 254 255
	m_thread.sendCmd(SimuThread::StartAcq);
	m_thread.waitNotStatus(SimuThread::Ready);
}

256 257
void Camera::stopAcq()
{
258 259
    DEB_MEMBER_FUNCT();

260
	m_thread.m_force_stop = true;//uggly but work
261 262 263 264
	m_thread.sendCmd(SimuThread::StopAcq);
	m_thread.waitStatus(SimuThread::Ready);
}

265
int Camera::getNbAcquiredFrames()
266
{
267
  return m_thread.m_acq_frame_nb;
268 269
}

270
ostream& lima::Simulator::operator <<(ostream& os, Camera& simu)
271 272 273
{
	string status;
	switch (simu.getStatus()) {
274
	case HwInterface::StatusType::Ready:
275
		status = "Ready"; break;
276
	case HwInterface::StatusType::Exposure:
277
		status = "Exposure"; break;
278
	case HwInterface::StatusType::Readout:
279
		status = "Readout"; break;
280
	case HwInterface::StatusType::Latency:
281
		status = "Latency"; break;
282 283 284 285 286 287
	default:
		status = "Unknown";
	}
	os << "<status=" << status << ">";
	return os;
}