shithub: oledsaver

Download patch

ref: b545233a9b54502f276f70a167f2b7f71c54161f
parent: 9d8d932b97da40af6d4c21208f0e51ccccf093c4
author: glenda <glenda@narshaddaa>
date: Mon May 1 04:04:42 EDT 2023

drawing in four directions and splitting out to a lib

--- a/bitart.c
+++ b/bitart.c
@@ -1,5 +1,7 @@
 #include <u.h>
 #include <libc.h>
+#include <draw.h>
+#include <memdraw.h>
 #include "bitart.h"
 
 int
@@ -42,4 +44,223 @@
 drand(int, int)
 {
 	return ntruerand(2);
-}
\ No newline at end of file
+}
+
+/* drawing order iterators */
+
+Memimage*
+topdown(drawfunc df, Memimage *img)
+{
+	Rectangle r;
+	
+	r.min.x = 0;
+	r.min.y = 0;
+	r.max.x = 1;
+	r.max.y = 1;	
+
+	int y, x, fd;
+	for(y = 0; y < h; y++){
+		// successive writes will fail if do not close/open
+		fd = open(path, OWRITE);
+		if(fd < 0){
+			sysfatal("could not open kbdoled file → %r");
+		}
+
+
+		for(x = 0; x < w; x++){
+			Memimage *todraw = memblack;
+			if(df(x, y))
+				todraw = memwhite;
+		
+			r.min.x = x;
+			r.min.y = y;
+
+			r.max.x = r.min.x+1;
+			r.max.y = r.min.y+1;
+
+			memimagedraw(img, r, todraw, ZP, nil, ZP, SoverD);
+
+		}
+		img = writeuncompressed(fd, img);
+		
+		// flush
+		fd = close(fd);
+		if(fd < 0)
+			sysfatal("close failed → %r");
+		sleep(ms);
+	}
+	return img;
+}
+
+Memimage*
+bottomup(drawfunc df, Memimage *img)
+{
+	Rectangle r;
+	
+	r.min.x = 0;
+	r.min.y = 0;
+	r.max.x = 1;
+	r.max.y = 1;	
+
+	int y, x, fd;
+	for(y = h-1; y >=0; y--){
+		// successive writes will fail if do not close/open
+		fd = open(path, OWRITE);
+		if(fd < 0){
+			sysfatal("could not open kbdoled file → %r");
+		}
+
+
+		for(x = 0; x < w; x++){
+			Memimage *todraw = memblack;
+			if(df(x, y))
+				todraw = memwhite;
+		
+			r.min.x = x;
+			r.min.y = y;
+
+			r.max.x = r.min.x+1;
+			r.max.y = r.min.y+1;
+
+			memimagedraw(img, r, todraw, ZP, nil, ZP, SoverD);
+
+		}
+		img = writeuncompressed(fd, img);
+		
+		// flush
+		fd = close(fd);
+		if(fd < 0)
+			sysfatal("close failed → %r");
+		sleep(ms);
+	}
+	return img;
+}
+
+Memimage*
+leftright(drawfunc df, Memimage *img)
+{
+	Rectangle r;
+	
+	r.min.x = 0;
+	r.min.y = 0;
+	r.max.x = 1;
+	r.max.y = 1;	
+
+	int y, x, fd;
+	for(x = 0; x < w; x++){
+		// successive writes will fail if do not close/open
+		fd = open(path, OWRITE);
+		if(fd < 0){
+			sysfatal("could not open kbdoled file → %r");
+		}
+
+
+		for(y = 0; y < h; y++){
+			Memimage *todraw = memblack;
+			if(df(x, y))
+				todraw = memwhite;
+		
+			r.min.x = x;
+			r.min.y = y;
+
+			r.max.x = r.min.x+1;
+			r.max.y = r.min.y+1;
+
+			memimagedraw(img, r, todraw, ZP, nil, ZP, SoverD);
+
+		}
+		img = writeuncompressed(fd, img);
+		
+		// flush
+		fd = close(fd);
+		if(fd < 0)
+			sysfatal("close failed → %r");
+		sleep(ms);
+	}
+	return img;
+}
+
+Memimage*
+rightleft(drawfunc df, Memimage *img)
+{
+	Rectangle r;
+	
+	r.min.x = 0;
+	r.min.y = 0;
+	r.max.x = 1;
+	r.max.y = 1;	
+
+	int y, x, fd;
+	for(x = w-1; x >= 0; x--){
+		// successive writes will fail if do not close/open
+		fd = open(path, OWRITE);
+		if(fd < 0){
+			sysfatal("could not open kbdoled file → %r");
+		}
+
+
+		for(y = 0; y < h; y++){
+			Memimage *todraw = memblack;
+			if(df(x, y))
+				todraw = memwhite;
+		
+			r.min.x = x;
+			r.min.y = y;
+
+			r.max.x = r.min.x+1;
+			r.max.y = r.min.y+1;
+
+			memimagedraw(img, r, todraw, ZP, nil, ZP, SoverD);
+
+		}
+		img = writeuncompressed(fd, img);
+		
+		// flush
+		fd = close(fd);
+		if(fd < 0)
+			sysfatal("close failed → %r");
+		sleep(ms);
+	}
+	return img;
+}
+
+// unloadimage will replace data in the image
+// so we build a new image as we unbuild the current one
+// free the old image
+// return the new image ptr
+Memimage*
+writeuncompressed(int fd, Memimage *m)
+{
+	char chanstr[32];
+	int bpl, y, j;
+	uchar *buf;
+	Memimage *out;
+	Rectangle r;
+	
+	out = allocmemimage(Rect(0, 0, w, h), GREY1);
+
+	if(chantostr(chanstr, m->chan) == nil)
+		sysfatal("can't convert channel descriptor: %r");
+	fprint(fd, "%11s %11d %11d %11d %11d ",
+		chanstr, m->r.min.x, m->r.min.y, m->r.max.x, m->r.max.y);
+
+	bpl = bytesperline(m->r, m->depth);
+	buf = malloc(bpl);
+	if(buf == nil)
+		sysfatal("malloc failed: %r");
+	for(y=m->r.min.y; y<m->r.max.y; y++){
+		r = Rect(m->r.min.x, y, m->r.max.x, y+1);
+		j = unloadmemimage(m, r, buf, bpl);
+		loadmemimage(out, r, buf, bpl);
+		if(j != bpl)
+			sysfatal("image unload failed → %r");
+
+		if(write(fd, buf, bpl) != bpl)
+			sysfatal("wu write failed → %r");
+	}
+	
+	free(buf);
+	freememimage(m);
+	
+	return out;
+}
--- a/bitart.h
+++ b/bitart.h
@@ -1,3 +1,10 @@
+extern int h, w;
+extern char *path;
+extern long ms;
+
+typedef int drawfunc(int x, int x);
+typedef Memimage* iterfunc(drawfunc, Memimage*);
+
 int
 ob0(int x, int y);
 
