extend ucg module

This commit is contained in:
devsaurus 2015-08-09 01:38:16 +02:00
parent 75041a32b5
commit ac1b5a1173
3 changed files with 661 additions and 10 deletions

View File

@ -47,7 +47,7 @@ static lucg_userdata_t *get_lud( lua_State *L )
}
// helper function: retrieve given number of integer arguments
static void lucg_get_int_args( lua_State *L, uint8_t stack, uint8_t num, uint8_t *args)
static void lucg_get_int_args( lua_State *L, uint8_t stack, uint8_t num, ucg_int_t *args)
{
while (num-- > 0)
{
@ -79,11 +79,202 @@ static int lucg_clearScreen( lua_State *L )
if ((lud = get_lud( L )) == NULL)
return 0;
ucg_ClearScreen( LUCG );
return 0;
}
// Lua: ucg.draw90Line( self, x, y, len, dir, col_idx )
static int lucg_draw90Line( lua_State *L )
{
lucg_userdata_t *lud;
if ((lud = get_lud( L )) == NULL)
return 0;
ucg_int_t args[5];
lucg_get_int_args( L, 2, 5, args );
ucg_Draw90Line( LUCG, args[0], args[1], args[2], args[3], args[4] );
return 0;
}
// Lua: ucg.drawBox( self, x, y, w, h )
static int lucg_drawBox( lua_State *L )
{
lucg_userdata_t *lud;
if ((lud = get_lud( L )) == NULL)
return 0;
ucg_int_t args[4];
lucg_get_int_args( L, 2, 4, args );
ucg_DrawBox( LUCG, args[0], args[1], args[2], args[3] );
return 0;
}
// Lua: ucg.drawGradientBox( self, x, y, w, h )
static int lucg_drawGradientBox( lua_State *L )
{
lucg_userdata_t *lud;
if ((lud = get_lud( L )) == NULL)
return 0;
ucg_int_t args[4];
lucg_get_int_args( L, 2, 4, args );
ucg_DrawGradientBox( LUCG, args[0], args[1], args[2], args[3] );
return 0;
}
// Lua: ucg.drawHLine( self, x, y, len )
static int lucg_drawHLine( lua_State *L )
{
lucg_userdata_t *lud;
if ((lud = get_lud( L )) == NULL)
return 0;
ucg_int_t args[3];
lucg_get_int_args( L, 2, 3, args );
ucg_DrawHLine( LUCG, args[0], args[1], args[2] );
return 0;
}
// Lua: ucg.drawLine( self, x1, y1, x2, y2 )
static int lucg_drawLine( lua_State *L )
{
lucg_userdata_t *lud;
if ((lud = get_lud( L )) == NULL)
return 0;
ucg_int_t args[4];
lucg_get_int_args( L, 2, 4, args );
ucg_DrawLine( LUCG, args[0], args[1], args[2], args[3] );
return 0;
}
// Lua: ucg.drawPixel( self, x, y )
static int lucg_drawPixel( lua_State *L )
{
lucg_userdata_t *lud;
if ((lud = get_lud( L )) == NULL)
return 0;
ucg_int_t args[2];
lucg_get_int_args( L, 2, 2, args );
ucg_DrawPixel( LUCG, args[0], args[1] );
return 0;
}
// Lua: ucg.drawString( self, x, y, dir, str )
static int lucg_drawString( lua_State *L )
{
lucg_userdata_t *lud;
if ((lud = get_lud( L )) == NULL)
return 0;
ucg_int_t args[3];
lucg_get_int_args( L, 2, 3, args );
const char *s = luaL_checkstring( L, 2 );
if (s == NULL)
return 0;
ucg_DrawString( LUCG, args[0], args[1], args[2], s );
return 0;
}
// Lua: ucg.drawTriangle( self, x0, y0, x1, y1, x2, y2 )
static int lucg_drawTriangle( lua_State *L )
{
lucg_userdata_t *lud;
if ((lud = get_lud( L )) == NULL)
return 0;
ucg_int_t args[6];
lucg_get_int_args( L, 2, 6, args );
ucg_DrawTriangle( LUCG, args[0], args[1], args[2], args[3], args[4], args[5] );
return 0;
}
// Lua: ucg.drawVLine( self, x, y, len )
static int lucg_drawVLine( lua_State *L )
{
lucg_userdata_t *lud;
if ((lud = get_lud( L )) == NULL)
return 0;
ucg_int_t args[3];
lucg_get_int_args( L, 2, 3, args );
ucg_DrawVLine( LUCG, args[0], args[1], args[2] );
return 0;
}
// Lua: ucg.getHeight( self )
static int lucg_getHeight( lua_State *L )
{
lucg_userdata_t *lud;
if ((lud = get_lud( L )) == NULL)
return 0;
lua_pushinteger( L, ucg_GetHeight( LUCG ) );
return 1;
}
// Lua: ucg.getWidth( self )
static int lucg_getWidth( lua_State *L )
{
lucg_userdata_t *lud;
if ((lud = get_lud( L )) == NULL)
return 0;
lua_pushinteger( L, ucg_GetWidth( LUCG ) );
return 1;
}
// Lua: ucg.setClipRange( self, x, y, w, h )
static int lucg_setClipRange( lua_State *L )
{
lucg_userdata_t *lud;
if ((lud = get_lud( L )) == NULL)
return 0;
ucg_int_t args[4];
lucg_get_int_args( L, 2, 4, args );
ucg_SetClipRange( LUCG, args[0], args[1], args[2], args[3] );
return 0;
}
// Lua: ucg.setColor( self, [idx], r, g, b )
static int lucg_setColor( lua_State *L )
{
@ -92,10 +283,10 @@ static int lucg_setColor( lua_State *L )
if ((lud = get_lud( L )) == NULL)
return 0;
uint8_t args[3];
ucg_int_t args[3];
lucg_get_int_args( L, 2, 3, args );
int16_t opt = luaL_optint( L, (1+3) + 1, -1 );
ucg_int_t opt = luaL_optint( L, (1+3) + 1, -1 );
if (opt < 0)
ucg_SetColor( LUCG, 0, args[0], args[1], args[2] );
@ -116,6 +307,8 @@ static int lucg_setFont( lua_State *L )
ucg_fntpgm_uint8_t *font = (ucg_fntpgm_uint8_t *)lua_touserdata( L, 2 );
if (font != NULL)
ucg_SetFont( LUCG, font );
else
luaL_argerror(L, 2, "font data expected");
return 0;
}
@ -148,6 +341,21 @@ static int lucg_print( lua_State *L )
return 0;
}
// Lua: ucg.setFontMode( self, fontmode )
static int lucg_setFontMode( lua_State *L )
{
lucg_userdata_t *lud;
if ((lud = get_lud( L )) == NULL)
return 0;
ucg_int_t fontmode = luaL_checkinteger( L, 2 );
ucg_SetFontMode( LUCG, fontmode );
return 0;
}
// Lua: ucg.setPrintPos( self, x, y )
static int lucg_setPrintPos( lua_State *L )
{
@ -156,7 +364,7 @@ static int lucg_setPrintPos( lua_State *L )
if ((lud = get_lud( L )) == NULL)
return 0;
uint8_t args[2];
ucg_int_t args[2];
lucg_get_int_args( L, 2, 2, args );
lud->tx = args[0];
@ -165,6 +373,71 @@ static int lucg_setPrintPos( lua_State *L )
return 0;
}
// Lua: ucg.setPrintDir( self, dir )
static int lucg_setPrintDir( lua_State *L )
{
lucg_userdata_t *lud;
if ((lud = get_lud( L )) == NULL)
return 0;
lud->tdir = luaL_checkinteger( L, 2 );
return 0;
}
// Lua: ucg.setRotate90( self )
static int lucg_setRotate90( lua_State *L )
{
lucg_userdata_t *lud;
if ((lud = get_lud( L )) == NULL)
return 0;
ucg_SetRotate90( LUCG );
return 0;
}
// Lua: ucg.setRotate180( self )
static int lucg_setRotate180( lua_State *L )
{
lucg_userdata_t *lud;
if ((lud = get_lud( L )) == NULL)
return 0;
ucg_SetRotate180( LUCG );
return 0;
}
// Lua: ucg.setRotate270( self )
static int lucg_setRotate270( lua_State *L )
{
lucg_userdata_t *lud;
if ((lud = get_lud( L )) == NULL)
return 0;
ucg_SetRotate270( LUCG );
return 0;
}
// Lua: ucg.undoRotate( self )
static int lucg_undoRotate( lua_State *L )
{
lucg_userdata_t *lud;
if ((lud = get_lud( L )) == NULL)
return 0;
ucg_UndoRotate( LUCG );
return 0;
}
static int16_t ucg_com_esp8266_hw_spi(ucg_t *ucg, int16_t msg, uint16_t arg, uint8_t *data)
@ -333,12 +606,30 @@ static int lucg_ili9341_18x240x320_hw_spi( lua_State *L )
static const LUA_REG_TYPE lucg_display_map[] =
{
{ LSTRKEY( "begin" ), LFUNCVAL( lucg_begin ) },
{ LSTRKEY( "clearScreen" ), LFUNCVAL( lucg_clearScreen ) },
{ LSTRKEY( "setColor" ), LFUNCVAL( lucg_setColor ) },
{ LSTRKEY( "setFont" ), LFUNCVAL( lucg_setFont ) },
{ LSTRKEY( "print" ), LFUNCVAL( lucg_print ) },
{ LSTRKEY( "setPrintPos" ), LFUNCVAL( lucg_setPrintPos ) },
{ LSTRKEY( "begin" ), LFUNCVAL( lucg_begin ) },
{ LSTRKEY( "clearScreen" ), LFUNCVAL( lucg_clearScreen ) },
{ LSTRKEY( "draw90Line" ), LFUNCVAL( lucg_draw90Line ) },
{ LSTRKEY( "drawBox" ), LFUNCVAL( lucg_drawBox ) },
{ LSTRKEY( "drawGradientBox" ), LFUNCVAL( lucg_drawGradientBox ) },
{ LSTRKEY( "drawHLine" ), LFUNCVAL( lucg_drawHLine ) },
{ LSTRKEY( "drawLine" ), LFUNCVAL( lucg_drawLine ) },
{ LSTRKEY( "drawPixel" ), LFUNCVAL( lucg_drawPixel ) },
{ LSTRKEY( "drawString" ), LFUNCVAL( lucg_drawString ) },
{ LSTRKEY( "drawTriangle" ), LFUNCVAL( lucg_drawTriangle ) },
{ LSTRKEY( "drawVLine" ), LFUNCVAL( lucg_drawVLine ) },
{ LSTRKEY( "getHeight" ), LFUNCVAL( lucg_getHeight ) },
{ LSTRKEY( "getWidth" ), LFUNCVAL( lucg_getWidth ) },
{ LSTRKEY( "print" ), LFUNCVAL( lucg_print ) },
{ LSTRKEY( "setClipRange" ), LFUNCVAL( lucg_setClipRange ) },
{ LSTRKEY( "setColor" ), LFUNCVAL( lucg_setColor ) },
{ LSTRKEY( "setFont" ), LFUNCVAL( lucg_setFont ) },
{ LSTRKEY( "setFontMode" ), LFUNCVAL( lucg_setFontMode ) },
{ LSTRKEY( "setPrintDir" ), LFUNCVAL( lucg_setPrintDir ) },
{ LSTRKEY( "setPrintPos" ), LFUNCVAL( lucg_setPrintPos ) },
{ LSTRKEY( "setRotate90" ), LFUNCVAL( lucg_setRotate90 ) },
{ LSTRKEY( "setRotate180" ), LFUNCVAL( lucg_setRotate180 ) },
{ LSTRKEY( "setRotate270" ), LFUNCVAL( lucg_setRotate270 ) },
{ LSTRKEY( "undoRotate" ), LFUNCVAL( lucg_undoRotate ) },
{ LSTRKEY( "__gc" ), LFUNCVAL( lucg_close_display ) },
#if LUA_OPTIMIZE_MEMORY > 0
@ -354,7 +645,11 @@ const LUA_REG_TYPE lucg_map[] =
#if LUA_OPTIMIZE_MEMORY > 0
// Register fonts
{ LSTRKEY( "font_helvB08_hr" ), LUDATA( (void *)(ucg_font_helvB08_hr) ) },
{ LSTRKEY( "font_helvB10_hr" ), LUDATA( (void *)(ucg_font_helvB10_hr) ) },
{ LSTRKEY( "font_helvB12_hr" ), LUDATA( (void *)(ucg_font_helvB12_hr) ) },
{ LSTRKEY( "font_ncenR12_tr" ), LUDATA( (void *)(ucg_font_ncenR12_tr) ) },
{ LSTRKEY( "font_ncenR14_hr" ), LUDATA( (void *)(ucg_font_ncenR14_hr) ) },
// Font modes
{ LSTRKEY( "FONT_MODE_TRANSPARENT" ), LNUMVAL( UCG_FONT_MODE_TRANSPARENT ) },
@ -381,7 +676,11 @@ LUALIB_API int luaopen_ucg( lua_State *L )
// Module constants
// Register fonts
MOD_REG_LUDATA( L, "font_helvB08_hr", (void *)(ucg_font_helvB08_hr) );
MOD_REG_LUDATA( L, "font_helvB10_hr", (void *)(ucg_font_helvB10_hr) );
MOD_REG_LUDATA( L, "font_helvB12_hr", (void *)(ucg_font_helvB12_hr) );
MOD_REG_LUDATA( L, "font_ncenR12_tr", (void *)(ucg_font_ncenR12_tr) );
MOD_REG_LUDATA( L, "font_ncenR14_hr", (void *)(ucg_font_ncenR14_hr) );
// Font modes
MOD_REG_NUMBER( L, "FONT_MODE_TRANSPARENT", UCG_FONT_MODE_TRANSPARENT );

View File

@ -0,0 +1,324 @@
-- setup SPI and connect display
function init_spi_display()
-- Hardware SPI CLK = GPIO14
-- Hardware SPI MOSI = GPIO13
-- Hardware SPI MISO = GPIO12 (not used)
-- CS, D/C, and RES can be assigned freely to available GPIOs
local cs = 8 -- GPIO15, pull-down 10k to GND
local dc = 4 -- GPIO2
local res = 0 -- GPIO16
spi.setup(1, spi.MASTER, spi.CPOL_LOW, spi.CPHA_LOW, spi.DATABITS_8, 0)
disp = ucg.ili9341_18x240x320_hw_spi(cs, dc, res)
end
-- switch statement http://lua-users.org/wiki/SwitchStatement
function switch(c)
local swtbl = {
casevar = c,
caseof = function (self, code)
local f
if (self.casevar) then
f = code[self.casevar] or code.default
else
f = code.missing or code.default
end
if f then
if type(f)=="function" then
return f(self.casevar,self)
else
error("case "..tostring(self.casevar).." not a function")
end
end
end
}
return swtbl
end
z = 127 -- start value
function lcg_rnd()
z = bit.band(65 * z + 17, 255)
return z
end
function ucglib_graphics_test()
--ucg.setMaxClipRange()
disp:setColor(0, 0, 40, 80)
disp:setColor(1, 80, 0, 40)
disp:setColor(2, 255, 0, 255)
disp:setColor(3, 0, 255, 255)
disp:drawGradientBox(0, 0, disp:getWidth(), disp:getHeight())
disp:setColor(255, 168, 0)
disp:setPrintDir(0)
disp:setPrintPos(2, 18)
disp:print("Ucglib")
disp:setPrintPos(2, 18+20)
disp:print("GraphicsTest")
end
function cross()
local mx, my
disp:setColor(0, 250, 0, 0)
disp:setColor(1, 255, 255, 30)
disp:setColor(2, 220, 235, 10)
disp:setColor(3, 205, 0, 30)
disp:drawGradientBox(0, 0, disp:getWidth(), disp:getHeight())
mx = disp:getWidth() / 2
my = disp:getHeight() / 2
disp:setColor(0, 255, 255, 255)
disp:setPrintPos(2,18)
disp:print("Cross")
disp:setColor(0, 0, 0x66, 0xcc)
disp:setPrintPos(mx+15, my-5)
disp:print("dir0")
disp:setPrintPos(mx+5, my+26)
disp:print("dir1")
disp:setPrintPos(mx-40, my+20)
disp:print("dir2")
disp:setPrintPos(mx+5,my-25)
disp:print("dir3")
disp:setColor(0, 0, 0x66, 0xff)
disp:setColor(1, 0, 0x66, 0xcc)
disp:setColor(2, 0, 0, 0x99)
disp:draw90Line(mx+2, my-1, 20, 0, 0)
disp:draw90Line(mx+2, my, 20, 0, 1)
disp:draw90Line(mx+2, my+1, 20, 0, 2)
disp:draw90Line(mx+1, my+2, 20, 1, 0)
disp:draw90Line(mx, my+2, 20, 1, 1)
disp:draw90Line(mx-1, my+2, 20, 1, 2)
disp:draw90Line(mx-2, my+1, 20, 2, 0)
disp:draw90Line(mx-2, my, 20, 2, 1)
disp:draw90Line(mx-2, my-1, 20, 2, 2)
disp:draw90Line(mx-1, my-2, 20, 3, 0)
disp:draw90Line(mx, my-2, 20, 3, 1)
disp:draw90Line(mx+1, my-2, 20, 3, 2)
end
function pixel_and_lines()
local mx
local x, xx
mx = disp:getWidth() / 2
--my = disp:getHeight() / 2
disp:setColor(0, 0, 0, 150)
disp:setColor(1, 0, 60, 40)
disp:setColor(2, 60, 0, 40)
disp:setColor(3, 120, 120, 200)
disp:drawGradientBox(0, 0, disp:getWidth(), disp:getHeight())
disp:setColor(255, 255, 255)
disp:setPrintPos(2, 18)
disp:setPrintDir(0)
disp:print("Pix&Line")
disp:drawPixel(0, 0)
disp:drawPixel(1, 0)
--disp:drawPixel(disp:getWidth()-1, 0)
--disp:drawPixel(0, disp:getHeight()-1)
disp:drawPixel(disp:getWidth()-1, disp:getHeight()-1)
disp:drawPixel(disp:getWidth()-1-1, disp:getHeight()-1)
x = 0
while x < mx do
xx = ((x)*255)/mx
disp:setColor(255, 255-xx/2, 255-xx)
disp:drawPixel(x, 24)
disp:drawVLine(x+7, 26, 13)
x = x + 1
end
end
function color_test()
local mx
local c, x
mx = disp:getWidth() / 2
--my = disp:getHeight() / 2
disp:setColor(0, 0, 0, 0)
disp:drawBox(0, 0, disp:getWidth(), disp:getHeight())
disp:setColor(255, 255, 255)
disp:setPrintPos(2,18)
disp:setPrintDir(0)
disp:print("Color Test")
disp:setColor(0, 127, 127, 127)
disp:drawBox(0, 20, 16*4+4, 5*8+4)
c = 0
x = 2
while c < 255 do
disp:setColor(0, c, c, c)
disp:drawBox(x, 22, 4, 8)
disp:setColor(0, c, 0, 0)
disp:drawBox(x, 22+8, 4, 8)
disp:setColor(0, 0, c, 0)
disp:drawBox(x, 22+2*8, 4, 8)
disp:setColor(0, 0, 0, c)
disp:drawBox(x, 22+3*8, 4, 8)
disp:setColor(0, c, 255-c, 0)
disp:drawBox(x, 22+4*8, 4, 8)
c = c + 17
x = x + 4
end
end
function millis()
local usec = tmr.now()
return usec/1000
end
function triangle()
local m
disp:setColor(0, 0, 80, 20)
disp:setColor(1, 60, 80, 20)
disp:setColor(2, 60, 120, 0)
disp:setColor(3, 0, 140, 30)
disp:drawGradientBox(0, 0, disp:getWidth(), disp:getHeight())
disp:setColor(255, 255, 255)
disp:setPrintPos(2, 18)
disp:print("Triangle")
m = millis() + T
while millis() < m do
disp:setColor(bit.band(lcg_rnd(), 127)+127, bit.band(lcg_rnd(), 31), bit.band(lcg_rnd(), 127)+64)
disp:drawTriangle(
bit.rshift(lcg_rnd() * (disp:getWidth()), 8),
bit.rshift(lcg_rnd() * (disp:getHeight()-20), 8) + 20,
bit.rshift(lcg_rnd() * (disp:getWidth()), 8),
bit.rshift(lcg_rnd() * (disp:getHeight()-20), 8) + 20,
bit.rshift(lcg_rnd() * (disp:getWidth()), 8),
bit.rshift(lcg_rnd() * (disp:getHeight()-20), 8) + 20
)
tmr.wdclr()
end
end
function fonts()
local d = 5
disp:setColor(0, 0, 40, 80)
disp:setColor(1, 150, 0, 200)
disp:setColor(2, 60, 0, 40)
disp:setColor(3, 0, 160, 160)
disp:drawGradientBox(0, 0, disp:getWidth(), disp:getHeight())
disp:setColor(255, 255, 255)
disp:setPrintDir(0)
disp:setPrintPos(2,18)
disp:print("Fonts")
disp:setFontMode(ucg.FONT_MODE_TRANSPARENT)
disp:setColor(255, 200, 170)
disp:setFont(ucg.font_helvB08_hr)
disp:setPrintPos(2,30+d)
disp:print("ABC abc 123")
disp:setFont(ucg.font_helvB10_hr)
disp:setPrintPos(2,45+d)
disp:print("ABC abc 123")
disp:setFont(ucg.font_helvB12_hr)
--disp:setPrintPos(2,62+d)
--disp:print("ABC abc 123")
disp:drawString(2,62+d, 0, "ABC abc 123") -- test drawString
disp:setFontMode(ucg.FONT_MODE_SOLID)
disp:setColor(255, 200, 170)
disp:setColor(1, 0, 100, 120) -- background color in solid mode
disp:setFont(ucg.font_helvB08_hr)
disp:setPrintPos(2,75+30+d)
disp:print("ABC abc 123")
disp:setFont(ucg.font_helvB10_hr)
disp:setPrintPos(2,75+45+d)
disp:print("ABC abc 123")
disp:setFont(ucg.font_helvB12_hr)
disp:setPrintPos(2,75+62+d)
disp:print("ABC abc 123")
disp:setFontMode(ucg.FONT_MODE_TRANSPARENT)
disp:setFont(ucg.font_ncenR14_hr)
end
function set_clip_range()
local x, y, w, h
w = bit.band(lcg_rnd(), 31)
h = bit.band(lcg_rnd(), 31)
w = w + 25
h = h + 25
x = bit.rshift(lcg_rnd() * (disp:getWidth() - w), 8)
y = bit.rshift(lcg_rnd() * (disp:getHeight() - h), 8)
disp:setClipRange(x, y, w, h)
end
function loop()
if (loop_idx == 0) then
switch(bit.band(r, 3)) : caseof {
[0] = function() disp:undoRotate() end,
[1] = function() disp:setRotate90() end,
[2] = function() disp:setRotate180() end,
default = function() disp:setRotate270() end
}
if ( r > 3 ) then
disp:clearScreen()
set_clip_range()
end
r = bit.band(r + 1, 255)
end
switch(loop_idx) : caseof {
[0] = function() end,
[1] = function() ucglib_graphics_test() end,
[2] = function() cross() end,
[3] = function() pixel_and_lines() end,
[4] = function() color_test() end,
[5] = function() triangle() end,
[6] = function() fonts() end,
default = function() loop_idx = -1 end
}
loop_idx = loop_idx + 1
end
T = 1500
r = 0
loop_idx = 0
init_spi_display()
disp:begin(ucg.FONT_MODE_TRANSPARENT)
disp:setFont(ucg.font_ncenR14_hr)
disp:clearScreen()
tmr.register(0, 3000, tmr.ALARM_AUTO, function() loop() end)
tmr.start(0)

View File

@ -0,0 +1,28 @@
-- setup SPI and connect display
function init_spi_display()
-- Hardware SPI CLK = GPIO14
-- Hardware SPI MOSI = GPIO13
-- Hardware SPI MISO = GPIO12 (not used)
-- CS, D/C, and RES can be assigned freely to available GPIOs
local cs = 8 -- GPIO15, pull-down 10k to GND
local dc = 4 -- GPIO2
local res = 0 -- GPIO16
spi.setup(1, spi.MASTER, spi.CPOL_LOW, spi.CPHA_LOW, spi.DATABITS_8, 0)
disp = ucg.ili9341_18x240x320_hw_spi(cs, dc, res)
end
init_spi_display()
disp:begin(ucg.FONT_MODE_TRANSPARENT)
disp:clearScreen()
disp:setFont(ucg.font_ncenR12_tr);
disp:setColor(255, 255, 255);
disp:setColor(1, 255, 0,0);
disp:setPrintPos(0, 25)
disp:print("Hello World!")