023bed186ee0dae06a6c883a698b2f1b0aff4d12
[svn42.git] / old-door-deamon / libparportled-0.5.1 / led-stat.txt
1 >From joev@mikasa.WPI.EDU Sun Jan  1 18:34:40 EST 1995
2 Article: 144 of comp.os.linux.development.system
3 Path: bigblue.oit.unc.edu!concert!gatech!bloom-beacon.mit.edu!news2.near.net!news.mathworks.com!bigboote.WPI.EDU!mikasa.WPI.EDU!joev
4 From: joev@mikasa.WPI.EDU (Joseph W. Vigneau)
5 Newsgroups: comp.os.linux.development.system
6 Subject: My LED performance meter thing...
7 Date: 1 Jan 1995 23:18:28 GMT
8 Organization: Worcester Polytechnic Institute
9 Lines: 147
10 Message-ID: <3e7d84$or5@bigboote.WPI.EDU>
11 NNTP-Posting-Host: mikasa.wpi.edu
12
13 Last week, someone wrote here asking about robotic contol via Linux... I
14 responded explaining how I used the parallel port to control a sort-of
15 "performance meter".  I've recieved a lot of requests on how I did this.
16 Here's the story:
17
18 Last year, at school (WPI), Silicon Graphics brought this huge 18-wheel
19 truck that is basically a demonstrationmobile.  Inside it had Indys,
20 Crimsons, and some of their big machines like a couple of Onyxes and an
21 *monsterous* Power Challenge.. I'm relatively young (17 at the time), and
22 I'd never seen a computer this big before: It looked like an oversized
23 refrigerator, with cooling ducts running in and out of it!  On the front of
24 this beast, was a little LCD backlit readout about the size of my HP-48G's
25 display.  It was labeled "CPU Activity", and had a little bar chart showing
26 how hard each processor was working.  I thought it would be cool to have one
27 of these mounted on my Linux box :)
28
29 I finally got a computer of my own this past November, and finally got to run
30 Linux on my own, instead of administering it for a number of people on my
31 floor.  I had a Shack attack, and went to Radio Shack, and picked up the 
32 following items: a breadboard, a 10-bar LED, breadboard wires, and a 
33 package of assorted resistors. [Side note: While at the Shack with a few of
34 my suitemates, we were way in back in the component section (the only good
35 part of the store any more), and were approached by a lady who thought we
36 were employees there :). Back to the project.]
37
38 It was wired up like this, via the parallel port:
39
40                                 pin 20 (ground)
41                                   |
42                150ohm     LED     |
43 pin 2 (D0) ----/\/\/------|>|-----+
44                                   |
45 pin 3 (D1) ----/\/\/------|>|-----+
46                                   |
47 pin 4 (D2) ----/\/\/------|>|-----+
48
49             [...]
50                                   |
51 pin 9 (D7) ----/\/\/------|>|-----+
52
53 Note: 2 of the LEDs weren't connected.
54
55 Now, the software part:
56
57 Two files were used: the first is a routine written by a roomate
58 (damianf@wpi.edu) used to blast raw bytes at a port, and read them. Please
59 contact him for more info, or if you want to use it in a progrm of your own.
60
61 static inline int port_in( int port )
62 {
63    unsigned char value;
64   __asm__ volatile ("inb %1,%0"
65                     : "=a" (value)
66                     : "d" ((unsigned short)port));
67    return value;
68 }
69
70 static inline void port_out( unsigned short int port, unsigned char val )
71 {
72   __asm__ volatile (
73                     "outb %0,%1\n"
74                     :
75                     : "a" (val), "d" (port)
76                     );
77 }
78
79 I originally wanted to use the load average to determine how many of the
80 LEDs lit up, but realized that it was only updated every minute.. I wanted a
81 display similar to xload or xosview, but I really coun't figure out how they
82 were determined.. What I ended up doing was reading the output of 'ps aux',
83 and summing up the %CPU column.  I then converted that into a number
84 representing how many LEDs should light, and blast it at the printer port.
85
86 NOTE: I wrote and built this thing in a bout 90 minutes, so it's quick and
87 dirty, and not at all as elegant as I hoped it to be.
88
89 Here's the program:
90
91 /* meter.c by Joseph W. Vigneau (joev@wpi.edu) (c)1994.
92    This program is covered under the GNU copyleft agreement.
93 */
94
95 #include <stdio.h>
96 #include <unistd.h>
97 #include <errno.h>
98 #include <time.h>
99 #include "port.h"
100
101 float loadavg(void)
102 {
103   FILE *f;
104   char line[80];
105   float cpu = 0.0, totalcpu = 0.0;
106
107   if((f = popen("/bin/ps -aux","r"))==NULL) {
108     fprintf(stderr,"Couldn't fork /bin/ps.\n");
109     exit(1);
110   }
111
112   fgets(line, 80, f);
113   while(!feof(f)) {
114     sscanf(line,"%*s %*d %f",&cpu);
115     totalcpu += cpu;
116     fgets(line, 80, f);
117   }
118 /*  printf("TOTAL: %f\n",totalcpu); */
119   pclose(f);
120   return totalcpu/100.0;
121 }
122
123
124 main()
125 {
126   unsigned char lights;
127   float ave;
128   char dir = 0;
129   char foo[10];
130   register unsigned char numlights, i;
131
132   if(ioperm(0x378,1,1)) {
133     fprintf(stderr,"ioperm error.\n");
134     exit(1);
135   }
136
137   while(1) {
138     ave = loadavg();
139     numlights = (int)(ave*8.0);
140 /*    printf("ave = %f, numlights = %d\n",ave, numlights); */
141
142     lights = 0;
143     for(i=0;i<numlights;i++)
144       lights |= (1<<i);
145
146     port_out(0x378, lights);
147     usleep(750000L);
148   }
149 }
150
151 This program has to be suid root, due to the ioperm call.
152
153 Good luck, and have fun! Your mileage may vary.. If this makes your computer
154 explode or something, I'm not responsible, etc..  If you improve upon this,
155 post it here, so everyone else can see!
156
157 -- 
158 joev@wpi.edu               WPI Computer Science '97                      Linux!
159              <a href="http://www.wpi.edu/~joev"> Click Here! </a>
160
161
162
163
164
165
166