Advertisement
tman1123

receiver.lua

Apr 1st, 2024 (edited)
820
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Adjust as needed for your setup:
  2.    
  3.     -- Modem
  4.     local modemSide = "top"
  5.     -- Current Floor Header (used in multiline displays only)
  6.     local multiH = "Current Floor"
  7.     -- Moving Text (Used when elevator is moving between floors on name displays)
  8.         -- Multiline config
  9.         local moveL1 = "Elevator is"  -- Line 1
  10.         local moveL2 = "moving"  -- Line 2
  11.         -- Single Line config
  12.         local moveSL = "Moving"
  13.  
  14.  
  15. --Code starts here
  16. local configFileName = "displays.txt"
  17.  
  18. function loadConfig()
  19.     if fs.exists(configFileName) then
  20.         local file = fs.open(configFileName, "r")
  21.         local config = textutils.unserialize(file.readAll())
  22.         file.close()
  23.         return config
  24.     else
  25.         error("Configuration file not found. Please run the setup script first.")
  26.     end
  27. end
  28.  
  29. local config = loadConfig()
  30.  
  31. -- Prepare a table to hold the last displayed text for each display to minimize updates
  32. local lastDisplayedText = {}
  33.  
  34.  
  35. rednet.open(modemSide)
  36.  
  37. function centerText(text, line)
  38.     local width, height = term.getSize()
  39.     term.setCursorPos((width - string.len(text)) / 2, line)
  40.     print(text)
  41. end
  42.  
  43. function displayUI()
  44.     term.clear()
  45.     centerText("Controlling Displays", 1)
  46.     centerText("Current Floor", 4)
  47. end
  48.  
  49.  
  50. function clearAndUpdateDisplay(display, text, displayId)
  51.     if lastDisplayedText[displayId] ~= text then
  52.         display.clear()
  53.         display.setCursorPos(1, 1)
  54.         display.write(text)
  55.         display.update()
  56.         lastDisplayedText[displayId] = text
  57.     end
  58. end
  59.  
  60. function updateDisplays(floor)
  61.     for i, dispConfig in ipairs(config.displays) do
  62.         local display = peripheral.wrap(dispConfig.side)
  63.         if display then
  64.             local displayText = dispConfig.type == "name" and floor.name or floor.num
  65.             local h, w = display.getSize()
  66.             local DispType = GetDispType(h, w)
  67.             centerText(floor.num .. " - " .. floor.name, 6)
  68.  
  69.             if dispConfig.type == "name" then
  70.                 if DispType == "multi" then
  71.                     if floor.num == "--" then
  72.                         centerTextAndUpdate(display, moveL1, i, 1)
  73.                         centerTextAndUpdate(display, moveL2, i, 2)
  74.                     else
  75.                         centerTextAndUpdate(display, multiH, i, 1)
  76.                         centerTextAndUpdate(display, displayText, i, 2)
  77.                     end
  78.                     lastDisplayedText[i] = displayText
  79.                 else
  80.                     if floor.num == "--" then
  81.                         centerTextAndUpdate(display, moveSL, i, 1)
  82.                     else
  83.                         centerTextAndUpdate(display, displayText, i, 1)
  84.                     end
  85.                     lastDisplayedText[i] = displayText
  86.                 end
  87.             else
  88.                 clearAndUpdateDisplay(display, displayText, i, 1)
  89.             end
  90.         else
  91.             print("Display not found on side: " .. dispConfig.side)
  92.         end
  93.     end
  94. end
  95.  
  96. function centerTextAndUpdate(display, text, displayId, line)
  97.     if lastDisplayedText[displayId] ~= text then
  98.         local h, w = display.getSize()
  99.         display.setCursorPos(((w - string.len(text)) / 2) + 1, line)
  100.         display.clearLine()
  101.         display.write(text)
  102.         display.update()
  103.     end
  104. end
  105.  
  106. function GetDispType(disph, dispw)
  107.     local dtype = ""
  108.     if disph == 1 then
  109.         dtype = "single"
  110.     else
  111.         dtype = "multi"
  112.     end
  113.     return dtype
  114. end
  115.  
  116. displayUI()
  117.  
  118. while true do
  119.     local senderId, message, protocol = rednet.receive("ElevatorFloor")
  120.     if protocol == "ElevatorFloor" then
  121.         updateDisplays(message)
  122.     end
  123. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement