This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Lupus590's profile picture

Redstone state reporter

Started by Lupus590, 30 December 2015 - 12:18 AM
Lupus590 #1
Posted 30 December 2015 - 01:18 AM
Just a quick thing I made on request, may as well share it.

It watches a redstone signal and displays a message to a monitor depending on the redstone state.


local inputSide = "bottom"
local passThroughSide = "top"
local monitorSide = "left"
local onMessage = "Laboratory active"
local offmessage = "Laboratory inactive"

local mon = peripheral.wrap(monitorSide)
if not mon then
  error("no monitor on expected side: "..monitorSide)
end
local event
mon.setCursorBlink(false)
while true do
  event = { os.pullEvent("redstone")}
  mon.clear()
  mon.setCursorPos(1,1)
  if redstone.getInput(inputSide) then
	mon.write(onMessage)
	redstone.setOutput(passThroughSide,true)
  else
   mon.write(offMessage)
	redstone.setOutput(passThroughSide,false)
  end
end
  

Edit: bug fixes
Edited on 30 December 2015 - 02:06 PM
moomoomoo3O9 #2
Posted 01 January 2016 - 11:26 PM
If I could make a few recommendations…
  • If the monitorside is nil or an empty string, use peripheral.find() to get the monitor.
  • if passThroughSide is nil or an empty string, don't pass the redstone through.
  • Use a centerPrint function! (It looks something like this):

function centerPrint(msg)
  term.setCursorPos(term.getSize()[1]/2-msg:len()/2,term.getCursorPos()[2])
  term.write(msg)
end
Edited on 01 January 2016 - 10:26 PM
Lupus590 #3
Posted 02 January 2016 - 01:38 AM
This was a quick and dirty script for a program request, thought I'd post it so that others don't make similar requests.

I have no intent to make changes or provide a lot of support for this.

If others wish to add the above improvements then go ahead.
Edited on 02 January 2016 - 12:38 AM
moomoomoo3O9 #4
Posted 04 January 2016 - 02:47 AM
Alright, here's my suggested improvements implemented:

local inputSide = "bottom"
local passThroughSide = "top"
local monitorSide = ""
local onMessage = "Laboratory active"
local offmessage = "Laboratory inactive"
local function centerPrint(msg)
	term.setCursorPos(({term.getSize()})[1]/2-(msg:len())/2,({term.getCursorPos()})[2])
    --#If you want the nth argument of something that returns multiple values in one line, you have to table it in parentheses first
    --#So, ({term.getSize()})[n] because it will try to do the indexing first
	term.write(msg)
end
local mon = (monitorSide and monitorSide:len()>0 and peripheral.wrap(monitorSide)) or peripheral.find("monitor")
if not mon then
	error("No monitor found.")
end
term.redirect(mon)
term.setCursorBlink(false)
while true do
	os.pullEvent("redstone") --#You don't need to store the event since you don't use its return values
	term.clear()
	term.setCursorPos(1,({term.getSize()})[2]) --#The middle of the monitor on Y
	if rs.getInput(inputSide) then
		centerPrint(onMessage)
		if passThroughSide and passThroughSide:len()>0 then
			rs.setOutput(passThroughSide,true)
		end
	else
		centerPrint(offMessage)
		if passThroughSide and passThroughSide:len()>0 then
			rs.setOutput(passThroughSide,false)
		end
	end
end
Edited on 04 January 2016 - 12:10 PM
Lupus590 #5
Posted 04 January 2016 - 01:29 PM
Alright, here's my suggested improvements implemented:
 --#snip 

no need to check passThroughSide:len, if the var is nil then the if will equate to false
Edited on 04 January 2016 - 12:30 PM
moomoomoo3O9 #6
Posted 05 January 2016 - 10:04 PM
Alright, here's my suggested improvements implemented:
 --#snip 

no need to check passThroughSide:len, if the var is nil then the if will equate to false
I only checked it so if you leave the var as "", it won't error, and just won't pass through. That's why I checked both, for nil and empty strings. I guess I could do side validation instead, which would work as a better catch-all.