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

button api, turn off another toggle button

Started by Pjevser, 27 January 2015 - 11:04 PM
Pjevser #1
Posted 28 January 2015 - 12:04 AM
so i use direwolf20's button API: http://pastebin.com/sCM3Lp3j

And i have my own script like this (i'm new to computercraft doing so i didnt know any other way to do the redstone output, and yeah it's buggy that's why i'm asking for pro help :)/>)

os.loadAPI("button")
m = peripheral.wrap("top")
m.clear()
function fillTable()
  button.setTable("T1 Slate", t1slate, 2,14,4,6)
  button.setTable("T2 Slate", t2slate, 2,14,8,10)
  button.setTable("T3 Slate", t3slate, 2,14,12,14)
  button.setTable("T4 Slate", t4slate, 2,14,16,18)
  button.setTable("T5 Slate", t5slate, 2,14,20,22)
  button.screen()
end
function getClick()
  event,side,x,y = os.pullEvent("monitor_touch")
  button.checkxy(x,y)
end
function t1slate()
  button.toggleButton("T1 Slate")
  if (redstone.getInput("front") == false) then
    redstone.setAnalogOutput("front", 2)
  else
    redstone.setOutput("front", false)
  end
end
function t2slate()
  button.toggleButton("T2 Slate")
  if (redstone.getInput("front") == false) then
    redstone.setAnalogOutput("front", 4)
  else
    redstone.setOutput("front", false)
  end
end
function t3slate()
  button.toggleButton("T3 Slate")
  if (redstone.getInput("front") == false) then
    redstone.setAnalogOutput("front", 6)
  else
    redstone.setOutput("front", false)
  end
end
function t4slate()
  button.toggleButton("T4 Slate")
  if (redstone.getInput("front") == false) then
    redstone.setAnalogOutput("front", 8)
  else
    redstone.setOutput("front", false)
  end
end
function t5slate()
  button.toggleButton("T5 Slate")
  if (redstone.getInput("front") == false) then
    redstone.setAnalogOutput("front", 10)
  else
    redstone.setOutput("front", false)
  end
end
button.heading("Craft:")
fillTable()
while true do
  getClick()
end

So lets say i have pressed "T1 Slate" button and it lights green, if i then press the button "T3 Slate" i want to be able to turn off "T1 Slate" 's redstone and then turn off the green color on the screen and thereafter turn on so "T3 Slate" turns green on the screen and the redstone strength for T3 goes on.

And of course i want to be able to do this with everyone. So if T3 is on and i wanna make T1 then T3's redstone goes off and goes red on the screen and then T1 goes green and output the redstone strength.

So can anyone help me with that? :)/>
HPWebcamAble #2
Posted 28 January 2015 - 02:42 AM
The problem with DW20's Button API is that it requires you to specify a function for each button
(EDIT: you could MAYBE do it with objects but idk how to use objects :(/> )

Mine doesn't (http://pastebin.com/RiZLCvRG)
If you use mine, then you would:
1. Create buttons
2. Wait for input
3. Check if input is on a button
4. Use the name to toggle the button and set the strength

You could define the button's strength's in a table like this:

bStrenght = {
  ["T1 Slate"] = 2,
  ["T2 Slate"] = 4
  --# and so on
}

Then wait for input:
(EDIT: Fixed slight issue)

while true do
  event,p1,p2,p3,p4 = os.pullEvent("monitor_touch")
  local tButton = checkCords(p2,p3)
  if tButton then
	button.toggleButton(tButton)
    redstone.setAnalogOutput("front", bStrength[tButton])
  end
end

Good luck!
Edited on 28 January 2015 - 03:08 AM
Bomb Bloke #3
Posted 28 January 2015 - 03:00 AM
In regards to which button should be which colour, you could organise them by using a tracking variable that updates whenever a button is pushed. Eg, this sort of thing:

function t4slate()
  button.toggleButton("T4 Slate")
  if lastButton then button.toggleButton(lastButton) end
  lastButton = "T4 Slate"
  etc
Pjevser #4
Posted 28 January 2015 - 03:33 AM
The problem with DW20's Button API is that it requires you to specify a function for each button
(EDIT: you could MAYBE do it with objects but idk how to use objects :(/> )

Mine doesn't (http://pastebin.com/RiZLCvRG)
If you use mine, then you would:
1. Create buttons
2. Wait for input
3. Check if input is on a button
4. Use the name to toggle the button and set the strength

You could define the button's strength's in a table like this:

bStrenght = {
  ["T1 Slate"] = 2,
  ["T2 Slate"] = 4
  --# and so on
}

Then wait for input:

while true do
  event,p1,p2,p3,p4 = os.pullEvent("monitor_touch")
  local tButton = checkCords(p2,p3)
  if tButton then
	button.toggleButton(tButton)
	if (redstone.getInput("front") == false) then
	  redstone.setAnalogOutput("front", bStrength[tButton])
	else
	  redstone.setOutput("front", false)
	end
  end
end

Good luck!

Would i have to do anything else besides adding them all into bStrenght and of course adding adding the buttons?

As i said in my post, i'm not that good at computercraft, so if i need some more coding than this. Then i'm probably not gonna get it to work since i have no clue on how :D/>

In regards to which button should be which colour, you could organise them by using a tracking variable that updates whenever a button is pushed. Eg, this sort of thing:

function t4slate()
  button.toggleButton("T4 Slate")
  if lastButton then button.toggleButton(lastButton) end
  lastButton = "T4 Slate"
  etc

Thanks :)/>

I manage to do this (i sucks at coding and have no idea what i'm doing almost):

function t1slate()
  if lastButton then button.toggleButton(lastButton) redstone.setOutput("front", false) end
  button.toggleButton("T1 Slate")
redstone.setAnalogOutput("front", 2)
lastButton = "T1 Slate"
end

and of course same code for all the functions.

It turns off the light for the other button when i press a new. The only problem i have now is i can't turn off the green light and the redstone signal if i press the same button again.

So if i can't find a fix to that, well then i could maybe add another button that could turn everything off (if i knew how of course :P/>).

EDIT: So i added a new button called turnoff, and just did:

function turnoff()
button.flash("Turn Off")
os.reboot()
end

So yeah a reboot helps also, so i guess i could use that for now :D/>
Edited on 28 January 2015 - 03:26 AM
HPWebcamAble #5
Posted 28 January 2015 - 04:21 AM
As i said in my post, i'm not that good at computercraft, so if i need some more coding than this. Then i'm probably not gonna get it to work since i have no clue on how :D/>

Haha its ok, you'll get better over time.

Bomb made a good point, so you could combine our code:

while true do
  event,p1,p2,p3,p4 = os.pullEvent("monitor_touch") --#get input
  local tButton = checkCords(p2,p3)
  if tButton then --#if a button was clicked
	local cState = button.toggleButton(tButton) --#toggle the clicked button
	if cState then --#if it was turned on
	  if lastButton then button.setState(lastButton,false) end --#turn off the last button
	  redstone.setAnalogOutput("front", bStrength[tButton])
	  lastButton = tButton
	else --#if it was turned off
	  redstone.setOutput("front", false)
	end
  end
end
I spaced this right, the forum just doesn't like me I guess

This should let you turn off the active button too :)/>

Feel free to post any more questions you have
Edited on 28 January 2015 - 03:24 AM