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
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()
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)