shithub: oledsaver

ref: 9d8d932b97da40af6d4c21208f0e51ccccf093c4
dir: oledsaver/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
#define NITERFUNCS 1

typedef int drawfunc(int x, int x);
typedef Memimage* iterfunc(drawfunc, Memimage*);

int h, w;
char *path;
long ms;

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

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
	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,
		ob1,//general kenobi! *growls* « "hello there"
		ob2,
		ob3,
		ob4,
		ob5,
		drand,
	};
	
	iterfunc *itertab[NITERFUNCS] = {
		topdown,
	};

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

	// FOR EVER
	for(;;){
		iteri = ntruerand(NITERFUNCS);
		drawi = ntruerand(NDRAWFUNCS);
		// blank the screen
		img = allocmemimage(Rect(0, 0, w, h), GREY1);
		if (!img)
			sysfatal("cannot allocate memimage: %r");

		// white on black; bg is black
		memfillcolor(img, DBlack);

		/* call the independent drawing routine of choice */
		itertab[iteri](drawtab[drawi], img);

		
		//angle = truerand();

		

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