Advertisement
Guest User

geeklights

a guest
Jul 28th, 2015
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.30 KB | None | 0 0
  1. -- This is a comment. It's for humans only. All
  2. -- the green text is not run by the program
  3.  
  4. mouseWidth = 0
  5. mouseHeight = 0
  6.  
  7. monitor = peripheral.wrap("back")
  8. monitor.clear()
  9.  
  10. monitor.setCursorPos(1,1)
  11.  
  12.  
  13. w,h=monitor.getSize()
  14.  
  15. print(w)
  16. print(h)
  17.  
  18. monitor.setBackgroundColour((colours.black))
  19.  
  20. monitor.setCursorPos(1,1)
  21. monitor.write(" 1st Floor ")
  22.  
  23. monitor.setCursorPos(2,2)
  24. monitor.write(" ON ")
  25.  
  26. monitor.setCursorPos(2,4)
  27. monitor.write(" OFF ")
  28.  
  29. monitor.setCursorPos(1,6)
  30. monitor.write(" 2nd Floor ")
  31.  
  32. monitor.setCursorPos(2,7)
  33. monitor.write(" ON ")
  34.  
  35. monitor.setCursorPos(2,9)
  36. monitor.write(" OFF ")
  37.  
  38. monitor.setBackgroundColour((colours.black))
  39. -- now we have drawn our buttons we should set
  40. -- the text background colour back to black
  41.  
  42.  
  43. -- Now we need to check if the button is clicked
  44.  
  45. -- First we are going to create a function called
  46. -- checkClickPosition(). A function will not run
  47. -- until you ask for it.
  48.  
  49. -- We know the first button starts at 2 from the
  50. -- top and 2 from the left. We also know it is 5
  51. -- spaces long. This means the button ends
  52. -- at width 7
  53.  
  54. -- We will be told which width and
  55. -- height the click happened at.
  56. -- If the width position is greater than 1 AND
  57. -- less than 8 we have clicked somewhere between
  58. -- 2 and 7.
  59.  
  60. -- If this is true we can then check the height
  61. -- position. Button one is at height 2 and button
  62. -- two is at height 4.
  63.  
  64. -- This means that if the width is greater than 1
  65. -- AND the width is less than 8 AND the height
  66. -- equals 2 we have clicked button 1
  67.  
  68. -- If the the width is greater than 1 AND the width
  69. -- is less than 8 AND the height equals 4 we have
  70. -- clicked button 2
  71.  
  72. -- now to write this as a function
  73. -- Functions are written like this
  74.  
  75. -- function exampleFunction()
  76. -- print("Hello")
  77. -- sleep(10)
  78. -- print("Goodbye")
  79. -- end
  80.  
  81. -- Now when you write exampleFunction() the program
  82. -- will print hello, sleep for 10 ticks and then
  83. -- print Goodbye.
  84. -- This is useful for making your programs easier
  85. -- to understand
  86.  
  87. function checkClickPosition()
  88. if mouseWidth > 1 and mouseWidth < 8 and mouseHeight == 2 then
  89. rs.setBundledOutput("top", colors.white)
  90.  
  91. elseif mouseWidth > 1 and mouseWidth < 8 and mouseHeight == 4 then
  92. rs.setBundledOutput("top", colors.black)
  93.  
  94. end -- ends the if loop
  95. end -- ends the function
  96.  
  97. function checkClickPosition()
  98. if mouseWidth > 1 and mouseWidth < 8 and mouseHeight == 7 then
  99. rs.setBundledOutput("top", colors.blue)
  100.  
  101. elseif mouseWidth > 1 and mouseWidth < 8 and mouseHeight == 9 then
  102. rs.setBundledOutput("top", colors.black)
  103.  
  104. end -- ends the if loop
  105. end -- ends the function
  106.  
  107. -- this function does nothing until you write
  108. -- checkClickPostion(). We will be doing this below
  109. -- It then checks the click position and turns the
  110. -- lamp on if button one is clicked or turns the
  111. -- lamp off if button two is clicked
  112.  
  113. -- OK. Now we need to check if a click happens
  114. -- we will use a repeat-until loop.
  115. -- In the loop we we use a os.pullEvent().
  116. -- an os.pullEvent() gives you different info
  117. -- depending on the event type. We will mainly
  118. -- check the "monitor_touch" event.
  119.  
  120. -- In the second line you will see
  121. -- event,p1,p2,p3 = os.pullEvent()
  122. -- if the event is a click on the monitor it
  123. -- will give us 4 bits of info:
  124. -- event will be "monitor_touch"
  125. -- p1 will be the side the monitor is on (top)
  126. -- p2 is the width postion of the click
  127. -- p3 is the height postition of the click
  128.  
  129.  
  130.  
  131. repeat
  132. -- repeat runs a loop of code.
  133.  
  134. event,p1,p2,p3 = os.pullEvent()
  135. -- this line tells the computer to wait until
  136. -- an event happens. We are waiting for a
  137. -- touchscreen event
  138.  
  139. if event=="monitor_touch" then
  140. -- this checks to see if the event was a
  141. -- touchscreen event
  142.  
  143. mouseWidth = p2 -- sets mouseWidth
  144. mouseHeight = p3 -- and mouseHeight
  145. checkClickPosition() -- this runs our function
  146.  
  147. end
  148. -- the end of the "if loop".
  149.  
  150.  
  151. until event=="char" and p1==("x")
  152. -- this is the end of the "repeat loop". This will
  153. -- stop the repeat loop if a "char" event happens
  154. -- A char event means you press a character on
  155. -- the keyboard. This line is looking for the x key
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement