132 lines
3.1 KiB
Lua
132 lines
3.1 KiB
Lua
|
|
-- peripheral identification
|
|
--
|
|
|
|
local sides = {
|
|
"left",
|
|
"right",
|
|
"back",
|
|
"front",
|
|
"bottom",
|
|
}
|
|
|
|
function Identify(name, data)
|
|
if name == data then
|
|
return true
|
|
else
|
|
return false
|
|
end
|
|
end
|
|
|
|
function getPeripheral(id)
|
|
local names = peripheral.getNames()
|
|
for k, v in pairs(names) do
|
|
local Type = peripheral.getType(v)
|
|
local isType = Identify(id, Type)
|
|
if isType then
|
|
for _, t in pairs(sides) do
|
|
if v == t then
|
|
return peripheral.wrap(t)
|
|
end
|
|
end
|
|
end
|
|
end
|
|
return nil
|
|
end
|
|
|
|
function periphSearch(type)
|
|
local names = peripheral.getNames()
|
|
local i, name
|
|
for i, name in pairs(names) do
|
|
if peripheral.getType(name) == type then
|
|
return peripheral.wrap(name)
|
|
end
|
|
end
|
|
return nil
|
|
end
|
|
|
|
-- formatting
|
|
|
|
function format_int(number)
|
|
|
|
if number == nil then number = 0 end
|
|
|
|
local i, j, minus, int, fraction = tostring(number):find('([-]?)(%d+)([.]?%d*)')
|
|
-- reverse the int-string and append a comma to all blocks of 3 digits
|
|
int = int:reverse():gsub("(%d%d%d)", "%1,")
|
|
|
|
-- reverse the int-string back remove an optional comma and put the
|
|
-- optional minus and fractional part back
|
|
return minus .. int:reverse():gsub("^,", "") .. fraction
|
|
end
|
|
|
|
-- monitor related
|
|
|
|
--display text text on monitor, "mon" peripheral
|
|
function draw_text(mon, x, y, text, text_color, bg_color)
|
|
mon.monitor.setBackgroundColor(bg_color)
|
|
mon.monitor.setTextColor(text_color)
|
|
mon.monitor.setCursorPos(x,y)
|
|
mon.monitor.write(text)
|
|
end
|
|
|
|
function draw_text_right(mon, offset, y, text, text_color, bg_color)
|
|
mon.monitor.setBackgroundColor(bg_color)
|
|
mon.monitor.setTextColor(text_color)
|
|
mon.monitor.setCursorPos(mon.X-string.len(tostring(text))-offset,y)
|
|
mon.monitor.write(text)
|
|
end
|
|
|
|
function draw_text_lr(mon, x, y, offset, text1, text2, text1_color, text2_color, bg_color)
|
|
draw_text(mon, x, y, text1, text1_color, bg_color)
|
|
draw_text_right(mon, offset, y, text2, text2_color, bg_color)
|
|
end
|
|
|
|
--draw line on computer terminal
|
|
function draw_line(mon, x, y, length, color)
|
|
if length < 0 then
|
|
length = 0
|
|
end
|
|
mon.monitor.setBackgroundColor(color)
|
|
mon.monitor.setCursorPos(x,y)
|
|
mon.monitor.write(string.rep(" ", length))
|
|
end
|
|
|
|
function draw_line_y(mon, x, y, length, color)
|
|
|
|
if length < 0 then
|
|
length = 0
|
|
end
|
|
|
|
mon.monitor.setBackgroundColor(color)
|
|
for i=y, length do
|
|
mon.monitor.setCursorPos(x, i)
|
|
mon.monitor.write(" ")
|
|
end
|
|
|
|
end
|
|
|
|
--create progress bar
|
|
--draws two overlapping lines
|
|
--background line of bg_color
|
|
--main line of bar_color as a percentage of minVal/maxVal
|
|
function progress_bar(mon, x, y, length, minVal, maxVal, bar_color, bg_color)
|
|
draw_line(mon, x, y, length, bg_color) --backgoround bar
|
|
local barSize = math.floor((minVal/maxVal) * length)
|
|
draw_line(mon, x, y, barSize, bar_color) --progress so far
|
|
end
|
|
|
|
|
|
function firstSet(mon)
|
|
mon.monitor.setTextScale(1)
|
|
mon.monitor.setBackgroundColor(colors.black)
|
|
mon.monitor.clear()
|
|
mon.monitor.setCursorPos(1,1)
|
|
end
|
|
|
|
|
|
local monitor = peripheral.find("monitor")
|
|
if monitor == nil then
|
|
error("Monitor not found. Make sure it connected!")
|
|
end
|
|
monitor.setTextScale(0.5) |