Advertisement
fames

startup

Dec 22nd, 2024 (edited)
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.23 KB | None | 0 0
  1. function init()
  2.     print("init")
  3.     require("lib")          -- common functions
  4.     require("reactor")      -- reactor object functions
  5.     require("controller")   -- PI controller
  6.     require("monitor")      -- monitor
  7.     require("button")
  8.     require("window")       -- sub window elements
  9.     require("gauge")
  10.     --require("varUnit")
  11.     controller = ControllerObj:new()
  12.     reactor = ReactorObj:new()
  13.     screen1 = MonitorObj:new(peripheral.find("monitor")[3], 0.5)
  14.  
  15.     settings.define("ctrl.setpoint", {"default = 50.0"})
  16.     settings.save()
  17.  
  18.     pRateCtrl = 0.1 -- controller updaterate
  19.     pRateScreen = 0.5
  20.  
  21.     pSPIncrements = 5
  22.  
  23.     pGain, pResetTime, pDerivative = calcParams(27.5, 1, 40, pRateCtrl)
  24.     controller:setGain(pGain)
  25.     controller:setResetTime(pResetTime)
  26.     controller:setDerivative(pDerivative)
  27.     controller:setDerivativeFilter(2)
  28.     controller:setDerivativeMode(false)
  29.  
  30.     -- screen elemements
  31.     oBCB_window = WindowObj:new()   -- Bottom Control Bar
  32.     oBCB_initWindow = false
  33.     oBCB_initButtons= false
  34.  
  35.     oTSB_RF_window = WindowObj:new()    -- Top Stats Bar (RF)
  36.     oTSB_RF_initWindow = false
  37.  
  38.     oTSB_fuel_window = WindowObj:new()  -- Top Stats Bar (fuel usage)
  39.     oTSB_fuel_initWindow = false
  40.  
  41.     oTSB_rods_window = WindowObj:new()  -- Top Stats Bar (rod insertion)
  42.     oTSB_rods_initWindow = false
  43.  
  44.     oBG_window = WindowObj:new()   -- Battery Gauge
  45.     oBG_initWindow = false
  46.     oBG_initGauge = false
  47.  
  48.     initDone = true
  49. end
  50.  
  51. function calcParams(baseGain, baseResetTime, baseDerivative, updateRate)
  52.     local x, y, z = reactor:getSize()
  53.     local rods = reactor:getRodCount()
  54.     local size = x*y*z
  55.     local resetTime = 0
  56.     local derivative = 0
  57.     local rateModifier = 1/(updateRate*10) -- tickrate will screw us over otherwise.
  58.  
  59.     local gain = ((baseGain/245/5)*size*rods) * rateModifier
  60.  
  61.     if baseResetTime >= 0 then
  62.         resetTime = ((baseResetTime/245*5)*size/rods) * rateModifier
  63.     else
  64.         resetTime = 0
  65.     end
  66.  
  67.     if baseDerivative >= 0 then
  68.         derivative = ((baseDerivative/245/5)*size*rods) * rateModifier
  69.     else
  70.         derivative = 0  
  71.     end
  72.  
  73.     return gain, resetTime, derivative
  74. end
  75.  
  76. function topStatsBar(screen) --oTSB_???_window
  77.     local w, h = screen:getSize()
  78.     local mon = screen:getMonitor()
  79.  
  80.     -- rf production
  81.     local initWindow_RF = function()
  82.         oTSB_RF_window:setMonitor(mon)
  83.         oTSB_RF_window:setColorBorder(colors.cyan)
  84.         oTSB_RF_window:setName("Power")
  85.  
  86.         oTSB_RF_initWindow  = true
  87.     end
  88.  
  89.     if not oTSB_RF_initWindow then initWindow_RF() end
  90.  
  91.     oTSB_RF_window:setX(2)
  92.     oTSB_RF_window:setY(2)
  93.     oTSB_RF_window:setW((w/4)-3)
  94.     oTSB_RF_window:setH(4)    
  95.  
  96.     oTSB_RF_window:draw()
  97.  
  98.     oTSB_RF_window:write(comma(round(reactor:getEnergyProduced()),1), 2, 2)
  99.     local unitRF = "RF/t"
  100.     oTSB_RF_window:write(unitRF, (oTSB_RF_window:getW() - string.len(unitRF)) - 1, 2)
  101.  
  102.     -- fuel usage
  103.     local initWindow_fuel = function()
  104.         oTSB_fuel_window:setMonitor(mon)
  105.         oTSB_fuel_window:setColorBorder(colors.cyan)
  106.         oTSB_fuel_window:setName("Fuel Usage")
  107.  
  108.         oTSB_fuel_initWindow = true
  109.     end
  110.  
  111.     if not oTSB_fuel_initWindow then initWindow_fuel() end
  112.  
  113.     oTSB_fuel_window:setX((w/4)+2)
  114.     oTSB_fuel_window:setY(2)
  115.     oTSB_fuel_window:setW((w/4)-3)
  116.     oTSB_fuel_window:setH(4)
  117.  
  118.     oTSB_fuel_window:draw()
  119.  
  120.     oTSB_fuel_window:write(round(reactor:getFuelUsage(),3), 2, 2)
  121.     local unitFuel = "mB/t"
  122.     oTSB_fuel_window:write(unitFuel, (oTSB_fuel_window:getW() - string.len(unitFuel)) - 1, 2)
  123.  
  124.     -- rod insertion
  125.     local initWindow_rods = function()
  126.         oTSB_rods_window:setMonitor(mon)
  127.         oTSB_rods_window:setColorBorder(colors.cyan)
  128.         oTSB_rods_window:setName("Rods")
  129.  
  130.         oTSB_rods_initWindow = true
  131.     end
  132.  
  133.     if not oTSB_rods_initWindow then initWindow_rods() end
  134.  
  135.     oTSB_rods_window:setX((w/4)+2+(w/4))
  136.     oTSB_rods_window:setY(2)
  137.     oTSB_rods_window:setW((w/4)-3)
  138.     oTSB_rods_window:setH(4)
  139.  
  140.     oTSB_rods_window:draw()
  141.  
  142.     oTSB_rods_window:write(round(reactor:getRodLevel(),1), 2, 2)
  143.     local unitRods = "%"
  144.     oTSB_rods_window:write(unitRods, (oTSB_rods_window:getW() - string.len(unitRods)) - 1, 2)
  145. end
  146.  
  147. function bottomControlBar(screen) --oBCB_window
  148.     local w, h = screen:getSize()
  149.     local mon = screen:getMonitor()
  150.  
  151.     local initWindow = function()
  152.         oBCB_window:setMonitor(mon)
  153.         oBCB_window:setColorBorder(colors.cyan)
  154.         oBCB_window:setName("Control")
  155.  
  156.         oBCB_SP_window = WindowObj:new()
  157.         oBCB_SP_window:setMonitor(mon)
  158.         oBCB_SP_window:setName("Setpoint")
  159.         oBCB_SP_window:setParent(oBCB_window)
  160.  
  161.         oBCB_initWindow = true  
  162.     end
  163.  
  164.     local initButtons = function()
  165.         -- Reactor On/Off
  166.         butOnOff = ButtonObj:new(screen, 1, 1, 1, 2, "off", "on")
  167.         butOnOff:setParent(oBCB_window)
  168.         butOnOff:setFunction(buttonReactorActive)
  169.  
  170.         butSPInc = ButtonObj:new(screen, 1, 1, 1, 2, "+"..pSPIncrements.."%")
  171.         butSPInc:setParent(oBCB_SP_window)
  172.         butSPInc:setFunction(spIncrease)
  173.         butSPInc:setCanToggle(false)
  174.  
  175.         butSPDec = ButtonObj:new(screen, 1, 1, 1, 2, "-"..pSPIncrements.."%")
  176.         butSPDec:setParent(oBCB_SP_window)
  177.         butSPDec:setFunction(spDecrease)
  178.         butSPDec:setCanToggle(false)
  179.  
  180.         oBCB_initButtons = true
  181.     end
  182.  
  183.     if not oBCB_initWindow then initWindow() end
  184.     if not oBCB_initButtons then initButtons() end
  185.  
  186.     oBCB_window:setX(2)
  187.     oBCB_window:setY(oTSB_RF_window:getY()+oTSB_RF_window:getH() + 2)
  188.     oBCB_window:setW((w-3)-(w/4))
  189.     oBCB_window:setH(h - oBCB_window:getY() - 1)
  190.  
  191.  
  192.     local wH_1 = oBCB_window:getH()
  193.     local wW_1 = oBCB_window:getW()
  194.  
  195.  
  196.     oBCB_SP_window:setX(oBCB_SP_window:getParent():getW()*(3/4)-3)
  197.     oBCB_SP_window:setY(1)
  198.     oBCB_SP_window:setW(oBCB_SP_window:getParent():getW()/4)
  199.     oBCB_SP_window:setH(10)
  200.  
  201.     local wH_2 = oBCB_SP_window:getH()
  202.     local wW_2 = oBCB_SP_window:getW()
  203.  
  204.     -- button updates
  205.     butOnOff:setW((butOnOff:getParent():getW()/4)-3)
  206.     butOnOff:setActive(reactor:getActive())
  207.  
  208.     butSPInc:setX(2)
  209.     butSPInc:setY(2)
  210.     butSPInc:setW((butSPInc:getParent():getW()-4))
  211.     butSPInc:setLocked(settings.get("ctrl.setpoint") >= 90)
  212.  
  213.     butSPDec:setX(2)
  214.     butSPDec:setY(butSPInc:getH() + butSPInc:getY() + 2)
  215.     butSPDec:setW((butSPDec:getParent():getW()-4))
  216.     butSPDec:setLocked(settings.get("ctrl.setpoint") <= 10)
  217.  
  218.     oBCB_window:draw()
  219. end
  220.  
  221. function buttonReactorActive(x)
  222.     reactor:setActive(x or false)
  223. end
  224.  
  225. function spIncrease()
  226.     settings.set("ctrl.setpoint", settings.get("ctrl.setpoint") + pSPIncrements)
  227.  
  228.     if settings.get("ctrl.setpoint") >= 90 then
  229.         settings.set("ctrl.setpoint", 90)
  230.     end
  231.     settings.save()
  232. end
  233.  
  234. function spDecrease()
  235.     settings.set("ctrl.setpoint", settings.get("ctrl.setpoint") - pSPIncrements)
  236.  
  237.     if settings.get("ctrl.setpoint") <= 10 then
  238.         settings.set("ctrl.setpoint", 10)
  239.     end
  240.     settings.save()
  241. end
  242.  
  243. function batteryGauge(screen)
  244.     local w, h = screen:getSize()
  245.     local mon = screen:getMonitor()
  246.     local scale = screen:getScale()
  247.  
  248.     -- rf production
  249.     local initWindow_BG = function()
  250.         oBG_window:setMonitor(mon)
  251.         oBG_window:setColorBorder(colors.cyan)
  252.         oBG_window:setName("Battery")
  253.  
  254.         oBG_initWindow  = true
  255.     end
  256.  
  257.     local initGauge_BG = function()
  258.         oBG_gauge = GaugeObj:new(screen, 1, 1, 1, 2, 0, 100, "%")
  259.         oBG_gauge:setParent(oBG_window)
  260.         oBG_gauge:setColorBackground(colors.red)
  261.         oBG_gauge:setColorGauge(colors.green)
  262.  
  263.         oBG_initGauge = true
  264.     end
  265.  
  266.     if not oBG_initWindow then initWindow_BG() end
  267.     if not oBG_initGauge then initGauge_BG() end
  268.  
  269.     oBG_window:setX((w/4)+2+(w/2))
  270.     oBG_window:setY(2)
  271.     oBG_window:setW((w/4)-3)
  272.     oBG_window:setH(h - 3)
  273.  
  274.     oBG_gauge:setX((oBG_gauge:getParent():getW()*(2/5)+1))
  275.     oBG_gauge:setY(1)
  276.     oBG_gauge:setW(oBG_gauge:getParent():getW()*(3/5))
  277.     oBG_gauge:setH(oBG_gauge:getParent():getH())
  278.     oBG_gauge:setLevel(round(percentage(reactor:getEnergyCapacity(), reactor:getEnergyStored())))
  279.     oBG_gauge:setSetpoint(settings.get("ctrl.setpoint"))    
  280.     oBG_gauge:setMarkCount(math.floor((h*scale)*(1/4)))
  281.     --oBG_gauge:setMarkCount(4)
  282.  
  283.     oBG_window:draw()
  284. end
  285.  
  286. function screenLoop()
  287.     print("screen")
  288.     while true do
  289.         sleep(pRateScreen)
  290.         --term.clear()
  291.         --term.setCursorPos(1,1)
  292.         screen1:clear()
  293.  
  294.         --print(settings.get("ctrl.setpoint"))
  295.  
  296.         topStatsBar(screen1)
  297.         bottomControlBar(screen1)
  298.         batteryGauge(screen1)
  299.     end
  300. end
  301.  
  302. function ctrlLoop()
  303.     print("controller")
  304.     while true do
  305.         sleep(pRateCtrl)
  306.         controller:setSetpoint(settings.get("ctrl.setpoint"))
  307.         controller:setEnabled(reactor:getActive())
  308.         local pv = percentage(reactor:getEnergyCapacity(), reactor:getEnergyStored())
  309.         local cv = controller:update(pv)
  310.         reactor:setRods(cv)
  311.     end
  312. end
  313.  
  314. function monitorTouchLoop()
  315.     print("monitor")
  316.     sleep(1)
  317.     while true do
  318.         local event, side, x, y = os.pullEvent("monitor_touch")
  319.  
  320.         local checkBut = function(o, i)
  321.             o[i]:check(x, y)
  322.         end
  323.  
  324.         local loop1 = function(obj)
  325.             local i
  326.             for i = 1, table.getn(obj), 1 do
  327.                 --pcall(checkBut, obj, i)
  328.                 pcall(checkBut, obj, i)
  329.             end
  330.         end
  331.         -- bottom control bar
  332.  
  333.         local bcb = oBCB_window:getObjects()
  334.         local bcb_sp = oBCB_SP_window:getObjects()
  335.         loop1(bcb)
  336.         loop1(bcb_sp)
  337.     end
  338. end
  339.  
  340. function monitorResize()
  341.     os.pullEvent("monitor_resize")
  342.     screen1:updateSize()
  343. end
  344.  
  345. function userinputLoop()
  346.     sleep(2)
  347.     print("userinput")
  348.     while true do
  349.         local event, key, is_held = os.pullEvent("key")
  350.  
  351.         if key == 290 then --F1
  352.             butOnOff:setActive(not butOnOff.isActive, true)
  353.         end
  354.     end
  355. end
  356.  
  357. init()
  358.  
  359. if initDone then
  360.     local dummy = parallel.waitForAll(ctrlLoop, screenLoop, monitorTouchLoop, monitorResize, userinputLoop)
  361. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement