Hardware Reference
In-Depth Information
for (;;) {
pipe = popen("ps -eo pcpu|sed 1d","r");
for ( total=0.0; fgets(buf,sizeof buf,pipe); ) {
sscanf(buf,"%f",&pct);
total += pct;
}
pclose(pipe);
printf("\r%.1f%% ",total);
fflush(stdout);
pwm_ratio(total,100);
usleep(300000);
}
In this section of code, we open a piped command to ps , with options to report the
percent of CPU used by each process. The sed command deletes the header line from the
ps command output.
The for loop reads each line, totaling the percent of CPU used. Occasionally, the
total exceeds 100% because of timing and other roughness in the calculations.
Once the CPU percent total is known, the function pwm_ratio() is called to alter the
ratio, thus changing the position of the meter's indicator.
1 /*********************************************************************
2 * pwm. c − PWM example program
3 *********************************************************************/
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <unistd.h>
8 #include <fcntl.h>
9 #include <sys/mman.h>
10 #include <errno . h>
11 #include <string . h>
12
13 #define BCM2835_PWM_CONTROL 0
14 #define BCM2835_PWM_STATUS 1
15 #define BCM2835_PWM0_RANGE 4
16 #define BCM2835_PWM0_DATA 5
17
18 #define BCM2708_PERI_BASE 0x20000000
19 #define BLOCK_SIZE (4*1024)
20
21 #define GPIO_BASE (BCM2708_PERI_BASE + 0x200000)
22 #define PWM_BASE (BCM2708_PERI_BASE + 0x20C000)
23 #define CLK_BASE (BCM2708_PERI_BASE + 0x101000)
24
 
Search WWH ::




Custom Search