shithub: oledsaver

Download patch

ref: f89c25789fc5e3ea793d417b8a6cf70f5a5c7740
parent: d4fa0493e43ad94c1f419d162e0d1ab224a33cf5
author: glenda <glenda@narshaddaa>
date: Mon May 1 03:01:28 EDT 2023

active drawing process works!

--- a/main.c
+++ b/main.c
@@ -1,10 +1,17 @@
 #include <u.h>
 #include <libc.h>
 #include <draw.h>
-#include "memdraw.h"
+#include <memdraw.h>
+#include "bitart.h"
 
 #define NPIXELS 4032
+#define H 32
+#define W 126
+#define NDRAWFUNCS 7
 
+typedef int drawfunc(int x, int x);
+int h, w;
+
 void
 usage(void)
 {
@@ -11,12 +18,20 @@
 	sysfatal("usage: %s [ -f subfont ] text", argv0);
 }
 
-void
+// 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");
@@ -28,29 +43,77 @@
 	if(buf == nil)
 		sysfatal("malloc failed: %r");
 	for(y=m->r.min.y; y<m->r.max.y; y++){
-		j = unloadmemimage(m, Rect(m->r.min.x, y, m->r.max.x, y+1), buf, bpl);
+		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)
+
 		if(write(fd, buf, bpl) != bpl)
 			sysfatal("wu write failed → %r");
 	}
+	
 	free(buf);
+	freememimage(m);
+	
+	return out;
 }
 
+int
+ob0(int x, int y)
+{
+	return ((-((-y) ^ (-x))) % (-((x - y) / (x / x)))) % 12;
+}
 
+int
+ob1(int x, int y)
+{
+	return ((-((y^23)|(x%2)))-(((~y)*(~y))%((-x)+(y*x))))%7;
+}
+
+int
+ob2(int x, int y)
+{
+	return ((-((x+23)-(-x)))%(~(~(14-y))))%6;
+}
+
+int
+ob3(int x, int y)
+{
+	return ((-(-(x|x)))%(((-x)^(y+x))/((y/x)-(x^x))))%3;
+}
+
+int
+ob4(int x, int y)
+{
+	return (((~(x/y))*(-(-y)))-(-((y-y)|(-x))))%11;
+}
+
+int
+ob5(int x, int y)
+{
+	return (((-(y|y))/((-y)+(y&x)))/(((y*y)&(~x))+((x/y)&(13&x))))%4;
+}
+
+int
+drand(int, int)
+{
+	return ntruerand(2);
+}
+
+
 void
 main(int argc, char **argv)
 {
 	Memimage *img;
 	# 126 x 32 oled ⇒ 4032 pixels
-	int h,w,i;
+	int i;
 	h = 32;
 	w = 126;
 	long ms;
-	ms = 1000;
-	Point topleft;
-	double angle;
+	ms = 100;
+	//Point topleft;
+	//double angle;
 	//angle = 0.0;
 	Rectangle pixel;
 	pixel.min.x = -1;
@@ -59,6 +122,16 @@
 	pixel.max.x = 2;
 	char *path;
 	path = "/mnt/reform/kbdoled";
+	//u8int fb[H][W];	// framebuffer
+	drawfunc *tab[NDRAWFUNCS] = {
+		ob0,
+		ob1,//general kenobi! *growls* << "hello there"
+		ob2,
+		ob3,
+		ob4,
+		ob5,
+		drand,
+	};
 
 	Rectangle r;
 	int fd;
@@ -71,43 +144,61 @@
 	if (memimageinit())
 		sysfatal("memimageinit failed: %r");
 
-	for(;;){
+	for(i = 0;; i++){
+		if(i >= NDRAWFUNCS)
+			i = 0;
+		print("i=%d\n", i);
+
+		
+		// clear rectangle brush position
 		r.min.x = 0;
 		r.min.y = 0;
 		r.max.x = 1;
-		r.max.y = 1;
-		angle = truerand();
+		r.max.y = 1;	
 
-		fd = open(path, OWRITE);
-		if(fd < 0){
-			sysfatal("could not open kbdoled file → %r");
-		}
-
+		// clear the screen
 		img = allocmemimage(Rect(0, 0, w, h), GREY1);
 		if (!img)
 			sysfatal("cannot allocate memimage: %r");
 		memfillcolor(img, DBlack);
-		topleft.y = 0;
-		topleft.x = 0;
-		for(i=0;i<w;i+=3){
 
-			r.min.x = abs(i%w);
-			r.min.y = abs(((int)(50.0*sin(angle*PI/180)))%h);
-			r.max.x = r.min.x+1;
-			r.max.y = r.min.y+1;
-			//fprint(2, "(x=%d,y=%d) to (x=%d,y=%d)\n", r.min.x, r.min.y, r.max.x, r.max.y);
+		/* call the independent drawing routine of choice */
 
-			memimagedraw(img, r, memwhite, ZP, nil, ZP, SoverD);
-			
 		
-			angle+=5;
+		//angle = truerand();
+
+		int y, x;
+		for(y = 0; y < h; y++){
+			fd = open(path, OWRITE);
+			if(fd < 0){
+				sysfatal("could not open kbdoled file → %r");
+			}
+
+
+			for(x = 0; x < w; x++){
+				Memimage *todraw = memblack;
+				if(tab[i](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);
 		}
 
-		writeuncompressed(fd, img);
 		freememimage(img);
-		fd = close(fd);
-		if(fd < 0)
-			sysfatal("close failed → %r");
 		
 		sleep(ms);
 	}