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

Adding redstone.setBundledOutput to a Touchscreen.

Started by Pixelmnkey, 31 January 2013 - 04:10 AM
Pixelmnkey #1
Posted 31 January 2013 - 05:10 AM
Hello! The basic idea of this is to have a touchscreen where u'll push 4 buttons and each one will send a bundledoutput color then go into an active state, once it's clicked again it will stop sending signal.

I've got the next code:


llocal term = peripheral.wrap("top")
term.setTextScale(1.5)
local mob={}

function setTable(name, freq, xmin, xmax, ymin, ymax)
   mob[name] = {}
   mob[name]["freq"] = freq
   mob[name]["active"] = false
   mob[name]["xmin"] = xmin
   mob[name]["ymin"] = ymin
   mob[name]["xmax"] = xmax
   mob[name]["ymax"] = ymax
end

function fillTable()
   setTable("Chicken", colors.red, 17, 24, 2, 3)
   setTable("Cow", colors.white, 17, 20, 6, 7)
   setTable("Pig", colors.yellow, 17, 20, 4, 5)
end

function fill(x,y,color,text)
  term.setBackgroundColor(color)
  term.setCursorPos(x,y)
  term.write(text)
  term.setBackgroundColor(colors.black)
end

function screen()
   local y = 2
   local currColor
   for name,data in pairs(mob) do
	  local on = data["active"]
	  if on == true then currColor = colors.lime else currColor = colors.red end
	  fill(17,y,currColor, name)
	  y = y+2
   end
end

function checkxy(x, y)
   for name, data in pairs(mob) do
	  if y>=data["ymin"] and  y <= data["ymax"] then
	    if x>=data["xmin"] and x<= data["xmax"] then
		  data["active"] = not data["active"]
		  print(name)
	    end
	  end
   end
end

function setWire()
   local wire = 0
   for name, data in pairs(mob) do
	  if data["active"] == false then
		 wire = colors.combine(wire, data["freq"])
	  end
   end
   redstone.setBundledOutput("bottom", wire)
end

fillTable()
setWire()
while true do
  term.clear()
  screen()
--  fill(17,2,colors.lime, "Chicken")
  local e,side,x,y = os.pullEvent("monitor_touch")
  print(x..":"..y)
  checkxy(x,y)
  setWire()
  sleep(.1)
end

I've got it from a Direwolf20 video, each time I try to change the size of the buttons or adding one more to the table, it messes up the touchscreen, so, i think it has something to do with where the touchscreen is showing the buttons and where is recognicing the click to change the state and send the rs signal. I can add more buttons with custom names and colors but the touchscreen won't chage them into green if i click on them as it does with the other ones. Wich part should i edit to get it working? I have to change the size in other place than the filltable function?

Please help, thanks!
Doyle3694 #2
Posted 31 January 2013 - 05:57 AM
I suggest you start it by yourself from scratch instead of relying on someone else's code which you don't know how it works :)/>
remiX #3
Posted 31 January 2013 - 06:33 AM
Are you friends with soccer16x? Because your code looks exactly like his. Or am I wrong - is it the other way round (his code looks like yours?)
KaoS #4
Posted 31 January 2013 - 06:43 AM
Are you friends with soccer16x? Because your code looks exactly like his. Or am I wrong - is it the other way round (his code looks like yours?)

I've got it from a Direwolf20 video
remiX #5
Posted 31 January 2013 - 06:47 AM
Are you friends with soccer16x? Because your code looks exactly like his. Or am I wrong - is it the other way round (his code looks like yours?)

I've got it from a Direwolf20 video

Oh, woops .. Maybe I shouldn't just read the code next time ;)/>/>

How exactly are adding it to the table (what co-ords are you giving it and how does it mess up the screen touchscreen?
Pixelmnkey #6
Posted 31 January 2013 - 07:28 AM
I suggest you start it by yourself from scratch instead of relying on someone else's code which you don't know how it works :)/>

I do know how it works, the problem is that i CAN'T manage to add any other button or change the size.


Are you friends with soccer16x? Because your code looks exactly like his. Or am I wrong - is it the other way round (his code looks like yours?)

I've got it from a Direwolf20 video



Oh, woops .. Maybe I shouldn't just read the code next time ;)/>/>

How exactly are adding it to the table (what co-ords are you giving it and how does it mess up the screen touchscreen?

From the function setTable, i'm calling another setTable from the fillTable function like that:


function fillTable()
   setTable("Chicken", colors.red, 17, 24, 2, 3)
   setTable("Cow", colors.white, 17, 20, 6, 7)
   setTable("Pig", colors.yellow, 17, 20, 4, 5)
   setTable("Enderman", colors.black, 17, 20, 8, 10)

It adds the button of the same size as the other but the touchscreen dosn't change the coords of where it recognices the punch, i mean you'll see the button but if click it it won't turn green like the others do, instead for example, the Chicken button changes, and so on.

So i was wondering if i need to add the coords again somewhere in that code to tell the touchscreen that there's another button.


EDIT:

I've made the program kinda from the scrap and i managed to send rs signal each time someone punches a button in the screen, but how do i make to turn it off when they punch the button again? for example, when it's green in the screen it's sending rs signal to the bundled cable and when they punch the screen again it stops? or if it's easier how can i just send rs signal for 1 tick or so and conect a toggle latch from rp2?

Here's the code:


local mon = peripheral.wrap("right")
mon.setTextScale(1)
mon.setTextColor(colors.white)
local button={}
mon.setBackgroundColor(colors.black)
	
function setTable(name, func, xmin, xmax, ymin, ymax)
   button[name] = {}
   button[name]["func"] = func
   button[name]["active"] = false
   button[name]["xmin"] = xmin
   button[name]["ymin"] = ymin
   button[name]["xmax"] = xmax
   button[name]["ymax"] = ymax
end

function pigman()
   print("Pigman")
   redstone.setBundledOutput("back", colors.black)

end
function enderman()
   print("Enderman")
end	   
function spider()
   print("Spider")
end
function skeleton()
   print("Skeleton")
end
function fillTable()
   setTable("Pigman", pigman, 5, 35, 4, 6)
   setTable("Enderman", enderman, 5, 35, 7, 9)
   setTable("Spider", spider, 5, 35, 10, 12)
   setTable("Skeleton", skeleton, 5, 35, 13, 15)
end   

function fill(text, color, bData)
   mon.setBackgroundColor(color)
   local yspot = math.floor((bData["ymin"] + bData["ymax"]) /2)
   local xspot = math.floor((bData["xmax"] - bData["xmin"] - string.len(text)) /2) +1
   for j = bData["ymin"], bData["ymax"] do
	  mon.setCursorPos(bData["xmin"], j)
	  if j == yspot then
		 for k = 0, bData["xmax"] - bData["xmin"] - string.len(text) +1 do
		    if k == xspot then
			   mon.write(text)
		    else
			   mon.write(" ")
		    end
		 end
	  else
		 for i = bData["xmin"], bData["xmax"] do
		    mon.write(" ")
		 end
	  end
   end
   mon.setBackgroundColor(colors.black)
end
	
function screen()
   local currColor
   for name,data in pairs(button) do
	  local on = data["active"]
	  if on == true then currColor = colors.lime else currColor = colors.red end
	  fill(name, currColor, data)
   end
end
	
function checkxy(x, y)
   for name, data in pairs(button) do
	  if y>=data["ymin"] and  y <= data["ymax"] then
		 if x>=data["xmin"] and x<= data["xmax"] then
		    data["func"]()
		    data["active"] = not data["active"]
		    print(name)
		 end
	  end
   end
end
	
function heading(text)
   w, h = mon.getSize()
   mon.setCursorPos((w-string.len(text))/2+1, 1)
   mon.write(text)
end
	
fillTable()
while true do
   mon.clear()
   heading("Pixel's Mobspawner Control")
   screen()
   local e,side,x,y = os.pullEvent("monitor_touch")
   print(x..":"..y)
   checkxy(x,y)
   sleep(.1)
end
KaRnaGe60 #7
Posted 16 February 2013 - 02:46 AM
http://pastebin.com/W271tcFm

and you add a toggle latch

Spoiler