MIDAS
Loading...
Searching...
No Matches
frontend.cxx
Go to the documentation of this file.
1/********************************************************************\
2
3 Name: frontend.c
4 Created by: Stefan Ritt
5
6 Contents: Experiment specific readout code (user part) of
7 Midas frontend. This example simulates a "trigger
8 event" and a "periodic event" which are filled with
9 random data.
10
11 The trigger event is filled with two banks (ADC0 and TDC0),
12 both with values with a gaussian distribution between
13 0 and 4096. About 100 event are produced per second.
14
15 The periodic event contains one bank (PRDC) with four
16 sine-wave values with a period of one minute. The
17 periodic event is produced once per second and can
18 be viewed in the history system.
19
20\********************************************************************/
21
22#undef NDEBUG // midas required assert() to be always enabled
23
24#include <stdio.h>
25#include <stdlib.h>
26#include <math.h>
27#include <assert.h> // assert()
28
29#include "midas.h"
30#include "experim.h"
31
32#include "mfe.h"
33
34/*-- Globals -------------------------------------------------------*/
35
36/* The frontend name (client name) as seen by other MIDAS clients */
37const char *frontend_name = "Sample Frontend";
38/* The frontend file name, don't change it */
39const char *frontend_file_name = __FILE__;
40
41/*-- Function declarations -----------------------------------------*/
42
43INT read_trigger_event(char *pevent, INT off);
44INT read_periodic_event(char *pevent, INT off);
45
47
48/*-- Equipment list ------------------------------------------------*/
49
51
53
54 {"Trigger", /* equipment name */
55 {1, 0, /* event ID, trigger mask */
56 "SYSTEM", /* event buffer */
57 EQ_POLLED, /* equipment type */
58 0, /* event source */
59 "MIDAS", /* format */
60 TRUE, /* enabled */
61 RO_RUNNING | /* read only when running */
62 RO_ODB, /* and update ODB */
63 100, /* poll for 100ms */
64 0, /* stop run after this event limit */
65 0, /* number of sub events */
66 0, /* don't log history */
67 "", "", "", "", "", 0, 0},
68 read_trigger_event, /* readout routine */
69 },
70
71 {"Periodic", /* equipment name */
72 {2, 0, /* event ID, trigger mask */
73 "SYSTEM", /* event buffer */
74 EQ_PERIODIC, /* equipment type */
75 0, /* event source */
76 "MIDAS", /* format */
77 TRUE, /* enabled */
78 RO_RUNNING | RO_TRANSITIONS | /* read when running and on transitions */
79 RO_ODB, /* and update ODB */
80 1000, /* read every 1000 msec (1 sec) */
81 0, /* stop run after this event limit */
82 0, /* number of sub events */
83 10, /* log history every ten seconds*/
84 "", "", "", "", "", 0, 0},
85 read_periodic_event, /* readout routine */
86 },
87
88 {""}
89};
90
91/********************************************************************\
92 Frontend callback routines
93
94 The function frontend_init gets called when the frontend program
95 is started. This routine should initialize the hardware, and can
96 optionally install several callback functions:
97
98 install_poll_event:
99 Install a function which gets called to check if a new event is
100 available for equipment of type EQ_POLLED.
101
102 install_frontend_exit:
103 Install a function which gets called when the frontend program
104 finishes.
105
106 install_begin_of_run:
107 Install a function which gets called when a new run gets started.
108
109 install_end_of_run:
110 Install a function which gets called when a new run gets stopped.
111
112 install_pause_run:
113 Install a function which gets called when a new run gets paused.
114
115 install_resume_run:
116 Install a function which gets called when a new run gets resumed.
117
118 install_frontend_loop:
119 Install a function which gets called inside the main event loop
120 as often as possible. This function gets all available CPU cycles,
121 so in order not to take 100% CPU, this function can use the
122 ss_sleep(10) function to give up some CPU cycles.
123
124 \********************************************************************/
125
126/*-- Frontend Init -------------------------------------------------*/
127
129{
130 /* install polling routine */
132
133 /* put any hardware initialization here */
134
135 /* print message and return FE_ERR_HW if frontend should not be started */
136 return SUCCESS;
137}
138
139/*------------------------------------------------------------------*/
140
141/********************************************************************\
142
143 Readout routines for different events
144
145\********************************************************************/
146
147/*-- Trigger event routines ----------------------------------------*/
148
150/* Polling routine for events. Returns TRUE if event
151 is available. If test equals TRUE, don't return. The test
152 flag is used to time the polling */
153{
154 int i;
155 DWORD flag;
156
157 for (i = 0; i < count; i++) {
158 /* poll hardware and set flag to TRUE if new event is available */
159 flag = TRUE;
160
161 if (flag)
162 if (!test)
163 return TRUE;
164 }
165
166 return 0;
167}
168
169/*-- Event readout -------------------------------------------------*/
170// This function gets called whenever poll_event() returns TRUE (the
171// MFE framework calls poll_event() regularly).
172
173INT read_trigger_event(char *pevent, INT off)
174{
175 UINT32 *pdata;
176
177 /* init bank structure */
178 bk_init(pevent);
179
180 /* create a bank called ADC0 */
181 bk_create(pevent, "ADC0", TID_UINT32, (void **)&pdata);
182
183 /* following code "simulates" some ADC data */
184 for (int i = 0; i < 4; i++)
185 *pdata++ = rand()%1024 + rand()%1024 + rand()%1024 + rand()%1024;
186
187 bk_close(pevent, pdata);
188
189 /* create another bank called TDC0 */
190 bk_create(pevent, "TDC0", TID_UINT32, (void **)&pdata);
191
192 /* following code "simulates" some TDC data */
193 for (int i = 0; i < 4; i++)
194 *pdata++ = rand()%1024 + rand()%1024 + rand()%1024 + rand()%1024;
195
196 bk_close(pevent, pdata);
197
198 /* limit event rate to 100 Hz. In a real experiment remove this line */
199 ss_sleep(10);
200
201 return bk_size(pevent);
202}
203
204/*-- Periodic event ------------------------------------------------*/
205// This function gets called periodically by the MFE framework (the
206// period is set in the EQUIPMENT structs at the top of the file).
207
208INT read_periodic_event(char *pevent, INT off)
209{
210 UINT32 *pdata;
211
212 /* init bank structure */
213 bk_init(pevent);
214
215 /* create a bank called PRDC */
216 bk_create(pevent, "PRDC", TID_UINT32, (void **)&pdata);
217
218 /* following code "simulates" some values in sine wave form */
219 for (int i = 0; i < 16; i++)
220 *pdata++ = 100*sin(M_PI*time(NULL)/60+i/2.0)+100;
221
222 bk_close(pevent, pdata);
223
224 return bk_size(pevent);
225}
const char * frontend_file_name
Definition frontend.cxx:25
INT frontend_init()
Frontend initialization.
Definition frontend.cxx:118
BOOL equipment_common_overwrite
Definition frontend.cxx:42
EQUIPMENT equipment[]
Definition frontend.cxx:81
const char * frontend_name
Definition frontend.cxx:23
INT poll_trigger_event(INT source, INT count, BOOL test)
Definition frontend.cxx:149
INT read_periodic_event(char *pevent, INT off)
Definition frontend.cxx:208
INT read_trigger_event(char *pevent, INT off)
Definition frontend.cxx:173
INT bk_close(void *event, void *pdata)
Definition midas.cxx:16780
void bk_init(void *event)
Definition midas.cxx:16406
void bk_create(void *event, const char *name, WORD type, void **pdata)
Definition midas.cxx:16561
INT bk_size(const void *event)
Definition midas.cxx:16495
unsigned int DWORD
Definition mcstd.h:51
#define SUCCESS
Definition mcstd.h:54
#define EQ_POLLED
Definition midas.h:415
#define RO_ODB
Definition midas.h:438
#define EQ_PERIODIC
Definition midas.h:414
#define TID_UINT32
Definition midas.h:337
#define RO_TRANSITIONS
Definition midas.h:434
#define RO_RUNNING
Definition midas.h:426
INT ss_sleep(INT millisec)
Definition system.cxx:3628
double count
Definition mdump.cxx:33
INT i
Definition mdump.cxx:32
void install_poll_event(INT(*f)(INT, INT, BOOL))
Definition mfed.cxx:54
DWORD BOOL
Definition midas.h:105
int INT
Definition midas.h:129
#define TRUE
Definition midas.h:182
unsigned int UINT32
Definition midas.h:140
program test
Definition miniana.f:6