From 07493b94b141506e051b0adb9f68132ebfc583c0 Mon Sep 17 00:00:00 2001 From: Chris Xiong Date: Thu, 8 Nov 2018 19:47:52 +0800 Subject: Added the max7219 stuff. --- rpi/max7219/Makefile | 17 +++++++ rpi/max7219/README.md | 2 + rpi/max7219/lajimon_w.c | 118 +++++++++++++++++++++++++++++++++++++++++++++ rpi/max7219/max7219.c | 84 ++++++++++++++++++++++++++++++++ rpi/max7219/max7219.h | 19 ++++++++ rpi/max7219/max7219_test.c | 15 ++++++ 6 files changed, 255 insertions(+) create mode 100644 rpi/max7219/Makefile create mode 100644 rpi/max7219/README.md create mode 100644 rpi/max7219/lajimon_w.c create mode 100644 rpi/max7219/max7219.c create mode 100644 rpi/max7219/max7219.h create mode 100644 rpi/max7219/max7219_test.c diff --git a/rpi/max7219/Makefile b/rpi/max7219/Makefile new file mode 100644 index 0000000..3e5b214 --- /dev/null +++ b/rpi/max7219/Makefile @@ -0,0 +1,17 @@ +LINK=-lwiringPi + +all:lajimon_w +test:max7219_test +max7219.o: + gcc max7219.c -c -o max7219.o +lajimon_w.o: + gcc lajimon_w.c -c -o lajimon_w.o +lajimon_w:lajimon_w.o max7219.o + gcc lajimon_w.o max7219.o -o lajimon_w $(LINK) +max7219_test.o: + gcc max7219_test.c -c -o max7219_test.o +max7219_test: + gcc max7219.o max7219_test.o -o max7219_test $(LINK) + +clean: + rm *.o max7219_test lajimon_w diff --git a/rpi/max7219/README.md b/rpi/max7219/README.md new file mode 100644 index 0000000..0ad35c5 --- /dev/null +++ b/rpi/max7219/README.md @@ -0,0 +1,2 @@ +A basic interface to the max7219 LED matrix / 7-segment display driver. +Also a stupid system monitor using it. diff --git a/rpi/max7219/lajimon_w.c b/rpi/max7219/lajimon_w.c new file mode 100644 index 0000000..31c7c46 --- /dev/null +++ b/rpi/max7219/lajimon_w.c @@ -0,0 +1,118 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#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; +} diff --git a/rpi/max7219/max7219.c b/rpi/max7219/max7219.c new file mode 100644 index 0000000..33a7fed --- /dev/null +++ b/rpi/max7219/max7219.c @@ -0,0 +1,84 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "max7219.h" + +#define wait nanosleep(&cl,NULL) +const struct timespec cl={0,10}; +/* + * _sendpacket(d) + * sends a 16 bit packet to the data line. + * for internal usage only. + */ +void _sendpacket(unsigned short d) +{ + unsigned char i; + for(i=16;i>0;--i) + { + unsigned short mask=1<<(i-1); + digitalWrite(P_CLK,0);wait; + digitalWrite(P_DATA,(d&mask)?1:0); + wait;digitalWrite(P_CLK,1);wait; + } +} +/* + * max7219_send(r,d,chip) + * sets the value of register #d in chip #chip to d. + * chips are 0-indexed. chip=-1 means send to all chips. + */ +void max7219_send(unsigned char r,unsigned char d,int chip) +{ + digitalWrite(P_CS,0);wait; + for(int i=0;i +#include "max7219.h" +int main() +{ + max7219_init(); + for(int c=0;c<4;++c) + { + for(int cc=0;cc<4;++cc) + for(int i=0;i<8;++i) + max7219_send(i+1,c==cc?(1<<(i+1))-1:0,cc); + sleep(2); + } + max7219_deinit(1); + return 0; +} -- cgit v1.2.3