BrainStone

Lua Screen

Jan 12th, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.27 KB | None | 0 0
  1. -- APIs
  2. os.loadAPI("api/Protect")
  3. os.loadAPI("api/Logger")
  4. os.loadAPI("api/Touchpoint")
  5.  
  6. -- Globals
  7. screen_main = "monitor_2"
  8. screen_logger = "monitor_3"
  9. sensor = "openperipheral_sensor_1"
  10.  
  11. bundled_cable_side = "front"
  12. bundled_cable_event_functions = {}
  13. bundled_cable = 0
  14.  
  15. monitor = nil
  16. page1 = nil
  17.  
  18. players = {}
  19.  
  20. -- Functions
  21.  
  22. -- Util
  23. function activeToText(active)
  24.   if active then
  25.     return "on"
  26.   else
  27.     return "off"
  28.   end
  29. end
  30.  
  31. function setBundledCableColor(side, color, value)
  32.   local bus = rs.getBundledOutput(side)
  33.  
  34.   if value then
  35.     bus = bit.bor(bus, color)
  36.   else
  37.     bus = bit.band(bus, bit.bnot(color))
  38.   end
  39.  
  40.   rs.setBundledOutput(side, bus)
  41. end
  42.  
  43. function getBundledCableColor(side, color)
  44.   local bus = rs.getBundledInput(side)
  45.  
  46.   return bit.band(bus, color) ~= 0
  47. end
  48.  
  49. function toggleBundledCableColor(side, color)
  50.   local bus = rs.getBundledOutput(side)
  51.  
  52.   bus = bit.bxor(bus, color)
  53.  
  54.   rs.setBundledOutput(side, bus)
  55. end
  56.  
  57. function savePage(page, file_name)
  58.   local save_table = {}
  59.  
  60.   for label, data in pairs(page.buttonList) do
  61.     save_table[label] = data.active
  62.   end
  63.  
  64.   local handle = fs.open(file_name, "w")
  65.  
  66.   if handle then
  67.     handle.write(textutils.serialize(save_table))
  68.    
  69.     handle.close()
  70.   end
  71. end
  72.  
  73. function loadPage(page, file_name)
  74.   local handle = fs.open(file_name, "r")
  75.  
  76.   if handle then
  77.     local load_table = textutils.unserialize(handle.readAll())
  78.    
  79.     handle.close()
  80.    
  81.     for label, state in pairs(load_table) do
  82.       local button = page.buttonList[label]
  83.       if button then
  84.         button.active = state
  85.        
  86.         if button.func then
  87.           button.func(label, state, true)
  88.         end
  89.       end
  90.     end
  91.   end
  92. end
  93.  
  94. -- Handler
  95. function toggleManOverride(button, state, loading)
  96.   if loading then
  97.     state = not state
  98.   end
  99.  
  100.   setBundledCableColor(bundled_cable_side, colors.white, not state)
  101. end
  102.  
  103. function updateAKWState(state)
  104.   page1.buttonList["Status"].active = not state
  105. end
  106.  
  107. function toggleMeImport(button, state, loading)
  108.   if loading then
  109.     state = not state
  110.   end
  111.  
  112.   setBundledCableColor(bundled_cable_side, colors.magenta, state)
  113. end
  114.  
  115. -- Init
  116. function initTouchpoint()
  117.   monitor = peripheral.wrap(screen_main)
  118.   page1 = Touchpoint.new(screen_main)
  119. end
  120.  
  121. function addButtons()
  122.   page1:add("Atomkraftwerk", nil, 1, 1, 25, 3, colors.black, colors.black)
  123.   page1:add("Status", nil, 2, 4, 9, 6, colors.red, colors.lime)
  124.   page1:add("Man.Override", toggleManOverride, 11, 4, 24, 6, colors.red, colors.orange)
  125.  
  126.   page1:add("Sonstige", nil, 27, 1, 37, 3, colors.black, colors.black)
  127.   page1:add("ME Import", toggleMeImport, 27, 4, 37, 6, colors.red, colors.lime)
  128. end
  129.  
  130. function loadSettings()
  131.   loadPage(page1, "save/page1.txt")
  132. end
  133.  
  134. function addBundledCableEvents()
  135.   bundled_cable_event_functions = {
  136.     [colors.orange] = { updateAKWState }
  137.   }
  138. end
  139.  
  140. function updateAKW()
  141.   toggleBundledCableColor(bundled_cable_side, colors.white)
  142.  
  143.   sleep(0.1)
  144.  
  145.   toggleBundledCableColor(bundled_cable_side, colors.white)
  146. end
  147.  
  148. -- Loops
  149. function renderLoop()
  150.   while not Protect.isTerminated() do
  151.     page1:draw()
  152.    
  153.     coroutine.yield()
  154.   end
  155. end
  156.  
  157. function touchScreenLoop()
  158.   local event, button
  159.  
  160.   while true do
  161.     event = {page1:handleEvents(os.pullEvent("monitor_touch"))}
  162.    
  163.     if event[1] == "button_click" then
  164.       button = page1.buttonList[event[2]]
  165.      
  166.       if button.func then
  167.         button.active = button.func(event[2], button.active, false) or (not button.active)
  168.        
  169.         Logger.message(event[2] .. " turned " .. activeToText(button.active) .. " (" .. table.concat(players, ", ") .. ")", "ok")
  170.       end
  171.      
  172.       savePage(page1, "save/page1.txt")
  173.     end
  174.   end
  175. end
  176.  
  177. function sensorLoop()
  178.   local sensor_obj = peripheral.wrap(sensor)
  179.   local next_players
  180.  
  181.   while true do
  182.     next_players = {}
  183.    
  184.     for _, v in ipairs(sensor_obj.getPlayers()) do
  185.       next_players[#next_players + 1] = v.name
  186.     end
  187.    
  188.     players = next_players
  189.    
  190.     sleep(0.1)
  191.   end
  192. end
  193.  
  194. function redstoneEventLoop()
  195.   local first_run = true
  196.  
  197.   while true do
  198.     local current_bundled_cable = rs.getBundledInput(bundled_cable_side)
  199.     local changes = bit.bxor(current_bundled_cable, bundled_cable)
  200.    
  201.     for color, functions in pairs(bundled_cable_event_functions) do
  202.       if (bit.band(changes, color) ~= 0) or first_run then
  203.         local new_state = (bit.band(current_bundled_cable, color) ~= 0)
  204.        
  205.         for _, func in ipairs(functions) do
  206.           func(new_state)
  207.         end
  208.       end
  209.     end
  210.    
  211.     bundled_cable = current_bundled_cable
  212.     first_run = false
  213.  
  214.     os.pullEvent("redstone")
  215.   end
  216. end
  217.  
  218. -- Main
  219. function main()
  220.   Logger.init(screen_logger)
  221.  
  222.   Logger.log("Initializing Touchscreen", initTouchpoint)
  223.   Logger.log("Adding Buttons", addButtons)
  224.   Logger.log("Loading Settings", loadSettings)
  225.   Logger.log("Adding BundledCable Events", addBundledCableEvents)
  226.   Logger.log("Refreshing AKW redstone cable", updateAKW)
  227.  
  228.   Logger.message("Starting main loopa", "ok")
  229.  
  230.   parallel.waitForAny(renderLoop, touchScreenLoop, sensorLoop, redstoneEventLoop)
  231. end
  232.  
  233. -- Code
  234. Protect.runNotify("TmOnlineMapper95", main)
  235.  
  236. Logger.close()
Add Comment
Please, Sign In to add comment