From 96d5ee9018e437e780bc3a1bcac613941dc47955 Mon Sep 17 00:00:00 2001 From: Michal Cieslakiewicz Date: Sat, 9 Feb 2019 12:20:27 +0100 Subject: [PATCH] sl_controller: add battery status command. Added $battery() function that returns battery percentage (0-100). Signed-off-by: Michal Cieslakiewicz --- sl_controller/controller.lua | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/sl_controller/controller.lua b/sl_controller/controller.lua index 2784da3..9344c81 100644 --- a/sl_controller/controller.lua +++ b/sl_controller/controller.lua @@ -130,6 +130,22 @@ sl_controller.register_function("position", { " returns the position '(x,y,z)' of the device\n with given number." }) +sl_controller.register_action("battery", { + cmnd = function(self) + local meta = minetest.get_meta(self.meta.pos) + local batpos = minetest.string_to_pos(meta:get_string("battery")) + local batmeta = minetest.get_meta(batpos) + local val = (sl_controller.battery_capacity - + math.min(batmeta:get_int("content") or 0, + sl_controller.battery_capacity)) + return 100 - math.floor((val * 100.0 / sl_controller.battery_capacity)) + end, + help = " $battery()\n".. + " Get charge level of battery connected to Controller.\n".. + " Function returns percent number (0-100) where 100 means full.\n".. + " example: battery_percent = $battery()" +}) + local function formspec0(meta) local state = meta:get_int("state") == tubelib.RUNNING