RavenSH4

SensorDoorControl

Sep 26th, 2021 (edited)
805
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --SensorDoorControl
  2.  
  3. --initialize
  4. monitor = peripheral.wrap("back")
  5. outputSide = "bottom"
  6. rs.setOutput(outputSide, false)
  7. monitor.clear()
  8. monitor.setBackgroundColour(colors.black)
  9.  
  10. --VAR
  11. IsOpen = false
  12. Password = "1234"
  13. CurrPass = ""
  14. sTextColor = colors.black
  15. sTxtBgColor = colors.white
  16. RS_SkipCounter = 0
  17.  
  18.  
  19. --draw a PINpad
  20. function Draw()
  21.     monitor.clear()
  22.     strl = string.len(CurrPass)
  23.    
  24.     --draw a PINPAD header (or inputed text)
  25.     if (IsOpen) then
  26.         DrawText(1,1,"OPENED", colors.green, colors.black)
  27.     elseif (strl > 0) then
  28.         DrawText(1,1,">", colors.white, colors.black) --CurrPass
  29.         for i=2, strl+1, 1 do
  30.             DrawText(i,1,"*", colors.white, colors.black)
  31.         end
  32.     else
  33.         DrawText(1,1,"CLOSED", colors.red, colors.black)
  34.     end
  35.  
  36.     --Symbols
  37.     DrawText(3,2, "1", sTextColor, sTxtBgColor)
  38.     DrawText(4,2, "2", sTextColor, sTxtBgColor)
  39.     DrawText(5,2, "3", sTextColor, sTxtBgColor)
  40.     --
  41.     DrawText(3,3, "4", sTextColor, sTxtBgColor)
  42.     DrawText(4,3, "5", sTextColor, sTxtBgColor)
  43.     DrawText(5,3, "6", sTextColor, sTxtBgColor)
  44.     --
  45.     DrawText(3,4, "7", sTextColor, sTxtBgColor)
  46.     DrawText(4,4, "8", sTextColor, sTxtBgColor)
  47.     DrawText(5,4, "9", sTextColor, sTxtBgColor)
  48.     --colors.red
  49.     DrawText(3,5, "R", sTextColor, colors.red)
  50.     DrawText(4,5, "0", sTextColor, sTxtBgColor)
  51.     --colors.green
  52.     DrawText(5,5, "E", sTextColor, colors.green)
  53. end
  54.  
  55. --draw a symbol
  56. function DrawText(PosX, PosY, Text, TextColor, BgColor)
  57.     monitor.setCursorPos(PosX,PosY)
  58.     monitor.setBackgroundColor(BgColor) --colors.black
  59.     monitor.setTextColor(TextColor) --colors.white
  60.     monitor.write(Text)
  61.     monitor.setTextColor(colors.black)
  62.     monitor.setBackgroundColor(colors.black)
  63. end
  64.  
  65. --Event on screen touch
  66. function TouchEvent(PosX, PosY)
  67.     digit = -1
  68.     clear = false
  69.     apply = false
  70.    
  71.     --get touched symbol
  72.     if (PosX == 3 and PosY == 2) then
  73.         digit = 1
  74.     elseif (PosX == 4 and PosY == 2) then
  75.         digit = 2
  76.     elseif (PosX == 5 and PosY == 2) then
  77.         digit = 3
  78.     elseif (PosX == 3 and PosY == 3) then
  79.         digit = 4
  80.     elseif (PosX == 4 and PosY == 3) then
  81.         digit = 5
  82.     elseif (PosX == 5 and PosY == 3) then
  83.         digit = 6
  84.     elseif (PosX == 3 and PosY == 4) then
  85.         digit = 7
  86.     elseif (PosX == 4 and PosY == 4) then
  87.         digit = 8
  88.     elseif (PosX == 5 and PosY == 4) then
  89.         digit = 9
  90.     elseif (PosX == 3 and PosY == 5) then
  91.         clear = true
  92.     elseif (PosX == 4 and PosY == 5) then
  93.         digit = 0
  94.     elseif (PosX == 5 and PosY == 5) then
  95.         apply = true
  96.     end
  97.    
  98.     --add symbol or process
  99.     if (digit >= 0) then
  100.         CurrPass = CurrPass..digit
  101.         if (string.len(CurrPass) > 6) then
  102.             CurrPass = ""
  103.         end
  104.     elseif (clear) then
  105.         CurrPass = ""
  106.         IsOpen = false
  107.     elseif (apply) then
  108.         if (CurrPass == Password) then
  109.             IsOpen = true
  110.         else
  111.             CurrPass = ""
  112.         end
  113.     end
  114.    
  115.     --Open or close door
  116.     rs.setOutput(outputSide, IsOpen)
  117.     RS_SkipCounter = 0
  118.    
  119.     --redraw screen
  120.     Draw()
  121. end
  122.  
  123. --Triggered by redstone
  124. function RedstoneEvent()
  125.     if (RS_SkipCounter >= 2) then
  126.         CurrPass = ""
  127.         IsOpen = false
  128.         rs.setOutput(outputSide, IsOpen)
  129.         Draw()
  130.         RS_SkipCounter = 0
  131.     else
  132.         RS_SkipCounter = RS_SkipCounter + 1
  133.     end
  134. end
  135.  
  136. --Fisrt draw
  137. Draw()
  138.  
  139. repeat
  140.     event,p1,p2,p3 = os.pullEvent()
  141.     if event=="monitor_touch" then
  142.         --p2 -- sets mouseWidth
  143.         --p3 -- and mouseHeight
  144.         TouchEvent(p2, p3)
  145.     end
  146.  
  147.   if event=="redstone" then
  148.     RedstoneEvent()
  149.   end
  150. until event=="char" and p1==("q")
RAW Paste Data