Line data Source code
1 : /********************************************************************\
2 :
3 : Name: mdev.h
4 : Created by: Stefan Ritt
5 :
6 : Contents: Generic base class for MIDAS device drivers
7 : \********************************************************************/
8 :
9 : #ifndef MDEV_H
10 : #define MDEV_H
11 :
12 : extern int hs_define_panel2(const char *group, const char *panel, const std::vector<std::string> vars,
13 : const std::vector<std::string> label, const std::vector<std::string> formula,
14 : const std::vector<std::string> color = {});
15 :
16 : class mdev {
17 : protected:
18 : int m_event_id;
19 : std::string m_equipment_name;
20 : DWORD m_readout_period;
21 : DWORD m_readout_last;
22 :
23 : public:
24 : mdev(std::string equipment_name) {
25 : m_readout_period = 1000; // 1s by default
26 : m_readout_last = 0;
27 : m_equipment_name = equipment_name;
28 : if (!midas::odb::exists("/Equipment/" + equipment_name))
29 : mthrow("Cannot find equipment \"" + equipment_name + "\" in ODB");
30 :
31 : midas::odb o("/Equipment/" + equipment_name);
32 : try {
33 : m_event_id = o["Common"]["Event ID"];
34 : } catch (mexception &e) {
35 : mthrow("Cannot find \"Event ID\" under equipment \"" + equipment_name + "\" in ODB");
36 : }
37 : }
38 0 : virtual ~mdev(void) {} // virtual destructor for proper cleanup
39 :
40 : // getters and setters
41 : int get_event_id(void) { return m_event_id; }
42 : std::string get_equipment_name() { return m_equipment_name; }
43 :
44 : // pure virtual functions
45 : virtual void odb_setup(void) = 0;
46 : virtual void init(void) = 0;
47 : virtual void exit(void) = 0;
48 : virtual void loop(void) = 0;
49 0 : virtual int read_event(char *pevent, int off) { return 0; };
50 :
51 : void static mdev_odb_setup(std::vector<mdev *> &mdev_table) {
52 : // Call odb_setup for all devices
53 : for (mdev *m : mdev_table)
54 : m->odb_setup();
55 : }
56 :
57 : void static mdev_init(std::vector<mdev *> &mdev_table) {
58 : // Call odb_setup for all devices
59 : for (mdev *m : mdev_table)
60 : m->init();
61 : }
62 :
63 : void static mdev_loop(std::vector<mdev *> &mdev_table) {
64 : // Call loop() function of all devices to read data
65 : for (mdev *m : mdev_table)
66 : m->loop();
67 : }
68 :
69 : void define_history_panel(std::string panelName,
70 : std::vector<std::string> names,
71 : std::vector<std::string> labels = {},
72 : std::vector<std::string> formula = {},
73 : std::vector<std::string> color = {}
74 : )
75 : {
76 : std::vector<std::string> vars;
77 : for (size_t i=0 ; i<names.size() ; i++)
78 : vars.push_back(m_equipment_name + std::string(":") + names[i]);
79 :
80 : hs_define_panel2(m_equipment_name.c_str(), panelName.c_str(), vars, labels, formula, color);
81 : }
82 :
83 : void set_readout_period(int period_ms) {
84 : m_readout_period = period_ms;
85 : }
86 : };
87 :
88 : #endif // MDEV_H
|