Advertisement
jackdell

Reactor Controller

Jan 27th, 2025 (edited)
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.41 KB | Source Code | 0 0
  1. -- Author: Jack Dell
  2. -- Website: jackdell.ca
  3. -- Date: 2025-01-27
  4.  
  5. -- Config
  6. local LISTEN_CHANNEL = 2
  7. local RELAY_CHANNEL = 1
  8. local TEXT_SCALE = 0.75
  9.  
  10. -- Connecting to peripherals
  11. local monitor = peripheral.wrap("back") or error("No monitor found")
  12. local modem = peripheral.wrap("top") or error("No modem found")
  13.  
  14. -- Getting the monitor width and height
  15. local w, h = monitor.getSize()
  16.  
  17. -- Button definitions
  18. local buttons = {
  19.    {text = "Auto", x = 2, y = h-4, width = 6, height = 3, color = colors.blue},
  20.    {text = "On", x = 10, y = h-4, width = 4, height = 3, color = colors.blue},
  21.    {text = "Off", x = 15, y = h-4, width = 5, height = 3, color = colors.blue}
  22. }
  23.  
  24. function drawButton(button)
  25.    monitor.setBackgroundColor(button.color)
  26.    -- Draw the button shape
  27.    for y = button.y, button.y + button.height - 1 do
  28.       monitor.setCursorPos(button.x, y)
  29.       monitor.write(string.rep(" ", button.width))
  30.    end
  31.    -- Draw text in middle row
  32.    monitor.setCursorPos(button.x + math.floor((button.width - #button.text) / 2), button.y + 1)
  33.    monitor.write(button.text)
  34.    monitor.setBackgroundColor(colors.black)
  35. end
  36.  
  37. function drawButtons(stats)
  38.     -- Set the active button to green, otherwise blue
  39.     buttons[1].color = stats.dynamic and colors.green or colors.blue
  40.     buttons[2].color = (not stats.dynamic and stats.active) and colors.green or colors.blue  
  41.     buttons[3].color = (not stats.dynamic and not stats.active) and colors.green or colors.blue
  42.  
  43.     -- Draw each button to the monitor
  44.     for _, button in pairs(buttons) do
  45.        drawButton(button)
  46.     end
  47. end
  48.  
  49. function formatRF(rf)
  50.    if rf >= 1000000 then
  51.        return string.format("%.2fM", rf/1000000)
  52.    else
  53.        return string.format("%.2fK", rf/1000)
  54.    end
  55. end
  56.  
  57. function drawStats(stats)
  58.    monitor.clear()
  59.    monitor.setCursorPos(1,1)
  60.    monitor.write(string.format("Power: %s RF/t", formatRF(stats.rf_output)))
  61.    monitor.setCursorPos(1,2)
  62.    monitor.write(string.format("Stored: %s/%s RF", formatRF(stats.rf_stored), formatRF(stats.rf_capacity)))
  63.    monitor.setCursorPos(1,3)
  64.    monitor.write(string.format("Temp: %.1f C", stats.temperature))
  65.    monitor.setCursorPos(1,4)
  66.    monitor.write(string.format("Fuel: %.1f%%", (stats.fuel_ammount/stats.fuel_capacity)*100))
  67.    monitor.setCursorPos(1,5)
  68.    monitor.write(string.format("Mode: %s", stats.dynamic and "Auto" or "Manual"))
  69. end
  70.  
  71. function draw(stats)
  72.     drawStats(stats)
  73.     drawButtons(stats)
  74. end
  75.  
  76. function handleMessage()
  77.    modem.open(LISTEN_CHANNEL)
  78.    while true do
  79.        local _, _, channel, _, message = os.pullEvent("modem_message")
  80.        if channel == LISTEN_CHANNEL
  81.            and type(message) == "table"
  82.        then
  83.            draw(message)
  84.        end
  85.    end
  86. end
  87.  
  88. function handleClick()
  89.    while true do
  90.        local _, _, x, y = os.pullEvent("monitor_touch")
  91.        for i, button in ipairs(buttons) do
  92.            if y >= button.y and y < button.y + button.height and
  93.               x >= button.x and x < button.x + button.width then
  94.                local message = {
  95.                    override = button.text ~= "Auto",
  96.                    state = button.text == "On"
  97.                }
  98.                modem.transmit(RELAY_CHANNEL, LISTEN_CHANNEL, message)
  99.            end
  100.        end
  101.    end
  102. end
  103.  
  104. function init()
  105.     monitor.clear()
  106.     monitor.setTextScale(TEXT_SCALE)
  107.     parallel.waitForAll(handleMessage, handleClick)
  108. end
  109.  
  110. init()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement