Jump to content




Easy Monitor Touch Screen For Bundled Cables

peripheral lua

  • You cannot reply to this topic
No replies to this topic

#1 Credinus

  • New Members
  • 1 posts

Posted 21 April 2013 - 09:09 PM

Hey folks, first time posting around here. I wanted to share a quick little code tutorial on based on a program I'm working on in my server. I searched for a long time to find existing tutorials to do something similar, but I really couldn't find anything useful and ended up going from scratch. So let's get right down to it:

What the program does: This program creates an interface with an advanced monitor, which lists various text options users can click on to send on/off signals through bundled cable.
What you'll need: You're going to need an advanced computer, some advanced monitors (I'm currently using a 2x3 widescreen setup), some bundled cable, some insulated wire, and some sort of output source for the cables (I'm using wireless transmitters). While not necessary, to follow exactly along with the code in this tutorial, you'll also need the bundled cable API (thread for information on that here: http://www.computerc...-bundled-cable/).

Once you've got everything you need, you'll want to setup your monitors on one side of your computer and your bundled cable going out to its output source. Download the bundleAPI and keep it in whatever directory your program is going to be in.

Now on with the code... I'll post it in snippits with explanations, and then the full example at the end.

os.unloadAPI("bundleAPI")
os.loadAPI("bundleAPI")
This is goes at the very beginning. I like to add an unloadAPI before the loadAPI just as a debugging precaution; make sure that the section in the quotes (in this case, bundleAPI) is the same as whatever you named your bundleAPI when you downloaded it.

--Set the position of your monitor relative to the computer for use in the code
mon = peripheral.wrap("left")

--Clears the monitor
--Cursor positions in X,Y format
mon.clear()
mon.setCursorPos(5,1)
mon.setTextColor(1)
mon.write("Some fancy header text!")
mon.setCursorPos(1,2)
mon.write("Please choose an option below:")
mon.setCursorPos(1,3)
mon.setTextColor(8)
mon.write("Option1")
mon.setCursorPos(1,4)
mon.setTextColor(4)
mon.write("Option2")

This is your next snippit, which is a block of code that will setup your display with two lines of headers, and a couple of options to choose from. Note, as per the comment, that the setCursorPos method is used to set where the next line of text is going to go, with the first number being the x-coord and the second number being the y-coord. As such, whenever you want to go to the next line to start, just go up by one on the second number. The setTextColor option is used distinguish a difference between the two options more distinctly. Especially important here is the y-coordinates for the "Option1" and "Option2" text, which will be what we use to determine which option was selected by the click.

while true do
event, side, xPos, yPos = os.pullEvent("monitor_touch")
   if (yPos) == 3 then
	bundleAPI.off("bottom","white")
	mon.setCursorPos(4,11)
	mon.setTextColor(16)
	mon.write("Selected Option1")
   elseif (yPos) == 4 then
	bundleAPI.on("bottom","white")
	mon.setCursorPos(4,11)
	mon.setTextColor(16)
	mon.write("Selected Option2")
   end
end

And here's the last bit, the most complicated part of the code. This function makes this program run indefinitely (or at least until someone terminates it) and records where a person clicks on the screen. It then checks to see if the clicked location was on the same line as one of the two options, and if so, turns the redstone signal through a white wire on/off. So let's go more in detail...

while true do
event, side, xPos, yPos = os.pullEvent("monitor_touch")
Leave this code as-is (though you can change the names of the event/side/xpos/ypos variables, but I don't recommend it). Just know that this sets up the program to pull the information whenever someone clicks on the monitor.

   if (yPos) == 3 then
	bundleAPI.off("bottom","white")
	mon.setCursorPos(4,11)
	mon.setTextColor(16)
	mon.write("Selected Option1")
Now the beginning of the conditional statement... By seeing if "yPos" equals 3, we are checking to see if the user's click was on the third line of the monitor. If it is, then it uses the bundleAPI.off method to turn off any redstone signal coming out of a white wire which is connected to the bundled cable coming out of the bottom of the computer. Important note: if your bundled cable isn't connect to the bottom side of your computer, be sure to change it to the relative side (top, left, right, etc.). Additionally, the "mon" functions here will add a new line of text on the 11th line of the monitor, starting 4 spaces over (to give it a more centered look), to let the user know that Option1 was selected.

   elseif (yPos) == 4 then
	bundleAPI.on("bottom","white")
	mon.setCursorPos(4,11)
	mon.setTextColor(16)
	mon.write("Selected Option2")
   end
end
And the last block... Basically, the same thing as with the snippit above, but for when the user clicks on the fourth line (Option2). Of course, two "end" statements to close the conditional statement and the while loop.


What am I using it for? Well, I'm using it to control railcraft switch tracks using wireless redstone signals. Basically, at my central station, the user selects which destination they want to take a train to on the monitor, and all of the appropriate redstone signals get activated/de-activated to make the track go to the right place. The reason I went with this approach rather than buttons (such as the Dark Buttons program that is built specifically for bundled cable) is because I needed to be able to activate/de-activate multiple cables with a single click, so using buttons would get hairy quick. Using this method, I can very easily add new locations, and it's very easy to read and use for other individuals.

Planned features to be added: For starters, I'm going to update it so that I can check both x and y position so that you can make more use of the screenspace, rather than a long vertical column of options. I will also change the destination output at the bottom of the screen to be based on a function that checks the input of cables, rather than the selected destination; this will allow for me to make destination selectors at other stations and not have them interfere with each other.

That's all for now, though I'll be adding a little more functionality to it in the coming week and will update the tutorial accordingly. I hope you found this tutorial to be helpful, and good luck with your program! And here is the full code:

os.unloadAPI ("bundleAPI")
os.loadAPI ("bundleAPI")

mon = peripheral.wrap("left")

mon.clear()
mon.setCursorPos(5,1)
mon.setTextColor(1)
mon.write("Header on line 1")
mon.setCursorPos(1,2)
mon.write("Please choose an option:")
mon.setCursorPos(1,3)
mon.setTextColor(8)
mon.write("Option1")
mon.setCursorPos(1,4)
mon.setTextColor(4)
mon.write("Option2")

while true do
event, side, xPos, yPos = os.pullEvent("monitor_touch")
   if (yPos) == 3 then
	bundleAPI.off("bottom","white")
	mon.setCursorPos(4,11)
	mon.setTextColor(16)
	mon.write("Selected Option1)
   elseif (yPos) == 4 then
	bundleAPI.on("bottom","white")
	mon.setCursorPos(4,11)
	mon.setTextColor(16)
	mon.write("Selected Option2")
   end
end

Screenshots of the program in action, plus setup:
http://imgur.com/p5l6Utr
http://imgur.com/EFsTsRU
http://imgur.com/Go4wnzg





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users