A modern Music Player Daemon based on Rockbox open source high quality audio player
libadwaita audio rust zig deno mpris rockbox mpd
at master 158 lines 5.4 kB view raw
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 24require("actions") -- Contains rb.actions & rb.contexts 25--require("buttons") -- Contains rb.buttons; not needed in this example 26 27-- Example function which splashes a message for x seconds 28function sayhello(seconds) 29 local message = string.format("Hello world from LUA for %d seconds", seconds) 30 rb.splash(seconds * rb.HZ, message) 31end 32 33-- Helper function which draws a (transparent) image at the center of the screen 34function draw_image(img) 35 local x, y = (rb.LCD_WIDTH - img:width()) / 2, (rb.LCD_HEIGHT - img:height()) / 2 36 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 40 41 func(img, 0, 0, img:width(), x, y, img:width(), img:height()) 42 rb.lcd_update() 43end 44 45-- Helper function that acts like a normal printf() would do 46local line = 0 47function printf(...) 48 local msg = string.format(...) 49 local res, w, h = rb.font_getstringsize(msg, rb.FONT_UI) 50 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() 57 58 line = line + 1 59 60 if(h * line >= rb.LCD_HEIGHT) then 61 line = 0 62 end 63end 64 65-- Helper function which reads the contents of a file 66function file_get_contents(filename) 67 local file = io.open(filename, "r") 68 if not file then 69 return nil 70 end 71 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 74 75 return contents 76end 77 78-- Helper function which saves contents to a file 79function 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 84 85 local ret = file:write(contents) == true 86 file:close() -- GC takes care of this if you would've forgotten it 87 return ret 88end 89 90-- Clear the screen 91rb.lcd_clear_display() 92 93-- Draw an X on the screen 94rb.lcd_drawline(0, 0, rb.LCD_WIDTH, rb.LCD_HEIGHT) 95rb.lcd_drawline(rb.LCD_WIDTH, 0, 0, rb.LCD_HEIGHT) 96 97if(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 102 end 103 end 104 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 107end 108 109-- Load a BMP file in the variable backdrop 110local 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 113 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 121local seconds = 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 -- 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 146 147-- Display some long lines 148for 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! 151end 152 153-- Loop until the user exits the program 154repeat 155 local action = rb.get_action(rb.contexts.CONTEXT_STD, rb.HZ) 156until action == rb.actions.ACTION_STD_CANCEL 157 158-- For a reference list of all functions available, see apps/plugins/lua/rocklib.c (and apps/plugin.h)