Advertisement
Jharakn

Touchscreen Monitor

Feb 16th, 2013
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.09 KB | None | 0 0
  1.  
  2. -- **************************************************************** --
  3. -- ***** Touchscreen Control for 3x2 or 3x4 advanced monitors ***** --
  4. -- ***** By Jharakn ***** --
  5. -- **************************************************************** --
  6.  
  7. --create a few global variables for names.
  8. messageOn = {long = " on ", short = " on "}
  9. messageOff = {long = " off ", short = " off "}
  10. messageAuto = {long = " auto ", short = " auto "}
  11. messageBlank = {long = " ", short = " "}
  12.  
  13. --build the matrix for holding all the button info
  14. local button = {}
  15. for i = 1, 8 do
  16. button[i] = {}
  17. end
  18.  
  19. button[1].name = " "
  20. button[1].status = messageOff.short
  21. button[1].colour = colors.red
  22. button[1].visible = true
  23. button[1].toggle = true
  24. button[1].wireColour = colours.white
  25. button[1].xPos =2
  26. button[1].yPos =8
  27.  
  28. button[2].name = " "
  29. button[2].status = messageOff.short
  30. button[2].colour = colors.red
  31. button[2].visible = true
  32. button[2].toggle = true
  33. button[2].wireColour = colours.orange
  34. button[2].xPos =2
  35. button[2].yPos =11
  36.  
  37. button[3].name = " "
  38. button[3].status = messageOff.short
  39. button[3].colour = colors.red
  40. button[3].visible = true
  41. button[3].toggle = true
  42. button[3].wireColour = colours.magenta
  43. button[3].xPos =2
  44. button[3].yPos =14
  45.  
  46. button[4].name = " "
  47. button[4].status = messageOff.long
  48. button[4].colour = colors.red
  49. button[4].visible = false
  50. button[4].toggle = true
  51. button[4].wireColour = colors.lightBlue
  52. button[4].xPos =22
  53. button[4].yPos =2
  54.  
  55. button[5].name = " "
  56. button[5].status = messageOff.long
  57. button[5].colour = colors.red
  58. button[5].visible = false
  59. button[5].toggle = true
  60. button[5].wireColour = colors.yellow
  61. button[5].xPos =22
  62. button[5].yPos =5
  63.  
  64. button[6].name = " "
  65. button[6].status = messageOff.long
  66. button[6].colour = colors.red
  67. button[6].visible = false
  68. button[6].toggle = true
  69. button[6].wireColour = colors.lime
  70. button[6].xPos =22
  71. button[6].yPos =8
  72.  
  73. button[7].name = " "
  74. button[7].status = messageOff.long
  75. button[7].colour = colors.red
  76. button[7].visible = false
  77. button[7].toggle = true
  78. button[7].wireColour = colors.pink
  79. button[7].xPos =22
  80. button[7].yPos =11
  81.  
  82. button[8].name = " "
  83. button[8].status = messageOff.long
  84. button[8].colour = colors.red
  85. button[8].visible = false
  86. button[8].toggle = true
  87. button[8].wireColour = colors.gray
  88. button[8].xPos =22
  89. button[8].yPos =14
  90.  
  91. button.redstoneValue = 0
  92. button.monitorSide = "right"
  93. button.redstoneSide = "left"
  94. button.modemSide = nil
  95. button.title = nil
  96. button.title2 = nil
  97. button.title3 = nil
  98. button.widescreen = false
  99.  
  100. --Clear the monitor and reset the curser in preperation for building the GUI
  101. local monitor = peripheral.wrap(button.monitorSide)
  102. monitor.setBackgroundColor(colors.black)
  103. monitor.setCursorPos(1, 1)
  104. monitor.clear()
  105.  
  106. --Create / refresh the numbered button
  107. function createButton(number)
  108. monitor.setCursorPos(button[number].xPos, button[number].yPos)
  109. monitor.setBackgroundColor(button[number].colour)
  110. monitor.write(button[number].name)
  111. monitor.setCursorPos(button[number].xPos, button[number].yPos+1)
  112. monitor.write(button[number].status)
  113. monitor.setBackgroundColor(colors.black)
  114. end
  115.  
  116.  
  117.  
  118. --start the program reading from the saveState and refreshing buttons
  119. function startup()
  120.  
  121. -- read the savestate and update the buttons array
  122. if fs.exists("saveState") then
  123. local file = fs.open("saveState","r")
  124. local data = file.readAll()
  125. file.close()
  126. local array = {}
  127. array = textutils.unserialize(data)
  128. for i = 1,8 do
  129. button[i].name = array[i].name
  130. button[i].status = array[i].status
  131. button[i].colour = array[i].colour
  132. button[i].visible = array[i].visible
  133. button[i].toggle = array[i].toggle
  134. button[i].wireColour = array[i].wireColour
  135. button[i].xPos = array[i].xPos
  136. button[i].yPos = array[i].yPos
  137. end
  138. button.redstoneValue = array.redstoneValue
  139. button.monitorSide = array.monitorSide
  140. button.redstoneSide = array.redstoneSide
  141. button.modemSide = array.modemSide
  142. button.title = array.title
  143. button.title2 = array.title2
  144. button.title3 = array.title3
  145. button.widescreen = array.widescreen
  146. end
  147.  
  148. -- set up the buttons
  149. for i = 1,8 do
  150. if button[i].visible then
  151. createButton(i)
  152. end
  153. end
  154.  
  155. --set the redstone output
  156. redstone.setBundledOutput(button.redstoneSide, button.redstoneValue)
  157.  
  158. --set the titles
  159. if button.title ~= nil then
  160. monitor.setCursorPos(2,2)
  161. monitor.write(button.title)
  162. end
  163. if button.title2 ~= nil then
  164. monitor.setCursorPos(2,4)
  165. monitor.write(button.title2)
  166. end
  167. if button.title3 ~= nil then
  168. monitor.setCursorPos(2,5)
  169. monitor.write(button.title3)
  170. end
  171.  
  172. pcall(writeState)
  173. end
  174.  
  175. --save the current button information to a file called saveState
  176. function writeState()
  177. local file = fs.open("saveState","w")
  178. file.write(textutils.serialize(button))
  179. file.close()
  180. end
  181.  
  182. -- ************ --
  183. -- Main Program --
  184. -- ************ --
  185.  
  186. startup()
  187. writeState()
  188.  
  189. while true do
  190. -- wait for a monitor click event to occur and output the results
  191. local event, side, xPos, yPos = os.pullEvent("monitor_touch")
  192.  
  193. local row = nil; local column = nil; local buttonPressed = nil
  194.  
  195. --check what row the button press was on
  196. if xPos > 1 and xPos < 20 then row = 1 end
  197. if xPos > 21 and xPos < 40 then row = 2 end
  198.  
  199. --check what column the button press was on
  200. if yPos > 1 and yPos < 4 then column = 1 end
  201. if yPos > 4 and yPos < 7 then column = 2 end
  202. if yPos > 7 and yPos < 10 then column = 3 end
  203. if yPos > 10 and yPos < 13 then column = 4 end
  204. if yPos > 13 and yPos < 16 then column = 5 end
  205.  
  206. -- calculate what button was pressed from the row and column
  207. if row == 1 and column == 3 then buttonPressed = 1 end
  208. if row == 1 and column == 4 then buttonPressed = 2 end
  209. if row == 1 and column == 5 then buttonPressed = 3 end
  210. if row == 2 and column == 1 then buttonPressed = 4 end
  211. if row == 2 and column == 2 then buttonPressed = 5 end
  212. if row == 2 and column == 3 then buttonPressed = 6 end
  213. if row == 2 and column == 4 then buttonPressed = 7 end
  214. if row == 2 and column == 5 then buttonPressed = 8 end
  215.  
  216. --check that a button was pressed not an empty patch of monitor and that button is supposed to be visible
  217. if buttonPressed ~= nil and button[buttonPressed].visible then
  218. --process for toggle buttons
  219. if button[buttonPressed].toggle then
  220. if button[buttonPressed].colour == colours.red then
  221. button[buttonPressed].colour = colours.blue
  222. if button.widescreen then
  223. button[buttonPressed].status = messageAuto.long
  224. else
  225. button[buttonPressed].status = messageAuto.short
  226. end
  227. elseif button[buttonPressed].colour == colours.blue then
  228. button[buttonPressed].colour = colours.green
  229. if button.widescreen then
  230. button[buttonPressed].status = messageOn.long
  231. else
  232. button[buttonPressed].status = messageOn.short
  233. end
  234. button.redstoneValue = button.redstoneValue + button[buttonPressed].wireColour
  235. elseif button[buttonPressed].colour == colours.green then
  236. button[buttonPressed].colour = colours.red
  237. if button.widescreen then
  238. button[buttonPressed].status = messageOff.long
  239. else
  240. button[buttonPressed].status = messageOff.short
  241. end
  242. button.redstoneValue = button.redstoneValue - button[buttonPressed].wireColour
  243. end
  244. pcall(writeState)
  245. createButton(buttonPressed)
  246. redstone.setBundledOutput(button.redstoneSide, button.redstoneValue)
  247. --process for non-toggle buttons
  248. else
  249. button[buttonPressed].colour = colors.purple
  250. button.redstoneValue = button.redstoneValue + button[buttonPressed].wireColour
  251. createButton(buttonPressed)
  252. redstone.setBundledOutput(button.redstoneSide, button.redstoneValue)
  253. sleep(1)
  254. button[buttonPressed].colour = colors.gray
  255. button.redstoneValue = button.redstoneValue - button[buttonPressed].wireColour
  256. createButton(buttonPressed)
  257. redstone.setBundledOutput(button.redstoneSide, button.redstoneValue)
  258. end
  259. end
  260.  
  261. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement