Commit 88ea3764 authored by seb's avatar seb
Browse files

first control compilation (CtSaving class)


git-svn-id: https://scm.blissgarden.org/svn/lima/trunk@64 45c4679d-1946-429d-baad-37912b19538b
parent ded78c59
MAKE_CMD = $(MAKE) -C src
all:
$(MAKE_CMD) all
clean:
$(MAKE_CMD) clean
......@@ -3,35 +3,40 @@
#include "HwInterface.h"
#include "ThreadUtils.h"
namespace lima {
class CtControl {
public:
class CtSaving;
class CtControl {
public:
struct ImageStatus {
long LastImageAcquired;
long LastBaseImageReady;
long LastImageReady;
long LastImageSaved;
long LastCounterReady;
};
struct ImageStatus {
long LastImageAcquired;
long LastBaseImageReady;
long LastImageReady;
long LastImageSaved;
long LastCounterReady;
};
CtControl(HwInterface *hw);
~CtControl();
CtControl(HwInterface *hw);
~CtControl();
void prepareAcq();
void startAcq();
void stopAcq();
void prepareAcq();
void startAcq();
void stopAcq();
void getAcqStatus(); // from HW
void getImageStatus(ImageStatus& status) const;
void getAcqStatus(); // from HW
void getImageStatus(ImageStatus& status) const;
void reset();
void reset();
private:
HwInterface m_hw;
ImageStatus m_img_status;
};
private:
HwInterface m_hw;
mutable Cond m_cond;
ImageStatus m_img_status;
CtSaving *m_ct_saving;
};
} // namespace lima
......
#ifndef CTSAVING_H
#define CTSAVING_H
namespace lima {
class CtSaving {
#include <map>
#include <list>
#include <string>
#include <fstream>
public:
#include "ThreadUtils.h"
CtSaving();
~CtSaving();
class Data;
enum FileFormat {
RAW,
EDF,
};
namespace lima {
enum SavingMode {
Manual,
AutoFrame,
AutoHeader,
};
class CtSaving {
friend class CtControl;
public:
CtSaving(CtControl&);
~CtSaving();
enum FileFormat {
RAW,
EDF,
};
enum SavingMode {
Manual,
AutoFrame,
AutoHeader,
};
enum OverwritePolicy {
Abort,
Overwrite,
Append,
};
enum OverwritePolicy {
Abort,
Overwrite,
Append,
};
struct Parameters {
string directory;
string prefix;
string suffix;
long nextNumber;
FileFormat fileFormat;
SavingMode savingMode;
OverwritePolicy overwritePolicy;
unsigned long framesPerFile;
};
struct Parameters {
std::string directory;
std::string prefix;
std::string suffix;
long nextNumber;
FileFormat fileFormat;
SavingMode savingMode;
OverwritePolicy overwritePolicy;
long framesPerFile;
};
typedef std::pairs<string, string> HeaderValue;
typedef std::list<HeaderValue> HeaderList;
typedef std::pair<std::string, std::string> HeaderValue;
typedef std::map<std::string,std::string> HeaderMap;
typedef std::map<long,Data> FrameMap;
// --- file parameters
// --- file parameters
void setParameters(Parameters pars);
void getParameters(Parameters& pars) const;
void setParameters(const Parameters &pars);
void getParameters(Parameters& pars) const;
void setDirectory(string directory);
void getDirectory(string& directory) const;
void setDirectory(const std::string &directory);
void getDirectory(std::string& directory) const;
void setPrefix(string prefix);
void getPrefix(string& prefix) const;
void setPrefix(const std::string &prefix);
void getPrefix(std::string& prefix) const;
void setSuffix(string suffix);
void getSuffix(string& suffix) const;
void setSuffix(const std::string &suffix);
void getSuffix(std::string& suffix) const;
void setNextNumber(long number);
void getNextNumber(long& number) const;
void setNextNumber(long number);
void getNextNumber(long& number) const;
void setFormat(FileFormat format);
void getFormat(FileFormat& format) const;
void setFormat(const FileFormat &format);
void getFormat(FileFormat& format) const;
// --- saving modes
// --- saving modes
void setSavingMode(SavingMode mode);
void getSavingMode(SavingMode& mode) const;
void setSavingMode(SavingMode mode);
void getSavingMode(SavingMode& mode) const;
void setOverwritePolicy(OverwritePolicy policy);
void getOverwritePolicy(OverwritePolicy& policy) const;
void setOverwritePolicy(OverwritePolicy policy);
void getOverwritePolicy(OverwritePolicy& policy) const;
void setFramesPerFile(unsigned long frames_per_file);
void getFramePerFile(unsigned long& frames_per_file) const;
void setFramesPerFile(unsigned long frames_per_file);
void getFramePerFile(unsigned long& frames_per_file) const;
// --- common headers
// --- common headers
void getStaticHeader(HeaderList& header) const;
void getStaticHeader(HeaderMap& header) const;
void resetCommonHeader();
void setCommonHeader(HeaderList header);
void getCommonHeader(HeaderList& header) const;
void addToCommonHeader(HeaderValue value);
void resetCommonHeader();
void setCommonHeader(const HeaderMap &header);
void updateCommonHeader(const HeaderMap &header);
void getCommonHeader(HeaderMap& header) const;
void addToCommonHeader(const HeaderValue &value);
// --- frame headers
// --- frame headers
void setFrameHeader(long frame_nr, HeaderList header);
void getFrameHeader(long frame_nr, HeaderList& header) const;
void takeFrameHeader(long frame_nr, HeaderList& header);
void addToFrameHeader(long frame_nr, HeaderValue value);
void setFrameHeader(long frame_nr, const HeaderMap &header);
void getFrameHeader(long frame_nr, HeaderMap& header) const;
void takeFrameHeader(long frame_nr, HeaderMap& header);
void removeFrameHeader(long frame_nr);
void removeAllFrameHeaders();
void removeFrameHeader(long frame_nr);
void removeAllFrameHeaders();
// --- final header
void takeHeader(long frame_nr, HeaderList& header);
private:
private:
class _SaveTask;
class _SaveContainer;
class _SaveCBK;
friend class _SaveCBK;
CtControl &m_ctrl;
_SaveContainer *m_save_cnt;
_SaveCBK *m_saving_cbk;
Parameters m_pars;
HeaderMap m_static_header;
HeaderMap m_common_header;
std::map<long, HeaderMap> m_frame_headers;
FrameMap m_frame_datas;
mutable Cond m_cond;
bool m_ready_flag;
long m_last_frameid_saved;
Parameters m_pars;
HeaderList m_static_header;
HeaderList m_common_header;
map<long, HeaderList> m_frame_headers;
};
void _get_common_header(HeaderMap&);
void _takeHeader(std::map<long,HeaderMap>::iterator&, HeaderMap& header);
void _frame_ready(Data &);
void _post_save_task(Data&,_SaveTask*);
void _save_finnished(Data&);
};
} // namespace lima
#undef // CTSAVING_H
#endif // CTSAVING_H
#include <sstream>
#include "CtSaving.h"
#include "TaskMgr.h"
#include "SinkTask.h"
using namespace lima;
static const char DIR_SEPARATOR = '/';
class CtSaving::_SaveContainer
{
public:
_SaveContainer(CtSaving&);
~_SaveContainer();
void writeFile(Data&,CtSaving::HeaderMap &);
void setStatisticSize(int aSize);
void getStatistic(std::list<double>&);
private:
CtSaving &m_saving;
int m_written_frames;
std::ofstream m_fout;
std::list<double> m_statistic_list;
int m_statistic_size;
mutable Cond m_cond;
void _open();
void _close();
void _writeEdfHeader(Data&,CtSaving::HeaderMap&);
};
/** @brief save task class
*/
class CtSaving::_SaveTask : public SinkTaskBase
{
public:
_SaveTask(CtSaving::_SaveContainer &save_cnt) : SinkTaskBase(),m_container(save_cnt) {}
virtual void process(Data &aData)
{
m_container.writeFile(aData,m_header);
}
CtSaving::HeaderMap m_header;
private:
_SaveContainer &m_container;
};
/** @brief save callback
*/
class CtSaving::_SaveCBK : public TaskEventCallback
{
public:
_SaveCBK(CtSaving &aCtSaving) : m_saving(aCtSaving) {}
virtual void finnished(Data &aData)
{
m_saving._save_finnished(aData);
}
private:
CtSaving &m_saving;
};
//@brief constructor
CtSaving::CtSaving(CtControl &aCtrl) :
m_ctrl(aCtrl),
m_ready_flag(true),
m_last_frameid_saved(0)
{
m_save_cnt = new _SaveContainer(*this);
m_saving_cbk = new _SaveCBK(*this);
}
//@brief destructor
CtSaving::~CtSaving()
{
delete m_save_cnt;
m_saving_cbk->unref();
}
void CtSaving::setParameters(const CtSaving::Parameters &pars)
{
AutoMutex aLock(m_cond.mutex());
m_pars = pars;
}
void CtSaving::getParameters(CtSaving::Parameters &pars) const
{
AutoMutex aLock(m_cond.mutex());
pars = m_pars;
}
void CtSaving::setDirectory(const std::string &directory)
{
AutoMutex aLock(m_cond.mutex());
m_pars.directory = directory;
}
void CtSaving::getDirectory(std::string& directory) const
{
AutoMutex aLock(m_cond.mutex());
directory = m_pars.directory;
}
void CtSaving::setPrefix(const std::string &prefix)
{
AutoMutex aLock(m_cond.mutex());
m_pars.prefix = prefix;
}
void CtSaving::getPrefix(std::string& prefix) const
{
AutoMutex aLock(m_cond.mutex());
prefix = m_pars.prefix;
}
void CtSaving::setSuffix(const std::string &suffix)
{
AutoMutex aLock(m_cond.mutex());
m_pars.suffix = suffix;
}
void CtSaving::getSuffix(std::string& suffix) const
{
AutoMutex aLock(m_cond.mutex());
suffix = m_pars.suffix;
}
void CtSaving::setNextNumber(long number)
{
AutoMutex aLock(m_cond.mutex());
m_pars.nextNumber = number;
}
void CtSaving::getNextNumber(long& number) const
{
AutoMutex aLock(m_cond.mutex());
number = m_pars.nextNumber;
}
void CtSaving::setFormat(const FileFormat &format)
{
AutoMutex aLock(m_cond.mutex());
m_pars.fileFormat = format;
}
void CtSaving::getFormat(FileFormat& format) const
{
AutoMutex aLock(m_cond.mutex());
format = m_pars.fileFormat;
}
void CtSaving::setSavingMode(SavingMode mode)
{
AutoMutex aLock(m_cond.mutex());
m_pars.savingMode = mode;
}
void CtSaving::getSavingMode(SavingMode& mode) const
{
AutoMutex aLock(m_cond.mutex());
mode = m_pars.savingMode;
}
void CtSaving::setOverwritePolicy(OverwritePolicy policy)
{
AutoMutex aLock(m_cond.mutex());
m_pars.overwritePolicy = policy;
}
void CtSaving::getOverwritePolicy(OverwritePolicy& policy) const
{
AutoMutex aLock(m_cond.mutex());
policy = m_pars.overwritePolicy;
}
void CtSaving::setFramesPerFile(unsigned long frames_per_file)
{
AutoMutex aLock(m_cond.mutex());
m_pars.framesPerFile = frames_per_file;
}
void CtSaving::getFramePerFile(unsigned long& frames_per_file) const
{
AutoMutex aLock(m_cond.mutex());
frames_per_file = m_pars.framesPerFile;
}
void CtSaving::getStaticHeader(HeaderMap& header) const
{
AutoMutex aLock(m_cond.mutex());
header = m_static_header;
}
void CtSaving::resetCommonHeader()
{
AutoMutex aLock(m_cond.mutex());
m_common_header.clear();
}
void CtSaving::setCommonHeader(const HeaderMap &header)
{
AutoMutex aLock(m_cond.mutex());
m_common_header = header;
}
void CtSaving::updateCommonHeader(const HeaderMap &header)
{
AutoMutex aLock(m_cond.mutex());
m_common_header.insert(header.begin(),header.end());
}
void CtSaving::getCommonHeader(HeaderMap& header) const
{
AutoMutex aLock(m_cond.mutex());
header = HeaderMap(m_common_header);
}
void CtSaving::addToCommonHeader(const HeaderValue &value)
{
AutoMutex aLock(m_cond.mutex());
m_common_header.insert(value);
}
void CtSaving::setFrameHeader(long frame_nr, const HeaderMap &header)
{
AutoMutex aLock(m_cond.mutex());
switch(m_pars.savingMode)
{
case CtSaving::AutoHeader:
{
FrameMap::iterator frame_iter = m_frame_datas.find(frame_nr);
if(frame_iter != m_frame_datas.end() &&
m_ready_flag && m_last_frameid_saved == frame_nr - 1)
{
_SaveTask *aSaveTaskPt = new _SaveTask(*m_save_cnt);
_get_common_header(aSaveTaskPt->m_header);
Data aData = frame_iter->second;
m_frame_datas.erase(frame_iter);
m_ready_flag = false,m_last_frameid_saved = frame_nr;
aLock.unlock();
aSaveTaskPt->m_header.insert(header.begin(),header.end());
_post_save_task(aData,aSaveTaskPt);
break;
}
}
default:
m_frame_headers.insert(std::pair<int,HeaderMap>(frame_nr,header));
break;
}
}
void CtSaving::getFrameHeader(long frame_nr, HeaderMap& header) const
{
AutoMutex aLock(m_cond.mutex());
std::map<long,HeaderMap>::const_iterator i = m_frame_headers.find(frame_nr);
if(i != m_frame_headers.end())
header.insert(i->second.begin(),i->second.end());
}
void CtSaving::takeFrameHeader(long frame_nr, HeaderMap& header)
{
AutoMutex aLock(m_cond.mutex());
std::map<long,HeaderMap>::iterator i = m_frame_headers.find(frame_nr);
if(i != m_frame_headers.end())
{
header = i->second;
m_frame_headers.erase(i);
}
}
void CtSaving::removeFrameHeader(long frame_nr)
{
AutoMutex aLock(m_cond.mutex());
m_frame_headers.erase(frame_nr);
}
void CtSaving::removeAllFrameHeaders()
{
AutoMutex aLock(m_cond.mutex());
m_frame_headers.clear();
}
// Private methodes
void CtSaving::_get_common_header(HeaderMap &header)
{
header.insert(m_static_header.begin(),m_static_header.end());
header.insert(m_common_header.begin(),m_common_header.end());
}
void CtSaving::_takeHeader(std::map<long,HeaderMap>::iterator &headerIter, HeaderMap& header)
{
_get_common_header(header);
HeaderMap &aFrameHeaderMap = headerIter->second;
header.insert(aFrameHeaderMap.begin(),aFrameHeaderMap.end());
m_frame_headers.erase(headerIter);
}
void CtSaving::_frame_ready(Data &aData)
{
AutoMutex aLock(m_cond.mutex());
switch(m_pars.savingMode)
{
case CtSaving::AutoFrame:
{
if(m_ready_flag && m_last_frameid_saved == aData.frameNumber - 1)
{
_SaveTask *aSaveTaskPt = new _SaveTask(*m_save_cnt);
_get_common_header(aSaveTaskPt->m_header);
m_ready_flag = false,m_last_frameid_saved = aData.frameNumber;
aLock.unlock();
_post_save_task(aData,aSaveTaskPt);
}
else
m_frame_datas.insert(std::pair<long,Data>(aData.frameNumber,aData));
}
break;
case CtSaving::AutoHeader:
{
std::map<long,HeaderMap>::iterator aHeaderIter = m_frame_headers.find(aData.frameNumber);
if(aHeaderIter != m_frame_headers.end() &&
m_ready_flag && m_last_frameid_saved == aData.frameNumber - 1)
{
_SaveTask *aSaveTaskPt = new _SaveTask(*m_save_cnt);
_takeHeader(aHeaderIter,aSaveTaskPt->m_header);
m_ready_flag = false,m_last_frameid_saved = aData.frameNumber;
aLock.unlock();
_post_save_task(aData,aSaveTaskPt);
}
else
m_frame_datas.insert(std::pair<long,Data>(aData.frameNumber,aData));
}
break;
default:
m_frame_datas.insert(std::pair<long,Data>(aData.frameNumber,aData));
break;
}
}
void CtSaving::_post_save_task(Data &aData,_SaveTask *aSaveTaskPt)
{