From 336fef53afdb880fdc3c65610cd3abc4420ff1ae Mon Sep 17 00:00:00 2001 From: Michal Cieslakiewicz Date: Sun, 31 Jan 2021 19:10:14 +0100 Subject: [PATCH] Allow read of Distributor slot status Add support for reading distributor slot status. This action is complementary to already present filter setting function. 'port' distributor message is modified to accept filter color only (without '=on/off'), device returns selected filter state. A wrapper function ($get_filter(...)) for Lua Controller and condition selector for ICTA Controller are implemented as well. Signed-off-by: Michal Cieslakiewicz --- basic_machines/distributor.lua | 15 +++++++++-- icta_controller/commands.lua | 49 ++++++++++++++++++++++++++++++++++ lua_controller/commands.lua | 13 +++++++++ 3 files changed, 75 insertions(+), 2 deletions(-) diff --git a/basic_machines/distributor.lua b/basic_machines/distributor.lua index cef664e..3db44c3 100644 --- a/basic_machines/distributor.lua +++ b/basic_machines/distributor.lua @@ -27,7 +27,7 @@ local STANDBY_TICKS = 3 local COUNTDOWN_TICKS = 4 local CYCLE_TIME = 4 -local INFO = [[Turn port on/off: command = 'port', payload = red/green/blue/yellow=on/off]] +local INFO = [[Turn port on/off or read its state: command = 'port', payload = red/green/blue/yellow{=on/off}]] --local Side2Color = {B="red", L="green", F="blue", R="yellow"} @@ -374,6 +374,13 @@ local function change_filter_settings(pos, slot, val) return true end +-- techage command to read filter channel status (on/off) +local function read_filter_settings(pos, slot) + local slots = {["red"] = 1, ["green"] = 2, ["blue"] = 3, ["yellow"] = 4} + local filter = minetest.deserialize(M(pos):get_string("filter")) + return filter[slots[slot]] and "on" or "off" +end + local function can_dig(pos, player) if minetest.is_protected(pos, player:get_player_name()) then return false @@ -437,7 +444,11 @@ local tubing = { elseif topic == "port" then -- "red"/"green"/"blue"/"yellow" = "on"/"off" local slot, val = techage.ident_value(payload) - return change_filter_settings(pos, slot, val) + if val == "" then + return read_filter_settings(pos, slot) + else + return change_filter_settings(pos, slot, val) + end else return CRD(pos).State:on_receive_message(pos, topic, payload) end diff --git a/icta_controller/commands.lua b/icta_controller/commands.lua index f2bab58..feb9cd0 100644 --- a/icta_controller/commands.lua +++ b/icta_controller/commands.lua @@ -744,3 +744,52 @@ techage.icta_register_action("set_filter", { end end, }) + +techage.icta_register_condition("get_filter", { + title = "read state of a Distributor filter slot", + formspec = { + { + type = "number", + name = "number", + label = "distri number", + default = "", + }, + { + type = "textlist", + name = "color", + label = "filter port", + choices = "red,green,blue,yellow", + default = "red", + }, + { + type = "textlist", + name = "operand", + choices = "is,is not", + default = "is", + }, + { + type = "textlist", + name = "value", + label = "state", + choices = "on,off", + default = "off", + }, + { + type = "label", + name = "lbl", + label = "Read state of a Distributor filter slot.\n", + }, + }, + button = function(data, environ) -- default button label + return 'fltr('..techage.fmt_number(data.number)..","..data.color..' '..data.operand..' '..data.value..')' + end, + code = function(data, environ) + local condition = function(env, idx) + return techage.send_single(environ.number, data.number, "port", data.color) + end + local result = function(val) + return techage.compare(val, data.value, data.operand) + end + return condition, result + end, +}) diff --git a/lua_controller/commands.lua b/lua_controller/commands.lua index ccb67f3..db1ed3f 100644 --- a/lua_controller/commands.lua +++ b/lua_controller/commands.lua @@ -105,6 +105,19 @@ techage.lua_ctlr.register_action("set_filter", { ' example: $set_filter("1234", "red", "off")' }) +techage.lua_ctlr.register_action("get_filter", { + cmnd = function(self, num, slot) + num = tostring(num or "") + slot = tostring(slot or "red") + if not_protected(self.meta.owner, num) then + return techage.send_single(self.meta.number, num, "port", slot) + end + end, + help = " $get_filter(num, slot)\n".. + ' Read state of a Distributor filter slot.\n'.. + ' Return value is "on" or "off".\n'.. + ' example: state = $get_filter("1234", "red")' +}) techage.lua_ctlr.register_action("display", { cmnd = function(self, num, row, text)