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

Bundled Cable Toggle Script

Started by NukeInMyPantz, 10 September 2016 - 04:53 AM
NukeInMyPantz #1
Posted 10 September 2016 - 06:53 AM
Hey everybody I have been banging my head against my keyboard and so far nothan is working as planned. So what I have been trying to do is simply make a script that toggles a Bundled cable that connects to about 9 different colors that connect to Wireless Transmitters that connect to Lamps. My problem is I can't find a script that works even when I make/modify my own or others scripts.

I know there are different ways other then using a computer to toggle the lights but I like to Program and to have everything setup this way.
If anyone could help me by making a toggle script that can work with either one or more colors at a time that would be awesome.


Also what I'm trying to do is control this buildings lights that is about 200m away from my home and this would be on the Server computer, I would be accessing this program via Wireless Modem from my Client.
If you have any questions let me know. I'm tired and i'm going to bed :P/>/>

Spoiler

local function menu(...)
term.clear()
term.setCursorPos(1,1)
printC("Server Building Light System Interface")
term.setCursorPos(1,19)
printC("Use the Arrow Keys to Navigate, Enter to Accept.")
term.setCursorPos(10,4)
		local sel = 1
		local list = {...}
		local offX,offY = term.getCursorPos()
		local curX,curY = term.getCursorPos()
		while true do
				if sel > #list then sel = 1 end
				if sel < 1 then sel = #list end
				for i = 1,#list do
						term.setCursorPos(offX,offY+i-1)
						if sel == i then
								print("["..list[i].."]") --[[ The characters between the quotation marks are the characters used to show which option is active ]]--
						else
								print(" "..list[i].." ")
						end
				end
				while true do
						local e,e1,e2,e3,e4,e5 = os.pullEvent() --[[ Pulls the event for when a key is pressed ]]--
						if e == "key" then
								if e1 == 200 then --[[ This detects when the up arrow is pressed, it will then move one up. If you click it when you have the first option active, it will return to the bottom ]]--
										sel = sel-1
												break
								end
								if e1 == 208 then --[[ This detects when the down arrow is pressed, it will then move one down. If you click it when you have the last option active, it will return to the top ]]--
										sel = sel+1
												break
								end
								if e1 == 28 then  --[[ This is when the enter key is pressed. It returns which ever option in the selection is active ]]--
										term.setCursorPos(curX,curY)
										return list[sel],sel
								end
						end
				end
		end
