PiXLFAIL

cc_timer_gui_1

May 10th, 2026
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.41 KB | None | 0 0
  1. -- =========================================
  2. -- Klasse 1: Config (Persistenz)
  3. -- =========================================
  4. local Config = {}
  5. Config.__index = Config
  6.  
  7. function Config:new(filename)
  8.     local obj = {
  9.         file = filename,
  10.         data = {
  11.             active = true,
  12.             invert = false,
  13.             onTime = 0.5,
  14.             offTime = 1.0
  15.         }
  16.     }
  17.     setmetatable(obj, Config)
  18.     return obj
  19. end
  20.  
  21. function Config:load()
  22.     if fs.exists(self.file) then
  23.         local f = fs.open(self.file, "r")
  24.         local loaded = textutils.unserialize(f.readAll())
  25.         f.close()
  26.         if type(loaded) == "table" then
  27.             for k, v in pairs(loaded) do
  28.                 self.data[k] = v
  29.             end
  30.         end
  31.     end
  32. end
  33.  
  34. function Config:save()
  35.     local f = fs.open(self.file, "w")
  36.     f.write(textutils.serialize(self.data))
  37.     f.close()
  38. end
  39.  
  40. -- =========================================
  41. -- Klasse 2: TimerCore (Logik & Hardware)
  42. -- =========================================
  43. local TimerCore = {}
  44. TimerCore.__index = TimerCore
  45.  
  46. function TimerCore:new(configRef)
  47.     local obj = {
  48.         config = configRef,
  49.         inSide = "bottom",
  50.         outSide = "top",
  51.         timerId = nil,
  52.         isLampOn = false
  53.     }
  54.     setmetatable(obj, TimerCore)
  55.     return obj
  56. end
  57.  
  58. function TimerCore:shouldRun()
  59.     if not self.config.data.active then return false end
  60.    
  61.     local rsInput = rs.getInput(self.inSide)
  62.     if self.config.data.invert then
  63.         return not rsInput -- Startet wenn Redstone AUS ist
  64.     else
  65.         return rsInput     -- Startet wenn Redstone AN ist
  66.     end
  67. end
  68.  
  69. function TimerCore:updateState()
  70.     if self:shouldRun() then
  71.         if not self.timerId then
  72.             -- Zyklus starten
  73.             self.isLampOn = true
  74.             rs.setOutput(self.outSide, true)
  75.             self.timerId = os.startTimer(self.config.data.onTime)
  76.         end
  77.     else
  78.         -- Stoppen und resetten
  79.         if self.timerId then
  80.             os.cancelTimer(self.timerId)
  81.             self.timerId = nil
  82.         end
  83.         self.isLampOn = false
  84.         rs.setOutput(self.outSide, false)
  85.     end
  86. end
  87.  
  88. function TimerCore:handleTimer(eventId)
  89.     if eventId == self.timerId then
  90.         if self.isLampOn then
  91.             -- Umschalten auf Aus-Phase
  92.             self.isLampOn = false
  93.             rs.setOutput(self.outSide, false)
  94.             self.timerId = os.startTimer(self.config.data.offTime)
  95.         else
  96.             -- Umschalten auf An-Phase
  97.             self.isLampOn = true
  98.             rs.setOutput(self.outSide, true)
  99.             self.timerId = os.startTimer(self.config.data.onTime)
  100.         end
  101.     end
  102. end
  103.  
  104. function TimerCore:cleanup()
  105.     rs.setOutput(self.outSide, false)
  106.     if self.timerId then os.cancelTimer(self.timerId) end
  107. end
  108.  
  109. -- =========================================
  110. -- Klasse 3: App (GUI & Event-Loop)
  111. -- =========================================
  112. local App = {}
  113. App.__index = App
  114.  
  115. function App:new()
  116.     local cfg = Config:new("timer_settings.txt")
  117.     cfg:load()
  118.    
  119.     local obj = {
  120.         config = cfg,
  121.         core = TimerCore:new(cfg),
  122.         running = true
  123.     }
  124.     setmetatable(obj, App)
  125.     return obj
  126. end
  127.  
  128. function App:drawGUI()
  129.     term.clear()
  130.     term.setCursorPos(1,1)
  131.     print("=== Redstone Timer GUI ===")
  132.     print("[1] Status   : " .. (self.config.data.active and "LÄUFT" or "PAUSIERT"))
  133.     print("[2] Invert.  : " .. (self.config.data.invert and "JA" or "NEIN"))
  134.     print("[3] An-Zeit  : " .. self.config.data.onTime .. " s")
  135.     print("[4] Aus-Zeit : " .. self.config.data.offTime .. " s")
  136.     print("[5] Beenden")
  137.     print("--------------------------")
  138.     print("Klicke oder drücke Zahl 1-5")
  139. end
  140.  
  141. function App:cycleValue(key)
  142.     if key == "1" then
  143.         self.config.data.active = not self.config.data.active
  144.     elseif key == "2" then
  145.         self.config.data.invert = not self.config.data.invert
  146.     elseif key == "3" then
  147.         self.config.data.onTime = self.config.data.onTime + 0.5
  148.         if self.config.data.onTime > 5.0 then self.config.data.onTime = 0.5 end
  149.     elseif key == "4" then
  150.         self.config.data.offTime = self.config.data.offTime + 0.5
  151.         if self.config.data.offTime > 10.0 then self.config.data.offTime = 0.5 end
  152.     elseif key == "5" then
  153.         self.running = false
  154.     end
  155.    
  156.     self.config:save()
  157.     self.core:updateState()
  158.     self:drawGUI()
  159. end
  160.  
  161. function App:run()
  162.     self:drawGUI()
  163.     self.core:updateState()
  164.    
  165.     while self.running do
  166.         local event, p1, p2, p3 = os.pullEvent()
  167.        
  168.         if event == "redstone" then
  169.             self.core:updateState()
  170.            
  171.         elseif event == "timer" then
  172.             self.core:handleTimer(p1)
  173.            
  174.         elseif event == "char" then
  175.             -- Tastatur-Input (1-5)
  176.             self:cycleValue(p1)
  177.            
  178.         elseif event == "mouse_click" then
  179.             -- Maus-Input (Advanced Computers)
  180.             -- p3 ist die Y-Koordinate (Zeile)
  181.             if p3 >= 2 and p3 <= 6 then
  182.                 self:cycleValue(tostring(p3 - 1))
  183.             end
  184.         end
  185.     end
  186.    
  187.     self.core:cleanup()
  188.     term.clear()
  189.     term.setCursorPos(1,1)
  190.     print("Timer beendet.")
  191. end
  192.  
  193. -- =========================================
  194. -- Main Execution
  195. -- =========================================
  196. local mainApp = App:new()
  197. mainApp:run()
Advertisement
Add Comment
Please, Sign In to add comment