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

[LUA][Proofing][RP2] Toggle program

Started by Ridous, 02 October 2012 - 03:53 AM
Ridous #1
Posted 02 October 2012 - 05:53 AM
I'm working on a program that will toggle on and off a color from any specified side.

Right now I'm trying to work out kinks and tidying up as much as possible.
Only problem on my side is that I have no access to MC to test the code.

If there are any kinks can you write in notes so I can figure it out.(keep it simple, still not good at coding)

I'm also going to make it wait for signals in an indefinite loop to toggle all sides for the same color
or as a router to multiple cabling. (I'll get to that later :3)

Here's the code

Spoiler


local tArgs = {...} --reads the inputs and puts them in a table
local side = tArgs[1]
local color = tArgs[2]

local tColors = { --Table containing all the colors
['white'] = 1, --unknown if this is really needed
['orange'] = 2,
['magenta'] = 4,
['lightBlue'] = 8,
['yellow'] = 16,
['lime'] = 32,
['pink'] = 64,
['gray'] = 128,
['lightGray'] = 256,
['cyan'] = 512,
['purple'] = 1024,
['blue'] = 2048,
['brown'] = 4096,
['green'] = 8192,
['red'] = 16384,
['black'] = 32768}

local tSides = { --table containing all vaild sides
['top'] = top --unknown if this is really needed
['bottom'] = bottom
['left'] = left
['right'] = right
['front'] = front
['back'] = back
}

local function toggle(tSides or side, tColors or color)
--uses the input from the arguments or the input after the program
--runs.

if rs.testBundledOutput(side, color) == true then
	colors.subtract(side, color)
else
	colors.combine(side, color)
end
end

if not tArgs then --this runs if you just start the program
while true do
term.clear()
setCursor(1,1)
print('This is a redstone toggle system. Please enter a valid side')
print('and color. If a you only put a side, then it will activate')
print('either all the cable signals or the default redstone.')
print('NOTE~ all inputs must be in ALL LOWER CASE!!!')
print('Valid side please: ')
local side = read()
print('Valid color please:')
local color = read()

end
end
toogle()--runs the function normaly with either the table method or
		--the input method

KaoS #2
Posted 02 October 2012 - 07:45 AM
try this


local function toggle(side, color)
  rs.setBundledOutput(side,((colors.test(rs.getBundledOutput(side),color) and colors.subtract(rs.getBundledOutput(side),color)) or colors.combine(rs.getBundledOutput(side),color)))
end


local tSides={front=true;back=true;left=true;right=true;top=true;bottom=true}
while true do
  print([[This is a redstone toggle system. Please enter a valid side
  and color. If a you only put a side, then it will activate
  either all the cable signals or the default redstone.
  NOTE~ all inputs must be in ALL LOWER CASE!!!]])
  while true do
   write('color: ')
   local color=colors[read()]
   if not color then
    print('invalid choice')
   else
    break
   end
  end
  while true do
   write('side: ')
   local side=read()
   if not tSides[side] then
    print('invalid choice')
   else
    break
   end
  end
  toggle(side,color)
end
Ridous #3
Posted 03 January 2013 - 09:23 PM
after a long hiatus I got rusty and tried to make this alot simpler (lost most of my coding experience xD)
also, not even sure if I'm allowed to necro my own threads
Spoiler

-- Toggle

function tgUp(colors)
	if colors.test(colors) == true then
		rs.setBundledOutput("up",(colors.subtract(colors))
	else rs.setBundledOutput("up",(colors.combine(colors))
		end
		end
	end
end

function tgDown(colors)
	if colors.test(colors) == true then
		rs.setBundledOutput("bottom",(colors.subtract(colors))
	else rs.setBundledOutput("bottom",(colors.combine(colors))
		end
		end
	end
end

function tgBack(colors)
	if colors.test(colors) == true then
		rs.setBundledOutput("back",(colors.subtract(colors))
	else rs.setBundledOutput("back",(colors.combine(colors))
		end
		end
	end
end

function tgFront(colors)
	if colors.test(colors) == true then
		rs.setBundledOutput("front",(colors.subtract(colors))
	else rs.setBundledOutput("front",(colors.combine(colors))
		end
		end
	end
end

function tgLeft(colors)
	if colors.test(colors) == true then
		rs.setBundledOutput("left",(colors.subtract(colors))
	else rs.setBundledOutput("left",(colors.combine(colors))
		end
		end
	end
end

function tgRight(colors)
	if colors.test(colors) == true then
		rs.setBundledOutput("right",(colors.subtract(colors))
	else rs.setBundledOutput("right",(colors.combine(colors))
		end
		end
	end
end

function tgAll(side)
	if colors.test >= 1 then
		rs.setBundledoutPut(side, 0)
	else rs.setBundledoutPut(side, 65535)
	end
end

Kingdaro #4
Posted 04 January 2013 - 02:24 AM
Even smaller:

function toggle(colorset, side)
  local input = rs.getBundledInput(side)
  rs.setBundledOutput(side,
    colors.test(input, colorset) and
    colors.subtract(input, colorset) or
    colors.combine(input, colorset)
  )
end
The last couple of "colors." lines could be in one, but I have a long lines anti-fetish, haha.