Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function init()
- print("init")
- require("lib") -- common functions
- require("reactor") -- reactor object functions
- require("controller") -- PI controller
- require("monitor") -- monitor
- require("button")
- require("window") -- sub window elements
- require("gauge")
- --require("varUnit")
- controller = ControllerObj:new()
- reactor = ReactorObj:new()
- screen1 = MonitorObj:new(peripheral.find("monitor")[3], 0.5)
- settings.define("ctrl.setpoint", {"default = 50.0"})
- settings.save()
- pRateCtrl = 0.1 -- controller updaterate
- pRateScreen = 0.5
- pSPIncrements = 5
- pGain, pResetTime, pDerivative = calcParams(27.5, 1, 40, pRateCtrl)
- controller:setGain(pGain)
- controller:setResetTime(pResetTime)
- controller:setDerivative(pDerivative)
- controller:setDerivativeFilter(2)
- controller:setDerivativeMode(false)
- -- screen elemements
- oBCB_window = WindowObj:new() -- Bottom Control Bar
- oBCB_initWindow = false
- oBCB_initButtons= false
- oTSB_RF_window = WindowObj:new() -- Top Stats Bar (RF)
- oTSB_RF_initWindow = false
- oTSB_fuel_window = WindowObj:new() -- Top Stats Bar (fuel usage)
- oTSB_fuel_initWindow = false
- oTSB_rods_window = WindowObj:new() -- Top Stats Bar (rod insertion)
- oTSB_rods_initWindow = false
- oBG_window = WindowObj:new() -- Battery Gauge
- oBG_initWindow = false
- oBG_initGauge = false
- initDone = true
- end
- function calcParams(baseGain, baseResetTime, baseDerivative, updateRate)
- local x, y, z = reactor:getSize()
- local rods = reactor:getRodCount()
- local size = x*y*z
- local resetTime = 0
- local derivative = 0
- local rateModifier = 1/(updateRate*10) -- tickrate will screw us over otherwise.
- local gain = ((baseGain/245/5)*size*rods) * rateModifier
- if baseResetTime >= 0 then
- resetTime = ((baseResetTime/245*5)*size/rods) * rateModifier
- else
- resetTime = 0
- end
- if baseDerivative >= 0 then
- derivative = ((baseDerivative/245/5)*size*rods) * rateModifier
- else
- derivative = 0
- end
- return gain, resetTime, derivative
- end
- function topStatsBar(screen) --oTSB_???_window
- local w, h = screen:getSize()
- local mon = screen:getMonitor()
- -- rf production
- local initWindow_RF = function()
- oTSB_RF_window:setMonitor(mon)
- oTSB_RF_window:setColorBorder(colors.cyan)
- oTSB_RF_window:setName("Power")
- oTSB_RF_initWindow = true
- end
- if not oTSB_RF_initWindow then initWindow_RF() end
- oTSB_RF_window:setX(2)
- oTSB_RF_window:setY(2)
- oTSB_RF_window:setW((w/4)-3)
- oTSB_RF_window:setH(4)
- oTSB_RF_window:draw()
- oTSB_RF_window:write(comma(round(reactor:getEnergyProduced()),1), 2, 2)
- local unitRF = "RF/t"
- oTSB_RF_window:write(unitRF, (oTSB_RF_window:getW() - string.len(unitRF)) - 1, 2)
- -- fuel usage
- local initWindow_fuel = function()
- oTSB_fuel_window:setMonitor(mon)
- oTSB_fuel_window:setColorBorder(colors.cyan)
- oTSB_fuel_window:setName("Fuel Usage")
- oTSB_fuel_initWindow = true
- end
- if not oTSB_fuel_initWindow then initWindow_fuel() end
- oTSB_fuel_window:setX((w/4)+2)
- oTSB_fuel_window:setY(2)
- oTSB_fuel_window:setW((w/4)-3)
- oTSB_fuel_window:setH(4)
- oTSB_fuel_window:draw()
- oTSB_fuel_window:write(round(reactor:getFuelUsage(),3), 2, 2)
- local unitFuel = "mB/t"
- oTSB_fuel_window:write(unitFuel, (oTSB_fuel_window:getW() - string.len(unitFuel)) - 1, 2)
- -- rod insertion
- local initWindow_rods = function()
- oTSB_rods_window:setMonitor(mon)
- oTSB_rods_window:setColorBorder(colors.cyan)
- oTSB_rods_window:setName("Rods")
- oTSB_rods_initWindow = true
- end
- if not oTSB_rods_initWindow then initWindow_rods() end
- oTSB_rods_window:setX((w/4)+2+(w/4))
- oTSB_rods_window:setY(2)
- oTSB_rods_window:setW((w/4)-3)
- oTSB_rods_window:setH(4)
- oTSB_rods_window:draw()
- oTSB_rods_window:write(round(reactor:getRodLevel(),1), 2, 2)
- local unitRods = "%"
- oTSB_rods_window:write(unitRods, (oTSB_rods_window:getW() - string.len(unitRods)) - 1, 2)
- end
- function bottomControlBar(screen) --oBCB_window
- local w, h = screen:getSize()
- local mon = screen:getMonitor()
- local initWindow = function()
- oBCB_window:setMonitor(mon)
- oBCB_window:setColorBorder(colors.cyan)
- oBCB_window:setName("Control")
- oBCB_SP_window = WindowObj:new()
- oBCB_SP_window:setMonitor(mon)
- oBCB_SP_window:setName("Setpoint")
- oBCB_SP_window:setParent(oBCB_window)
- oBCB_initWindow = true
- end
- local initButtons = function()
- -- Reactor On/Off
- butOnOff = ButtonObj:new(screen, 1, 1, 1, 2, "off", "on")
- butOnOff:setParent(oBCB_window)
- butOnOff:setFunction(buttonReactorActive)
- butSPInc = ButtonObj:new(screen, 1, 1, 1, 2, "+"..pSPIncrements.."%")
- butSPInc:setParent(oBCB_SP_window)
- butSPInc:setFunction(spIncrease)
- butSPInc:setCanToggle(false)
- butSPDec = ButtonObj:new(screen, 1, 1, 1, 2, "-"..pSPIncrements.."%")
- butSPDec:setParent(oBCB_SP_window)
- butSPDec:setFunction(spDecrease)
- butSPDec:setCanToggle(false)
- oBCB_initButtons = true
- end
- if not oBCB_initWindow then initWindow() end
- if not oBCB_initButtons then initButtons() end
- oBCB_window:setX(2)
- oBCB_window:setY(oTSB_RF_window:getY()+oTSB_RF_window:getH() + 2)
- oBCB_window:setW((w-3)-(w/4))
- oBCB_window:setH(h - oBCB_window:getY() - 1)
- local wH_1 = oBCB_window:getH()
- local wW_1 = oBCB_window:getW()
- oBCB_SP_window:setX(oBCB_SP_window:getParent():getW()*(3/4)-3)
- oBCB_SP_window:setY(1)
- oBCB_SP_window:setW(oBCB_SP_window:getParent():getW()/4)
- oBCB_SP_window:setH(10)
- local wH_2 = oBCB_SP_window:getH()
- local wW_2 = oBCB_SP_window:getW()
- -- button updates
- butOnOff:setW((butOnOff:getParent():getW()/4)-3)
- butOnOff:setActive(reactor:getActive())
- butSPInc:setX(2)
- butSPInc:setY(2)
- butSPInc:setW((butSPInc:getParent():getW()-4))
- butSPInc:setLocked(settings.get("ctrl.setpoint") >= 90)
- butSPDec:setX(2)
- butSPDec:setY(butSPInc:getH() + butSPInc:getY() + 2)
- butSPDec:setW((butSPDec:getParent():getW()-4))
- butSPDec:setLocked(settings.get("ctrl.setpoint") <= 10)
- oBCB_window:draw()
- end
- function buttonReactorActive(x)
- reactor:setActive(x or false)
- end
- function spIncrease()
- settings.set("ctrl.setpoint", settings.get("ctrl.setpoint") + pSPIncrements)
- if settings.get("ctrl.setpoint") >= 90 then
- settings.set("ctrl.setpoint", 90)
- end
- settings.save()
- end
- function spDecrease()
- settings.set("ctrl.setpoint", settings.get("ctrl.setpoint") - pSPIncrements)
- if settings.get("ctrl.setpoint") <= 10 then
- settings.set("ctrl.setpoint", 10)
- end
- settings.save()
- end
- function batteryGauge(screen)
- local w, h = screen:getSize()
- local mon = screen:getMonitor()
- local scale = screen:getScale()
- -- rf production
- local initWindow_BG = function()
- oBG_window:setMonitor(mon)
- oBG_window:setColorBorder(colors.cyan)
- oBG_window:setName("Battery")
- oBG_initWindow = true
- end
- local initGauge_BG = function()
- oBG_gauge = GaugeObj:new(screen, 1, 1, 1, 2, 0, 100, "%")
- oBG_gauge:setParent(oBG_window)
- oBG_gauge:setColorBackground(colors.red)
- oBG_gauge:setColorGauge(colors.green)
- oBG_initGauge = true
- end
- if not oBG_initWindow then initWindow_BG() end
- if not oBG_initGauge then initGauge_BG() end
- oBG_window:setX((w/4)+2+(w/2))
- oBG_window:setY(2)
- oBG_window:setW((w/4)-3)
- oBG_window:setH(h - 3)
- oBG_gauge:setX((oBG_gauge:getParent():getW()*(2/5)+1))
- oBG_gauge:setY(1)
- oBG_gauge:setW(oBG_gauge:getParent():getW()*(3/5))
- oBG_gauge:setH(oBG_gauge:getParent():getH())
- oBG_gauge:setLevel(round(percentage(reactor:getEnergyCapacity(), reactor:getEnergyStored())))
- oBG_gauge:setSetpoint(settings.get("ctrl.setpoint"))
- oBG_gauge:setMarkCount(math.floor((h*scale)*(1/4)))
- --oBG_gauge:setMarkCount(4)
- oBG_window:draw()
- end
- function screenLoop()
- print("screen")
- while true do
- sleep(pRateScreen)
- --term.clear()
- --term.setCursorPos(1,1)
- screen1:clear()
- --print(settings.get("ctrl.setpoint"))
- topStatsBar(screen1)
- bottomControlBar(screen1)
- batteryGauge(screen1)
- end
- end
- function ctrlLoop()
- print("controller")
- while true do
- sleep(pRateCtrl)
- controller:setSetpoint(settings.get("ctrl.setpoint"))
- controller:setEnabled(reactor:getActive())
- local pv = percentage(reactor:getEnergyCapacity(), reactor:getEnergyStored())
- local cv = controller:update(pv)
- reactor:setRods(cv)
- end
- end
- function monitorTouchLoop()
- print("monitor")
- sleep(1)
- while true do
- local event, side, x, y = os.pullEvent("monitor_touch")
- local checkBut = function(o, i)
- o[i]:check(x, y)
- end
- local loop1 = function(obj)
- local i
- for i = 1, table.getn(obj), 1 do
- --pcall(checkBut, obj, i)
- pcall(checkBut, obj, i)
- end
- end
- -- bottom control bar
- local bcb = oBCB_window:getObjects()
- local bcb_sp = oBCB_SP_window:getObjects()
- loop1(bcb)
- loop1(bcb_sp)
- end
- end
- function monitorResize()
- os.pullEvent("monitor_resize")
- screen1:updateSize()
- end
- function userinputLoop()
- sleep(2)
- print("userinput")
- while true do
- local event, key, is_held = os.pullEvent("key")
- if key == 290 then --F1
- butOnOff:setActive(not butOnOff.isActive, true)
- end
- end
- end
- init()
- if initDone then
- local dummy = parallel.waitForAll(ctrlLoop, screenLoop, monitorTouchLoop, monitorResize, userinputLoop)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement