Guest User

Untitled

a guest
Jun 24th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.04 KB | None | 0 0
  1. --By Arongy
  2. --L_state_# = the state ON/OFF of the light by default.
  3. --L_id_# = the ID of the light, can be anything you want, but you will have to call the exact same ID when the program ask!
  4. --L_color_# = the bundled colored wire attach to this light.
  5.  
  6. -- CONFIG
  7. local Debug = true --Turn this to false if your done setting up your lightning system.
  8. local L_state_1 = "false"   local L_id_1 = "outside"        local L_color_1 = "blue"    local L_side_1 = "bottom"
  9. local L_state_2 = "false"   local L_id_2 = "inside"         local L_color_2 = "yellow"  local L_side_2 = "bottom"
  10. local L_state_3 = "false"   local L_id_3 = "somewhere1"     local L_color_3 = "red" local L_side_3 = "bottom"
  11. local L_state_4 = "false"   local L_id_4 = "somewhere2"     local L_color_4 = "lime"    local L_side_4 = "bottom"
  12. -- END CONFIG
  13.  
  14. function WriteFile() --Write into the txt file the current states.
  15.     file = io.open("lightstates.txt", "w")
  16.     file:write(L_state_1.."\n"..L_state_2.."\n"..L_state_3.."\n"..L_state_4)
  17.     file:close()
  18. end
  19.  
  20. function ReadFile() --Read into the txt file the current states.
  21.     file = io.open("lightstates.txt", "r")
  22.     L_state_1 = file:read()
  23.     L_state_2 = file:read()
  24.     L_state_3 = file:read()
  25.     L_state_4 = file:read()
  26.     file:close()
  27. end
  28.  
  29. function SwitchStates(light, state) --Verify input and switch the state of a single light.
  30.     if state == "true" or state == "false" then
  31.         if light == L_id_1 then
  32.             L_state_1 = state
  33.         elseif light == L_id_2 then
  34.             L_state_2 = state
  35.         elseif light == L_id_3 then
  36.             L_state_3 = state
  37.         elseif light == L_id_4 then
  38.             L_state_4 = state
  39.         else
  40.             print("This is not a valid ID.") --If the entered ID doesnt exist!
  41.         end
  42.         WriteFile() -- write the states then
  43.         TogglePower() -- toggle the light
  44.     else
  45.         print("This is not a valid state!") --If states is else than 'true' or 'false'
  46.     end
  47. end
  48.  
  49. function SwitchAll(state) --Verify input and switch the state of all light.
  50.     if state == "true" or state == "false" then
  51.         L_state_1 = state
  52.         L_state_2 = state
  53.         L_state_3 = state
  54.         L_state_4 = state
  55.         WriteFile() --write the states then
  56.         TogglePower() --toggle all light
  57.     else
  58.         print("This is not a valid state!") --If states is else than 'true' or 'false'
  59.     end
  60. end
  61.  
  62. function TogglePower() --Will toggle the wire on/off
  63.     ReadFile() --Read into the txt file to get the right states
  64.     shell.run("redset", L_side_1, L_color_1, L_state_1)
  65.     shell.run("redset", L_side_2, L_color_2, L_state_2)
  66.     shell.run("redset", L_side_3, L_color_3, L_state_3)
  67.     shell.run("redset", L_side_4, L_color_4, L_state_4)
  68. end
  69.  
  70. function ShowLightState() --GUI, show the light states
  71.     ReadFile() --Read into the txt file to get the right states
  72.     term.setCursorPos(1,2)
  73.     if Debug == false then
  74.         print("---ID-------STATE-------------") --if debug disabled
  75.         print(" "..L_id_1.." = "..L_state_1)
  76.         print(" "..L_id_2.." = "..L_state_2)
  77.         print(" "..L_id_3.." = "..L_state_3)
  78.         print(" "..L_id_4.." = "..L_state_4)
  79.     elseif Debug == true then
  80.         print("---ID-------STATE---DEBUG-----") --if debug enabled will show the color and side status.
  81.         print(" "..L_id_1.." = "..L_state_1.." ["..L_color_1..", "..L_side_1.."]")
  82.         print(" "..L_id_2.." = "..L_state_2.." ["..L_color_2..", "..L_side_2.."]")
  83.         print(" "..L_id_3.." = "..L_state_3.." ["..L_color_3..", "..L_side_3.."]")
  84.         print(" "..L_id_4.." = "..L_state_4.." ["..L_color_4..", "..L_side_4.."]")
  85.     end
  86.     print("------------------------------")
  87. end
  88.  
  89. function clearText() --Clear the screen leaving the title so its not re-writing again.
  90.     term.setCursorPos(1,2)
  91.     cnt = 0
  92.     while cnt < 15 do --Erase the screen from line 2, keep the title written.
  93.         print("                                                    ")
  94.         cnt = cnt + 1
  95.     end
  96.     ShowLightState() --Show the light states again with new values.
  97.     term.setCursorPos(1,8)
  98. end
  99.  
  100. function header() --Header is called once at computer startup.
  101.     TogglePower() --Toggle the power by default at computer startup
  102.     term.clear()
  103.     ShowLightState()
  104.     term.setCursorPos(1,1)
  105.     textutils.slowPrint("LIGHTNING CONTROL PANEL") --Title, showed once.
  106.     term.setCursorPos(1,2)
  107. end
  108.  
  109. function MainProgram() --The main program where you enter input to select what you wanna do.
  110.     textutils.slowPrint("1-Single   2-All")
  111.     option = read() --Option is to choose if you toggle one or all light at once.
  112.     if option == "1" then --Single
  113.         clearText()
  114.         textutils.slowPrint("Enter an ID?") --wich Light you want to toggle.
  115.         light = read()
  116.         textutils.slowPrint("Enter a state? (true / false)") --states is True for ON false for OFF
  117.         state = read()
  118.         SwitchStates(light, state) -- Switch the states...
  119.         print("Setting "..light.." to "..state)
  120.     elseif option == "2" then --All
  121.         clearText()
  122.         textutils.slowPrint("Enter a state? (true / false)") --Now you simply choose the states you want all light to be.
  123.         state = read()
  124.         SwitchAll(state) --Switch all light to the written state!
  125.         print("Setting all to "..state)
  126.     end
  127.     sleep(2)
  128. end
  129.  
  130. --The Program itself calling function.
  131. header()
  132. while true do
  133. clearText()
  134. term.setCursorPos(1,8)
  135. MainProgram() --Call Main program function so it can start!
  136. end
Add Comment
Please, Sign In to add comment