From f6dd0bd669a5408c98ed67c898c5d4ed0ee74667 Mon Sep 17 00:00:00 2001 From: itzmarkoni Date: Fri, 12 Dec 2025 14:29:57 -0500 Subject: [PATCH] updated --- consumer/chat.lua | 1 - consumer/main.lua | 8 ++- kernel.lua | 16 ++++- program-driver.lua | 92 ---------------------------- programs/chat.lua | 25 ++++++++ programs/gps.lua | 24 ++++++++ task-manager.lua | 146 +++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 215 insertions(+), 97 deletions(-) delete mode 100644 consumer/chat.lua delete mode 100644 program-driver.lua create mode 100644 programs/chat.lua create mode 100644 programs/gps.lua create mode 100644 task-manager.lua diff --git a/consumer/chat.lua b/consumer/chat.lua deleted file mode 100644 index b95f18c..0000000 --- a/consumer/chat.lua +++ /dev/null @@ -1 +0,0 @@ -print("Chat.lua loaded") \ No newline at end of file diff --git a/consumer/main.lua b/consumer/main.lua index b15b7c3..f73a86f 100644 --- a/consumer/main.lua +++ b/consumer/main.lua @@ -1,8 +1,9 @@ local kernel = require("kernel") +local taskManager = require("task-manager") local monitor = kernel.addDriver("monitor-driver") local data = kernel.addDriver("disk-driver") -local taskManager = kernel.addDriver("program-driver") -local chatProgram = kernel.addFolderDriver("consumer","chat") +local chat = kernel.addProgram("chat") +local gps = kernel.addProgram("gps") local function run() monitor.initialize() @@ -14,7 +15,8 @@ local function run() os.setComputerLabel(computerName) monitor.writeLine("Computer name: " .. computerName) - taskManager.addProgram("Coms", "chat.lua"); + taskManager.addProgram("Communication", chat); + taskManager.addProgram("GPS", gps); taskManager.listPrograms() end diff --git a/kernel.lua b/kernel.lua index 29bc839..d339928 100644 --- a/kernel.lua +++ b/kernel.lua @@ -1,3 +1,5 @@ + + local function addDriver(fileName) local extension = ".lua" local fullFile = fileName .. extension @@ -18,4 +20,16 @@ local function addFolderDriver(folder, fileName) return require(fileName) end -return { addDriver = addDriver, addFolderDriver = addFolderDriver } \ No newline at end of file +local function addProgram(fileName) + local extension = ".lua" + local fullFile = fileName .. extension + shell.execute("rm", fullFile) + local baseRoute = "https://git.astrocore.space/root/nova-corp/raw/branch/main/" + shell.execute("wget", baseRoute .. "programs" .. "/" .. fullFile) + sleep(2) + return require(fileName) +end + +addDriver("task-manager") + +return { addDriver = addDriver, addFolderDriver = addFolderDriver, addProgram = addProgram } \ No newline at end of file diff --git a/program-driver.lua b/program-driver.lua deleted file mode 100644 index 5b43b4e..0000000 --- a/program-driver.lua +++ /dev/null @@ -1,92 +0,0 @@ -local monitor = peripheral.find("monitor") -local programs = {} -local isRunning = false - -local function addProgram(name, path) - table.insert(programs, {name = name, path = path}) -end - -local function runProgram(path) - if fs.exists(path) then - local w, h = monitor.getSize() - - monitor.setBackgroundColor(colors.black) - monitor.clear() - - -- Draw X button - monitor.setCursorPos(w, 1) - monitor.setBackgroundColor(colors.red) - monitor.setTextColor(colors.white) - monitor.write("X") - monitor.setBackgroundColor(colors.black) - - -- Create window for program - local win = window.create(monitor, 1, 2, w, h - 1) - local oldTerm = term.redirect(win) - - local function run() - shell.run(path) - end - - local function watchExit() - while true do - local _, _, x, y = os.pullEvent("monitor_touch") - if x == w and y == 1 then - return - end - end - end - - parallel.waitForAny(run, watchExit) - - term.redirect(oldTerm) - return true - else - print("Program not found: " .. path) - return false - end -end - -local function exitProgram() - isRunning = false -end - -local function drawList() - monitor.clear() - monitor.setCursorPos(1, 1) - monitor.write("Available Programs:") - - for i, program in ipairs(programs) do - monitor.setCursorPos(1, i + 2) -- Start from line 3 - monitor.write(i .. ". " .. program.name) - end - - monitor.setCursorPos(1, #programs + 4) - monitor.write("Click to run") -end - -local function listPrograms() - isRunning = true - while isRunning do - drawList() - - local event, side, x, y = os.pullEvent("monitor_touch") - local programIndex = y - 2 - - if programIndex > 0 and programIndex <= #programs then - local program = programs[programIndex] - monitor.clear() - monitor.setCursorPos(1,1) - monitor.write("Running " .. program.name .. "...") - sleep(1) - runProgram(program.path) - end - end -end - -return { - addProgram = addProgram, - runProgram = runProgram, - exitProgram = exitProgram, - listPrograms = listPrograms -} diff --git a/programs/chat.lua b/programs/chat.lua new file mode 100644 index 0000000..4f38169 --- /dev/null +++ b/programs/chat.lua @@ -0,0 +1,25 @@ +local function start() + print("Chat program started.") +end + +local function stop() + print("Chat program stopped.") +end + +local function restart() + stop() + start() +end + +local function main() + print("Chat main loop running...") + while true do + local event = os.pullEvent() + -- Chat logic here + if event == "char" then + print("Char typed") + end + end +end + +return { start = start, stop = stop, restart = restart, main = main } diff --git a/programs/gps.lua b/programs/gps.lua new file mode 100644 index 0000000..1c2b63c --- /dev/null +++ b/programs/gps.lua @@ -0,0 +1,24 @@ +local function start() + print("GPS program started.") +end + +local function stop() + print("GPS program stopped.") +end + +local function restart() + stop() + start() +end + +local function main() + print("Chat main loop running...") + while true do + local event = os.pullEvent() + if event == "char" then + print("GPS Char") + end + end +end + +return { start = start, stop = stop, restart = restart, main = main } diff --git a/task-manager.lua b/task-manager.lua new file mode 100644 index 0000000..1c1904b --- /dev/null +++ b/task-manager.lua @@ -0,0 +1,146 @@ +local monitor = peripheral.find("monitor") +local programs = {} +local isRunning = false + + +local function addProgram(name, program) + table.insert(programs, {name = name, program = program}) +end + +local function runProgram(programOrPath) + local programModule + + if type(programOrPath) == "string" then + if fs.exists(programOrPath) then + -- Load the file as a chunk. In Lua 5.2+ use the env parameter instead of setfenv. + local env = _ENV or _G + local programChunk, err = loadfile(programOrPath, "t", env) + if not programChunk then + print("Error loading program: " .. err) + return false + end + + -- Run the chunk to get the module table + local success, result = pcall(programChunk) + + if not success then + print("Error executing program file: " .. tostring(result)) + return false + end + programModule = result + else + print("Program not found: " .. programOrPath) + return false + end + elseif type(programOrPath) == "table" then + programModule = programOrPath + else + print("Invalid program type") + return false + end + + if type(programModule) ~= "table" or not programModule.main then + print("Error: Program must return a table with at least a 'main' function.") + return false + end + + -- Setup Monitor + local w, h = monitor.getSize() + monitor.setBackgroundColor(colors.black) + monitor.clear() + + -- Draw X button + monitor.setCursorPos(w, 1) + monitor.setBackgroundColor(colors.red) + monitor.setTextColor(colors.white) + monitor.write("X") + monitor.setBackgroundColor(colors.black) + + -- Create window for program + local win = window.create(monitor, 1, 2, w, h - 1) + local oldTerm = term.redirect(win) + + -- Lifecycle: START + if programModule.start then + local ok, err = pcall(programModule.start) + if not ok then + print("Error in start(): " .. tostring(err)) + term.redirect(oldTerm) + return false + end + end + + -- Lifecycle: MAIN (Run in parallel with exit watcher) + local function runMain() + -- We wrap main in pcall to catch errors without crashing the driver + local ok, err = pcall(programModule.main) + if not ok then + win.setTextColor(colors.red) + win.write("Runtime Error: " .. tostring(err)) + sleep(2) -- Let user see error + end + end + + local function watchExit() + while true do + local _, _, x, y = os.pullEvent("monitor_touch") + if x == w and y == 1 then + return + end + end + end + + parallel.waitForAny(runMain, watchExit) + + -- Lifecycle: STOP + if programModule.stop then + pcall(programModule.stop) + end + + term.redirect(oldTerm) + return true +end + +local function exitProgram() + isRunning = false +end + +local function drawList() + monitor.clear() + monitor.setCursorPos(1, 1) + monitor.write("Available Programs:") + + for i, program in ipairs(programs) do + monitor.setCursorPos(1, i + 2) -- Start from line 3 + monitor.write(i .. ". " .. program.name) + end + + monitor.setCursorPos(1, #programs + 4) + monitor.write("Click to run") +end + +local function listPrograms() + isRunning = true + while isRunning do + drawList() + + local event, side, x, y = os.pullEvent("monitor_touch") + local programIndex = y - 2 + + if programIndex > 0 and programIndex <= #programs then + local program = programs[programIndex] + monitor.clear() + monitor.setCursorPos(1,1) + monitor.write("Running " .. program.name .. "...") + sleep(0.5) + runProgram(program.program) + end + end +end + +return { + addProgram = addProgram, + runProgram = runProgram, + exitProgram = exitProgram, + listPrograms = listPrograms +}