树莓派--利用wiring库实现树莓派当前内存使用率实时显示

树莓派--利用wiring库实现树莓派当前内存使用率实时显示


显示板基于tm1616,具有两个8位数码管、两个LED灯和一个红外接收头。

引脚定义: 1、RCV(白) 2、SGND(橙) 3、VCC(蓝) 4、STB(红) 5、CLK(褐) 6、DIN(黑)

//数码管显示编码表 0 1 2 3 4 5 6 7 8 9
const uint8_t LedTab[] = {0x7B, 0x41, 0x6E, 0x6D, 0x55, 0x3D, 0x3F, 0x61, 0x7F, 0x7D};


显示板与树莓派连接实物图:



以下为tm1616芯片的驱动程序(使用wiring PI库控制gpio口)

#include 

#define MODE_SET       0x00
#define DISP_WRITE     0x44
#define DISP_OFF       0x80
#define DISP_ON        0x8f

#define STB_PIN        27
#define CLK_PIN        28
#define DIN_PIN        29

#define STB_PIN_HIGH   digitalWrite(STB_PIN,HIGH)   //STB
#define STB_PIN_LOW    digitalWrite(STB_PIN,LOW)
#define CLK_PIN_HIGH   digitalWrite(CLK_PIN,HIGH)   //CLK
#define CLK_PIN_LOW    digitalWrite(CLK_PIN,LOW)
#define DIN_PIN_HIGH   digitalWrite(DIN_PIN,HIGH)   //DIN
#define DIN_PIN_LOW    digitalWrite(DIN_PIN,LOW)


void disp_port_init();
void tm1616_write_byte(unsigned char byte);
void tm1616_write_data(unsigned char *data);

void tm1616_init()
{
   disp_port_init();
   delayMicroseconds(1);
   
   STB_PIN_HIGH;              //STB为高电平,CLK、DIO无效//端口配置初始化
   CLK_PIN_HIGH;
   DIN_PIN_HIGH;
   
   tm1616_write_byte(MODE_SET); //显示模式//0010 1110 SW2~3 PWM2~3 KEY0~3
   delayMicroseconds(1);
   
   STB_PIN_HIGH;
   tm1616_write_byte(0x80);     //显示关 //1000 0111  b7b6:10,显示控制;B4:0->0FF,1->ON;B3:0->1/3偏压
   delayMicroseconds(1);
}

void disp_port_init()
{
     wiringPiSetup();
     delayMicroseconds(1);
     
     pinMode(STB_PIN, OUTPUT);
     pinMode(CLK_PIN, OUTPUT);
     pinMode(DIN_PIN, OUTPUT);
}


void tm1616_write_byte(unsigned char byte)
{
     unsigned char i;
     
     STB_PIN_LOW;
     delayMicroseconds(1);

     for(i=0; i<8; i++)
     {
 	CLK_PIN_LOW;
		
	if(byte & 0x01)
	{
	   DIN_PIN_HIGH; 
	}
	else 
	{
	   DIN_PIN_LOW;
	}
   
        delayMicroseconds(1);
	CLK_PIN_HIGH;
	byte >>= 1;
     }

     delayMicroseconds(1);
     CLK_PIN_LOW;
}


void tm1616_write_data(unsigned char *data)
{
    STB_PIN_HIGH;
    tm1616_write_byte(MODE_SET);   //显示模式 
    delayMicroseconds(1);
   
    STB_PIN_HIGH;
    tm1616_write_byte(DISP_WRITE); //固定地址方式写数据到显示寄存器//0100 0100
   
    delayMicroseconds(1);
    
    STB_PIN_HIGH;
    tm1616_write_byte(0xc0);       //显示寄存器的00H单元开始
    tm1616_write_byte(data[0]);     //给显示寄存器写入数据
    delayMicroseconds(1);
   
    STB_PIN_HIGH;
    tm1616_write_byte(0xc2);       //显示寄存器的02H单元
    tm1616_write_byte(data[1]);
    delayMicroseconds(1);
   
    STB_PIN_HIGH;  
    tm1616_write_byte(0xc4);
    tm1616_write_byte(data[2]);
    delayMicroseconds(1);
   
    STB_PIN_HIGH;
    tm1616_write_byte(0xc6);
    tm1616_write_byte(data[3]);
    delayMicroseconds(1);
    
    STB_PIN_HIGH;
    tm1616_write_byte(0x8f);       //显示开//1000 1111   
    delayMicroseconds(1);
    STB_PIN_HIGH;
}

获取linux系统内存使用情况,并计算内存使用率的代码:

#include 
#include 

int get_meminfo()
{
    FILE * mem_fd;
    char line[29];
    int mem_total = 0, mem_free = 0, mem_use;

    system("cat /proc/meminfo > meminfo");
   
    mem_fd = fopen("meminfo","r");

    fread(line, 1, 28, mem_fd);
    line[28] = '\0';
//    printf("%s",line);    	

    int i = 0;
    while(!isdigit(line[i])) i++;	
    while(isdigit(line[i])){
         mem_total = mem_total*10 + line[i] - '0';
	 i++; 
    }
  //  printf("get the total mem is %d\n",mem_total);

    fread(line, 1, 27, mem_fd);
    line[27] = '\0';
 //   printf("%s\n",line);    	

    i = 0;  
    while(!isdigit(line[i])) i++;	
    while(isdigit(line[i])){
         mem_free = mem_free*10 + line[i] - '0';
	 i++; 
    }
 //   printf("get the free  mem is %d\n",mem_free);
  
    mem_use = ((mem_total-mem_free)*100)/mem_total;    
//    printf("get the useage of memory is %d\n",((mem_total-mem_free)*100)/mem_total);    

    fclose(mem_fd);
    return mem_use;
}

显示主循环:

#include 

//8位数码管显示表0~9
const unsigned char LedTab[] = {0x7B, 0x41, 0x6E, 0x6D, 0x55, 0x3D, 0x3F, 0x61, 0x7F, 0x7D};

//根据硬件定义显示数据结构
typedef union
{
  unsigned char buffer[4];	

   struct
   {
    unsigned char reserve1 :8;
    unsigned char ten      :8;
    unsigned char one      :8;
    unsigned char reserve2 :1;   
    unsigned char reserve3 :1;   
    unsigned char reserve4 :1;   
    unsigned char reserve5 :1;   
    unsigned char reserve6 :1;   
    unsigned char reserve7 :1;   
    unsigned char yellow   :1;   
    unsigned char green    :1;   
   }bit;

}DISPLAY_U;

DISPLAY_U display_info;

extern void  tm1616_init();
extern void tm1616_write_data(unsigned char *data);
extern int get_meminfo();

int main(int argc, char **argv)
{
   int mem_use;
 
   tm1616_init();

    while(1)
    {
	if(mem_use != get_meminfo())
	{
            mem_use = get_meminfo();   
            display_info.bit.yellow = 1;
            display_info.bit.ten = LedTab[mem_use/10];
            display_info.bit.one = LedTab[mem_use%10];
	    tm1616_write_data(display_info.buffer);
	}
	
	//printf("The usuage of memory is %d\n",mem_use);
	usleep(100000);
	//sleep(1);
    }

    return 0;
}

Makefile文件:

object = display.o drv_tm1616.o get_meminfo.o
cc = gcc

demo:${object}
	cc -o demo ${object} -lwiringPi

display.o:display.c
	cc -c display.c

drv_tm1616.o:drv_tm1616.c
	cc -c drv_tm1616.c

get_meminfo.o:get_meminfo.c
	cc -c get_meminfo.c

clean:
	rm -f *.o demo



你可能感兴趣的:(玩转树莓派)