Support pinning decoded images to RAM

custom
jacqueline 1 year ago
parent a05d93a1e2
commit 654dcb34d6
  1. 38
      lib/luavgl/src/imgdata.c
  2. 2
      lib/luavgl/src/luavgl.c
  3. 20
      lua/playing.lua
  4. 42
      lua/widgets.lua
  5. 13
      luals-stubs/lvgl.lua

@ -0,0 +1,38 @@
#include "draw/lv_img_buf.h"
#include "draw/lv_img_decoder.h"
#include "lauxlib.h"
#include "lua.h"
#include "luavgl.h"
#include "misc/lv_color.h"
#include "misc/lv_mem.h"
#include "misc/lv_types.h"
#include "private.h"
#include <stdint.h>
#include <string.h>
static int luavgl_imgdata_create(lua_State *L)
{
if (!lua_isstring(L, 1)) {
return luaL_argerror(L, 1, "expect string");
}
lv_img_decoder_dsc_t descriptor;
lv_res_t res =
lv_img_decoder_open(&descriptor, lua_tostring(L, 1), lv_color_black(), 0);
if (res != LV_RES_OK) {
return luaL_error(L, "failed to decode image.");
}
lv_img_dsc_t *data = lv_mem_alloc(sizeof(lv_img_dsc_t));
data->header = descriptor.header;
data->data_size = data->header.w * data->header.h * sizeof(uint32_t); // ???
uint8_t *data_copy = lv_mem_alloc(data->data_size);
memcpy(data_copy, descriptor.img_data, data->data_size);
data->data = data_copy;
lv_img_decoder_close(&descriptor);
lua_pushlightuserdata(L, data);
return 1;
}

@ -7,6 +7,7 @@
#include "font.c" #include "font.c"
#include "fs.c" #include "fs.c"
#include "group.c" #include "group.c"
#include "imgdata.c"
#include "indev.c" #include "indev.c"
#include "obj.c" #include "obj.c"
#include "timer.c" #include "timer.c"
@ -17,6 +18,7 @@ static const struct luaL_Reg luavgl_methods[] = {
{"Font", luavgl_font_create }, /* font.c */ {"Font", luavgl_font_create }, /* font.c */
{"Style", luavgl_style_create}, /* style.c */ {"Style", luavgl_style_create}, /* style.c */
{"Anim", luavgl_anim_create }, /* anim.c */ {"Anim", luavgl_anim_create }, /* anim.c */
{"ImgData", luavgl_imgdata_create}, /* imgdata.c */
{NULL, NULL }, {NULL, NULL },
}; };

@ -7,16 +7,16 @@ local queue = require("queue")
local screen = require("screen") local screen = require("screen")
local img = { local img = {
play = "//lua/img/play.png", play = lvgl.ImgData("//lua/img/play.png"),
pause = "//lua/img/pause.png", pause = lvgl.ImgData("//lua/img/pause.png"),
next = "//lua/img/next.png", next = lvgl.ImgData("//lua/img/next.png"),
next_disabled = "//lua/img/next_disabled.png", next_disabled = lvgl.ImgData("//lua/img/next_disabled.png"),
prev = "//lua/img/prev.png", prev = lvgl.ImgData("//lua/img/prev.png"),
prev_disabled = "//lua/img/prev_disabled.png", prev_disabled = lvgl.ImgData("//lua/img/prev_disabled.png"),
shuffle = "//lua/img/shuffle.png", shuffle = lvgl.ImgData("//lua/img/shuffle.png"),
shuffle_disabled = "//lua/img/shuffle_disabled.png", shuffle_disabled = lvgl.ImgData("//lua/img/shuffle_disabled.png"),
repeat_enabled = "//lua/img/repeat.png", repeat_enabled = lvgl.ImgData("//lua/img/repeat.png"),
repeat_disabled = "//lua/img/repeat_disabled.png", repeat_disabled = lvgl.ImgData("//lua/img/repeat_disabled.png"),
} }
local is_now_playing_shown = false local is_now_playing_shown = false

