Files
BarotraumaLuaProjects/atlas_os/AtlasOS.lua

113 lines
3.4 KiB
Lua
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
-- CLI бортового компьютера AtlasOS v1.1
-- Вход: пин 1 - команда; остальные пины не используются
-- Выходы: 1 - текст, 2 - очистка (1), 3 - цвет (R,G,B)
-- Глобальный цвет по умолчанию (зелёный)
local defaultColor = "0,255,0"
local currentColor = defaultColor
-- Устанавливаем начальный цвет
out[3] = currentColor
-- Таблица команд
local commands = {}
-- Регистрация команды
local function register_command(name, func, description)
commands[name] = { func = func, desc = description }
end
-- Вывод обычного текста с текущим цветом
local function print(text)
out[3] = currentColor
out[1] = text
end
-- Вывод текста ошибки (красный) с последующим возвратом цвета
local function printerror(text)
out[3] = "255,40,40"
out[1] = text
out[3] = currentColor
end
-- Обработчик команд
local function process_command(cmd_str)
local parts = {}
for token in tostring(cmd_str):gmatch("%S+") do
table.insert(parts, token)
end
local cmd = parts[1]
local args = {}
for i = 2, #parts do
table.insert(args, parts[i])
end
local cmd_entry = commands[cmd]
if cmd_entry then
local success, result = pcall(cmd_entry.func, args)
if success then
if result ~= nil then
-- Команда вернула строку для вывода
print(result)
end
-- если result == nil, команда сама обработала вывод
else
printerror(tostring(result))
end
else
printerror("Error: Unknown command. Type 'help' for list.")
end
end
-- ---- Команды ----
-- help
register_command("help", function(args)
local help_text = "Available commands:\n"
for name, entry in pairs(commands) do
help_text = help_text .. " " .. name .. " - " .. entry.desc .. "\n"
end
return help_text
end, "Show this help")
-- echo
register_command("echo", function(args)
if #args == 0 then
error("echo requires text.") -- будет перехвачено pcall
end
return table.concat(args, " ")
end, "Echo the input text")
-- clear
register_command("clear", function(args)
out[2] = 1
return nil
end, "Clear the screen")
-- color (устанавливает цвет для последующих обычных выводов)
register_command("color", function(args)
if #args < 3 then
error("color requires 3 numbers (R G B)")
end
local r, g, b = tonumber(args[1]), tonumber(args[2]), tonumber(args[3])
if not (r and g and b) then
error("invalid color values")
end
r = math.max(0, math.min(255, math.floor(r)))
g = math.max(0, math.min(255, math.floor(g)))
b = math.max(0, math.min(255, math.floor(b)))
currentColor = r .. "," .. g .. "," .. b
print("Color set to " .. currentColor)
return nil -- print уже вывел сообщение
end, "Set text color (R G B)")
-- status
register_command("status", function(args)
return "AtlasOS v1.1 - All systems nominal."
end, "Show system status")
-- Входной сигнал
function inp(pin, val)
if pin == 1 then
process_command(val)
end
end