end
function Toggle_All()   --[[ Option1, Option2, Option3 are the functions for each option in the menu. You don't have to do this, I just do it because it looks cleaner. ]]--
-- I was trying something like this
rs.setBundledOutput("bottom", colors.pink)
  sleep(1)
end
function Toggle_Pink()
  sleep(3)
end
function Toggle_Lime()
end
function Toggle_Orange()
  term.setCursorPos(1,6)
  print("You have selected option 2!")
  sleep(3)
end
function Toggle_Brown()
  term.setCursorPos(1,6)
  print("You have selected option 3!")
  sleep(3)
end

function Toggle_Red()
  term.setCursorPos(1,6)
  print("You have selected option 2!")
  sleep(3)
end
function Toggle_Blue()
  term.setCursorPos(1,6)
  print("You have selected option 3!")
  sleep(3)
end
function Toggle_Purple()
  term.setCursorPos(1,6)
  print("You have selected option 2!")
  sleep(3)
end
function Toggle_Grey()
  term.setCursorPos(1,6)
  print("You have selected option 3!")
  sleep(3)
end
function Toggle_Black()
  term.setCursorPos(1,6)
  print("You have selected option 2!")
  sleep(3)
end
function Toggle_Cyan()
  term.setCursorPos(1,6)
  print("You have selected option 3!")
  sleep(3)
end
while true do
		local selection = menu( --[[ This is what is displayed on the screen. Separate each with a comma ]]--
		"Toggle All",
		"Toggle Pink",
		"Toggle Lime",
  "Toggle Orange",
  "Toggle Brown",
  "Toggle Red",
  "Toggle Blue",
  "Toggle Purple",
  "Toggle Grey",
  "Toggle Black",
  "Toggle Cyan")
		if selection == "Toggle All" then		  --[[ If the selected option is this one after clicking enter it would call Option1 function. and so on... ]]--
		 Toggle_All()
		elseif selection == "Toggle Pink" then
		 Toggle_Pink()
		elseif selection == "Toggle Lime" then
		 Toggle_Lime()
  elseif selection == "Toggle Orange" then
		 Toggle_Orange()
		elseif selection == "Toggle Brown" then
		 Toggle_Brown()
  elseif selection == "Toggle Red" then
		 Toggle_Red()
		elseif selection == "Toggle Blue" then
		 Toggle_Blue()
  elseif selection == "Toggle Purple" then
		 Toggle_Purple()
		elseif selection == "Toggle Grey" then
		 Toggle_Grey()
  elseif selection == "Toggle Black" then
		 Toggle_Black()
		elseif selection == "Toggle Cyan" then
		 Toggle_Cyan()
		end
end
Edited on 11 September 2016 - 03:50 AM
CCJJSax #2
Posted 10 September 2016 - 08:22 AM
You really should use the color API.


function toggleColor(side, col)
 if colors.test(rs.getBundledInput(side), col) == true then
  rs.setBundledOutput(side, colors.subtract(col, rs.getBundledInput(side)))
 else
  rs.setBundledOutput(side, colors.combine(col, rs.getBundledInput(side)))
 end
end

toggleColor("back", colors.orange)


and to make it do more than one color at a time..


toggleColor("back", colors.white)
toggleColor("back", colors.orange)

on top of that you could make the function loop through as many colors as you pass it.
Edited on 10 September 2016 - 06:38 AM
Bomb Bloke #3
Posted 11 September 2016 - 06:05 AM
You've also got some references to an undefined "printC" function in there. You'll probably want to replace those with regular "print" calls.
NukeInMyPantz #4
Posted 11 September 2016 - 07:43 AM
Ok CCJJSax you are awesome that worked. I have one more question. The Computer I used to control the lights I have another computer that is connected to a monitor. How can I make it send out a signal to check and see if one or more color bundled cables are active or not, then with that information it will simply change a text from "Active" to "Not Active". I looked up and tried to use and mess around with others scripts but for some reason I have bad luck with this stuff working for me. Just to give you a better insight on what I'm doing.

Also if anyone else wanted a good script menu like this here you go :P/>

-- Interactive Menu for Enabling / Disabling Lamps insdie the Server Building.
-- Computer ID #10

-- Functions

local function printC(text)
local x,y = term.getSize()
local x2,y2 = term.getCursorPos()
term.setCursorPos(math.ceil((x / 2) - (text:len() / 2)), y2)
write(text)
end
function toggleColor(side, col)
if colors.test(rs.getBundledInput(side), col) == true then
  rs.setBundledOutput(side, colors.subtract(col, rs.getBundledInput(side)))
else
  rs.setBundledOutput(side, colors.combine(col, rs.getBundledInput(side)))
end
end

local function menu(...)
c = rs.getBundledOutput("bottom")
term.clear()
term.setCursorPos(1,1)
printC("Server Building Light System Interface")
term.setCursorPos(1,19)
printC("Use the Arrow Keys to Navigate, Enter to Accept.")
term.setCursorPos(15,4)
	    local sel = 1
	    local list = {...}
	    local offX,offY = term.getCursorPos()
	    local curX,curY = term.getCursorPos()
	    while true do
			    if sel > #list then sel = 1 end
			    if sel < 1 then sel = #list end
			    for i = 1,#list do
					    term.setCursorPos(offX,offY+i-1)
					    if sel == i then
							    print("["..list[i].."]") --[[ The characters between the quotation marks are the characters used to show which option is active ]]--
					    else
							    print(" "..list[i].." ")
					    end
			    end
			    while true do
					    local e,e1,e2,e3,e4,e5 = os.pullEvent() --[[ Pulls the event for when a key is pressed ]]--
					    if e == "key" then
							    if e1 == 200 then --[[ This detects when the up arrow is pressed, it will then move one up. If you click it when you have the first option active, it will return to the bottom ]]--
									    sel = sel-1
											    break
							    end
							    if e1 == 208 then --[[ This detects when the down arrow is pressed, it will then move one down. If you click it when you have the last option active, it will return to the top ]]--
									    sel = sel+1
											    break
							    end
							    if e1 == 28 then  --[[ This is when the enter key is pressed. It returns which ever option in the selection is active ]]--
									    term.setCursorPos(curX,curY)
									    return list[sel],sel
							    end
					    end
			    end
	    end
end
function Toggle_All_ON()   --[[ Option1, Option2, Option3 are the functions for each option in the menu. You don't have to do this, I just do it because it looks cleaner. ]]--
toggleColor("bottom", colors.pink+colors.lime+colors.orange+colors.brown+
colors.red+colors.blue+colors.purple+colors.gray+colors.black+colors.cyan)
end
function Toggle_Pink()
toggleColor("bottom", colors.pink)
end
function Toggle_Lime()
toggleColor("bottom", colors.lime)
end
function Toggle_Orange()
toggleColor("bottom", colors.orange)
end
function Toggle_Brown()
toggleColor("bottom", colors.brown)
end

function Toggle_Red()
toggleColor("bottom", colors.red)
end
function Toggle_Blue()
toggleColor("bottom", colors.blue)
end
function Toggle_Purple()
toggleColor("bottom", colors.purple)
end
function Toggle_Gray()
toggleColor("bottom", colors.gray)
end
function Toggle_Black()
toggleColor("bottom", colors.black)
end
function Toggle_Cyan()
toggleColor("bottom", colors.cyan)
end
function Toggle_All_OFF()
rs.setBundledOutput("bottom", 0)
end
while true do
	    local selection = menu( --[[ This is what is displayed on the screen. Separate each with a comma ]]--
	    "Toggle All ON",
	    "Toggle Pink",
	    "Toggle Lime",
  "Toggle Orange",
  "Toggle Brown",
  "Toggle Red",
  "Toggle Blue",
  "Toggle Purple",
  "Toggle Gray",
  "Toggle Black",
  "Toggle Cyan",
  "Toggle All OFF")
	    if selection == "Toggle All ON" then		  --[[ If the selected option is this one after clicking enter it would call Option1 function. and so on... ]]--
		 Toggle_All_ON()
	    elseif selection == "Toggle Pink" then
		 Toggle_Pink()
	    elseif selection == "Toggle Lime" then
		 Toggle_Lime()
  elseif selection == "Toggle Orange" then
		 Toggle_Orange()
	    elseif selection == "Toggle Brown" then
		 Toggle_Brown()
  elseif selection == "Toggle Red" then
		 Toggle_Red()
	    elseif selection == "Toggle Blue" then
		 Toggle_Blue()
  elseif selection == "Toggle Purple" then
		 Toggle_Purple()
	    elseif selection == "Toggle Gray" then
		 Toggle_Gray()
  elseif selection == "Toggle Black" then
		 Toggle_Black()
	    elseif selection == "Toggle Cyan" then
		 Toggle_Cyan() 
  elseif selection == "Toggle All OFF" then
   Toggle_All_OFF()
	    end
end
CCJJSax #5
Posted 11 September 2016 - 08:39 AM
I'm not exactly sure what you are asking. You mean that if there is any bundle color on it will print "active"? if so see below.



function checkActive()
for i = 0, 15 do
  if colors.test( rs.getBundledInput("back"), 2^i) then
   print("active")
   return true
  end
end
print("inactive")
end


edit: also why make all the color functions when you could just use toggleColor(side, col) in the if statement itself?
Edited on 11 September 2016 - 07:01 AM
NukeInMyPantz #6
Posted 12 September 2016 - 12:06 AM
CCJJSax

I am still learning how to program so if you thought I was doing something wrong please tell me so I know.
For the bundled cable check I simply wanted to display the information onto the Monitor for quick info rather then going into the computer and checking

edit: also why make all the color functions when you could just use toggleColor(side, col) in the if statement itself?
Because I was just happy getting it to work rather then making it look pretty :P/>
I still have alot of other things to do such as Hooking up a Master computer in my massive tree house that connects to just about everything I am making. Just for the fun of it.
CCJJSax #7
Posted 12 September 2016 - 08:36 AM
The cool thing about programming is that there is no right or wrong way of doing something as long as it works, just more efficient ways of doing it. You could put term.redirect(peripheral.wrap(side)) below if e == "key" and it will display it on the monitor. If you wanted to use the monitor to select it you can use Lyqyd's Touchpoint API. Or you could use the computer for selecting and have the monitor display just quick info.

As for anything I see wrong with the script, it seems fine so far. No errors or anything with the menu function or the print function. If you meant to center the options, then it's off center. but that is super minor. You seem to be doing well overall.

edit: and I'm just waiting for lyqyd or bomb bloke or a real professional to get in here and show me up here, I'm rather new too. no judgement here.
Edited on 12 September 2016 - 06:44 AM
NukeInMyPantz #8
Posted 13 September 2016 - 08:51 AM
Next thing I'm trying to figure out is, I want to make a simple To Do List but I want to be able to add / delete / edit the list without going into the code to do it.
The list will be projected onto the Monitor but I want the Options to be shown only in the Computer for the user.

I'm guessing I would have to save the data to a file but I have not used that at all. Anyone have any ideas on how I can accomplish this ?
CCJJSax #9
Posted 13 September 2016 - 09:52 AM
These came from I think bomb bloke awhile back on one of my questions when I was learning.



function readLines(sPath)
local fHandle = fs.open(sPath, 'r')
if fHandle then
  local tLines = {}
  for sLine in fHandle.readLine do
   tLines[#tLines+1] = sLine
  end
  fHandle.close()
  return tLines
end
end

function writeLines(sPath, tLines)
local fHandle = fs.open(sPath, 'w')  
if fHandle then
  fHandle.writeLine(table.concat(tLines, '\n')) -- the last line comes blank.  might be fine
  fHandle.close()
end
end


edit: and I say 'when I was learning' as if I'm not still learning. but that's beside the point.
Edited on 13 September 2016 - 07:53 AM