summaryrefslogtreecommitdiff
path: root/apps/plugins/helloworld.lua
diff options
context:
space:
mode:
authorMaurus Cuelenaere <mcuelenaere@gmail.com>2009-06-28 14:55:16 +0000
committerMaurus Cuelenaere <mcuelenaere@gmail.com>2009-06-28 14:55:16 +0000
commit681ca21a1e6731d7ce46be2a2419e600e957fae9 (patch)
tree77f111b10cb3ab26e1b97f9047972bc9004f69ae /apps/plugins/helloworld.lua
parent8e4098bd663c18ea800f47f88e657206d23a76ae (diff)
downloadrockbox-681ca21a1e6731d7ce46be2a2419e600e957fae9.tar.gz
rockbox-681ca21a1e6731d7ce46be2a2419e600e957fae9.zip
Lua:
* add IO lib (adapted to Rockbox) * remove old IO wrappers * rework helloworld.lua to work with the IO lib * do some stuff in helloworld.lua better (part of FS#10379, by James Callahan) git-svn-id: svn://svn.rockbox.org/rockbox/trunk@21541 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'apps/plugins/helloworld.lua')
-rw-r--r--apps/plugins/helloworld.lua74
1 files changed, 31 insertions, 43 deletions
diff --git a/apps/plugins/helloworld.lua b/apps/plugins/helloworld.lua
index ed8726c022..94e55af3cc 100644
--- a/apps/plugins/helloworld.lua
+++ b/apps/plugins/helloworld.lua
@@ -21,31 +21,29 @@
21 21
22]]-- 22]]--
23 23
24require("actions") -- Contains rb.actions & rb.contexts 24require("actions") -- Contains rb.actions & rb.contexts
25--require("buttons") -- Contains rb.buttons; not needed in this example
25 26
26-- Example function which splashes a message for x seconds 27-- Example function which splashes a message for x seconds
27function sayhello(seconds) 28function sayhello(seconds)
28 message = string.format("Hello world from LUA for %d seconds", seconds) 29 local message = string.format("Hello world from LUA for %d seconds", seconds)
29 rb.splash(seconds * rb.HZ, message) 30 rb.splash(seconds * rb.HZ, message)
30end 31end
31 32
32-- Helper function which draws a transparent image at the center of the screen 33-- Helper function which draws a (transparent) image at the center of the screen
33function draw_image(img) 34function draw_image(img)
34 local x, y = (rb.LCD_WIDTH - img:width()) / 2, (rb.LCD_HEIGHT - img:height()) / 2 35 local x, y = (rb.LCD_WIDTH - img:width()) / 2, (rb.LCD_HEIGHT - img:height()) / 2
35 36
36 local func = rb.lcd_bitmap_transparent_part 37 local func = rb.lcd_bitmap_transparent_part
37 if(func == nil) then 38 or rb.lcd_bitmap_part -- Fallback version for grayscale targets
38 func = rb.lcd_bitmap_part -- Fallback version for grayscale targets 39 or rb.lcd_mono_bitmap_part -- Fallback version for mono targets
39 if(func == nil) then 40
40 func = rb.lcd_mono_bitmap_part -- Fallback version for mono targets
41 end
42 end
43 func(img, 0, 0, img:width(), x, y, img:width(), img:height()) 41 func(img, 0, 0, img:width(), x, y, img:width(), img:height())
44 rb.lcd_update() 42 rb.lcd_update()
45end 43end
46 44
47-- Helper function that acts like a normal printf() would do 45-- Helper function that acts like a normal printf() would do
48line = 0 46local line = 0
49function printf(...) 47function printf(...)
50 local msg = string.format(...) 48 local msg = string.format(...)
51 local res, w, h = rb.font_getstringsize(msg, rb.FONT_UI) 49 local res, w, h = rb.font_getstringsize(msg, rb.FONT_UI)
@@ -66,37 +64,26 @@ end
66 64
67-- Helper function which reads the contents of a file 65-- Helper function which reads the contents of a file
68function file_get_contents(filename) 66function file_get_contents(filename)
69 if(not rb.file_exists(filename)) then 67 local file = io.open(filename, "r")
68 if(file == nil) then
70 return nil 69 return nil
71 end 70 end
72 71
73 local fd = rb.open(filename, rb.O_RDONLY) 72 local contents = file:read("*all") -- See Lua manual for more information
74 if(fd == -1) then 73 file:close() -- GC takes care of this if you would've forgotten it
75 return nil
76 end
77
78 local contents = rb.read(fd, rb.filesize(fd))
79 rb.close(fd)
80 74
81 return contents 75 return contents
82end 76end
83 77
84-- Helper function which saves contents to a file 78-- Helper function which saves contents to a file
85function file_put_contents(filename, contents) 79function file_put_contents(filename, contents)
86 local flags = rb.O_WRONLY 80 local file = io.open(filename, "w+") -- See Lua manual for more information
87 if(rb.file_exists(filename)) then 81 if(file == nil) then
88 flags = bit.bor(flags, rb.O_APPEND) -- binary OR O_APPEND if the file exists
89 else
90 flags = bit.bor(flags, rb.O_CREAT) -- binary OR O_CREAT if the file doesn't exist
91 end
92
93 local fd = rb.open(filename, flags)
94 if(fd == -1) then
95 return false 82 return false
96 end 83 end
97 84
98 local ret = rb.write(fd, contents) ~= string.len(contents) 85 local ret = file:write(contents) == 1
99 rb.close(fd) 86 file:close() -- GC takes care of this if you would've forgotten it
100 return ret 87 return ret
101end 88end
102 89
@@ -120,13 +107,10 @@ if(rb.lcd_rgbpack ~= nil) then -- Only do this when we're on a color target, i.e
120end 107end
121 108
122-- Load a BMP file in the variable backdrop 109-- Load a BMP file in the variable backdrop
123local backdrop = rb.read_bmp_file("/.rockbox/icons/tango_small_viewers.bmp") -- This image should always be present? 110local backdrop = rb.read_bmp_file("/.rockbox/icons/tango_small_viewers.bmp") -- This image should always be present...
124if(backdrop == nil) then 111 or rb.read_bmp_file("/.rockbox/icons/tango_small_viewers_mono.bmp") -- ... if not, try using the mono version...
125 backdrop = rb.read_bmp_file("/.rockbox/icons/tango_small_viewers_mono.bmp") -- Try using the mono version 112 or rb.read_bmp_file("/.rockbox/icons/viewers.bmp") -- ... or try using the builtin version
126 if(backdrop == nil) then 113
127 backdrop = rb.read_bmp_file("/.rockbox/icons/viewers.bmp") -- Try using the builtin version
128 end
129end
130-- Draws the image using our own draw_image() function; see up 114-- Draws the image using our own draw_image() function; see up
131draw_image(backdrop) 115draw_image(backdrop)
132 116
@@ -134,7 +118,7 @@ draw_image(backdrop)
134rb.lcd_update() 118rb.lcd_update()
135 119
136-- Sleep for 2 seconds 120-- Sleep for 2 seconds
137seconds = 2 121local seconds = 2
138rb.sleep(seconds * rb.HZ) -- rb.HZ equals to the amount of ticks that fit in 1 second 122rb.sleep(seconds * rb.HZ) -- rb.HZ equals to the amount of ticks that fit in 1 second
139 123
140-- Call the function sayhello() with arguments seconds 124-- Call the function sayhello() with arguments seconds
@@ -152,14 +136,18 @@ file_put_contents(pathname, "Hello from Lua!")
152-- Splash the contents of pathname 136-- Splash the contents of pathname
153printf(file_get_contents(pathname)) 137printf(file_get_contents(pathname))
154 138
155line = line + 1 -- empty line 139line = line + 1 -- Add empty line
140
141-- Some examples of how bitlib works
142printf("1 | 2 = %d <> 7 & 3 = %d", bit.bor(1, 2), bit.band(7, 3))
143printf("~8 = %d <> 5 ^ 1 = %d", bit.bnot(8), bit.bxor(5, 1))
144
145line = line + 1 -- Add empty line
156 146
157-- Display some long lines 147-- Display some long lines
158local tmp = "" 148for i=0, 4 do
159for i=1, 5 do 149 printf("This is a %slong line!", string.rep("very very very ", i))
160 printf("This is a %s long line!", tmp) 150 rb.yield() -- Always try to yield to give other threads some breathing space!
161 tmp = tmp .. " very very very"
162 rb.yield()
163end 151end
164 152
165-- Loop until the user exits the program 153-- Loop until the user exits the program