Jump to content




Custom Switch By Arongy


  • You cannot reply to this topic
9 replies to this topic

#1 arongy

  • New Members
  • 37 posts
  • LocationQuebec, Canada

Posted 10 February 2012 - 02:32 AM

Custom Switch V3
By Arongy

Quote

-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:
Spoiler

PICTURES:
Spoiler


#2 Advert

    Custom Title

  • Moderators
  • 459 posts
  • LocationLondon

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


#3 arongy

  • New Members
  • 37 posts
  • LocationQuebec, Canada

Posted 10 February 2012 - 01:30 PM

I am going to a Rendez-Vous and i'll take a bigger look at this, thanks.

#4 arongy

  • New Members
  • 37 posts
  • LocationQuebec, Canada

Posted 10 February 2012 - 07:09 PM

View PostAdvert, on 10 February 2012 - 09:24 AM, said:

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 ;)/>

#5 Advert

    Custom Title

  • Moderators
  • 459 posts
  • LocationLondon

Posted 10 February 2012 - 07:46 PM

View Postarongy, on 10 February 2012 - 07:09 PM, said:

View PostAdvert, on 10 February 2012 - 09:24 AM, said:

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.

#6 arongy

  • New Members
  • 37 posts
  • LocationQuebec, Canada

Posted 10 February 2012 - 08:46 PM

Updated to V3

Thank you for the help and tips. ;)/>

#7 Advert

    Custom Title

  • Moderators
  • 459 posts
  • LocationLondon

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.

#8 arongy

  • New Members
  • 37 posts
  • LocationQuebec, Canada

Posted 10 February 2012 - 09:35 PM

What are you coding if it is'nt too much personal ^_^/>

Im Curious :D/>

#9 Advert

    Custom Title

  • Moderators
  • 459 posts
  • LocationLondon

Posted 10 February 2012 - 09:48 PM

View Postarongy, on 10 February 2012 - 09:35 PM, said:

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 :>

#10 arongy

  • New Members
  • 37 posts
  • LocationQuebec, Canada

Posted 10 February 2012 - 09:58 PM

It's look good, but there's a lot that i don't understand well haha





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users