Skip to content

Instantly share code, notes, and snippets.

@biomood
Created March 18, 2012 17:30
Show Gist options
  • Save biomood/2078146 to your computer and use it in GitHub Desktop.
Save biomood/2078146 to your computer and use it in GitHub Desktop.
Lua library for accessing the framebuffer of a chumby
local screen = require("screen")
print("loaded screen")
img, success = screen.initScreen("/dev/fb0")
if success then
print("Opened frame buffer")
else
error("Faild to open frame buffer")
end
c, c2 = screen.colour(img, 255, 0, 255)
print("Set colour: "..c..", "..c2)
screen.background(img)
--screen.colour(img, 255, 0, 0)
--img = screen.pixel(img, 0, 239)
--screen.line(img, 0, 0, 319, 239)
--screen.line(img, 0, 239, 319, 0)
screen.draw(img)
/*
* A Lua library in C to display interface
* with the chumby frame buffer
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <math.h>
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
#define SCREEN "Screen"
#define SCREENMETA "Screen.meta"
#define FRAMESIZE 320*244*2
typedef struct {
FILE * fd;
char framebuffer[FRAMESIZE];
char colour[2];
}Screen;
// static Screen* checkScreen(lua_State *L, int index);
static void setPixel(int x, int y, Screen *scr);
static Screen* pushScreen(lua_State *L, Screen scr);
int iinit_screen(lua_State *L);
int iset_colour(lua_State *L);
int iset_pixel(lua_State *L);
int iset_background(lua_State *L);
int iset_line(lua_State *L);
int idraw_screen(lua_State *L);
int iclose(lua_State *L);
int luaopen_screen(lua_State *L);
const struct luaL_Reg screenLib[] = {
{"initScreen", iinit_screen},
{"colour", iset_colour},
{"pixel", iset_pixel},
{"background", iset_background},
{"draw", idraw_screen},
{"line", iset_line},
{NULL, NULL}
};
static Screen* pushScreen(lua_State *L, Screen scr)
{
Screen *pi = (Screen *)lua_newuserdata(L, sizeof(Screen));
*pi = scr;
luaL_getmetatable(L, SCREENMETA);
lua_setmetatable(L, -2);
return pi;
}
static Screen* checkScreen(lua_State *L, int index) {
void *ud = luaL_checkudata(L, 1, SCREENMETA);
luaL_argcheck(L, ud != NULL, 1, "`Screen' expected");
return (Screen*)ud;
}
static void setPixel(int x, int y, Screen *scr) {
int pos = (((320*y)+x)*2);
scr->framebuffer[pos] = scr->colour[0];
scr->framebuffer[pos+1] = scr->colour[1];
}
// create new screen
int iinit_screen(lua_State *L){
const char *serialport = luaL_checkstring(L, 1);
Screen scr;
scr.fd = fopen(serialport, "r+b");
pushScreen(L, scr);
int success = 1;
if (!scr.fd)
success = 0;
lua_pushboolean(L, success);
return 2;
}
int iset_colour(lua_State *L) {
Screen *scr = checkScreen(L, 1);
int r = luaL_checkint(L, 2);
int g = luaL_checkint(L, 3);
int b = luaL_checkint(L, 4);
short int colour16 = (short)(((b&0xf8)<<8) + ((r&0xfc)<<3) + ((g&0xf8)>>3));
scr->colour[0] = ((colour16 >> 8) & 0xff);
scr->colour[1] = ((colour16 >> 0) & 0xff);
lua_pushnumber(L, scr->colour[0]);
lua_pushnumber(L, scr->colour[1]);
return 2;
}
int iset_pixel(lua_State *L) {
Screen *scr = checkScreen(L, 1);
int x = luaL_checkint(L, 2);
int y = luaL_checkint(L, 3);
setPixel(x, y, scr);
// pushScreen(L, scr);
return 0;
}
int iset_background(lua_State *L) {
Screen *scr = checkScreen(L, 1);
int i;
for (i=0; i<320; i++) {
int j;
for (j=0; j<240; j++) {
setPixel(i, j, scr);
}
}
// pushScreen(L, scr);
return 0;
}
int iset_line(lua_State *L) {
Screen *scr = checkScreen(L, 1);
int x0 = luaL_checkint(L, 2);
int y0 = luaL_checkint(L, 3);
int x1 = luaL_checkint(L, 4);
int y1 = luaL_checkint(L, 5);
int dx = abs(x1-x0);
int dy = abs(y1-y0);
int sx, sy, err;
if (x0 < x1)
sx = 1;
else
sx = -1;
if (y0 < y1)
sy = 1;
else
sy = -1;
while (1) {
setPixel(x0, y0, scr);
if ((x0==x1)&&(y0==y1))
break;
int e2 = 2*err;
if (e2 > -dy) {
err = err - dy;
x0 = x0 + sx;
}
if (e2 < dx) {
err = err + dx;
y0 = y0 + sy;
}
}
// pushScreen(L, scr);
return 0;
}
int idraw_screen(lua_State *L) {
Screen *scr = checkScreen(L, 1);
rewind(scr->fd);
fwrite(scr->framebuffer, 1, FRAMESIZE, scr->fd);
return 0;
}
int iclose(lua_State *L) {
Screen *scr = checkScreen(L, 1);
fclose(scr->fd);
return 0;
}
int luaopen_screen(lua_State *L) {
luaL_newmetatable(L, SCREENMETA);
luaL_register(L, "screen", screenLib);
return 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment