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

Custom Switch By Arongy

Started by arongy, 10 February 2012 - 01:32 AM
arongy #1
Posted 10 February 2012 - 02:32 AM
Custom Switch V3
By Arongy

-You can set the WireType to 'redstone' or 'bundled'.
-Set wich color you want to activate.
-Choose to show or not the switch status. (Exemple: Is currently On)
-You can choose wich key need to be pressed.
-TerminalMode will change how the CTRL+T act. (Disable it, make it reboot or leave it default)
-You can change any text in the programs from the TEXT area.
-Set the title as you want or leave it empty to do not show the title.
-Debug Mode for wiring & color information.
-Clean script and good explanation.

Feel free to modify and/or distribute!

CODE:
SpoilerThe code to add to the computer folder in a file called startup:

--Custom Switch V3
--Coded by Arongy
--I made this script for those who want an easy way to make a Customized Switch.
--This will lock or not the computer to become a switch for light, door or anything you want to imagine.
--You can modify any part of the code as you need to.

--CONFIGS
local Title = "CustomSwitch v3" --Enter the title you want to show when the program start.
local Status = false --Set to 'true' if you want the redpower to be ON by default.
local Side = "back" --Set the side of the computer that will send power. [front-back-top-bottom-left-right]
local Key = " " --Enter the character Key to be pressed to switch ON/OFF. [leave to " " if you want spacebar]
local WireType = "bundled" --Set to 'redstone' or 'bundled' depending of what type of wiring your using.
local RedColor = "lime" --If using 'bundled' type, this will be the color to send the power.
local ShowStatus = true --Set to 'false' if you do not want to show the 'Text_Status' message.
local TerminalMode = 0 --Set to '0' for disabling CTRL+T, '1' for reboot on CTRL+T, '2' for Terminate normally.
local DebugMode = false --Set to true for activating the debug mode.
--TEXT
local Text_Status = "Light is currently %s" --Set your own Status message. Use '%s' to show the 'Text_On'/'Text_Off' word.
local Text_Toggle = "Press Spacebar to turn %s!" --Set your own Turn On message. Use '%s' to show the 'Text_On'/'Text_Off' word.
local Text_On = "On" --The word 'On' in your desired language.
local Text_Off = "Off" --The word 'Off' in your desired language.

--PROGRAM FUNCTIONS
--Function TerminalMode
function os.pullEvent()
	local event, p1, p2, p3, p4, p5 = os.pullEventRaw()
	if event == "terminate" then
		if TerminalMode == 1 then
			os.reboot()
		elseif TerminalMode == 2 then
			error( "Terminated" )
		end
	end
	return event, p1, p2, p3, p4, p5
end
--Function SwitchStatus
function SwitchStatus(s)
if s == true then
	if WireType == "redstone" then
		rs.setOutput(Side, true)
	elseif WireType == "bundled" then
		shell.run ("redset", Side, RedColor, "true")
	end
	Status = true
else
	if WireType == "redstone" then
		rs.setOutput(Side, false)
	elseif WireType == "bundled" then
		shell.run ("redset", Side, RedColor, "false")
	end
	Status = false
end
end
--Function Display
function Display()
	if Title > "" then
		print(Title)
	end
	
	if DebugMode == true then
		print("===DEBUG===")
		print("WireType: "..WireType)
		if WireType == "bundled" then
			print("RedColor: "..RedColor)
		end
		print("TerminalMode: "..TerminalMode)
		print("===========")
	end
	
	local Text_State, Text_rState = Text_Off, Text_On
	if Status == true then
		Text_State = Text_On
		Text_rState = Text_Off
	end

	if Status == true then
		if ShowStatus == true then
			print(string.format(Text_Status, Text_State))
		end
	print(string.format(Text_Toggle, Text_rState))
	else
		if ShowStatus == true then
			print(string.format(Text_Status, Text_State))
		end
	print(string.format(Text_Toggle, Text_rState))
	end
end

--MAIN PROGRAM
SwitchStatus(Status)--This will set the default switch status on program startup.
while true do

	term.clear()--Clear the terminal.
	term.setCursorPos(1,1)--Set cursor position.

	Display()

	event, param1, param2 = os.pullEvent()
	if event == "char" and param1 == Key then
		if Status == true then
			SwitchStatus(false)
		else
			SwitchStatus(true)
		end
	end
end

PICTURES:
Spoiler



Advert #2
Posted 10 February 2012 - 09:24 AM
Pretty cool!
I have some suggestions to your code:
1) Use local variables:

