Chat controlled speed controllers
authorOlav Bakke Svendsen <mail@olavbs.no>
Thu, 30 May 2024 19:22:54 +0000 (21:22 +0200)
committerOlav Bakke Svendsen <mail@olavbs.no>
Thu, 30 May 2024 19:22:54 +0000 (21:22 +0200)
rscr/config.lua
rscr/controller.lua
rscr/remote.lua

index 30e07460f664f0975e30fdf6663c389e39d4a60f..1e9a7145f8e79673eab2282933ead0aa61a85665 100644 (file)
@@ -6,6 +6,7 @@ return
   , monitor = nil
   , speed_controller = nil
   , stressometer = nil
+  , chatbox = nil
   , controller_port = 15
   , remote_port = 16
   , min_speed = 0
index c7bfb4ef154abc47610a70d1f9f5a3f26ef41181..8d7c79e8f196f0fb1f29fa184a18ebaabde368a5 100644 (file)
@@ -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)
index d88d57947bfbb738fbea7d349a02ec0c265530db..a6636b0f88de7a56cafdb0b6d2c1758c7b7faabe 100644 (file)
@@ -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