/* flashy.c */ /* program for displaying the current processor usage as a bar of leds on the * parallel port. Uses the parapin driver/library http://parapin.sf.net */ /* 1/26/05 eric buehl; eric@thebuehls.com */ #define SAMPLE 500000 /* the delay time between sampling processor usage */ #include #include int main(int argc, char ** argv) { FILE * proc; char buffer[25]; long user_i=0,user=0; long nice_i=0,nice=0; long system_i=0,system=0; long idle_i=0,idle=0; float usage=0.0; int x=0; pin_init_user(LPT1); pin_mode(LP_PIN02, LP_OUTPUT); while(1) { clear_pin((1<<8)-1); x=((1<<((int)(7*usage+1)))-1); set_pin(x); if(argc-1) printf("%f %i\n",usage,x); proc=fopen("/proc/stat","r"); if(proc==NULL) return -1; /* seek to the part about the cpu; the part we want! */ while(strcmp(buffer,"cpu")&&buffer[0]!=EOF) fscanf(proc,"%s",&buffer); fscanf(proc,"%s",buffer); user_i=atoi(buffer); fscanf(proc,"%s",buffer); nice_i=atoi(buffer); fscanf(proc,"%s",buffer); system_i=atoi(buffer); fscanf(proc,"%s",buffer); idle_i=atoi(buffer); /* compute the current processor usage in percent as a decimal */ usage=1-((float)(idle_i-idle)/((user_i-user)+(nice_i-nice)+(system_i-system)+(idle_i-idle))); user=user_i; nice=nice_i; system=system_i; idle=idle_i; fclose(proc); usleep(SAMPLE); } }