@@ -18,3 +25,18 @@
 
 int
 drand(int, int);
+
+Memimage*
+topdown(drawfunc df, Memimage *img);
+
+Memimage*
+bottomup(drawfunc df, Memimage *img);
+
+Memimage*
+leftright(drawfunc df, Memimage *img);
+
+Memimage*
+rightleft(drawfunc df, Memimage *img);
+
+Memimage*
+writeuncompressed(int fd, Memimage *m);
--- a/main.c
+++ b/main.c
@@ -8,11 +8,8 @@
 #define H 32
 #define W 126
 #define NDRAWFUNCS 7
-#define NITERFUNCS 1
+#define NITERFUNCS 4
 
-typedef int drawfunc(int x, int x);
-typedef Memimage* iterfunc(drawfunc, Memimage*);
-
 int h, w;
 char *path;
 long ms;
@@ -23,111 +20,16 @@
 	sysfatal("usage: %s [ -f subfont ] text", argv0);
 }
 
-// unloadimage will replace data in the image
-// so we build a new image as we unbuild the current one
-// free the old image
-// return the new image ptr
-Memimage*
-writeuncompressed(int fd, Memimage *m)
-{
-	char chanstr[32];
-	int bpl, y, j;
-	uchar *buf;
-	Memimage *out;
-	Rectangle r;
-	
-	out = allocmemimage(Rect(0, 0, w, h), GREY1);
-
-	if(chantostr(chanstr, m->chan) == nil)
-		sysfatal("can't convert channel descriptor: %r");
-	fprint(fd, "%11s %11d %11d %11d %11d ",
-		chanstr, m->r.min.x, m->r.min.y, m->r.max.x, m->r.max.y);
-
-	bpl = bytesperline(m->r, m->depth);
-	buf = malloc(bpl);
-	if(buf == nil)
-		sysfatal("malloc failed: %r");
-	for(y=m->r.min.y; y<m->r.max.y; y++){
-		r = Rect(m->r.min.x, y, m->r.max.x, y+1);
-		j = unloadmemimage(m, r, buf, bpl);
-		loadmemimage(out, r, buf, bpl);
-		if(j != bpl)
-			sysfatal("image unload failed → %r");
-
-		if(write(fd, buf, bpl) != bpl)
-			sysfatal("wu write failed → %r");
-	}
-	
-	free(buf);
-	freememimage(m);
-	
-	return out;
-}
-
-Memimage*
-topdown(drawfunc df, Memimage *img)
-{
-	Rectangle r;
-	
-	r.min.x = 0;
-	r.min.y = 0;
-	r.max.x = 1;
-	r.max.y = 1;	
-
-	int y, x, fd;
-	for(y = 0; y < h; y++){
-		// successive writes will fail if do not close/open
-		fd = open(path, OWRITE);
-		if(fd < 0){
-			sysfatal("could not open kbdoled file → %r");
-		}
-
-
-		for(x = 0; x < w; x++){
-			Memimage *todraw = memblack;
-			if(df(x, y))
-				todraw = memwhite;
-		
-			r.min.x = x;
-			r.min.y = y;
-
-			r.max.x = r.min.x+1;
-			r.max.y = r.min.y+1;
-
-			memimagedraw(img, r, todraw, ZP, nil, ZP, SoverD);
-
-		}
-		img = writeuncompressed(fd, img);
-		
-		// flush
-		fd = close(fd);
-		if(fd < 0)
-			sysfatal("close failed → %r");
-		sleep(ms);
-	}
-	return img;
-}
-
-
 void
 main(int argc, char **argv)
 {
 	Memimage *img;
-	# 126 x 32 oled ⇒ 4032 pixels
+	// 126 x 32 oled ⇒ 4032 pixels
 	int iteri, drawi;
 	h = 32;
 	w = 126;
 	ms = 100;	// « change to ≤10 to get a kernel fault 
-	//Point topleft;
-	//double angle;
-	//angle = 0.0;
-	Rectangle pixel;
-	pixel.min.x = -1;
-	pixel.min.y = 1;
-	pixel.max.y = 2;
-	pixel.max.x = 2;
 	path = "/mnt/reform/kbdoled";
-	//u8int fb[H][W];	// framebuffer
 	
 	drawfunc *drawtab[NDRAWFUNCS] = {
 		ob0,
@@ -141,6 +43,9 @@
 	
 	iterfunc *itertab[NITERFUNCS] = {
 		topdown,
+		bottomup,
+		leftright,
+		rightleft,
 	};
 
 	ARGBEGIN{
@@ -155,6 +60,7 @@
 	for(;;){
 		iteri = ntruerand(NITERFUNCS);
 		drawi = ntruerand(NDRAWFUNCS);
+
 		// blank the screen
 		img = allocmemimage(Rect(0, 0, w, h), GREY1);
 		if (!img)
@@ -164,16 +70,9 @@
 		memfillcolor(img, DBlack);
 
 		/* call the independent drawing routine of choice */
-		itertab[iteri](drawtab[drawi], img);
+		itertab[iteri](drawtab[drawi], img);		
 
-		
-		//angle = truerand();
-
-		
-
 		freememimage(img);
-		
-		//sleep(ms);
 	}
 	
 }
\ No newline at end of file