@ -6,6 +6,20 @@ local backstack = require("backstack")
local theme = require("theme") local theme = require("theme")
local database = require("database") local database = require("database")
local img = {
db = lvgl.ImgData("//lua/img/db.png"),
chg = lvgl.ImgData("//lua/img/bat/chg.png"),
bat_100 = lvgl.ImgData("//lua/img/bat/100.png"),
bat_80 = lvgl.ImgData("//lua/img/bat/80.png"),
bat_60 = lvgl.ImgData("//lua/img/bat/60.png"),
bat_40 = lvgl.ImgData("//lua/img/bat/40.png"),
bat_20 = lvgl.ImgData("//lua/img/bat/20.png"),
bat_0 = lvgl.ImgData("//lua/img/bat/0.png"),
bat_0chg = lvgl.ImgData("//lua/img/bat/0chg.png"),
bt_conn = lvgl.ImgData("//lua/assets/bt_conn.png"),
bt = lvgl.ImgData("//lua/assets/bt.png")
}
local widgets = {} local widgets = {}
function widgets.MenuScreen(opts) function widgets.MenuScreen(opts)
@ -93,14 +107,10 @@ function widgets.StatusBar(parent, opts)
status_bar.title:set { text = opts.title } status_bar.title:set { text = opts.title }
end end
status_bar.db_updating = status_bar.root:Image { status_bar.db_updating = status_bar.root:Image { src = img.db }
src = "//lua/img/db.png"
}
status_bar.bluetooth = status_bar.root:Image {} status_bar.bluetooth = status_bar.root:Image {}
status_bar.battery = status_bar.root:Image {} status_bar.battery = status_bar.root:Image {}
status_bar.chg = status_bar.battery:Image { status_bar.chg = status_bar.battery:Image { src = img.chg }
src = "//lua/img/bat/chg.png"
}
status_bar.chg:center() status_bar.chg:center()
local is_charging = nil local is_charging = nil
@ -110,20 +120,20 @@ function widgets.StatusBar(parent, opts)
if is_charging == nil or percent == nil then return end if is_charging == nil or percent == nil then return end
local src local src
if percent >= 95 then if percent >= 95 then
src = "100.png" src = img.bat_100
elseif percent >= 75 then elseif percent >= 75 then
src = "80.png" src = img.bat_80
elseif percent >= 55 then elseif percent >= 55 then
src = "60.png" src = img.bat_60
elseif percent >= 35 then elseif percent >= 35 then
src = "40.png" src = img.bat_40
elseif percent >= 15 then elseif percent >= 15 then
src = "20.png" src = img.bat_20
else else
if is_charging then if is_charging then
src = "0chg.png" src = img.bat_0chg
else else
src = "0.png" src = img.bat_0
end end
end end
if is_charging then if is_charging then
@ -131,7 +141,7 @@ function widgets.StatusBar(parent, opts)
else else
status_bar.chg:add_flag(lvgl.FLAG.HIDDEN) status_bar.chg:add_flag(lvgl.FLAG.HIDDEN)
end end
status_bar.battery:set_src("//lua/img/bat/" .. src) status_bar.battery:set_src(src)
end end
status_bar.bindings = { status_bar.bindings = {
@ -159,9 +169,9 @@ function widgets.StatusBar(parent, opts)
end), end),
bluetooth.connected:bind(function(connected) bluetooth.connected:bind(function(connected)
if connected then if connected then
status_bar.bluetooth:set_src("//lua/assets/bt_conn.png") status_bar.bluetooth:set_src(img.bt_conn)
else else
status_bar.bluetooth:set_src("//lua/assets/bt.png") status_bar.bluetooth:set_src(img.bt)
end end
end), end),
} }

@ -427,6 +427,13 @@ end
function lvgl.Font(family, size, weight) function lvgl.Font(family, size, weight)
end end
--- Decodes an image from the filesystem and pins it into RAM, returning a
--- lightuserdata that can be passed to `img:set_src`.
--- @param path? string path to the encoded image
--- @return ImgData
function lvgl.ImgData(path)
end
--- Create a new style that can be applied to objects via `obj:add_style`. --- Create a new style that can be applied to objects via `obj:add_style`.
--- @param p? StyleProp Style properties that will be applied by this style. --- @param p? StyleProp Style properties that will be applied by this style.
--- @return Style --- @return Style
@ -1248,6 +1255,12 @@ Font is a light userdata that can be uset to set style text_font.
local font = {} local font = {}
--- Decoded image data that is pinned to memory.
--- @class ImgData
local ImgData = {}
--- ---
--- @class Style : lightuserdata --- @class Style : lightuserdata
--- ---

Loading…
Cancel
Save