diff options
Diffstat (limited to 'apps/plugins/helloworld.lua')
-rw-r--r-- | apps/plugins/helloworld.lua | 74 |
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 | ||
24 | require("actions") -- Contains rb.actions & rb.contexts | 24 | require("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 |
27 | function sayhello(seconds) | 28 | function 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) |
30 | end | 31 | end |
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 |
33 | function draw_image(img) | 34 | function 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() |
45 | end | 43 | end |
46 | 44 | ||
47 | -- Helper function that acts like a normal printf() would do | 45 | -- Helper function that acts like a normal printf() would do |
48 | line = 0 | 46 | local line = 0 |
49 | function printf(...) | 47 | function 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 |
68 | function file_get_contents(filename) | 66 | function 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 |
82 | end | 76 | end |
83 | 77 | ||
84 | -- Helper function which saves contents to a file | 78 | -- Helper function which saves contents to a file |
85 | function file_put_contents(filename, contents) | 79 | function 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 |
101 | end | 88 | end |
102 | 89 | ||
@@ -120,13 +107,10 @@ if(rb.lcd_rgbpack ~= nil) then -- Only do this when we're on a color target, i.e | |||
120 | end | 107 | end |
121 | 108 | ||
122 | -- Load a BMP file in the variable backdrop | 109 | -- Load a BMP file in the variable backdrop |
123 | local backdrop = rb.read_bmp_file("/.rockbox/icons/tango_small_viewers.bmp") -- This image should always be present? | 110 | local backdrop = rb.read_bmp_file("/.rockbox/icons/tango_small_viewers.bmp") -- This image should always be present... |
124 | if(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 | ||
129 | end | ||
130 | -- Draws the image using our own draw_image() function; see up | 114 | -- Draws the image using our own draw_image() function; see up |
131 | draw_image(backdrop) | 115 | draw_image(backdrop) |
132 | 116 | ||
@@ -134,7 +118,7 @@ draw_image(backdrop) | |||
134 | rb.lcd_update() | 118 | rb.lcd_update() |
135 | 119 | ||
136 | -- Sleep for 2 seconds | 120 | -- Sleep for 2 seconds |
137 | seconds = 2 | 121 | local seconds = 2 |
138 | rb.sleep(seconds * rb.HZ) -- rb.HZ equals to the amount of ticks that fit in 1 second | 122 | rb.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 |
153 | printf(file_get_contents(pathname)) | 137 | printf(file_get_contents(pathname)) |
154 | 138 | ||
155 | line = line + 1 -- empty line | 139 | line = line + 1 -- Add empty line |
140 | |||
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)) | ||
144 | |||
145 | line = line + 1 -- Add empty line | ||
156 | 146 | ||
157 | -- Display some long lines | 147 | -- Display some long lines |
158 | local tmp = "" | 148 | for i=0, 4 do |
159 | for 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() | ||
163 | end | 151 | end |
164 | 152 | ||
165 | -- Loop until the user exits the program | 153 | -- Loop until the user exits the program |