dofile("actions.lua")

function sayhello(seconds)
local message = string.format("Hello from LUA for %d seconds", seconds)
rb.splash(seconds * rb.HZ, message)
end

line = 0
function printf(message)
rb.lcd_puts(0, line, message)
rb.lcd_update()

line = line + 1
if(line > rb.LCD_HEIGHT/10) then
line = 0
end
end

function read_file(filename)
local fd = rb.open(filename, rb.O_RDONLY)

if(fd < 0) then
return nil
end

local text = rb.read(fd, rb.filesize(fd))
rb.close(fd)

return text
end

function write_file(filename, text)
local flags = rb.O_WRONLY
if not rb.file_exists(filename) then
flags = flags + rb.O_CREAT
end

local fd = rb.open(filename, flags)

if(fd < 0) then
return false
end

local size = rb.write(fd, text)
rb.close(fd)

return size == string.len(text)
end

function print_r (t, indent)
local indent = indent or ''
local ret = ''
for key, value in pairs(t) do
ret = ret .. indent .. '[' .. tostring(key) .. ']'
if type(value)=="table" then
printf(ret .. ':')
ret = ''
print_r(value, indent..' ')
else
printf(ret .. ' = ' .. tostring(value))
ret = ''
end
end
end

-- Draw an X on the screen
rb.lcd_clear_display()
rb.lcd_drawline(0, 0, rb.LCD_WIDTH, rb.LCD_HEIGHT)
rb.lcd_drawline(rb.LCD_WIDTH, 0, 0, rb.LCD_HEIGHT)
rb.lcd_fillrect(10, 10, 10, 10)
rb.lcd_update()

printf("test")
printf(string.format("test %d", 2))
printf(string.format("a rockbox hz = %d", rb.HZ))
print_r({ a = 5, rb.HZ, {5, 3} })
--print_r(rb.actions)

seconds = 2

rb.sleep(seconds * rb.HZ)

sayhello(seconds)

rb.splash(3 * rb.HZ, rb.kbd_input())

rb.splash(3 * rb.HZ, string.format("Hi there! %d", rb.actions.ACTION_YESNO_ACCEPT))
--rb.splash(3 * rb.HZ, read_file("/test/test.lua"))
--write_file("/test/test2.lua", "haai!")

while(true) do
action = rb.get_action(rb.contexts.CONTEXT_STD, rb.HZ/2)
if(action == rb.actions.ACTION_TOUCHSCREEN) then
result, x, y = rb.action_get_touchscreen_press()
rb.splash(rb.HZ, string.format("%d %d %d", result, x, y))
else
rb.splash(rb.HZ/2, string.format("%d %d", action, rb.actions.ACTION_TOUCHSCREEN))
end
rb.yield()
end