summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--apps/plugins/helloworld.lua154
1 files changed, 154 insertions, 0 deletions
diff --git a/apps/plugins/helloworld.lua b/apps/plugins/helloworld.lua
new file mode 100644
index 0000000000..fcdd4de026
--- /dev/null
+++ b/apps/plugins/helloworld.lua
@@ -0,0 +1,154 @@
1--[[
2 __________ __ ___.
3 Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 \/ \/ \/ \/ \/
8 $Id$
9
10 Example Lua script
11
12 Copyright (C) 2009 by Maurus Cuelenaere
13
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.
18
19 This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20 KIND, either express or implied.
21
22]]--
23
24dofile("actions.lua") -- Contains rb.actions & rb.contexts
25
26-- Example function which splashes a message for x seconds
27function sayhello(seconds)
28 message = string.format("Hello world from LUA for %d seconds", seconds)
29 rb.splash(seconds * rb.HZ, message)
30end
31
32-- Helper function which draws a transparent image at the center of the screen
33function draw_image(img)
34 local x, y = (rb.LCD_WIDTH - img:width()) / 2, (rb.LCD_HEIGHT - img:height()) / 2
35 rb.lcd_bitmap_transparent_part(img, 0, 0, img:width(), x, y, img:width(), img:height())
36 rb.lcd_update()
37end
38
39-- Helper function that acts like a normal printf() would do
40line = 0
41function printf(...)
42 local msg = string.format(...)
43 local res, w, h = rb.font_getstringsize(msg, rb.FONT_UI)
44
45 if(w >= rb.LCD_WIDTH) then
46 rb.lcd_puts_scroll(0, line, msg)
47 else
48 rb.lcd_puts(0, line, msg)
49 end
50 rb.lcd_update()
51
52 line = line + 1
53
54 if(h * line >= rb.LCD_HEIGHT) then
55 line = 0
56 end
57end
58
59-- Helper function which reads the contents of a file
60function file_get_contents(filename)
61 if(not rb.file_exists(filename)) then
62 return nil
63 end
64
65 local fd = rb.open(filename, rb.O_RDONLY)
66 if(fd == -1) then
67 return nil
68 end
69
70 local contents = rb.read(fd, rb.filesize(fd))
71 rb.close(fd)
72
73 return contents
74end
75
76-- Helper function which saves contents to a file
77function file_put_contents(filename, contents)
78 local flags = rb.O_WRONLY
79 if(rb.file_exists(filename)) then
80 flags = bit.bor(flags, rb.O_APPEND) -- binary OR O_APPEND if the file exists
81 else
82 flags = bit.bor(flags, rb.O_CREAT) -- binary OR O_CREAT if the file doesn't exist
83 end
84
85 local fd = rb.open(filename, flags)
86 if(fd == -1) then
87 return false
88 end
89
90 local ret = rb.write(fd, contents) ~= string.len(contents)
91 rb.close(fd)
92 return ret
93end
94
95-- Clear the screen
96rb.lcd_clear_display()
97
98-- Draw an X on the screen
99rb.lcd_drawline(0, 0, rb.LCD_WIDTH, rb.LCD_HEIGHT)
100rb.lcd_drawline(rb.LCD_WIDTH, 0, 0, rb.LCD_HEIGHT)
101
102local rectangle = rb.new_image(10, 15) -- Create a new image with width 10 and height 15
103for i=1, 10 do
104 for j=1, 15 do
105 rectangle:set(i, j, rb.lcd_rgbpack(200, i*20, j*20)) -- Set the pixel at position i, j to the specified color
106 end
107end
108
109-- rb.lcd_bitmap_part(src, src_x, src_y, stride, x, y, width, height)
110rb.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
111
112-- Load a BMP file in the variable backdrop
113local backdrop = rb.read_bmp_file("/.rockbox/icons/tango_small_viewers.bmp") -- This image should always be present?
114-- Draws the image using our own draw_image() function; see up
115draw_image(backdrop)
116
117-- Flush the contents from the framebuffer to the LCD
118rb.lcd_update()
119
120-- Sleep for 2 seconds
121seconds = 2
122rb.sleep(seconds * rb.HZ) -- rb.HZ equals to the amount of ticks that fit in 1 second
123
124-- Call the function sayhello() with arguments seconds
125sayhello(seconds)
126
127-- Clear display
128rb.lcd_clear_display()
129
130-- Construct a pathname using the current path and a file name
131local pathname = rb.current_path() .. "test.txt"
132
133-- Put the string 'Hello from Lua!' in pathname
134file_put_contents(pathname, "Hello from Lua!")
135
136-- Splash the contents of pathname
137printf(file_get_contents(pathname))
138
139line = line + 1 -- empty line
140
141-- Display some long lines
142local tmp = ""
143for i=1, 5 do
144 printf("This is a %s long line!", tmp)
145 tmp = tmp .. " very very very"
146 rb.yield()
147end
148
149-- Loop until the user exits the program
150repeat
151 local action = rb.get_action(rb.contexts.CONTEXT_STD, rb.HZ)
152until action == rb.actions.ACTION_STD_CANCEL
153
154-- For a reference list of all functions available, see apps/plugins/lua/rocklib.c (and apps/plugin.h)