Commit cd108f10 authored by ahoms's avatar ahoms
Browse files

* added FrameDim * and / operators with Bin in SizeUtils

* extended BufferSave to integrate functionality in espia_acq_lib
* fixed StdBufferCbMgr frame cb timestamp to be relative to the acq. start
* added real buffer filling and frame callback to Simulator
* modified test and testsimulator to use latest class interfaces



git-svn-id: https://scm.blissgarden.org/svn/lima/trunk@37 45c4679d-1946-429d-baad-37912b19538b
parent c06c861a
...@@ -34,6 +34,9 @@ class Simulator ...@@ -34,6 +34,9 @@ class Simulator
void setBin(const Bin& bin); void setBin(const Bin& bin);
void getBin(Bin& bin); void getBin(Bin& bin);
void setFrameDim(const FrameDim& frame_dim);
void getFrameDim(FrameDim& frame_dim);
Status getStatus(); Status getStatus();
private: private:
......
...@@ -42,6 +42,9 @@ void Simulator::SimuThread::execStartAcq() ...@@ -42,6 +42,9 @@ void Simulator::SimuThread::execStartAcq()
StdBufferCbMgr& buffer_mgr = m_simu.m_buffer_cb_mgr; StdBufferCbMgr& buffer_mgr = m_simu.m_buffer_cb_mgr;
buffer_mgr.setStartTimestamp(Timestamp::now()); buffer_mgr.setStartTimestamp(Timestamp::now());
FrameBuilder& frame_builder = m_simu.m_frame_builder;
frame_builder.resetFrameNr();
struct timespec treq, trem; struct timespec treq, trem;
double exp_time = m_simu.m_exp_time; double exp_time = m_simu.m_exp_time;
treq.tv_sec = int(floor(exp_time)); treq.tv_sec = int(floor(exp_time));
...@@ -52,7 +55,14 @@ void Simulator::SimuThread::execStartAcq() ...@@ -52,7 +55,14 @@ void Simulator::SimuThread::execStartAcq()
for (frame_nb = 0; frame_nb < nb_frames; frame_nb++) { for (frame_nb = 0; frame_nb < nb_frames; frame_nb++) {
setStatus(Exposure); setStatus(Exposure);
nanosleep(&treq, &trem); nanosleep(&treq, &trem);
setStatus(Readout); setStatus(Readout);
int buffer_nb = frame_nb % buffer_mgr.getNbBuffers();
typedef unsigned char *BufferPtr;
BufferPtr ptr = BufferPtr(buffer_mgr.getBufferPtr(buffer_nb));
frame_builder.getNextFrame(ptr);
buffer_mgr.newFrameReady(frame_nb);
} }
setStatus(Ready); setStatus(Ready);
} }
...@@ -113,6 +123,16 @@ void Simulator::getBin(Bin& bin) ...@@ -113,6 +123,16 @@ void Simulator::getBin(Bin& bin)
m_frame_builder.getBin(bin); m_frame_builder.getBin(bin);
} }
void Simulator::setFrameDim(const FrameDim& frame_dim)
{
m_frame_builder.setFrameDim(frame_dim);
}
void Simulator::getFrameDim(FrameDim& frame_dim)
{
m_frame_builder.getFrameDim(frame_dim);
}
Simulator::Status Simulator::getStatus() Simulator::Status Simulator::getStatus()
{ {
int thread_status = m_thread.getStatus(); int thread_status = m_thread.getStatus();
......
#include "BufferSave.h"
#include <ctime> #include <ctime>
#include <cstdio> #include <cstdio>
#include <cstring> #include <cstring>
#include <fstream> #include <fstream>
#include <sstream>
#include <unistd.h> #include <unistd.h>
#include <sys/time.h> #include <sys/time.h>
#include "BufferSave.h"
using namespace lima; using namespace lima;
using namespace std; using namespace std;
#define EDF_HEADER_LEN 1024
#define EDF_HEADER_BUFFER_LEN (10 * EDF_HEADER_LEN)
BufferSave::BufferSave( FileFormat format, const String& prefix, int idx,
const String& suffix, bool overwrite,
int tot_file_frames )
: m_format(format), m_prefix(prefix), m_idx(idx), m_suffix(suffix),
m_overwrite(overwrite), m_tot_file_frames(tot_file_frames)
{
m_written_frames = 0;
m_fout = NULL;
if (m_suffix == "")
m_suffix = getDefSuffix();
}
BufferSave::BufferSave( )
: m_format(Raw), m_prefix(), m_idx(0), m_overwrite(false),
m_tot_file_frames(1)
{
m_written_frames = 0;
m_fout = NULL;
if (m_suffix == "")
m_suffix = getDefSuffix();
}
BufferSave::~BufferSave( )
{
closeFile();
}
BufferSave::String BufferSave::getDefSuffix() const
{
return (m_format == EDF) ? ".edf" : ".raw";
}
void BufferSave::openFile()
{
if (isFileOpen())
return;
ostringstream idx;
idx.width(4);
idx.fill('0');
idx << m_idx;
m_file_name = m_prefix + idx.str() + m_suffix;
m_fout = new ofstream(m_file_name.c_str(),
ios_base::out | ios_base::binary);
}
int BufferSave::writeEdfHeader( const FrameInfoType &finfo, void BufferSave::closeFile()
std::ofstream &file ) {
if (!isFileOpen())
return;
delete m_fout;
m_fout = NULL;
m_file_name = "";
m_written_frames = 0;
m_idx++;
}
void BufferSave::writeEdfHeader( const FrameInfoType& finfo )
{ {
time_t ctime_now; time_t ctime_now;
struct timeval tod_now; struct timeval tod_now;
char time_str[64], buffer[EDF_HEADER_BUFFER_LEN], *p; char time_str[64], buffer[EDF_HEADER_BUFFER_LEN], *p;
int l, len, rem; int l, len, rem;
const FrameDim *fdim = finfo.frame_dim;
const Size& frame_size = fdim->getSize();
int depth = fdim->getDepth();
int image = m_written_frames + 1;
time(&ctime_now); time(&ctime_now);
gettimeofday(&tod_now, NULL); gettimeofday(&tod_now, NULL);
...@@ -25,19 +97,23 @@ int BufferSave::writeEdfHeader( const FrameInfoType &finfo, ...@@ -25,19 +97,23 @@ int BufferSave::writeEdfHeader( const FrameInfoType &finfo,
p = buffer; p = buffer;
p += sprintf(p, "{\n"); p += sprintf(p, "{\n");
p += sprintf(p, "HeaderID = EH:%06lu:000000:000000 ;\n", finfo.acq_frame_nb); p += sprintf(p, "HeaderID = EH:%06u:000000:000000 ;\n", image);
p += sprintf(p, "Image = %lu ;\n", finfo.acq_frame_nb); p += sprintf(p, "Image = %u ;\n", image);
p += sprintf(p, "ByteOrder = LowByteFirst ;\n"); p += sprintf(p, "ByteOrder = LowByteFirst ;\n");
p += sprintf(p, "DataType = %s ;\n", p += sprintf(p, "DataType = %s ;\n",
(finfo.depth == 1) ? "UnsignedByte" : (depth == 1) ? "UnsignedByte" :
((finfo.depth == 2) ? "UnsignedShort" : "UnsignedLong")); ((depth == 2) ? "UnsignedShort" : "UnsignedLong"));
p += sprintf(p, "Size = %d ;\n", finfo.width*finfo.height*finfo.depth); p += sprintf(p, "Size = %d ;\n", fdim->getMemSize());
p += sprintf(p, "Dim_1 = %d ;\n", finfo.width); p += sprintf(p, "Dim_1 = %d ;\n", frame_size.getWidth());
p += sprintf(p, "Dim_2 = %d ;\n", finfo.height); p += sprintf(p, "Dim_2 = %d ;\n", frame_size.getHeight());
p += sprintf(p, "acq_frame_nb = %d ;\n", finfo.acq_frame_nb);
p += sprintf(p, "time = %s ;\n", time_str); p += sprintf(p, "time = %s ;\n", time_str);
p += sprintf(p, "time_of_day = %ld.%06ld ;\n", p += sprintf(p, "time_of_day = %ld.%06ld ;\n",
tod_now.tv_sec, tod_now.tv_usec); tod_now.tv_sec, tod_now.tv_usec);
if (finfo.frame_timestamp.isSet())
p += sprintf(p, "time_of_frame = %.6f ;\n",
double(finfo.frame_timestamp));
l = p - buffer; l = p - buffer;
len = l; len = l;
...@@ -47,40 +123,96 @@ int BufferSave::writeEdfHeader( const FrameInfoType &finfo, ...@@ -47,40 +123,96 @@ int BufferSave::writeEdfHeader( const FrameInfoType &finfo,
p += sprintf(p, "%*s}\n", len - (l + 2), ""); p += sprintf(p, "%*s}\n", len - (l + 2), "");
len = p - buffer; len = p - buffer;
file.write(buffer, len); m_fout->write(buffer, len);
return len;
} }
void BufferSave::writeFrame( const FrameInfoType& finfo )
{
const FrameDim *fdim = finfo.frame_dim;
if (fdim == NULL)
throw LIMA_HW_EXC(InvalidValue, "Null finfo.fdim");
openFile();
if ( m_format == EDF )
writeEdfHeader(finfo);
m_fout->write((char *)finfo.frame_ptr, fdim->getMemSize());
BufferSave::BufferSave( const string &prefix, enum FileFormat format ) : m_written_frames++;
m_prefix(prefix), m_format(format) if (m_written_frames == m_tot_file_frames)
closeFile();
}
bool BufferSave::isFileOpen() const
{ {
return (m_fout != NULL);
} }
void BufferSave::setPrefix(const String& prefix)
{
if (prefix == m_prefix)
return;
if (isFileOpen())
throw LIMA_HW_EXC(InvalidValue, "Set prefix with open file");
BufferSave::~BufferSave( ) m_prefix = prefix;
}
void BufferSave::getPrefix(String& prefix) const
{ {
prefix = m_prefix;
} }
void BufferSave::setFormat(FileFormat format)
{
if (format == m_format)
return;
if (isFileOpen())
throw LIMA_HW_EXC(InvalidValue, "Set format with open file");
m_format = format;
m_suffix = getDefSuffix();
int BufferSave::writeFrame( FrameInfoType &finfo ) }
void BufferSave::getFormat(FileFormat& format) const
{ {
string name; format = m_format;
char num_str[64]; }
name = m_prefix; void BufferSave::setIndex(int idx)
snprintf(num_str, 63, "%06lu", finfo.acq_frame_nb); {
name += string(num_str); if (idx == m_idx)
name += ".edf"; return;
ofstream file(name.c_str(), ios_base::out | ios_base::binary); if (isFileOpen())
throw LIMA_HW_EXC(InvalidValue, "Set idx with open file");
if( m_format == FMT_EDF ) m_idx = idx;
writeEdfHeader(finfo, file); }
file.write((char *)finfo.frame_ptr, finfo.width*finfo.height*finfo.depth); void BufferSave::getIndex(int& idx) const
{
idx = m_idx;
}
file.close(); void BufferSave::setTotFileFrames(int tot_file_frames)
{
if (tot_file_frames == m_tot_file_frames)
return;
if (isFileOpen())
throw LIMA_HW_EXC(InvalidValue,
"Set tot_file_frames with open file");
m_tot_file_frames = tot_file_frames;
}
return 0; void BufferSave::getTotFileFrames(int& tot_file_frames) const
{
tot_file_frames = m_tot_file_frames;
} }
#ifndef BUFFERSAVE_H #ifndef BUFFERSAVE_H
#define BUFFERSAVE_H #define BUFFERSAVE_H
#include <stdio.h>
#include <string> #include <string>
#include <fstream> #include <fstream>
#include "FrameBuilder.h" #include "FrameBuilder.h"
#include "HwFrameCallback.h"
namespace lima { namespace lima {
#define EDF_HEADER_LEN 1024 class BufferSave {
#define EDF_HEADER_BUFFER_LEN (10 * EDF_HEADER_LEN) public :
enum FileFormat {
Raw, EDF,
};
typedef std::string String;
typedef struct FrameInfo { BufferSave( );
unsigned long acq_frame_nb; BufferSave( FileFormat format, const String& prefix,
void *frame_ptr; int idx = 0, const String& suffix = "",
int width, height, depth; bool overwrite = false , int tot_file_frames = 1);
double frame_time_stamp; ~BufferSave( );
} FrameInfoType;
void writeFrame( const FrameInfoType& finfo );
enum FileFormat { void setPrefix(const String& prefix);
FMT_RAW, void getPrefix(String& prefix) const;
FMT_EDF,
};
void setFormat(FileFormat format);
void getFormat(FileFormat& format) const;
class BufferSave { void setIndex(int idx);
public : void getIndex(int& idx) const;
std::string m_prefix;
enum FileFormat m_format;
BufferSave( const std::string &prefix, enum FileFormat format ); void setTotFileFrames(int tot_file_frames);
~BufferSave( ); void getTotFileFrames(int& tot_file_frames) const;
int writeFrame( FrameInfoType &finfo ); void getOpenFileName(String& file_name) const;
bool isFileOpen() const;
private: private:
int writeEdfHeader( const FrameInfoType &finfo, std::ofstream &file ); String getDefSuffix() const;
void openFile();
void closeFile();
void writeEdfHeader( const FrameInfoType& finfo );
FileFormat m_format;
String m_prefix;
int m_idx;
String m_suffix;
String m_file_name;
bool m_overwrite;
int m_written_frames;
int m_tot_file_frames;
FrameInfoType m_last_frame;
std::ofstream *m_fout;
}; };
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#include "BufferSave.h" #include "BufferSave.h"
#include "SizeUtils.h" #include "SizeUtils.h"
#include "Exceptions.h" #include "Exceptions.h"
#include "AutoObj.h"
using namespace std; using namespace std;
using namespace lima; using namespace lima;
...@@ -12,45 +13,31 @@ int main( void ) ...@@ -12,45 +13,31 @@ int main( void )
{ {
FrameBuilder fb; FrameBuilder fb;
FrameDim full_fd;
fb.getFrameDim(full_fd);
Bin bin = Bin(2,2); Bin bin = Bin(2,2);
fb.setBin(bin); fb.setBin(bin);
int binX = bin.getX();
int binY = bin.getY(); FrameDim fd = full_fd / bin;
FrameDim fd; BufferSave bs(BufferSave::EDF, "boza");
fb.getFrameDim(fd); bs.setTotFileFrames(5);
int width = fd.getSize().getWidth();
int height = fd.getSize().getHeight(); int size = fd.getMemSize();
int depth = fd.getDepth(); AutoPtr<unsigned char, true> buffer;
buffer = new unsigned char[size];
BufferSave bs("boza", FMT_EDF);
unsigned char *buffer; Timestamp start = Timestamp::now();
FrameInfoType finfo;
buffer = NULL;
try {
int size = width * height * depth;
buffer = new unsigned char[size];
} catch( bad_alloc& ) {
goto end;
}
for( int i=0; i<10; i++ ) { for( int i=0; i<10; i++ ) {
int frame_nb = fb.getFrameNr();
fb.getNextFrame( buffer ); fb.getNextFrame( buffer );
finfo.acq_frame_nb = fb.getFrameNr(); Timestamp t = Timestamp::now() - start;
finfo.frame_ptr = buffer; FrameInfoType finfo(frame_nb, buffer, &fd, t);
finfo.width = width/binX;
finfo.height = height/binY;
finfo.depth = depth;
finfo.frame_time_stamp = 0; /* XXX */
bs.writeFrame(finfo); bs.writeFrame(finfo);
} }
end:
if( buffer )
delete[] buffer;
return 0; return 0;
} }
...@@ -5,9 +5,37 @@ ...@@ -5,9 +5,37 @@
using namespace lima; using namespace lima;
using namespace std; using namespace std;
class TestFrameCallback : public HwFrameCallback
{
public:
TestFrameCallback(Simulator& simu) : m_simu(simu) {}
protected:
virtual bool newFrameReady(const FrameInfoType& frame_info);
private:
Simulator& m_simu;
};
bool TestFrameCallback::newFrameReady(const FrameInfoType& frame_info)
{
cout << "acq_frame_nb=" << frame_info.acq_frame_nb << ", "
<< "ts=" << frame_info.frame_timestamp << ", "
<< "simu=" << m_simu << endl;
return true;
}
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
Simulator simu; Simulator simu;
TestFrameCallback cb(simu);
FrameDim frame_dim;
simu.getFrameDim(frame_dim);
BufferCtrlMgr& buffer_mgr = simu.getBufferMgr();
buffer_mgr.setFrameDim(frame_dim);
buffer_mgr.setNbBuffers(1);
buffer_mgr.registerFrameCallback(&cb);
cout << "simu=" << simu << endl; cout << "simu=" << simu << endl;
simu.startAcq(); simu.startAcq();
...@@ -23,5 +51,14 @@ int main(int argc, char *argv[]) ...@@ -23,5 +51,14 @@ int main(int argc, char *argv[])
simu.stopAcq(); simu.stopAcq();
cout << "simu=" << simu << endl; cout << "simu=" << simu << endl;
simu.setExpTime(2);
simu.setNbFrames(3);
cout << "simu=" << simu << endl;
simu.startAcq();
cout << "simu=" << simu << endl;
simu.stopAcq();
cout << "simu=" << simu << endl;
return 0; return 0;
} }
...@@ -375,6 +375,9 @@ class FrameDim ...@@ -375,6 +375,9 @@ class FrameDim
static int getImageTypeBpp(ImageType type); static int getImageTypeBpp(ImageType type);
static int getImageTypeDepth(ImageType type); static int getImageTypeDepth(ImageType type);
FrameDim& operator *=(const Bin& bin);
FrameDim& operator /=(const Bin& bin);