Imported Upstream version 5.1
[debian/gcpegg] / eggui.c
1 /* PROGRAM:     eggsh
2  * FILE:        $Header: /home/egg/src/RCS/eggui.c,v 1.8 1999/02/28 20:02:54 ghn Exp $
3  * PURPOSE:     EGG site user interface
4  * AUTHOR:      Greg Nelson
5  * DATE:        98-06-28
6  *
7  * REVISED:
8  * $Log: eggui.c,v $
9  * Revision 1.8  1999/02/28 20:02:54  ghn
10  * Version 5.1: The user interface now leaves the cursor at the bottom of
11  * the screen and responds to ^L to redraw the entire screen.
12  *
13  * Revision 1.7  1999/01/01 23:58:56  ghn
14  * Take out "CPU_BOUND" conditionals; we won't run CPU_BOUND any more.
15  *
16  * Revision 1.6  1998/12/31 22:07:56  ghn
17  * Rev 5 code: includes multi-reg support, HTML, etc.
18  *
19  * Revision 1.5  1998/08/03 20:36:28  kelvin
20  * Show time of last data collection by basket.
21  *
22  * Revision 1.4  1998/08/01  21:33:28  ghn
23  * Added real implementation for ncursed() based UI for egg.
24  *
25  * Revision 1.3  1998/08/01 18:51:25  ghn
26  * Added John's byte-order-independence changes.
27  *
28  * Revision 1.2  1998/08/01 17:05:48  ghn
29  * Trivial additions of curses related code.
30  *
31  * Revision 1.1  1998/07/21 11:41:23  ghn
32  * Initial revision
33  *
34  * Copyright 1998 - Greg Nelson
35  */
36
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <curses.h>
40
41 /*  Curses takes it upon itself to define TRUE and FALSE,
42     incompatibly, as it happens, with the definitions in
43     global.h.  So, clear out the CURSES definitions.  */
44 #ifdef TRUE
45 #undef TRUE
46 #endif
47 #ifdef FALSE
48 #undef FALSE
49 #endif
50
51 #include "global.h"
52 #include "genlib.h"
53 #include "errnos.h"
54 #include "regs.h"
55 #include "version.h"
56
57 #ifndef NO_UI
58 static int32 inittm = 0;
59 #endif
60
61 extern int32 lastDataSent;
62 extern int32 time_latency, time_housekeeping;
63
64 /* Initialize user interface, as needed. */
65 int32 UIInit(void) {
66 #ifndef NO_UI
67   initscr(); 
68   cbreak(); noecho(); 
69   nonl();
70   intrflush(stdscr, FALSE);
71   keypad(stdscr, TRUE);
72   timeout(0);
73   clear();
74   move(0, 0);
75   printw("Please wait, initializing...");
76   refresh();
77   inittm = 0;
78 #endif
79   return ERR_NONE;
80 }
81
82 /* Close down user interface */
83 int32 UIClose(void) {
84 #ifndef NO_UI
85   endwin();
86 #endif
87   return ERR_NONE;
88 }
89
90 /* Update user interface once on every data collection, receiving the
91    current collection result and CollectRecord. */
92 int32 UIUpdate(int32 cres, CollectRecord *coll) {
93 #ifndef NO_UI
94   char *tmstr;
95   double recavg;
96   static double grandavg = 0;
97   static int32 totsamp = 0;
98   int32 i, line, now;
99
100   if ((i = getch()) != ERR) {
101     if (i == 12) clear();
102   }
103
104   if (inittm == 0) {
105     inittm = getzulutime(NULL);
106     clear();
107   }
108
109   /* Show protocol. */
110
111   if (coll->sampct == 1) {
112     line = 11;
113     move(line++, 5); printw("Samples per record: %3d", (int)coll->opts.samp_rec);
114     move(line++, 5); printw("Seconds per record: %3d", (int)coll->opts.sec_rec);
115     move(line++, 5); printw("Records per packet: %3d", (int)coll->opts.rec_pkt);
116     move(line++, 5); printw("Bits per trial:     %3d", (int)coll->opts.trialsz);
117     refresh();
118   }
119
120   if (cres < 0) return ERR_NONE;
121
122   line = 0;
123   move(line++, 3);
124   printw("EGG %s ID %d REG %s %s", eggtable[0].name, eggtable[0].id,
125          configuredREG->reg_name, Version);
126
127   tmstr = ctime(&inittm); tmstr[strlen(tmstr)-1] = 0;
128   move(line++, 5);
129   printw("Up since       %25s", tmstr);
130
131   move(line++, 5);
132   now = getzulutime(NULL);
133   tmstr = ctime(&now); tmstr[strlen(tmstr)-1] = 0;
134   printw("Last sample at %25s", tmstr);
135
136   move(line++, 5);
137   tmstr = ctime(&lastDataSent);
138   tmstr[strlen(tmstr)-1] = 0;
139   printw("Last packet at %25s", lastDataSent == 0 ? "Never" : tmstr);
140
141   /* Note that for a non-CPU_BOUND built, UIUpdate is passed a
142      collection record filled with the last 10 samples collected.
143      Since missing samples do not figure in this record, there
144      is no need to test EGG_MISSING_DATA when computing the
145      mean below. */
146
147   /* Show mean since egg started and, each time 10 samples
148      are collected, the mean for the last 10 samples. */
149
150   line++;
151   grandavg *= totsamp;
152   grandavg += coll->data.trials[coll->sampct-1]; totsamp++;
153   grandavg /= totsamp;
154   move(line++, 5);
155   printw("Grand mean:       %6.2f", grandavg);
156
157   if (cres == 1) {
158     for (recavg = 0, i = 0; i < coll->opts.samp_rec; i++)
159       recavg += coll->data.trials[i];
160     recavg /= coll->opts.samp_rec;
161     move(line, 5); 
162     printw("Last record mean: %6.2f (%d seconds)", recavg, coll->opts.sec_rec);
163   }
164   line++;                             /* Advance whether we show record mean or not */
165
166   /* Show latency (lapse between start of second as measured by
167      the computer's clock and when collection of the sample
168      actually began) and time elapsed in housekeeping (saving
169      packets in local files, talking to the basket, updating
170      the user interface, etc.) following collection of the sample.
171      If housekeeping time frequently consumes a substantial portion
172      of the inter-sample interval, the probability of lost
173      samples increases.  Both times are shown in milliseconds. */
174
175   line++;
176   move(line++, 5);
177   printw("Sampling latency:   ");
178   clrtoeol();
179   printw("%.3f ms", time_latency / 1000.0);
180   move(line++, 5);
181   printw("Housekeeping time:  ");
182   clrtoeol();
183   printw("%.3f ms", time_housekeeping / 1000.0);
184
185   move(16, 0);
186
187   refresh();
188 #endif
189
190   return ERR_NONE;
191 }