/* -*- mode: c++ -*- */
#ifndef __MyModule_h__
#define __MyModule_h__
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <vector>
#include <DAQ++/ObserverModel.h>
#include <daq/GenModule.h>
#include "TAmanager.h"
class Analysis;
template<typename T>
class MyParameter;
class MyModule : public GenModule {
private:
double _delay;
public:
enum ROmode { Off=0, On=1, Serial=2, Sparse=4, SparseAdj=8, FORoff=16 };
MyModule(const DAQpp::DAQid &id,unsigned long add);
~MyModule();
bool hasData();
char *getData(int &size);
int PrepareForRun ();
int Start (int = 0);
int Stop (int = 0) { return 0; };
int Pause () { return 0; };
int Continue () { return 0; };
int Reset ();
int GetReady ();
int Trigger ();
void flush () {};
void SoftStart();
void set_threshold(int x, bool nt=false);
void set_rout( int x, bool nt=false);
void set_nchip( int x, bool nt=false);
void set_adj( int x, bool nt=false);
void set_ta( TAmanager &x, bool nt=false);
int get_threshold() const;
int get_rout() const;
int get_nchip() const;
int get_adj() const;
TAmanager &get_ta() const;
TAmanager &get_ta();
void set_delay(double x) { _delay=x; }
double get_delay() const { return _delay; }
void step_advance(int chan);
private:
enum Registers {
RW_FIFO = 0x0,
WCHAN = 0x4,
WADJCHAN = 0x8,
R_WC = 0xc,
RST = 0x10,
WDAC = 0x14,
PULSE = 0x18,
WCNTREG = 0x1c, // Write data to Control Reg (Xilinx) 5 bits
TROUT = 0x20,
TEVENT = 0x2c,
SOFTSTART = 0x24,
RWREG = 0x28, // Read data from Control Reg of chip (line 31)
SOFTCLK = 0x30,
REGCLK = 0x34, // Clock for register. No data
SHIFTIN_CPU = 0x38
};
enum CRAM {
// These variables for CRAM
MEMCH0 =0x00002000,
MEMCH1 =0x00004000,
FIFOCH0 =0x00000008,
FIFOCH1 =0x0000000c,
CHANNUM =0x00000004,
STATREG =0x00000002,
RESMOD =0x00000006,
WCCH1 =0x00000012,
WCCH0 =0x00000010,
TESTPA0 =0x00000014,
TESTPA1 =0x00000016
};
// Parameters
DAQpp::Par<int> *threshold;
DAQpp::Par<int> *nchip;
DAQpp::Par<int> *adj;
DAQpp::Par<int> *rout;
DAQpp::Par<TAmanager> *ta;
MyParameter<int> *threshold_p;
MyParameter<int> *nchip_p;
MyParameter<int> *adj_p;
MyParameter<int> *rout_p;
MyParameter<TAmanager> *ta_p;
// Callbacks
void change_threshold(int &x);
void change_nchip(int &x);
void change_adj(int &x);
void change_rout(int &x);
void change_ta(TAmanager &x);
void change_ped( std::vector<double> &) {}
int channel;
// This variables are used with dummy VME drivers to
// simulate the data
double _gain;
double _mean;
double _noise;
double _ped;
double _cmmd;
int _theChannel;
double _thePulse;
double _theGain;
double _theThreshold;
double _theThresholdGain;
double _theValue;
public:
//---------------------------------------------------------------------
// TA functions
//---------------------------------------------------------------------
// maks all the channels
void mask_all();
// masks a given channel
void mask_channel(int ch);
// selects one channel for triggering
void select_channel(int ch);
// enable all channels for triggering
void select_all();
// sets the gain
void set_gain(int ichip, int gain);
// send the TA mask to the chip
void load_register();
// sets the trim of a given channel
void trim_channel(int ch, int tr);
void set_scan_point(ScanPoint &);
void set_header_description(unsigned long &data);
unsigned long *write_pedestals(int &);
void test_channel(int);
};
template<class T>
class MyParameter : public Observer {
private:
MyModule *module;
void (MyModule::*func)(T &);
DAQpp::Par< T > *par;
public:
MyParameter(MyModule *m, void (MyModule::*f)(T &), DAQpp::Par< T > *p=0)
: module(m),func(f), par(0)
{
if (p)
set_par(p);
}
~MyParameter() {
par->deleteObserver( *dynamic_cast<Observer *>(this) );
}
void set_par( DAQpp::Par< T > *p) {
if (par)
par->deleteObserver( *dynamic_cast<Observer *>(this) );
par = p;
par->addObserver( *dynamic_cast<Observer *>(this) );
}
const MyModule *get_module() const { return module; }
void update(Observable *o, Argument *arg) {
Arg<T> *v = static_cast< Arg<T> * >(arg);
(module->*func)( v->get_value() );
}
};
/*
template<class T>
class MyParameter : public Observer, public DAQpp::Par<T> {
private:
MyModule *module;
void (MyModule::*func)(T &);
public:
MyParameter(MyModule *m,
void (MyModule::*f)(T &),
const char *s, const T &x = T())
: DAQpp::Par<T>(x),module(m),func(f)
{
DAQpp::Par<T>::addObserver( *dynamic_cast<Observer *>(this) );
module->add_par(s,dynamic_cast<DAQpp::Parameter *>(this));
}
~MyParameter() {
DAQpp::Par<T>::deleteObserver( *dynamic_cast<Observer *>(this) );
}
const MyModule *get_module() const { return module; }
void update(Observable *o, Argument *arg) {
Arg<T> *v = static_cast< Arg<T> * >(arg);
(module->*func)( v->get_value() );
}
};
*/
inline MyModule::~MyModule() {
std::cout << "...destroying Module " << get_id() << std::endl;
}
inline int MyModule::get_threshold() const {
return threshold->get_value();
}
inline int MyModule::get_rout() const {
return rout->get_value();
}
inline int MyModule::get_nchip() const {
return nchip->get_value();
}
inline int MyModule::get_adj() const {
return adj->get_value();
}
inline TAmanager &MyModule::get_ta() const {
return ta->get_value();
}
inline TAmanager &MyModule::get_ta() {
return ta->get_value();
}
#endif