--Configs
local title = "CustomSwitch V2" --Title of program, set it to "" for no title.
local status = 0 --0 for On by default, 1 for Off by default
...
2) You can use booleans: (true/false, you dont need them in a string)

showstatustext = true -- set to false to do not show the status of light.

3) The description you are looking for is: (though, it's not easy to describe)

local ontext = "On" --you know
local offtext = "Off" --The world 'Off' in your language.

4) Use if then else:

function os.pullEvent()
local event, p1, p2, p3, p4, p5 = os.pullEventRaw()
if event == "terminate" then
  --if TerminalMode == 0 then -- We don't need to check for this, as we are doing nothing! (' for cleaness)
  --end
  if TerminalMode == 1 then
   os.reboot()
  elseif TerminalMode == 2 then -- This has no effect on the code, really, since we reboot if above is true, but it's easier to see what's happening.
   error( "Terminated" )
  end
end
return event, p1, p2, p3, p4, p5
end

5) You could also take a look at string.format, but it may be a bit confusing:

local statustext = "Light is currently %s" --Can be something else than a light, you choose the use!
local toggletext = "Press Spacebar to turn %s!" --You can customise aswell!

Now, you might ask what %s is.
You can use string.format like so, to change %s to represent on/off:

if showstatustext then
local statetext, rstatetext = offtext, ontext
if status == 1 then
  statetext = ontext
  rstatetext = offtext -- this is reversed, for "space to turn on/off"
end
print(string.format(statustext, statetext))
print(string.format(toggletext, rstatetext))
end

6) I have a few more suggestions, but I dont' have time to put them in code right now:
Check that all the configuration settings are acceptable! User-error is very common.

Try to remove the display code from the loop, and make a function for it, and call that function in the loop instead.
This'll just make it easier to read.
You could do the same for the event code, but, this isn't as important as the above, in my opinion, but it will make your code more portable.

Loop would look something like:

while true do
local breakLoop = false
updateScreen()
breakLoop = breakLoop or checkEvents() -- Return true in checkEvents function to break the loop.
-- the "or" statement means that, if breakLoop is true, it will not set it to false if checkEvents returns that.
-- Basically, so you can have multiple functions that can stop the loop without them interfering -- I can elaborate on this if you want.
if breakLoop then
  break
end
end
arongy #3
Posted 10 February 2012 - 01:30 PM
I am going to a Rendez-Vous and i'll take a bigger look at this, thanks.
arongy #4
Posted 10 February 2012 - 07:09 PM
Pretty cool!
I have some suggestions to your code:
1) Use local variables:

--Configs
local title = "CustomSwitch V2" --Title of program, set it to "" for no title.
local status = 0 --0 for On by default, 1 for Off by default
...
2) You can use booleans: (true/false, you dont need them in a string)

showstatustext = true -- set to false to do not show the status of light.

3) The description you are looking for is: (though, it's not easy to describe)

local ontext = "On" --you know
local offtext = "Off" --The world 'Off' in your language.

4) Use if then else:

function os.pullEvent()
local event, p1, p2, p3, p4, p5 = os.pullEventRaw()
if event == "terminate" then
  --if TerminalMode == 0 then -- We don't need to check for this, as we are doing nothing! (' for cleaness)
  --end
  if TerminalMode == 1 then
   os.reboot()
  elseif TerminalMode == 2 then -- This has no effect on the code, really, since we reboot if above is true, but it's easier to see what's happening.
   error( "Terminated" )
  end
end
return event, p1, p2, p3, p4, p5
end

5) You could also take a look at string.format, but it may be a bit confusing:

local statustext = "Light is currently %s" --Can be something else than a light, you choose the use!
local toggletext = "Press Spacebar to turn %s!" --You can customise aswell!

Now, you might ask what %s is.
You can use string.format like so, to change %s to represent on/off:

if showstatustext then
local statetext, rstatetext = offtext, ontext
if status == 1 then
  statetext = ontext
  rstatetext = offtext -- this is reversed, for "space to turn on/off"
end
print(string.format(statustext, statetext))
print(string.format(toggletext, rstatetext))
end

6) I have a few more suggestions, but I dont' have time to put them in code right now:
Check that all the configuration settings are acceptable! User-error is very common.

Try to remove the display code from the loop, and make a function for it, and call that function in the loop instead.
This'll just make it easier to read.
You could do the same for the event code, but, this isn't as important as the above, in my opinion, but it will make your code more portable.

Loop would look something like:

while true do
local breakLoop = false
updateScreen()
breakLoop = breakLoop or checkEvents() -- Return true in checkEvents function to break the loop.
-- the "or" statement means that, if breakLoop is true, it will not set it to false if checkEvents returns that.
-- Basically, so you can have multiple functions that can stop the loop without them interfering -- I can elaborate on this if you want.
if breakLoop then
  break
end
end

1) May I know what is the difference with a local variable?

2) I will.

3) I understood that and will make better descriptions :)/>/>

4) Thanks for the tips.

5) I like the idea.

6) I think you helped enought ;)/>/>
Advert #5
Posted 10 February 2012 - 07:46 PM
Pretty cool!
I have some suggestions to your code:
1) Use local variables:

--Configs
local title = "CustomSwitch V2" --Title of program, set it to "" for no title.
local status = 0 --0 for On by default, 1 for Off by default
...
2) You can use booleans: (true/false, you dont need them in a string)

showstatustext = true -- set to false to do not show the status of light.

3) The description you are looking for is: (though, it's not easy to describe)

local ontext = "On" --you know
local offtext = "Off" --The world 'Off' in your language.

4) Use if then else:

function os.pullEvent()
local event, p1, p2, p3, p4, p5 = os.pullEventRaw()
if event == "terminate" then
  --if TerminalMode == 0 then -- We don't need to check for this, as we are doing nothing! (' for cleaness)
  --end
  if TerminalMode == 1 then
   os.reboot()
  elseif TerminalMode == 2 then -- This has no effect on the code, really, since we reboot if above is true, but it's easier to see what's happening.
   error( "Terminated" )
  end
end
return event, p1, p2, p3, p4, p5
end

5) You could also take a look at string.format, but it may be a bit confusing:

local statustext = "Light is currently %s" --Can be something else than a light, you choose the use!
local toggletext = "Press Spacebar to turn %s!" --You can customise aswell!

Now, you might ask what %s is.
You can use string.format like so, to change %s to represent on/off:

if showstatustext then
local statetext, rstatetext = offtext, ontext
if status == 1 then
  statetext = ontext
  rstatetext = offtext -- this is reversed, for "space to turn on/off"
end
print(string.format(statustext, statetext))
print(string.format(toggletext, rstatetext))
end

6) I have a few more suggestions, but I dont' have time to put them in code right now:
Check that all the configuration settings are acceptable! User-error is very common.

Try to remove the display code from the loop, and make a function for it, and call that function in the loop instead.
This'll just make it easier to read.
You could do the same for the event code, but, this isn't as important as the above, in my opinion, but it will make your code more portable.

Loop would look something like:

while true do
local breakLoop = false
updateScreen()
breakLoop = breakLoop or checkEvents() -- Return true in checkEvents function to break the loop.
-- the "or" statement means that, if breakLoop is true, it will not set it to false if checkEvents returns that.
-- Basically, so you can have multiple functions that can stop the loop without them interfering -- I can elaborate on this if you want.
if breakLoop then
  break
end
end

1) May I know what is the difference with a local variable?

2) I will.

3) I understood that and will make better descriptions :)/>/>

4) Thanks for the tips.

5) I like the idea.

6) I think you helped enought ;)/>/>

The difference between Global and Local variables is that:
Global: Can be accessed anywhere, if you're working on a larger project, you may accidentally have two globals with the same name, and this will cause them to overwrite each other, meaning you can get buggy behavior or errors
Locals: are faster (slightly, not sure about LuaJ, but they are in C lua)
are locked to the scope you put them in:



a = 1
function getA()
print(a)
end
local a = 2
function getA2()
print(a)
end
function getA3()
local a = 3
print(a)
end
function getA4()
print(a)
end
getA()
getA2()
getA3()
getA4()

Try this, and see if you can figure out how it works :)/>/>

You're most welcome, feel free to ask if you have any more questions.
arongy #6
Posted 10 February 2012 - 08:46 PM
Updated to V3

Thank you for the help and tips. ;)/>/>
Advert #7
Posted 10 February 2012 - 08:57 PM
Well done, I can't critique your code right now though, too absorbed into coding ;)/>/>. I'll take another look after I'm done.
arongy #8
Posted 10 February 2012 - 09:35 PM
What are you coding if it is'nt too much personal ^_^/>/>

Im Curious :D/>/>
Advert #9
Posted 10 February 2012 - 09:48 PM
What are you coding if it is'nt too much personal ^_^/>/>

Im Curious :D/>/>

Just a locking mechanism, that I'm probably overcomplicating; Basically, what you have here, but with a password instead of spacebar.
http://pastebin.com/52s3FC8u <– my current progress, still have some biggies left, e.g display, etc. I'm getting too distracted by IRC :>
arongy #10
Posted 10 February 2012 - 09:58 PM
It's look good, but there's a lot that i don't understand well haha