-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstartup.lua
141 lines (117 loc) · 4.03 KB
/
startup.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
-- Redstone signal input side
INPUT_SIDE = "left"
-- Update interval in seconds
-- Has to be a multiple of 0.05 (1 game tick)
UPDATE_INTERVAL = 0.5
-- Colors for the tank level display
-- The first value is the tank level in percent, above which the color (second value) is used
-- Has to be sorted in descending order
-- If you only want a single solid color, set the first entry to 0 and only it will be used
LEVEL_COLORS = {
{ 80, colors.red },
{ 50, colors.yellow },
{ 0, colors.green },
}
-- Text that's displayed above the tank level
-- Can be 4 lines max, but up to 3 are recommended
-- When using 1 or 2 lines, they will be vertically centered
-- Use empty lines to offset the text
-- Whitespaces will use the FG color, missing characters will use the BG color
-- Each line can have 7 characters max
DISPLAY_TEXT = {
" Water ",
" Level ",
}
-- Background color of the text display
DISPLAY_TEXT_BG = colors.blue
-- Foreground (text) color of the text display
DISPLAY_TEXT_FG = colors.white
-- Default background color (behind the tank level bar)
DEFAULT_BG = colors.gray
-- Background color of the unfilled part of the tank level bar
UNFILLED_BAR_BG = colors.lightGray
-- DON'T EDIT BELOW THIS LINE --
local lastValue = nil
function updateDisplay()
local monitor = peripheral.find("monitor")
local value = redstone.getAnalogInput(INPUT_SIDE)
if value ~= lastValue then
-- #SECTION draw text
lastValue = value
monitor.setBackgroundColor(DEFAULT_BG)
monitor.clear()
monitor.setBackgroundColor(DISPLAY_TEXT_BG)
monitor.setTextColor(DISPLAY_TEXT_FG)
local yPos = 1
monitor.setCursorPos(1, yPos)
if tableLength(DISPLAY_TEXT) < 3 then
monitor.write(" ")
yPos = yPos + 1
monitor.setCursorPos(1, yPos)
end
for _, line in pairs(DISPLAY_TEXT) do
monitor.write(line)
yPos = yPos + 1
monitor.setCursorPos(1, yPos)
end
if tableLength(DISPLAY_TEXT) < 4 then
monitor.write(" ")
yPos = yPos + 1
monitor.setCursorPos(1, yPos)
end
if tableLength(DISPLAY_TEXT) < 2 then
monitor.write(" ")
yPos = yPos + 1
monitor.setCursorPos(1, yPos)
end
local level = math.floor(redstoneToPercent(value))
local spaces = level < 10 and " " or level < 100 and " " or ""
local percText = " "..spaces..level.." % "
monitor.setCursorPos(1, 5)
monitor.write(percText)
monitor.setCursorPos(1, 6)
monitor.write(" ")
-- #SECTION draw level bar
local barHeight = math.floor(value * (12 / 15))
local barColor = colors.black
for i, colData in ipairs(LEVEL_COLORS) do
local percent, col = colData[1], colData[2]
if level >= percent then
barColor = col
break
end
end
local filledIdx = 1
for i = 19, 8, -1 do
if filledIdx > barHeight then
monitor.setBackgroundColor(UNFILLED_BAR_BG)
else
monitor.setBackgroundColor(barColor)
end
monitor.setCursorPos(2, i)
monitor.write(" ")
filledIdx = filledIdx + 1
end
end
end
-- Converts a 0-15 redstone signal to a 0-100 percent value
-- Magnitude is the number of decimal places, as a power of 10
-- (1 = 1 decimal place, 10 = 2 decimal places, 100 = 3 decimal places, etc.)
function redstoneToPercent(value, magnitude)
if magnitude == nil then magnitude = 1 end
return math.floor(value * (100 / 15) * magnitude) / magnitude
end
function tableLength(table)
local count = 0
for _ in pairs(table) do count = count + 1 end
return count
end
function run()
print("\n| Tank Level Display by Sv443")
print("| https://github.com/Sv443/ComputerCraft-Projects\n")
while true do
updateDisplay()
os.sleep(UPDATE_INTERVAL)
end
end
run()