Minenat69

Monitor Program

Aug 1st, 2023 (edited)
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.04 KB | None | 0 0
  1. COORDS = {
  2. {8, 5}, -- Engine 1
  3. {8, 13}, -- Engine 2
  4. {8, 21}, -- Engine 3
  5. {38, 5}, -- Engine 4
  6. {38, 13}, -- Engine 5
  7. {38, 21} -- Engine 6
  8. }
  9.  
  10. ENGINE_STATUSES = {
  11. false,
  12. false,
  13. false,
  14. false,
  15. false,
  16. false
  17. }
  18.  
  19. ALTERNATOR_STATUSES = {
  20. false,
  21. false,
  22. false,
  23. false,
  24. false,
  25. false
  26. }
  27.  
  28.  
  29. function printCentered(text)
  30. local maxw, maxh = monitor.getSize() --screen dimensions
  31. local curx, cury = monitor.getCursorPos() --cursor pos
  32. monitor.setCursorPos((maxw-#text)/2,cury) --set it to the point where write will make the text centred
  33. monitor.write(text) --write the text
  34. end
  35.  
  36. function startingEngineMessage(nbEngine, isStarting)
  37. local onofftxt = ""
  38. if isStarting then
  39. onofftxt = "on"
  40. else
  41. onofftxt = "off"
  42. end
  43. monitor.setCursorPos(0, 26)
  44. monitor.setTextColor(colors.yellow)
  45. printCentered("Turning "..onofftxt.." Engine "..nbEngine.."...")
  46. end
  47.  
  48. function processClick(x, y)
  49. print("Click at x :"..x.." and y : "..y)
  50. local maxw, maxh = monitor.getSize() --screen dimensions
  51. local ton = "Turn ON"
  52. local toff = "Turn OFF"
  53. -- Check if clicked for Engine 1
  54. if (y == COORDS[1][2] + 2 and x < maxw / 2) then
  55. print("Focused Engine 1 !")
  56. if (x >= COORDS[1][1] and x <= COORDS[1][1] + #ton + 1) then
  57. print("Turn ON")
  58. modem.transmit(3, 2, "turn_on")
  59. startingEngineMessage("1", true)
  60. else
  61. print("Turn OFF")
  62. modem.transmit(3, 2, "turn_off")
  63. startingEngineMessage("1", false)
  64. end
  65. end
  66.  
  67. -- Check if clicked for Engine 2
  68. if (y == COORDS[2][2] + 2 and x < maxw / 2) then
  69. print("Focused Engine 2 !")
  70. if (x >= COORDS[2][1] and x <= COORDS[2][1] + #ton + 1) then
  71. print("Turn ON")
  72. modem.transmit(4, 2, "turn_on")
  73. startingEngineMessage("2", true)
  74. else
  75. print("Turn OFF")
  76. modem.transmit(4, 2, "turn_off")
  77. startingEngineMessage("2", false)
  78. end
  79. end
  80.  
  81. -- Check if clicked for Engine 3
  82. if (y == COORDS[3][2] + 2 and x < maxw / 2) then
  83. print("Focused Engine 3 !")
  84. if (x >= COORDS[3][1] and x <= COORDS[3][1] + #ton + 1) then
  85. print("Turn ON")
  86. modem.transmit(5, 2, "turn_on")
  87. startingEngineMessage("3", true)
  88. else
  89. print("Turn OFF")
  90. modem.transmit(5, 2, "turn_off")
  91. startingEngineMessage("3", false)
  92. end
  93. end
  94.  
  95. -- Check if clicked for Engine 4
  96. if (y == COORDS[4][2] + 2 and x > maxw / 2) then
  97. print("Focused Engine 4 !")
  98. if (x >= COORDS[4][1] and x <= COORDS[4][1] + #ton + 1) then
  99. print("Turn ON")
  100. modem.transmit(6, 2, "turn_on")
  101. startingEngineMessage("4", true)
  102. else
  103. print("Turn OFF")
  104. modem.transmit(6, 2, "turn_off")
  105. startingEngineMessage("4", false)
  106. end
  107. end
  108.  
  109. -- Check if clicked for Engine 5
  110. if (y == COORDS[5][2] + 2 and x > maxw / 2) then
  111. print("Focused Engine 5 !")
  112. if (x >= COORDS[5][1] and x <= COORDS[5][1] + #ton + 1) then
  113. print("Turn ON")
  114. modem.transmit(7, 2, "turn_on")
  115. startingEngineMessage("5", true)
  116. else
  117. print("Turn OFF")
  118. modem.transmit(7, 2, "turn_off")
  119. startingEngineMessage("5", false)
  120. end
  121. end
  122.  
  123. -- Check if clicked for Engine 6
  124. if (y == COORDS[6][2] + 2 and x > maxw / 2) then
  125. print("Focused Engine 6 !")
  126. if (x >= COORDS[6][1] and x <= COORDS[6][1] + #ton + 1) then
  127. print("Turn ON")
  128. modem.transmit(8, 2, "turn_on")
  129. startingEngineMessage("6", true)
  130. else
  131. print("Turn OFF")
  132. modem.transmit(8, 2, "turn_off")
  133. startingEngineMessage("6", false)
  134. end
  135. end
  136.  
  137.  
  138. end
  139.  
  140. function shutdownMonitor()
  141. os.reboot()
  142. end
  143.  
  144.  
  145. function wait ( time )
  146. local timer = os.startTimer(time)
  147. while true do
  148. local event = {os.pullEvent()}
  149. if (event[1] == "timer" and event[2] == timer) then
  150. break
  151. elseif event[1] == "monitor_touch" then
  152. processClick(event[3], event[4]) -- a custom function in which you would deal with received events
  153. elseif event[1] == "modem_message" then
  154. local mess = event[5]
  155. print("Got modem message "..mess)
  156. if mess == "shutdown_monitor" then
  157. shutdownMonitor()
  158. break
  159. end
  160. end
  161. end
  162. end
  163.  
  164. function displayWheel(switch, x, y, nb_engine)
  165. if ENGINE_STATUSES[nb_engine] then
  166. if ALTERNATOR_STATUSES[nb_engine] then
  167. monitor.setTextColor(colors.orange)
  168. else
  169. monitor.setTextColor(colors.green)
  170. end
  171. else
  172. monitor.setTextColor(colors.red)
  173. end
  174.  
  175. monitor.setCursorPos(x - 6, y - 1)
  176. monitor.write(" ___ ")
  177. monitor.setCursorPos(x - 6, y)
  178. if (switch and ENGINE_STATUSES[nb_engine]) then
  179. monitor.write("\/ I \\")
  180. else
  181. monitor.write("\/\\ \/\\")
  182. end
  183.  
  184. monitor.setCursorPos(x - 6, y + 1)
  185. if (switch and ENGINE_STATUSES[nb_engine]) then
  186. monitor.write("|-O-|")
  187. else
  188. monitor.write("| O |")
  189. end
  190.  
  191. monitor.setCursorPos(x - 6, y + 2)
  192. if (switch and ENGINE_STATUSES[nb_engine]) then
  193. monitor.write("\\_I_\/")
  194. else
  195. monitor.write("\\\/_\\\/")
  196. end
  197.  
  198. end
  199.  
  200. function displayEngineSection(nbEngine, startX, startY)
  201. monitor.setCursorPos(startX, startY)
  202. monitor.setTextColor(colors.yellow)
  203. monitor.write("Engine #"..nbEngine)
  204.  
  205. monitor.setCursorPos(startX, startY + 1)
  206. monitor.write("Status : ")
  207. monitor.setCursorPos(startX + 9, startY + 1)
  208. if(ENGINE_STATUSES[nbEngine]) then
  209. monitor.write("ON ")
  210. else
  211. monitor.write("OFF")
  212. end
  213.  
  214. monitor.setCursorPos(startX, startY + 2)
  215. monitor.setTextColor(colors.green)
  216. monitor.write("Turn ON")
  217.  
  218. monitor.setCursorPos(startX + 9, startY + 2)
  219. monitor.setTextColor(colors.red)
  220. monitor.write("Turn OFF")
  221. end
  222.  
  223. function getEngineStatuses()
  224. -- Send request
  225. modem.transmit(9, 2, "get_engine_status_request")
  226.  
  227. -- Wait for response
  228. local event, modemSide, senderChannel, replyChannel, message, senderDistance = os.pullEvent("modem_message")
  229. if message[1]=="get_engine_status_response" then
  230. if ENGINE_STATUSES[1] ~= message[2] then
  231. monitor.setCursorPos(0, 26)
  232. monitor.clearLine()
  233. end
  234. ENGINE_STATUSES[1] = message[2]
  235.  
  236. if ENGINE_STATUSES[2] ~= message[3] then
  237. monitor.setCursorPos(0, 26)
  238. monitor.clearLine()
  239. end
  240. ENGINE_STATUSES[2] = message[3]
  241.  
  242. if ENGINE_STATUSES[3] ~= message[4] then
  243. monitor.setCursorPos(0, 26)
  244. monitor.clearLine()
  245. end
  246. ENGINE_STATUSES[3] = message[4]
  247.  
  248. if ENGINE_STATUSES[4] ~= message[5] then
  249. monitor.setCursorPos(0, 26)
  250. monitor.clearLine()
  251. end
  252. ENGINE_STATUSES[4] = message[5]
  253.  
  254. if ENGINE_STATUSES[5] ~= message[6] then
  255. monitor.setCursorPos(0, 26)
  256. monitor.clearLine()
  257. end
  258. ENGINE_STATUSES[5] = message[6]
  259.  
  260. if ENGINE_STATUSES[6] ~= redstone.getInput("right") then
  261. monitor.setCursorPos(0, 26)
  262. monitor.clearLine()
  263. end
  264. ENGINE_STATUSES[6] = redstone.getInput("right")
  265. end
  266. end
  267.  
  268. function getAlternatorStatuses()
  269. -- Send request
  270. modem.transmit(10, 2, "get_alternator_status_request")
  271.  
  272. -- Wait for response
  273. local event, modemSide, senderChannel, replyChannel, message, senderDistance = os.pullEvent("modem_message")
  274. if message[1]=="get_alternator_status_response" then
  275. ALTERNATOR_STATUSES[1] = message[2]
  276. ALTERNATOR_STATUSES[2] = message[3]
  277. ALTERNATOR_STATUSES[3] = message[4]
  278. ALTERNATOR_STATUSES[4] = message[5]
  279. ALTERNATOR_STATUSES[5] = message[6]
  280. ALTERNATOR_STATUSES[6] = redstone.getInput("top")
  281. end
  282. end
  283.  
  284. function clearMonitor()
  285. monitor.setCursorPos(0, 0)
  286. monitor.clearLine()
  287. monitor.setCursorPos(0, 1)
  288. monitor.clearLine()
  289. monitor.setCursorPos(0, 2)
  290. monitor.clearLine()
  291. monitor.setCursorPos(0, 3)
  292. monitor.clearLine()
  293. monitor.setCursorPos(0, 4)
  294. monitor.clearLine()
  295. monitor.setCursorPos(0, 5)
  296. monitor.clearLine()
  297. monitor.setCursorPos(0, 6)
  298. monitor.clearLine()
  299. monitor.setCursorPos(0, 7)
  300. monitor.clearLine()
  301. monitor.setCursorPos(0, 8)
  302. monitor.clearLine()
  303. monitor.setCursorPos(0, 9)
  304. monitor.clearLine()
  305. monitor.setCursorPos(0, 10)
  306. monitor.clearLine()
  307. monitor.setCursorPos(0, 11)
  308. monitor.clearLine()
  309. monitor.setCursorPos(0, 12)
  310. monitor.clearLine()
  311. monitor.setCursorPos(0, 13)
  312. monitor.clearLine()
  313. monitor.setCursorPos(0, 14)
  314. monitor.clearLine()
  315. monitor.setCursorPos(0, 15)
  316. monitor.clearLine()
  317. monitor.setCursorPos(0, 16)
  318. monitor.clearLine()
  319. monitor.setCursorPos(0, 17)
  320. monitor.clearLine()
  321. monitor.setCursorPos(0, 18)
  322. monitor.clearLine()
  323. monitor.setCursorPos(0, 19)
  324. monitor.clearLine()
  325. monitor.setCursorPos(0, 20)
  326. monitor.clearLine()
  327. monitor.setCursorPos(0, 21)
  328. monitor.clearLine()
  329. monitor.setCursorPos(0, 22)
  330. monitor.clearLine()
  331. monitor.setCursorPos(0, 23)
  332. monitor.clearLine()
  333. monitor.setCursorPos(0, 24)
  334. monitor.clearLine()
  335. monitor.setCursorPos(0, 25)
  336. monitor.clearLine()
  337. end
  338.  
  339.  
  340. function mainTerminalDisplay()
  341. monitor.setTextScale(1.5)
  342. local maxw, maxh = monitor.getSize() --screen dimensions
  343.  
  344. local switch = false
  345.  
  346. while true do
  347. getEngineStatuses()
  348. getAlternatorStatuses()
  349. clearMonitor()
  350.  
  351. -- Display Title
  352. monitor.setCursorPos(1, 2)
  353. monitor.setTextColor(colors.white)
  354. printCentered("ENGINE CONTROL SCREEN")
  355.  
  356. -- Display Engine sections
  357. displayEngineSection(1, COORDS[1][1], COORDS[1][2])
  358. displayWheel(switch, COORDS[1][1], COORDS[1][2], 1)
  359.  
  360. displayEngineSection(2, COORDS[2][1], COORDS[2][2])
  361. displayWheel(switch, COORDS[2][1], COORDS[2][2], 2)
  362.  
  363. displayEngineSection(3, COORDS[3][1], COORDS[3][2])
  364. displayWheel(switch, COORDS[3][1], COORDS[3][2], 3)
  365.  
  366. displayEngineSection(4, COORDS[4][1], COORDS[4][2])
  367. displayWheel(switch, COORDS[4][1], COORDS[4][2], 4)
  368.  
  369. displayEngineSection(5, COORDS[5][1], COORDS[5][2])
  370. displayWheel(switch, COORDS[5][1], COORDS[5][2], 5)
  371.  
  372. displayEngineSection(6, COORDS[6][1], COORDS[6][2])
  373. displayWheel(switch, COORDS[6][1], COORDS[6][2], 6)
  374.  
  375. switch = not switch
  376. wait(0.2)
  377. end
  378. end
  379.  
  380. function getEvent()
  381. local event, modemSide, senderChannel, replyChannel, message, senderDistance = os.pullEvent("modem_message")
  382.  
  383. print("I just received a message on channel: "..senderChannel)
  384. print("I should apparently reply on channel: "..replyChannel)
  385. print("The message was: "..message)
  386. if message=="launch_monitor" then
  387. mainTerminalDisplay()
  388. end
  389. end
  390.  
  391. modem = peripheral.wrap("left")
  392. monitor = peripheral.wrap("back")
  393. monitor.clear()
  394. modem.open(2)
  395. getEvent()
Advertisement
Add Comment
Please, Sign In to add comment