shithub: oledsaver

ref: f89c25789fc5e3ea793d417b8a6cf70f5a5c7740
dir: /main.c/

View raw version
#include <u.h>
#include <libc.h>
#include <draw.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)
{
	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;
}

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 i;
	h = 32;
	w = 126;
	long ms;
	ms = 100;
	//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;
	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;

	ARGBEGIN{
	default:
		usage();
	}ARGEND;
	
	if (memimageinit())
		sysfatal("memimageinit failed: %r");

	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;	

		// clear the screen
		img = allocmemimage(Rect(0, 0, w, h), GREY1);
		if (!img)
			sysfatal("cannot allocate memimage: %r");
		memfillcolor(img, DBlack);

		/* call the independent drawing routine of choice */

		
		//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);
		}

		freememimage(img);
		
		sleep(ms);
	}
	
}