Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local component = require("component")
- local computer = require("computer")
- local event = require("event")
- local fs = require("filesystem")
- local function loadCommands(filePath)
- local commands = {}
- if not fs.exists(filePath) then
- return commands
- end
- local file = io.open(filePath, "r")
- for line in file:lines() do
- local freq, time, state = line:match("([^,]+),([^,]+),([^,]+)")
- if freq and time and state then
- table.insert(commands, {tonumber(freq), tonumber(time), state})
- end
- end
- file:close()
- return commands
- end
- local function playCommands(commands)
- local lastTime = 0
- local startTime = computer.uptime()
- for i, command in ipairs(commands) do
- local currentTime = computer.uptime()
- local elapsedTime = currentTime - startTime
- local freq, time, state = command[1], command[2], command[3]
- local delay = time - lastTime
- while elapsedTime < time do
- os.sleep(0.05)
- elapsedTime = computer.uptime() - startTime
- end
- if state == 'on' then
- computer.beep(freq, 0.05) -- Включаем ноту на короткий промежуток времени
- end
- lastTime = time
- -- Вставляем вызов event.pull(0) каждые 10 команд, чтобы избежать ошибки "too long without yielding"
- if i % 10 == 0 then
- event.pull(0)
- end
- end
- end
- local commands = loadCommands("/home/commands.txt")
- playCommands(commands)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement