aboutsummaryrefslogblamecommitdiff
path: root/rpi/max7219/lajimon_w.c
blob: 31c7c461ca6bfaf6243658ba159e1dec12b8f056 (plain) (tree)





















































































































                                                                              
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <time.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <unistd.h>
#include "max7219.h"
int pipefd[2][2],term,pid;
char buf[1024];
char disp[32][8];
double histcpu[16],histmem[8],histsw[8];
void _child()
{
	close(pipefd[0][0]);close(pipefd[1][1]);
	dup2(pipefd[1][0],STDIN_FILENO);
	dup2(pipefd[0][1],STDOUT_FILENO);
	close(pipefd[0][1]);close(pipefd[1][0]);
	execlp("/usr/bin/ksysguardd","ksysguardd",NULL);
}
void terminate(int e)
{
	term=1;
}
void display()
{
	unsigned short batch[4];
	int dd[32]={0},i,j;
	for(i=0;i<32;++i)
	for(j=0;j<8;++j)
	dd[j+(i&0x18)]|=disp[i][j]<<(i&7);
	for(i=0;i<8;++i)
	{
		for(j=0;j<4;++j)
			batch[j]=(i+1)<<8|dd[j*8+i];
		max7219_batch(batch);
	}
}
void _main()
{
	int i,j,r;
	term=0;
	signal(SIGINT,terminate);
	signal(SIGTERM,terminate);
	signal(SIGQUIT,terminate);
	close(pipefd[0][1]);
	close(pipefd[1][0]);
	max7219_init();
	sleep(1);
	read(pipefd[0][0],buf,1024);
	while(!term)
	{
		write(pipefd[1][1],"cpu/system/TotalLoad\n",21);
		r=read(pipefd[0][0],buf,1024);buf[r]=0;
		if(r<=0)continue;
		sscanf(buf,"%lf",&histcpu[15]);
		{
			int memtot,memavail,swtot,swfree,tr=0;
			FILE* fm=fopen("/proc/meminfo","r");
			while(fgets(buf,1024,fm))
			{
				tr+=sscanf(buf,"MemTotal: %d",&memtot);
				tr+=sscanf(buf,"MemAvailable: %d",&memavail);
				tr+=sscanf(buf,"SwapTotal: %d",&swtot);
				tr+=sscanf(buf,"SwapFree: %d",&swfree);
			}
			if(tr-4)continue;
			histmem[7]=100*(1-1.*memavail/memtot);
			histsw[7]=100*(1-1.*swfree/swtot);
		}
		for(i=0;i<15;++i)histcpu[i]=histcpu[i+1];
		for(i=0;i<7;++i)histmem[i]=histmem[i+1],histsw[i]=histsw[i+1];
		printf("%f %f %f\n",histcpu[15],histmem[7],histsw[7]);
		memset(disp,0,sizeof(disp));
		for(i=0;i<16;++i)
		for(j=0;j<8;++j)
		if(histcpu[i]>11*(j+1))disp[i][j]=1;
		for(i=0;i<8;++i)
		for(j=0;j<8;++j)
		{
			if(histmem[i]>11*(j+1))disp[16+i][j]=1;
			if(histsw[i]>11*(j+1))disp[24+i][j]=1;
		}
		disp[0][7]=disp[1][7]=disp[2][7]=1;
		disp[0][6]=1;
		disp[0][5]=disp[1][5]=disp[2][5]=1;
		disp[16][7]=disp[17][7]=disp[18][7]=1;
		disp[16][6]=disp[17][6]=disp[18][6]=1;
		disp[16][5]=disp[18][5]=1;
		disp[24][7]=disp[25][7]=1;
		disp[24][6]=disp[25][6]=disp[26][6]=1;
		disp[25][5]=disp[26][5]=1;
		display();
		sleep(2);
	}
	max7219_deinit(1);
	write(pipefd[1][1],"quit\n",5);
	waitpid(pid,NULL,0);
	close(pipefd[0][0]);
	close(pipefd[1][1]);
}
int main()
{
	if(pipe(pipefd[0]))return puts("pipe()"),1;
	if(pipe(pipefd[1]))return puts("pipe()"),1;
	switch(pid=fork())
	{
		case -1:return puts("fork()"),1;
		case 0:
			_child();
		break;
		default:
			_main();
		break;
	}
	return 0;
}