aboutsummaryrefslogtreecommitdiff
path: root/rpi
diff options
context:
space:
mode:
authorGravatar Chris Xiong <chirs241097@gmail.com> 2018-11-08 19:47:52 +0800
committerGravatar Chris Xiong <chirs241097@gmail.com> 2018-11-08 19:47:52 +0800
commit07493b94b141506e051b0adb9f68132ebfc583c0 (patch)
tree9962ad4f3794a30e11b2666ee1548eee28f22505 /rpi
parentedd226da50ab7e960aee5e12004d9e8c42e23d24 (diff)
downloadoddities-07493b94b141506e051b0adb9f68132ebfc583c0.tar.xz
Added the max7219 stuff.
Diffstat (limited to 'rpi')
-rw-r--r--rpi/max7219/Makefile17
-rw-r--r--rpi/max7219/README.md2
-rw-r--r--rpi/max7219/lajimon_w.c118
-rw-r--r--rpi/max7219/max7219.c84
-rw-r--r--rpi/max7219/max7219.h19
-rw-r--r--rpi/max7219/max7219_test.c15
6 files changed, 255 insertions, 0 deletions
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 <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;
+}
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 <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <signal.h>
+#include <time.h>
+#include <wiringPi.h>
+#include <sys/wait.h>
+#include <fcntl.h>
+#include <unistd.h>
+#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<NCHIPS;++i)
+ _sendpacket(chip==-1||i+chip==NCHIPS-1?(r<<8)|d:0);
+ digitalWrite(P_CS,1);wait;
+}
+/*
+ * max7219_batch(d)
+ * sends a batch of data to the chips. each element in d
+ * is a register-data pair. length of d should equal to
+ * the number of chips.
+ * the first chip receives the first data pair,
+ * the second chip receives the second one etc.
+ */
+void max7219_batch(unsigned short *d)
+{
+ digitalWrite(P_CS,0);wait;
+ for(int i=0;i<NCHIPS;++i)
+ _sendpacket(d[NCHIPS-1-i]);
+ digitalWrite(P_CS,1);wait;
+}
+/*
+ * max7219_init()
+ * initialize all chips, putting them into matrix mode.
+ */
+void max7219_init()
+{
+ wiringPiSetup();
+ pinMode(P_DATA,OUTPUT);
+ pinMode(P_CS,OUTPUT);
+ pinMode(P_CLK,OUTPUT);
+ max7219_send(SCAN_LIMIT,7,-1);
+ max7219_send(DECODE_MODE,0,-1);
+ max7219_send(DISPLAY_TEST,0,-1);
+ max7219_send(INTENSITY,1,-1);
+ max7219_send(SHUTDOWN,1,-1);
+}
+/*
+ * max7219_deinit(clear)
+ * putting all chips into sleep, optionally clearing
+ * all the registers when `clear` is set
+ */
+void max7219_deinit(int clear)
+{
+ max7219_send(SHUTDOWN,0,-1);
+ if(clear)
+ for(int i=0;i<8;++i)
+ max7219_send(i+1,0,-1);
+}
diff --git a/rpi/max7219/max7219.h b/rpi/max7219/max7219.h
new file mode 100644
index 0000000..2e5b1ba
--- /dev/null
+++ b/rpi/max7219/max7219.h
@@ -0,0 +1,19 @@
+#define NCHIPS 4
+//pins
+#define P_DATA 0
+#define P_CS 1
+#define P_CLK 2
+//registers
+#define NO_OP 0x00
+//register #1 to #8 are for the digits
+#define DECODE_MODE 0x09
+#define INTENSITY 0x0a
+#define SCAN_LIMIT 0x0b
+#define SHUTDOWN 0x0c
+#define DISPLAY_TEST 0x0f
+
+void max7219_send(unsigned char r,unsigned char d,int chip);
+void max7219_batch(unsigned short *d);
+void max7219_init();
+void max7219_deinit(int clear);
+
diff --git a/rpi/max7219/max7219_test.c b/rpi/max7219/max7219_test.c
new file mode 100644
index 0000000..d01a32c
--- /dev/null
+++ b/rpi/max7219/max7219_test.c
@@ -0,0 +1,15 @@
+#include <unistd.h>
+#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;
+}