Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- =========================================
- -- Klasse 1: Config (Persistenz)
- -- =========================================
- local Config = {}
- Config.__index = Config
- function Config:new(filename)
- local obj = {
- file = filename,
- data = {
- active = true,
- invert = false,
- onTime = 0.5,
- offTime = 1.0
- }
- }
- setmetatable(obj, Config)
- return obj
- end
- function Config:load()
- if fs.exists(self.file) then
- local f = fs.open(self.file, "r")
- local loaded = textutils.unserialize(f.readAll())
- f.close()
- if type(loaded) == "table" then
- for k, v in pairs(loaded) do
- self.data[k] = v
- end
- end
- end
- end
- function Config:save()
- local f = fs.open(self.file, "w")
- f.write(textutils.serialize(self.data))
- f.close()
- end
- -- =========================================
- -- Klasse 2: TimerCore (Logik & Hardware)
- -- =========================================
- local TimerCore = {}
- TimerCore.__index = TimerCore
- function TimerCore:new(configRef)
- local obj = {
- config = configRef,
- inSide = "bottom",
- outSide = "top",
- timerId = nil,
- isLampOn = false
- }
- setmetatable(obj, TimerCore)
- return obj
- end
- function TimerCore:shouldRun()
- if not self.config.data.active then return false end
- local rsInput = rs.getInput(self.inSide)
- if self.config.data.invert then
- return not rsInput -- Startet wenn Redstone AUS ist
- else
- return rsInput -- Startet wenn Redstone AN ist
- end
- end
- function TimerCore:updateState()
- if self:shouldRun() then
- if not self.timerId then
- -- Zyklus starten
- self.isLampOn = true
- rs.setOutput(self.outSide, true)
- self.timerId = os.startTimer(self.config.data.onTime)
- end
- else
- -- Stoppen und resetten
- if self.timerId then
- os.cancelTimer(self.timerId)
- self.timerId = nil
- end
- self.isLampOn = false
- rs.setOutput(self.outSide, false)
- end
- end
- function TimerCore:handleTimer(eventId)
- if eventId == self.timerId then
- if self.isLampOn then
- -- Umschalten auf Aus-Phase
- self.isLampOn = false
- rs.setOutput(self.outSide, false)
- self.timerId = os.startTimer(self.config.data.offTime)
- else
- -- Umschalten auf An-Phase
- self.isLampOn = true
- rs.setOutput(self.outSide, true)
- self.timerId = os.startTimer(self.config.data.onTime)
- end
- end
- end
- function TimerCore:cleanup()
- rs.setOutput(self.outSide, false)
- if self.timerId then os.cancelTimer(self.timerId) end
- end
- -- =========================================
- -- Klasse 3: App (GUI & Event-Loop)
- -- =========================================
- local App = {}
- App.__index = App
- function App:new()
- local cfg = Config:new("timer_settings.txt")
- cfg:load()
- local obj = {
- config = cfg,
- core = TimerCore:new(cfg),
- running = true
- }
- setmetatable(obj, App)
- return obj
- end
- function App:drawGUI()
- term.clear()
- term.setCursorPos(1,1)
- print("=== Redstone Timer GUI ===")
- print("[1] Status : " .. (self.config.data.active and "LÄUFT" or "PAUSIERT"))
- print("[2] Invert. : " .. (self.config.data.invert and "JA" or "NEIN"))
- print("[3] An-Zeit : " .. self.config.data.onTime .. " s")
- print("[4] Aus-Zeit : " .. self.config.data.offTime .. " s")
- print("[5] Beenden")
- print("--------------------------")
- print("Klicke oder drücke Zahl 1-5")
- end
- function App:cycleValue(key)
- if key == "1" then
- self.config.data.active = not self.config.data.active
- elseif key == "2" then
- self.config.data.invert = not self.config.data.invert
- elseif key == "3" then
- self.config.data.onTime = self.config.data.onTime + 0.5
- if self.config.data.onTime > 5.0 then self.config.data.onTime = 0.5 end
- elseif key == "4" then
- self.config.data.offTime = self.config.data.offTime + 0.5
- if self.config.data.offTime > 10.0 then self.config.data.offTime = 0.5 end
- elseif key == "5" then
- self.running = false
- end
- self.config:save()
- self.core:updateState()
- self:drawGUI()
- end
- function App:run()
- self:drawGUI()
- self.core:updateState()
- while self.running do
- local event, p1, p2, p3 = os.pullEvent()
- if event == "redstone" then
- self.core:updateState()
- elseif event == "timer" then
- self.core:handleTimer(p1)
- elseif event == "char" then
- -- Tastatur-Input (1-5)
- self:cycleValue(p1)
- elseif event == "mouse_click" then
- -- Maus-Input (Advanced Computers)
- -- p3 ist die Y-Koordinate (Zeile)
- if p3 >= 2 and p3 <= 6 then
- self:cycleValue(tostring(p3 - 1))
- end
- end
- end
- self.core:cleanup()
- term.clear()
- term.setCursorPos(1,1)
- print("Timer beendet.")
- end
- -- =========================================
- -- Main Execution
- -- =========================================
- local mainApp = App:new()
- mainApp:run()
Advertisement
Add Comment
Please, Sign In to add comment