From a62f883d364de9390c696116d485042cdc189149 Mon Sep 17 00:00:00 2001 From: Joachim Stolberg Date: Thu, 30 May 2024 20:22:59 +0200 Subject: [PATCH] Improve command interface --- basis/command.lua | 2 ++ logic/button.lua | 74 +++++++++++++++++++++++++++++++++++++++------ logic/button_2x.lua | 44 ++++++++++++++++++++++++++- 3 files changed, 109 insertions(+), 11 deletions(-) diff --git a/basis/command.lua b/basis/command.lua index 0d53d2d..edc8af3 100644 --- a/basis/command.lua +++ b/basis/command.lua @@ -457,6 +457,7 @@ function techage.beduino_send_cmnd(src, number, topic, payload) if ninfo and ninfo.name and ninfo.pos then local ndef = NodeDef[ninfo.name] if ndef and ndef.on_beduino_receive_cmnd then + techage_counting_hit() return ndef.on_beduino_receive_cmnd(ninfo.pos, src, topic, payload or {}) end end @@ -469,6 +470,7 @@ function techage.beduino_request_data(src, number, topic, payload) if ninfo and ninfo.name and ninfo.pos then local ndef = NodeDef[ninfo.name] if ndef and ndef.on_beduino_request_data then + techage_counting_hit() return ndef.on_beduino_request_data(ninfo.pos, src, topic, payload or {}) end end diff --git a/logic/button.lua b/logic/button.lua index 07b6d2f..be8b094 100644 --- a/logic/button.lua +++ b/logic/button.lua @@ -87,7 +87,7 @@ local WRENCH_MENU4 = { }, } -local function switch_on(pos) +local function switch_on(pos, no_sound) local cycle_time = M(pos):get_int("cycle_time") local name = techage.get_node_lvm(pos).name if name == "techage:ta3_button_off" then @@ -96,14 +96,16 @@ local function switch_on(pos) logic.swap_node(pos, "techage:ta4_button_on") end logic.send_cmnd(pos, "command", "on", cycle_time) - minetest.sound_play("techage_button", { - pos = pos, - gain = 0.5, - max_hear_distance = 5, - }) + if not no_sound then + minetest.sound_play("techage_button", { + pos = pos, + gain = 0.5, + max_hear_distance = 5, + }) + end end -local function switch_off(pos, is_button) +local function switch_off(pos, no_sound) local name = techage.get_node_lvm(pos).name if name == "techage:ta3_button_on" then logic.swap_node(pos, "techage:ta3_button_off") @@ -115,7 +117,7 @@ local function switch_off(pos, is_button) (not meta:contains("command") or meta:get_string("command") == "on") then logic.send_off(pos, M(pos)) end - if not is_button then + if not no_sound then minetest.sound_play("techage_button", { pos = pos, gain = 0.5, @@ -425,13 +427,54 @@ minetest.register_craft({ }, }) -techage.register_node({"techage:ta3_button_off", "techage:ta3_button_on"}, {}) +techage.register_node({"techage:ta3_button_off", "techage:ta3_button_on"}, { + on_recv_message = function(pos, src, topic, payload) + if topic == "on" then + switch_on(pos, true) + return true + elseif topic == "off" then + switch_off(pos, true) + return true + elseif topic == "state" then + local name = techage.get_node_lvm(pos).name + return name == "techage:ta3_button_on" and "on" or "off" + else + return "unsupported" + end + end, + on_beduino_receive_cmnd = function(pos, src, topic, payload) + if topic == 1 and payload[1] == 1 then + switch_on(pos, true) + return 0 + elseif topic == 1 and payload[1] == 0 then + switch_off(pos, true) + return 0 + else + return 2 + end + end, + on_beduino_request_data = function(pos, src, topic, payload) + if topic == 131 then -- State + local name = techage.get_node_lvm(pos).name + return 0, name == "techage:ta3_button_on" and {1} or {0} + else + return 2, "" + end + end, + } +) techage.register_node({ "techage:ta4_button_off", "techage:ta4_button_on", }, { on_recv_message = function(pos, src, topic, payload) - if topic == "state" then + if topic == "on" then + switch_on(pos, true) + return true + elseif topic == "off" then + switch_off(pos, true) + return true + elseif topic == "state" then local name = techage.get_node_lvm(pos).name return name == "techage:ta4_button_on" and "on" or "off" elseif topic == "name" then @@ -444,6 +487,17 @@ techage.register_node({ return "unsupported" end end, + on_beduino_receive_cmnd = function(pos, src, topic, payload) + if topic == 1 and payload[1] == 1 then + switch_on(pos, true) + return 0 + elseif topic == 1 and payload[1] == 0 then + switch_off(pos, true) + return 0 + else + return 2 + end + end, on_beduino_request_data = function(pos, src, topic, payload) if topic == 131 then -- State local name = techage.get_node_lvm(pos).name diff --git a/logic/button_2x.lua b/logic/button_2x.lua index e38244d..a016f7a 100644 --- a/logic/button_2x.lua +++ b/logic/button_2x.lua @@ -244,7 +244,49 @@ minetest.register_node("techage:ta4_button_2x", { sounds = default.node_sound_glass_defaults(), }) -techage.register_node({"techage:ta4_button_2x"}, {}) +techage.register_node({"techage:ta4_button_2x"}, { + on_recv_message = function(pos, src, topic, payload) + local num = math.max(tonumber(payload) or 0, 1) + if topic == "on" then + switch_on(pos, num) + send_cmnd(pos, num) + return true + elseif topic == "off" then + switch_off(pos, num) + return true + elseif topic == "state" then + local nvm = techage.get_nvm(pos) + nvm.button = nvm.button or {} + return nvm.button[num] == true + else + return "unsupported" + end + end, + on_beduino_receive_cmnd = function(pos, src, topic, payload) + local num = math.max(payload[1], 1) + if topic == 23 and payload[2] == 1 then + switch_on(pos, num) + send_cmnd(pos, num) + return 0 + elseif topic == 23 and payload[2] == 0 then + switch_off(pos, num) + return 0 + else + return 2 + end + end, + on_beduino_request_data = function(pos, src, topic, payload) + if topic == 152 then -- State + local num = math.max(payload[1], 1) + local nvm = techage.get_nvm(pos) + nvm.button = nvm.button or {} + return 0, nvm.button[num] and {1} or {0} + else + return 2, "" + end + end, + } +) minetest.register_craft({ output = "techage:ta4_button_2x",