SlsDetectorCamera.h 8.05 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//###########################################################################
// 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/>.
//###########################################################################

23
24
#ifndef __SLS_DETECTOR_CAMERA_H
#define __SLS_DETECTOR_CAMERA_H
25

26
#include "SlsDetectorArgs.h"
27
#include "SlsDetectorReceiver.h"
28
#include "SlsDetectorCPUAffinity.h"
29

30
#include "slsDetectorUsers.h"
31

32
#include "lima/HwBufferMgr.h"
33
#include "lima/HwMaxImageSizeCallback.h"
34
#include "lima/Event.h"
35

36
#include <queue>
37
38
39
40
41
42
43

namespace lima 
{

namespace SlsDetector
{

44
class Camera : public HwMaxImageSizeCallbackGen, public EventCallbackGen
45
{
46
47
48
	DEB_CLASS_NAMESPC(DebModCamera, "Camera", "SlsDetector");

public:
49
50
	typedef Defs::TrigMode TrigMode;
	typedef Defs::Settings Settings;
51
52
	typedef Defs::DACIndex DACIndex;
	typedef Defs::ADCIndex ADCIndex;
53
	typedef Defs::DetStatus DetStatus;
54
	typedef Defs::NetworkParameter NetworkParameter;
55

56
57
58
	Camera(std::string config_fname);
	virtual ~Camera();

59
60
61
62
	Type getType();

	Model *getModel()
	{ return m_model; }
63

64
	NameList getHostnameList()
65
66
67
68
69
	{ return m_input_data->host_name_list; }

	int getNbDetModules()
	{ return m_input_data->host_name_list.size(); }

70
71
72
	int getNbDetSubModules()
	{ return m_det->getNMods(); }

73
	int getTotNbPorts()
74
	{ return m_recv_list.size() * m_recv_nb_ports; }
75

76
	int getPortIndex(int recv_idx, int port)
77
78
79
80
	{ return recv_idx * m_recv_nb_ports + port; }

	std::pair<int, int> splitPortIndex(int port_idx)
	{ return std::pair<int, int>(port_idx / m_recv_nb_ports, 
81
				     port_idx % m_recv_nb_ports); }
82

83
84
85
	void setBufferCbMgr(StdBufferCbMgr *buffer_cb_mgr)
	{ m_buffer_cb_mgr = buffer_cb_mgr; }

86
87
88
	void setPixelDepth(PixelDepth  pixel_depth);
	void getPixelDepth(PixelDepth& pixel_depth);

89
90
	void setRawMode(bool  raw_mode);
	void getRawMode(bool& raw_mode);
91

92
93
94
95
	State getState();
	void waitState(State state);
	State waitNotState(State state);

96
97
98
99
100
	ImageType getImageType() const
	{ return m_image_type; }

	void getFrameDim(FrameDim& frame_dim, bool raw = false)
	{ m_model->getFrameDim(frame_dim, raw); }
101

102
103
	const FrameMap& getFrameMap()
	{ return m_frame_map; }
104
105
106
107

	void putCmd(const std::string& s, int idx = -1);
	std::string getCmd(const std::string& s, int idx = -1);

108
	int getFramesCaught();
109
	DetStatus getDetStatus();
110

111
112
	void setTrigMode(TrigMode  trig_mode);
	void getTrigMode(TrigMode& trig_mode);
113
114
	void setNbFrames(FrameType  nb_frames);
	void getNbFrames(FrameType& nb_frames);
115
116
	void setExpTime(double  exp_time);
	void getExpTime(double& exp_time);
117
118
	void setLatTime(double  lat_time);
	void getLatTime(double& lat_time);
119
120
121
	void setFramePeriod(double  frame_period);
	void getFramePeriod(double& frame_period);

122
123
124
125
126
127
128
129
130
131
132
	// setDAC: sub_mod_idx: 0-N=sub_module, -1=all
	void setDAC(int sub_mod_idx, DACIndex dac_idx, int  val, 
		    bool milli_volt = false);
	void getDAC(int sub_mod_idx, DACIndex dac_idx, int& val, 
		    bool milli_volt = false);
	void getDACList(DACIndex dac_idx, IntList& val_list,
			bool milli_volt = false);

	void getADC(int sub_mod_idx, ADCIndex adc_idx, int& val);
	void getADCList(ADCIndex adc_idx, IntList& val_list);

133
134
	void setSettings(Settings  settings);
	void getSettings(Settings& settings);
135

136
137
138
	void setNetworkParameter(NetworkParameter net_param, std::string& val);
	void getNetworkParameter(NetworkParameter net_param, std::string& val);

139
140
141
	void setTolerateLostPackets(bool  tol_lost_packets);
	void getTolerateLostPackets(bool& tol_lost_packets);

142
143
144
145
	int getNbBadFrames(int port_idx);
	void getBadFrameList(int port_idx, int first_idx, int last_idx,
			     IntList& bad_frame_list);
	void getBadFrameList(int port_idx, IntList& bad_frame_list);
146

147
148
149
	void prepareAcq();
	void startAcq();
	void stopAcq();
150

151
152
153
	void registerTimeRangesChangedCallback(TimeRangesChangedCallback& cb);
	void unregisterTimeRangesChangedCallback(TimeRangesChangedCallback& cb);

154
	void getStats(Stats& stats, int port_idx=-1);
155

156
157
158
	void setPixelDepthCPUAffinityMap(PixelDepthCPUAffinityMap aff_map);
	void getPixelDepthCPUAffinityMap(PixelDepthCPUAffinityMap& aff_map);

159
	GlobalCPUAffinityMgr::ProcessingFinishedEvent *
160
161
		getProcessingFinishedEvent();

162
private:
163
	typedef std::map<int, int> RecvPortMap;
164
	typedef std::queue<int> FrameQueue;
165
166
	typedef std::vector<AutoPtr<Receiver> > RecvList;
	typedef std::vector<Receiver::Port *> RecvPortList;
167

168
169
170
171
172
173
	struct AppInputData
	{
		DEB_CLASS_NAMESPC(DebModCamera, "Camera::AppInputData", 
				  "SlsDetector");
	public:
		std::string config_file_name;
174
		NameList host_name_list;
175
176
177
178
179
		RecvPortMap recv_port_map;
		AppInputData(std::string cfg_fname);
		void parseConfigFile();
	};

180
181
182
183
184
185
	class AcqThread : public Thread
	{
		DEB_CLASS_NAMESPC(DebModCamera, "Camera::AcqThread", 
				  "SlsDetector");
	public:
		AcqThread(Camera *cam);
186
187
188

		void queueFinishedFrame(FrameType frame);
		virtual void start();
189
		void stop(bool wait);
190

191
192
	protected:
		virtual void threadFunction();
193

194
	private:
195
196
197
198
199
200
201
202
203
204
		class ExceptionCleanUp : Thread::ExceptionCleanUp
		{
			DEB_CLASS_NAMESPC(DebModCamera, 
					  "Camera::AcqThread::ExceptionCleanUp",
					  "SlsDetector");
		public:
			ExceptionCleanUp(AcqThread& thread);
			virtual ~ExceptionCleanUp();
		};

205
		bool newFrameReady(FrameType frame);
206
207
208
		void startAcq();
		void stopAcq();
		void cleanUp();
209

210
		Camera *m_cam;
211
		Cond& m_cond;
212
		State& m_state;
213
		FrameQueue m_frame_queue;
214
	};
215

216
	friend class Model;
217
	friend class Receiver;
218
	friend class GlobalCPUAffinityMgr;
219

220
221
	void setModel(Model *model);

222
223
224
	RecvPortList getRecvPortList();
	Receiver::Port *getRecvPort(int port_idx);

225
226
227
	AutoMutex lock()
	{ return AutoMutex(m_cond.mutex()); }

228
	void updateImageSize();
229
	void updateTimeRanges();
230
	void updateCPUAffinity(bool recv_restarted);
231
	void setRecvCPUAffinity(const RecvCPUAffinity& recv_affinity);
232

233
	static int64_t NSec(double x)
234
235
	{ return int64_t(x * 1e9); }

236
237
	State getEffectiveState();

238
	char *getFrameBufferPtr(FrameType frame_nb);
239
	void removeSharedMem();
240
241
	void createReceivers();

242
243
244
	bool checkLostPackets();
	FrameType getLastReceivedFrame();

245
246
247
248
	void getSortedBadFrameList(IntList first_idx, IntList last_idx,
				   IntList& bad_frame_list );
	void getSortedBadFrameList(IntList& bad_frame_list)
	{ getSortedBadFrameList(IntList(), IntList(), bad_frame_list); }
249

250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
	template <class T>
	void putNbCmd(const std::string& cmd, T val, int idx = -1)
	{
		std::ostringstream os;
		os << cmd << " " << val;
		putCmd(os.str(), idx);
	}

	template <class T>
	T getNbCmd(const std::string& cmd, int idx = -1)
	{
		std::string ans = getCmd(cmd, idx);
		std::istringstream is(ans);
		T val;
		is >> val;
		return val;
	}
267

268
269
270
271
272
	void setReceiverFifoDepth(int fifo_depth);
	bool isTenGigabitEthernetEnabled();
	void setFlowControl10G(bool enabled);
	void resetFramesCaught();

273
	Model *m_model;
274
	Cond m_cond;
275
	AutoPtr<AppInputData> m_input_data;
276
	AutoPtr<slsDetectorUsers> m_det;
277
278
279
	FrameMap m_frame_map;
	int m_recv_nb_ports;
	RecvList m_recv_list;
280
	int m_recv_fifo_depth;
281
	TrigMode m_trig_mode;
282
	FrameType m_nb_frames;
283
	double m_exp_time;
284
	double m_lat_time;
285
	double m_frame_period;
286
	Settings m_settings;
287
	StdBufferCbMgr *m_buffer_cb_mgr;
288
	PixelDepth m_pixel_depth;
289
	ImageType m_image_type;
290
	bool m_raw_mode;
291
	State m_state;
292
293
	double m_new_frame_timeout;
	double m_abort_sleep_time;
294
	bool m_tol_lost_packets;
295
	FrameArray m_prev_ifa;
296
	TimeRangesChangedCallback *m_time_ranges_cb;
297
	PixelDepthCPUAffinityMap m_cpu_affinity_map;
298
	GlobalCPUAffinityMgr m_global_cpu_affinity_mgr;
299
	AutoPtr<AcqThread> m_acq_thread;
300
301
302
303
304
305
306
};

} // namespace SlsDetector

} // namespace lima


307
#endif // __SLS_DETECTOR_CAMERA_H