More dynamic vfdload
[de200vfd.git] / clients / vfdload.c
blob8fe05322840ba7cdc6be8ab136fcacb67065c221
1 // ****************************************************************************
2 // vfdload.c (C) 1992-2003 Christophe de Dinechin (ddd)
3 // Activity project
4 // ****************************************************************************
5 //
6 // File Description:
7 //
8 // Display the current load using a bar graph
9 //
10 //
11 //
12 //
13 //
14 //
15 //
16 //
17 // ****************************************************************************
18 // This document is confidential.
19 // Do not redistribute without written permission
20 // ****************************************************************************
21 // * File : $RCSFile$
22 // * Revision : $Revision$
23 // * Date : $Date$
24 // ****************************************************************************
26 #define VFDCLIENT "vfdload"
27 #define VFDOPTIONS "vfdload.tbl"
28 #define VFDCONFIG "load"
29 long refresh_rate = 100000;
31 #include "vfdclient.h"
33 typedef struct load
34 // ----------------------------------------------------------------------------
35 // Information about system load
36 // ----------------------------------------------------------------------------
38 unsigned long user, sys, nice, idle, total;
39 } load_t;
42 int main(int argc, char *argv[])
43 // ----------------------------------------------------------------------------
44 // Main entry point
45 // ----------------------------------------------------------------------------
47 int vfd;
48 vfd_configuration cf;
49 int x, y, w, h;
50 float load1, load2, load3;
51 int h1, h2, h3;
52 int by;
53 load_t cur, prev, diff;
54 int user[256], sys[256];
55 int index = 0;
56 int i;
58 vfd_options(argc, argv);
59 vfd = vfd_open(server, port);
61 // Use configuration
62 cf = vfd_configure(vfd, cfg);
63 x = cf.x; y = cf.y; w = cf.w; h = cf.h;
64 by = y + h - 1;
66 while (1)
68 FILE *f = fopen ("/proc/stat", "r");
69 fscanf(f, "cpu %lu %lu %lu %lu",
70 &cur.user, &cur.nice, &cur.sys, &cur.idle);
71 fclose(f);
72 cur.total = cur.user + cur.nice + cur.sys + cur.idle;
74 diff.user = cur.user - prev.user;
75 diff.sys = cur.sys - prev.sys;
76 diff.nice = cur.nice - prev.nice;
77 diff.idle = cur.idle - prev.idle;
78 diff.total = cur.total - prev.total;
80 prev = cur;
82 if (!diff.total)
83 continue;
85 user[index] = (h -2) * (diff.user + diff.nice) / diff.total;
86 sys[index] = (h - 2) * diff.sys / diff.total;
88 vfd_clear(vfd);
89 vfd_rectangle(vfd, x, y, w, h);
90 for (i = 0; i < w; i++)
92 int user_h = user[(index-i) & 255];
93 int sys_h = sys[(index - i) & 255];
94 vfd_fill_rectangle(vfd,
95 x + w - 1 - i, by - user_h,
96 1, user_h);
97 vfd_fill_rectangle(vfd,
98 x + w - 1 - i, y,
99 1, sys_h);
101 vfd_update(vfd);
103 index = (index + 1) & 255;
104 usleep(refresh_rate);
106 return 0;