Index: apps/plugins/test_viewports.lua =================================================================== --- apps/plugins/test_viewports.lua (revision 21490) +++ apps/plugins/test_viewports.lua (working copy) @@ -9,7 +9,7 @@ Port of test_viewports.c to Lua - Copyright (C) 2009 by Maurus Cuelenaere + Copyright (C) 2009 by Maurus Cuelenaere , Daurnimator This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License @@ -28,89 +28,84 @@ rb.LCD_DEFAULT_FG = rb.LCD_WHITE rb.LCD_DEFAULT_BG = rb.LCD_BLACK -BGCOLOR_1 = rb.lcd_rgbpack(255,255,0) -BGCOLOR_2 = rb.lcd_rgbpack(0,255,0) -FGCOLOR_1 = rb.lcd_rgbpack(0,0,255) +BGCOLOR_1 = rb.lcd_rgbpack ( 255 , 255 , 0 ) +BGCOLOR_2 = rb.lcd_rgbpack ( 0 , 255 , 0 ) +FGCOLOR_1 = rb.lcd_rgbpack ( 0 , 0 , 255 ) -local vp0 = -{ - x = 0, - y = 0, - width = rb.LCD_WIDTH, - height = 20, - font = rb.FONT_UI, - drawmode = rb.DRMODE_SOLID, - fg_pattern = rb.LCD_DEFAULT_FG, - bg_pattern = BGCOLOR_1 +local vp0 = { + x = 0 ; + y = 0 ; + width = rb.LCD_WIDTH ; + height = 20 ; + font = rb.FONT_UI ; + drawmode = rb.DRMODE_SOLID ; + fg_pattern = rb.LCD_DEFAULT_FG ; + bg_pattern = BGCOLOR_1 ; } -local vp1 = -{ - x = rb.LCD_WIDTH / 10, - y = 20, - width = rb.LCD_WIDTH / 3, - height = rb.LCD_HEIGHT / 2, - font = rb.FONT_SYSFIXED, - drawmode = rb.DRMODE_SOLID, - fg_pattern = rb.LCD_DEFAULT_FG, - bg_pattern = rb.LCD_DEFAULT_BG -}; +local vp1 = { + x = rb.LCD_WIDTH / 10 ; + y = 20 ; + width = rb.LCD_WIDTH / 3 ; + height = rb.LCD_HEIGHT / 2 ; + font = rb.FONT_SYSFIXED ; + drawmode = rb.DRMODE_SOLID ; + fg_pattern = rb.LCD_DEFAULT_FG ; + bg_pattern = rb.LCD_DEFAULT_BG ; +} -local vp2 = -{ - x = rb.LCD_WIDTH / 2, - y = 40, - width = rb.LCD_WIDTH / 3, - height = (rb.LCD_HEIGHT / 2), - font = rb.FONT_UI, - drawmode = rb.DRMODE_SOLID, - fg_pattern = FGCOLOR_1, - bg_pattern = BGCOLOR_2 -}; +local vp2 = { + x = rb.LCD_WIDTH / 2 ; + y = 40 ; + width = rb.LCD_WIDTH / 3 ; + height = rb.LCD_HEIGHT / 2 ; + font = rb.FONT_UI ; + drawmode = rb.DRMODE_SOLID ; + fg_pattern = FGCOLOR_1 ; + bg_pattern = BGCOLOR_2 ; +} - -local vp3 = -{ - x = rb.LCD_WIDTH / 4, - y = (5 * rb.LCD_HEIGHT) / 8, - width = rb.LCD_WIDTH / 2, - height = (rb.LCD_HEIGHT / 4), - font = rb.FONT_SYSFIXED, - drawmode = rb.DRMODE_SOLID, - fg_pattern = rb.LCD_BLACK, - bg_pattern = rb.LCD_WHITE +local vp3 = { + x = rb.LCD_WIDTH / 4 ; + y = (5 * rb.LCD_HEIGHT) / 8 ; + width = rb.LCD_WIDTH / 2 ; + height = (rb.LCD_HEIGHT / 4) ; + font = rb.FONT_SYSFIXED ; + drawmode = rb.DRMODE_SOLID ; + fg_pattern = rb.LCD_BLACK ; + bg_pattern = rb.LCD_WHITE ; }; -rb.set_viewport(vp0) -rb.clear_viewport() -rb.lcd_puts_scroll(0,0,"Viewport testing plugin - this is a scrolling title") +rb.set_viewport ( vp0 ) +rb.clear_viewport ( ) +rb.lcd_puts_scroll ( 0 , 0 , "Viewport testing plugin - this is a scrolling title" ) -rb.set_viewport(vp1); -rb.clear_viewport(); +rb.set_viewport ( vp1 ) +rb.clear_viewport ( ) -for i = 0, 3 do - rb.lcd_puts_scroll(0,i,string.format("Left text, scrolling_line %d",i)); +for i = 0 , 3 do + rb.lcd_puts_scroll ( 0 , i , string.format ( "Left text, scrolling_line %d" , i ) ) end -rb.set_viewport(vp2); -rb.clear_viewport(); -for i = 0, 3 do - rb.lcd_puts_scroll(1,i,string.format("Right text, scrolling line %d",i)); +rb.set_viewport ( vp2 ) +rb.clear_viewport ( ) +for i = 0 , 3 do + rb.lcd_puts_scroll ( 1 , i , string.format ( "Right text, scrolling line %d" , i ) ) end local y = -10 -for i = -10, vp2.width + 10, 5 do - rb.lcd_drawline(i, y, i, vp2.height - y); +for i = -10 , vp2.width + 10 , 5 do + rb.lcd_drawline ( i , y , i , vp2.height - y ) end -rb.set_viewport(vp3); -rb.clear_viewport(); -for i = 1, 2 do - rb.lcd_puts_scroll(2,i,string.format("Bottom text, a scrolling line %d",i)); +rb.set_viewport ( vp3 ) +rb.clear_viewport ( ) +for i = 1 , 2 do + rb.lcd_puts_scroll ( 2 , i , string.format ( "Bottom text , a scrolling line %d" , i ) ) end -rb.lcd_puts_scroll(4,3,"Short line") -rb.lcd_update() +rb.lcd_puts_scroll ( 4 , 3 , "Short line" ) +rb.lcd_update ( ) -rb.button_get(true) +rb.button_get ( true ) -rb.set_viewport(nil) +rb.set_viewport ( nil ) Index: apps/plugins/helloworld.lua =================================================================== --- apps/plugins/helloworld.lua (revision 21490) +++ apps/plugins/helloworld.lua (working copy) @@ -9,7 +9,7 @@ Example Lua script - Copyright (C) 2009 by Maurus Cuelenaere + Copyright (C) 2009 by Maurus Cuelenaere , Daurnimator This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License @@ -21,150 +21,125 @@ ]]-- -dofile("actions.lua") -- Contains rb.actions & rb.contexts +dofile ( "actions.lua" ) -- Contains rb.actions & rb.contexts -- Example function which splashes a message for x seconds -function sayhello(seconds) - message = string.format("Hello world from LUA for %d seconds", seconds) - rb.splash(seconds * rb.HZ, message) +function sayhello ( seconds ) + local message = string.format ( "Hello world from LUA for %d seconds" , seconds ) + rb.splash ( seconds * rb.HZ , message ) end -- Helper function which draws a transparent image at the center of the screen -function draw_image(img) - local x, y = (rb.LCD_WIDTH - img:width()) / 2, (rb.LCD_HEIGHT - img:height()) / 2 +function draw_image( img ) + local x , y = ( rb.LCD_WIDTH - img:width ( ) ) / 2 , ( rb.LCD_HEIGHT - img:height ( ) ) / 2 - local func = rb.lcd_bitmap_transparent_part - if(func == nil) then - func = rb.lcd_bitmap_part -- Fallback version for grayscale targets - if(func == nil) then - func = rb.lcd_mono_bitmap_part -- Fallback version for mono targets - end - end - func(img, 0, 0, img:width(), x, y, img:width(), img:height()) - rb.lcd_update() + local func = rb.lcd_bitmap_transparent_part + or rb.lcd_bitmap_part -- Fallback version for grayscale targets + or rb.lcd_mono_bitmap_part -- Fallback version for mono targets + + func ( img , 0 , 0 , img:width ( ) , x , y , img:width ( ) , img:height ( ) ) + rb.lcd_update ( ) end -- Helper function that acts like a normal printf() would do -line = 0 -function printf(...) - local msg = string.format(...) - local res, w, h = rb.font_getstringsize(msg, rb.FONT_UI) +local line = 0 +function printf ( ... ) + local msg = string.format ( ... ) + local res , w , h = rb.font_getstringsize ( msg , rb.FONT_UI ) + + if w >= rb.LCD_WIDTH then + rb.lcd_puts_scroll ( 0 , line , msg ) + else + rb.lcd_puts ( 0 , line , msg ) + end + rb.lcd_update ( ) - if(w >= rb.LCD_WIDTH) then - rb.lcd_puts_scroll(0, line, msg) - else - rb.lcd_puts(0, line, msg) - end - rb.lcd_update() + line = line + 1 - line = line + 1 - - if(h * line >= rb.LCD_HEIGHT) then - line = 0 - end + if ( h * line ) >= rb.LCD_HEIGHT then line = 0 end end -- Helper function which reads the contents of a file -function file_get_contents(filename) - if(not rb.file_exists(filename)) then - return nil - end +function file_get_contents ( filename ) + if not rb.file_exists ( filename ) then return end + + local fd = rb.open ( filename , rb.O_RDONLY ) + if fd == -1 then return end - local fd = rb.open(filename, rb.O_RDONLY) - if(fd == -1) then - return nil - end + local contents = rb.read ( fd , rb.filesize ( fd ) ) + rb.close ( fd ) - local contents = rb.read(fd, rb.filesize(fd)) - rb.close(fd) - - return contents + return contents end -- Helper function which saves contents to a file function file_put_contents(filename, contents) - local flags = rb.O_WRONLY - if(rb.file_exists(filename)) then - flags = bit.bor(flags, rb.O_APPEND) -- binary OR O_APPEND if the file exists - else - flags = bit.bor(flags, rb.O_CREAT) -- binary OR O_CREAT if the file doesn't exist - end + local flags = rb.O_WRONLY + + if rb.file_exists ( filename ) then + flags = bit.bor ( flags , rb.O_APPEND ) -- binary OR O_APPEND if the file exists + else + flags = bit.bor ( flags , rb.O_CREAT ) -- binary OR O_CREAT if the file doesn't exist + end - local fd = rb.open(filename, flags) - if(fd == -1) then - return false - end + local fd = rb.open ( filename , flags ) + if fd == -1 then return end - local ret = rb.write(fd, contents) ~= string.len(contents) - rb.close(fd) - return ret + local ret = rb.write(fd, contents) ~= string.len(contents) + rb.close(fd) + + return ret end --- Clear the screen -rb.lcd_clear_display() +rb.lcd_clear_display ( ) -- Clear the screen -- Draw an X on the screen -rb.lcd_drawline(0, 0, rb.LCD_WIDTH, rb.LCD_HEIGHT) -rb.lcd_drawline(rb.LCD_WIDTH, 0, 0, rb.LCD_HEIGHT) +rb.lcd_drawline ( 0 , 0 , rb.LCD_WIDTH , rb.LCD_HEIGHT ) +rb.lcd_drawline ( rb.LCD_WIDTH , 0 , 0 , rb.LCD_HEIGHT ) -if(rb.lcd_rgbpack ~= nil) then -- Only do this when we're on a color target, i.e. when LCD_RGBPACK is available - local rectangle = rb.new_image(10, 15) -- Create a new image with width 10 and height 15 - for i=1, 10 do - for j=1, 15 do - rectangle:set(i, j, rb.lcd_rgbpack(200, i*20, j*20)) -- Set the pixel at position i, j to the specified color - end - end +if rb.lcd_rgbpack ~= nil then -- Only do this when we're on a color target, i.e. when LCD_RGBPACK is available + local rectangle = rb.new_image ( 10 , 15 ) -- Create a new image with width 10 and height 15 + for i = 1 , 10 do + for j = 1 , 15 do + rectangle:set ( i , j , rb.lcd_rgbpack ( 200 , i * 20 , j * 20 ) ) -- Set the pixel at position i, j to the specified color + end + end - -- rb.lcd_bitmap_part(src, src_x, src_y, stride, x, y, width, height) - rb.lcd_bitmap_part(rectangle, 0, 0, 10, rb.LCD_WIDTH/2-5, rb.LCD_HEIGHT/10-1, 10, 10) -- Draws our rectangle at the top-center of the screen + -- rb.lcd_bitmap_part (src , src_x , src_y , stride , x , y , width , height ) + rb.lcd_bitmap_part ( rectangle , 0 , 0 , 10 , rb.LCD_WIDTH / 2 -5 , rb.LCD_HEIGHT / 10 - 1 , 10 , 10 ) -- Draws our rectangle at the top-center of the screen end -- Load a BMP file in the variable backdrop -local backdrop = rb.read_bmp_file("/.rockbox/icons/tango_small_viewers.bmp") -- This image should always be present? -if(backdrop == nil) then - backdrop = rb.read_bmp_file("/.rockbox/icons/tango_small_viewers_mono.bmp") -- Try using the mono version - if(backdrop == nil) then - backdrop = rb.read_bmp_file("/.rockbox/icons/viewers.bmp") -- Try using the builtin version - end -end --- Draws the image using our own draw_image() function; see up -draw_image(backdrop) +local backdrop = rb.read_bmp_file ( "/.rockbox/icons/tango_small_viewers.bmp" ) -- This image should always be present? + or rb.read_bmp_file ( "/.rockbox/icons/tango_small_viewers_mono.bmp" ) -- Try using the mono version + or rb.read_bmp_file ( "/.rockbox/icons/viewers.bmp" ) -- Try using the builtin version --- Flush the contents from the framebuffer to the LCD -rb.lcd_update() +draw_image ( backdrop ) -- Draws the image using our own draw_image() function; see up +rb.lcd_update ( ) -- Flush the contents from the framebuffer to the LCD + -- Sleep for 2 seconds -seconds = 2 +local seconds = 2 rb.sleep(seconds * rb.HZ) -- rb.HZ equals to the amount of ticks that fit in 1 second --- Call the function sayhello() with arguments seconds -sayhello(seconds) +sayhello ( seconds ) --- Clear display -rb.lcd_clear_display() +rb.lcd_clear_display ( ) -- Clear display --- Construct a pathname using the current path and a file name -local pathname = rb.current_path() .. "test.txt" +local pathname = rb.current_path ( ) .. "test.txt" -- Construct a pathname using the current path and a file name --- Put the string 'Hello from Lua!' in pathname -file_put_contents(pathname, "Hello from Lua!") +file_put_contents ( pathname , "Hello from Lua!" ) -- Put the string 'Hello from Lua!' in pathname --- Splash the contents of pathname -printf(file_get_contents(pathname)) +printf ( file_get_contents ( pathname ) ) -- Splash the contents of pathname line = line + 1 -- empty line -- Display some long lines -local tmp = "" -for i=1, 5 do - printf("This is a %s long line!", tmp) - tmp = tmp .. " very very very" - rb.yield() +for i = 1 , 5 do + printf ( "This is a %s long line!" , string.rep ( " very very very" , i - 1 ) ) + rb.yield ( ) end --- Loop until the user exits the program -repeat - local action = rb.get_action(rb.contexts.CONTEXT_STD, rb.HZ) -until action == rb.actions.ACTION_STD_CANCEL +repeat until rb.get_action ( rb.contexts.CONTEXT_STD , rb.HZ ) == rb.actions.ACTION_STD_CANCEL -- Loop until the user exits the program -- For a reference list of all functions available, see apps/plugins/lua/rocklib.c (and apps/plugin.h)