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

Toggling state of bundled cable depending on monitor button state.

Started by den_baron, 03 August 2013 - 07:48 AM
den_baron #1
Posted 03 August 2013 - 09:48 AM
Title = Toggling state of bundled cable depending on monitor button state.

Hey i'm trying to make a touch screen with 5 buttons able to turn on and off my 5 nuclear reactors. I've pretty much just copied DW20's touch screen code (thank you very much DW20 helped a lot of people) but i've now ran into a problem with toggling between on and off for bundled cables.
For all the buttons i'm using the toggleButton "mode" if you can call it that. So when the button is green i of course also want the computer to send out a signal to the desired bundle cable colour. When the button is then red i also want the signal for the respective colour that matches with that button to be turned off.

function left breeder()
  button.toggleButton("left breeder")
  -- This is where i need a way of both activating but also deactivating beneath signal. Been looking a jumping around with some getInput and if else statements.
  rs.setBundledOutput("left", colours.white)
end

Yeah how far i've gotten so far……
So i'm able to turn white on but i have no clue how to turn it off again.

[Lua] demo - Pastebin.com
[Lua] button - Pastebin.com

^^ above is DW20's touch program which i have copied (again thanks DW20 and no i would never be able to make above programs so i take no credit at all :P/>)

Thanks for any kind of help
Bubba #2
Posted 03 August 2013 - 10:32 AM
Split into new topic.
Bubba #3
Posted 03 August 2013 - 01:43 PM
So do you want the cable to go on and off quickly? Or do you want it to stay on/stay off until the button is clicked again?

Regardless, what you need is a way to bind a function to a button. It looks like you've already hooked up the four test functions to each button, so all that's left is determining what state to set the cable to. This is pretty simple.

I would use a table to store the current state of each color.

local cableState = {0,0,0,0} --#You have four buttons, so each 0 corresponds to a button
After registering your buttons, change the code of each test function to something along these lines

local function commitState()
  rs.setBundledOutput( colors.combine(unpack(cableState)) )

  --[[# What's going on here is that unpack takes the table and puts it into argument form. For example,
  # if I had a table like {1,2,3,4}, it would take that table and unpack it into arguments like 1,2,3,4.
  # That's probably confusing, so check it out here: http://www.lua.org/pil/5.1.html

  # Now the colors.combine function will take any number of colors and combine them into one number so that you 
  # can set the bundled output properly
  #]]
end

local function test1()
  if cableState[1] ~= 0 then
    cableState[1] = 0
  else
    cableState[1] = colors.red
  end
  commitState()
end

So each function sets the cable state table to the desired output, but doesn't actually change the state of the cable. The commitState function does that.
den_baron #4
Posted 03 August 2013 - 02:56 PM
Yeah when i press my button i want the signal to be kept on. Not just pulse but keep going until i press the button again, ohh and i also got 5 buttons but putting another null in the first array shouldn't be to hard :P/> (I think i've learned it's called an array but i'm far from being an expert)
Ahh i see cableState stores the values of the active colours and then you pack it together to one number that will then when unpacked only turn on the colours that have already been active.
When it then comes to adding a new colour to the "pack" you look at the specific place each button function has been given in the array and if the button is pressed the if else statement adds the colour to the "pack" and if it's not pressed it will keep being a 0.

What i don't understand is how do you turn it off again ?? I don't understand how when first fx the red wire is turned how does it turn off again when i press the button on my touch screen ??
Oh and VERY VERY VERY …… VERY many thanks i'm gonna try it out now (yeah i've just taken a look at above and analyzed it looking at what i understood and what i didn't understand :P/> )