From: Olav Bakke Svendsen Date: Thu, 30 May 2024 19:22:54 +0000 (+0200) Subject: Chat controlled speed controllers X-Git-Tag: rscr~1 X-Git-Url: http://git.olavbs.no/?a=commitdiff_plain;h=88f6268ba79b3688882bd53e66a3441bf09f3278;p=cc.git Chat controlled speed controllers --- diff --git a/rscr/config.lua b/rscr/config.lua index 30e0746..1e9a714 100644 --- a/rscr/config.lua +++ b/rscr/config.lua @@ -6,6 +6,7 @@ return , monitor = nil , speed_controller = nil , stressometer = nil + , chatbox = nil , controller_port = 15 , remote_port = 16 , min_speed = 0 diff --git a/rscr/controller.lua b/rscr/controller.lua index c7bfb4e..8d7c79e 100644 --- a/rscr/controller.lua +++ b/rscr/controller.lua @@ -101,24 +101,51 @@ local st = do if not stressometer then st.stress = nil end end +local set_times = {} +local set_timeout = function() + local now = os.clock() + table.insert(set_times, now) + while set_times[1] and now - set_times[1] > 10 do + table.remove(set_times, 1) + end + if #set_times >= 20 then + if chatbox then + peripheral.call(chatbox, "sendMessage", "Hey! Stop that!", config.controller_name.." speed controller") + end + return false + else + return true + end +end + -- if called without argument: (getter) -- watch for changes. returns true if changed, false otherwise. -- if called with table: (setter) -- make changes accordingly. return true -local update = function(set_speed) +local update = function(t) local new_st = textutils.unserialize(textutils.serialize(st)) local updated = false local x = config.max_speed - config.min_speed - if set_speed and set_speed >= 0 and set_speed <= 100 then - new_st.speed.absolute = math.floor(((set_speed / 100) * x) + config.min_speed + 0.5) + if t and (t.set_absolute or t.set_percent) and set_timeout() then + if t.set_absolute and + t.set_absolute >= config.min_speed and + t.set_absolute <= config.max_speed then + new_st.speed.absolute = math.floor(t.set_absolute + 0.5) + + elseif t.set_percent and + t.set_percent >= 0 and + t.set_percent <= 100 then + new_st.speed.absolute = math.floor(((t.set_percent / 100) * x) + config.min_speed + 0.5) + end peripheral.call(speed_controller, "setTargetSpeed", new_st.speed.absolute) - updated = true else new_st.speed.absolute = peripheral.call(speed_controller, "getTargetSpeed") - if new_st.speed.absolute ~= st.speed.absolute then updated = true end end - new_st.speed.percentage = math.floor(((new_st.speed.absolute - config.min_speed) / x * 10000) + 0.5) / 100 + if new_st.speed.absolute ~= st.speed.absolute then + new_st.speed.percentage = math.floor(((new_st.speed.absolute - config.min_speed) / x * 10000) + 0.5) / 100 + updated = true + end if stressometer then sleep(0) -- if speed was just set, this is needed @@ -146,11 +173,13 @@ receive = function() c_port == config.controller_port and r_port == config.remote_port then local t = textutils.unserialize(msg) - if t.connect then - transmit(st) - elseif t.set_speed then - update(t.set_speed) - transmit(st) + if t then + if t.connect then + transmit(st) + elseif t.set_percent or t.set_absolute then + update(t) + transmit(st) + end end end return receive() @@ -163,8 +192,46 @@ update_remotes = function() return update_remotes() end +local chat +chat = function() + local _, user, msg, _, _ = os.pullEvent("chat") + if not chatbox then return chat() end + if string.find(string.lower(msg), string.lower(config.controller_name)) then + local cb = peripheral.wrap(chatbox) + local percent = tonumber(string.match(msg, "(-?%d*%.?%d+)%%")) + local absolute = tonumber(string.match(msg, "-?%d+")) + local response = nil + if percent then + if percent >= 0 and percent <= 100 then + update({ set_percent = percent }) + transmit(st) + response = "Now running at "..tostring(st.speed.percentage).."% ("..tostring(st.speed.absolute).."RPM)" + else + response = "Invalid percentage" + end + elseif absolute then + if absolute >= config.min_speed and absolute <= config.max_speed then + update({ set_absolute = absolute }) + transmit(st) + response = "Now running at "..tostring(st.speed.absolute).."RPM ("..tostring(st.speed.percentage).."%)" + else + response = "Invalid RPM" + end + else + response = "Running at "..tostring(st.speed.absolute).."RPM ("..tostring(st.speed.percentage).."%)" + if stressometer then + local a, c, p = st.stress.absolute, st.stress.capacity, st.stress.percentage + response = response.."\n"..tostring(a).."SU / "..tostring(c).."SU used ("..tostring(p).."%)\n"..tostring(c-a).."SU available" + if a > c then response = response.."\n§l§cOverstressed!" end + end + end + if response then cb.sendMessage(response, "§6"..config.controller_name, "()", "§6") end + end + return chat() +end + term.setTextColor(colors.green) print("Controller online") term.setTextColor(colors.white) -parallel.waitForAny(receive, update_remotes) +parallel.waitForAny(receive, update_remotes, chat) diff --git a/rscr/remote.lua b/rscr/remote.lua index d88d579..a6636b0 100644 --- a/rscr/remote.lua +++ b/rscr/remote.lua @@ -183,7 +183,7 @@ touch_screen = function() -- todo: don't hard code the y position m.setCursorPos(x, 4) m.blit("|", "0", "f") - transmit({ set_speed = math.floor((x-1)/(width-1)*10000)/100 }) + transmit({ set_percent = math.floor((x-1)/(width-1)*10000)/100 }) end return touch_screen() end