Prepare new maemo release
[maemo-rb.git] / apps / plugins / helloworld.lua
blobaa2ef446d3d422c27be7358383764c7cf92d638a
1 --[[
2 __________ __ ___.
3 Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 \/ \/ \/ \/ \/
8 $Id$
10 Example Lua script
12 Copyright (C) 2009 by Maurus Cuelenaere
14 This program is free software; you can redistribute it and/or
15 modify it under the terms of the GNU General Public License
16 as published by the Free Software Foundation; either version 2
17 of the License, or (at your option) any later version.
19 This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20 KIND, either express or implied.
22 ]]--
24 require("actions") -- Contains rb.actions & rb.contexts
25 --require("buttons") -- Contains rb.buttons; not needed in this example
27 -- Example function which splashes a message for x seconds
28 function sayhello(seconds)
29 local message = string.format("Hello world from LUA for %d seconds", seconds)
30 rb.splash(seconds * rb.HZ, message)
31 end
33 -- Helper function which draws a (transparent) image at the center of the screen
34 function draw_image(img)
35 local x, y = (rb.LCD_WIDTH - img:width()) / 2, (rb.LCD_HEIGHT - img:height()) / 2
37 local func = rb.lcd_bitmap_transparent_part
38 or rb.lcd_bitmap_part -- Fallback version for grayscale targets
39 or rb.lcd_mono_bitmap_part -- Fallback version for mono targets
41 func(img, 0, 0, img:width(), x, y, img:width(), img:height())
42 rb.lcd_update()
43 end
45 -- Helper function that acts like a normal printf() would do
46 local line = 0
47 function printf(...)
48 local msg = string.format(...)
49 local res, w, h = rb.font_getstringsize(msg, rb.FONT_UI)
51 if(w >= rb.LCD_WIDTH) then
52 rb.lcd_puts_scroll(0, line, msg)
53 else
54 rb.lcd_puts(0, line, msg)
55 end
56 rb.lcd_update()
58 line = line + 1
60 if(h * line >= rb.LCD_HEIGHT) then
61 line = 0
62 end
63 end
65 -- Helper function which reads the contents of a file
66 function file_get_contents(filename)
67 local file = io.open(filename, "r")
68 if not file then
69 return nil
70 end
72 local contents = file:read("*all") -- See Lua manual for more information
73 file:close() -- GC takes care of this if you would've forgotten it
75 return contents
76 end
78 -- Helper function which saves contents to a file
79 function file_put_contents(filename, contents)
80 local file = io.open(filename, "w+") -- See Lua manual for more information
81 if not file then
82 return false
83 end
85 local ret = file:write(contents) == true
86 file:close() -- GC takes care of this if you would've forgotten it
87 return ret
88 end
90 -- Clear the screen
91 rb.lcd_clear_display()
93 -- Draw an X on the screen
94 rb.lcd_drawline(0, 0, rb.LCD_WIDTH, rb.LCD_HEIGHT)
95 rb.lcd_drawline(rb.LCD_WIDTH, 0, 0, rb.LCD_HEIGHT)
97 if(rb.lcd_rgbpack ~= nil) then -- Only do this when we're on a color target, i.e. when LCD_RGBPACK is available
98 local rectangle = rb.new_image(10, 15) -- Create a new image with width 10 and height 15
99 for i=1, 10 do
100 for j=1, 15 do
101 rectangle:set(i, j, rb.lcd_rgbpack(200, i*20, j*20)) -- Set the pixel at position i, j to the specified color
105 -- rb.lcd_bitmap_part(src, src_x, src_y, stride, x, y, width, height)
106 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
109 -- Load a BMP file in the variable backdrop
110 local backdrop = rb.read_bmp_file("/.rockbox/icons/tango_small_viewers.bmp") -- This image should always be present...
111 or rb.read_bmp_file("/.rockbox/icons/tango_small_viewers_mono.bmp") -- ... if not, try using the mono version...
112 or rb.read_bmp_file("/.rockbox/icons/viewers.bmp") -- ... or try using the builtin version
114 -- Draws the image using our own draw_image() function; see up
115 draw_image(backdrop)
117 -- Flush the contents from the framebuffer to the LCD
118 rb.lcd_update()
120 -- Sleep for 2 seconds
121 local seconds = 2
122 rb.sleep(seconds * rb.HZ) -- rb.HZ equals to the amount of ticks that fit in 1 second
124 -- Call the function sayhello() with arguments seconds
125 sayhello(seconds)
127 -- Clear display
128 rb.lcd_clear_display()
130 -- Construct a pathname using the current path and a file name
131 local pathname = rb.current_path() .. "test.txt"
133 -- Put the string 'Hello from Lua!' in pathname
134 file_put_contents(pathname, "Hello from Lua!")
136 -- Splash the contents of pathname
137 printf(file_get_contents(pathname))
139 line = line + 1 -- Add empty line
141 -- Some examples of how bitlib works
142 printf("1 | 2 = %d <> 7 & 3 = %d", bit.bor(1, 2), bit.band(7, 3))
143 printf("~8 = %d <> 5 ^ 1 = %d", bit.bnot(8), bit.bxor(5, 1))
145 line = line + 1 -- Add empty line
147 -- Display some long lines
148 for i=0, 4 do
149 printf("This is a %slong line!", string.rep("very very very ", i))
150 rb.yield() -- Always try to yield to give other threads some breathing space!
153 -- Loop until the user exits the program
154 repeat
155 local action = rb.get_action(rb.contexts.CONTEXT_STD, rb.HZ)
156 until action == rb.actions.ACTION_STD_CANCEL
158 -- For a reference list of all functions available, see apps/plugins/lua/rocklib.c (and apps/plugin.h)