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

Redtracker

Started by Engineer, 30 November 2015 - 02:55 PM
Engineer #1
Posted 30 November 2015 - 03:55 PM
Hello fellow forum members!

Recently I started to do some Lua again and I had to write boilerplate code over and over again to track the redstone inputs. Out of annoyance I wrote a simple API that keeps track of the states of all sides, but it needed more. I decided to implement callbacks for users to implement to not even have to check which side changed. This is how the API looks, it has three functions:
  1. redtracker.update()
  2. No arguments need to be supplied to this function. This function checks if any sides changed and if a side changed, it will call the callbacks which are registered. Note that the functions are called directly if input has changed, so the function which handles it should not take a long time to proces; it has not been designed to do so.
  3. redtracker.addCallback( mode, function [, side])
  4. This function does what you expect it to do, it adds a callback function to the specific side and mode. If the side is not given, it will register to all sides, this is useful if you need to look at all sides and don't care for the specific side. The mode is at this point 'regular', 'analog' or 'bundled.' This is done so the function and underlying code is dynamic. That means when another redstone.get*Input gets added, it is an easy thing to add this. When a callback function is called, it will be given the side and state of redstone, so let's imagine this code:
    
    	local function callback(side, active)
    		print("The redstone on the " .. side .. " is " .. (active and "" or "not ") .. "active!")
    	end
    	os.loadAPI("redtracker")
    	redtracker.addRegularCallback(callback)
    Let's imagine now the redstone on the top turns on; the redstone on the back turns on; the redstone on the top turns off. The output would be:
    
    	The redstone on the top is active!
    	The redstone on the back is active!
    	The redstone on the top is not active!
    The active parameter changes with type of redstone, for the bundled register the active parameter will be a number representing its colors; for the analog parameter it will be a number ranging from 0-15 representing the analog signal as specified on the wiki.
  5. redtracker.addRegularCallback( func [,side])
  6. This function is a synonym for redtracker.addCallback("regular", func, side)
  7. redtracker.addAnalogCallback( func [,side])
  8. This function is a synonym for redtracker.addCallback("analog", func, side)
  9. redtracker.addBundledCallback( func [,side])
  10. This function is a synonym for redtracker.addCallback("bundled", func, side)
  11. redtracker.waitRedstoneUpdate()
  12. This function simply waits for a redstone event to occur and then calls the update method. This is usefull if you don't want to call os.pullEvent yourself for some odd reason :P/>
If your application depends on the redstone alone, your code should look like this:

os.loadAPI("redtracker")
-- Add the callbacks to the sides
while true do
	redtracker.waitRedstoneUpdate()
end
When your application also needs to capture other events, simply do:

os.loadAPI("redtracker")
-- Add the callbacks to the sides
while true do
	local event, p1, p2, p3 = os.pullEvent()
	if event == "redstone" then
	   redtracker.update() -- You can always call the update function, but it only has literal function when redstone changes
	elseif event == "some other event " then
		-- blabla
	end
end

If you are still interested, feel free to download it on pastebin or get it directly on your CC PC:

pastebin get ANDVZV8V redtracker

If you want to use this code in a program of yours, a credit to me would be nice but is not really required since it is a very small utility.

Enjoy!
Edited on 30 November 2015 - 07:30 PM
apemanzilla #2
Posted 30 November 2015 - 04:17 PM
Thanks, this is actually almost exactly what I was looking for for one of my new programs! I'll definitely be using this.
Engineer #3
Posted 30 November 2015 - 08:32 PM
Thanks, this is actually almost exactly what I was looking for for one of my new programs! I'll definitely be using this.
Glad I could help you :)/>

On another note, I have updated the code so it works with bundled and analog redstone functions. Also the code is optimized to work with a new future input; it would take one line to update it to that specific function. This means that the main algorithm is dynamic! :D/> The docs are edited accordingly in the main post.