Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Configuration
- local CONFIG = {
- MONITOR_SIDE = "right",
- MATRIX_SIDE = "back",
- REDSTONE_SIDE = "front",
- UPDATE_RATE = 0.5,
- TEXT_SCALE = 0.5,
- START_THRESHOLD = 10.0, -- Start when power below 10%
- STOP_THRESHOLD = 90.0, -- Stop when power above 90%
- ENERGY_TYPE = 'FE',
- DEBUG = true, -- Add debug output
- DEFAULT_REDSTONE = true,
- }
- -- Colors
- local COLORS = {
- title = colors.yellow,
- text = colors.white,
- warning = colors.red,
- good = colors.lime,
- bar_empty = colors.gray,
- bar_filled = colors.red,
- header = colors.orange,
- charging = colors.lime,
- discharging = colors.orange
- }
- -- Energy conversion setup
- local energy_type = 'J'
- local energy_convert = function(energy) return energy end
- if mekanismEnergyHelper and mekanismEnergyHelper[('joulesTo%s'):format(CONFIG.ENERGY_TYPE)] then
- energy_type = CONFIG.ENERGY_TYPE
- energy_convert = mekanismEnergyHelper[('joulesTo%s'):format(CONFIG.ENERGY_TYPE)]
- end
- local currentRedstoneState = CONFIG.DEFAULT_REDSTONE
- -- Initialize peripherals
- local monitor = peripheral.wrap(CONFIG.MONITOR_SIDE)
- local matrix = peripheral.wrap(CONFIG.MATRIX_SIDE)
- -- Set up monitor
- if monitor then
- monitor.setTextScale(CONFIG.TEXT_SCALE)
- end
- -- Display buffer to prevent flicker
- local displayBuffer = {}
- local oldBuffer = {}
- function writeBuffer(x, y, text, color)
- displayBuffer[y] = displayBuffer[y] or {}
- for i = 1, #text do
- displayBuffer[y][x + i - 1] = {
- char = text:sub(i, i),
- color = color
- }
- end
- end
- function flushBuffer(monitor)
- for y, row in pairs(displayBuffer) do
- for x, cell in pairs(row) do
- local oldCell = oldBuffer[y] and oldBuffer[y][x]
- if not oldCell or oldCell.char ~= cell.char or oldCell.color ~= cell.color then
- monitor.setCursorPos(x, y)
- monitor.setTextColor(cell.color)
- monitor.write(cell.char)
- end
- end
- end
- oldBuffer = displayBuffer
- displayBuffer = {}
- end
- function formatEnergy(energy, decimals)
- decimals = decimals or 1
- local prefix = energy < 0 and '-' or ''
- local amount = energy_convert(math.abs(energy))
- local suffix = ''
- if amount >= 1000000000 then
- amount = amount / 1000000000
- suffix = 'G'
- elseif amount >= 1000000 then
- amount = amount / 1000000
- suffix = 'M'
- elseif amount >= 1000 then
- amount = amount / 1000
- suffix = 'k'
- end
- return string.format('%s%.1f%s%s', prefix, amount, suffix, energy_type)
- end
- function formatRate(rate)
- local prefix = rate < 0 and '-' or ''
- local amount = energy_convert(math.abs(rate))
- local suffix = ''
- if amount >= 1000000000 then
- amount = amount / 1000000000
- suffix = 'G'
- elseif amount >= 1000000 then
- amount = amount / 1000000
- suffix = 'M'
- elseif amount >= 1000 then
- amount = amount / 1000
- suffix = 'k'
- end
- return string.format('%s%.1f%s%s/t', prefix, amount, suffix, energy_type)
- end
- function formatTime(seconds)
- if seconds < 60 then
- return string.format("%ds", math.floor(seconds))
- elseif seconds < 3600 then
- return string.format("%dm %ds", math.floor(seconds/60), math.floor(seconds%60))
- else
- local hours = math.floor(seconds/3600)
- local minutes = math.floor((seconds%3600)/60)
- return string.format("%dh%dm", hours, minutes)
- end
- end
- function drawProgressBar(x, y, width, percentage, barColor)
- local filledWidth = math.floor((width * percentage) / 100)
- writeBuffer(x, y, string.rep("#", filledWidth) .. string.rep("-", width - filledWidth), barColor)
- end
- function getMatrixInfo()
- if not matrix then return nil end
- local info = {
- stored = matrix.getEnergy(),
- capacity = matrix.getMaxEnergy(),
- input = matrix.getLastInput(),
- output = matrix.getLastOutput(),
- transferCap = matrix.getTransferCap()
- }
- info.percentage = (info.stored / info.capacity) * 100
- info.change = info.input - info.output
- return info
- end
- function display(info, redstoneStatus)
- if not monitor or not info then return end
- -- Title
- writeBuffer(2, 1, "Matrix Monitor", COLORS.title)
- -- Storage section
- writeBuffer(2, 3, string.format("Storage: %d%%", math.floor(info.percentage)), COLORS.text)
- local barColor = info.percentage < 25 and COLORS.warning or COLORS.good
- drawProgressBar(2, 4, 30, info.percentage, barColor)
- -- Status section
- writeBuffer(2, 6, "Status", COLORS.header)
- writeBuffer(2, 7, string.format("Stored: %s", formatEnergy(info.stored)), COLORS.text)
- writeBuffer(2, 8, string.format("Capacity: %s", formatEnergy(info.capacity)), COLORS.text)
- writeBuffer(2, 9, string.format("Max IO: %s/t", formatEnergy(info.transferCap)), COLORS.text)
- -- Transfer section
- writeBuffer(2, 11, "Transfer Rates", COLORS.header)
- writeBuffer(2, 12, string.format("Input: %s", formatRate(info.input)), COLORS.charging)
- writeBuffer(2, 13, string.format("Output: %s", formatRate(info.output)), COLORS.discharging)
- writeBuffer(2, 14, string.format("Max: %s", formatRate(info.transferCap)), COLORS.text)
- -- Usage section
- writeBuffer(2, 16, "Usage Statistics", COLORS.header)
- local inputPercent = (info.input / info.transferCap) * 100
- local outputPercent = (info.output / info.transferCap) * 100
- writeBuffer(2, 17, string.format("Input: %.1f%%", inputPercent), COLORS.charging)
- writeBuffer(2, 18, string.format("Output: %.1f%%", outputPercent), COLORS.discharging)
- -- Time section
- writeBuffer(2, 20, "Time Estimates", COLORS.header)
- if info.change > 0 then
- local timeToFull = (info.capacity - info.stored) / (info.change * 20)
- writeBuffer(2, 21, "Full: " .. formatTime(timeToFull), COLORS.text)
- elseif info.change < 0 then
- local timeToEmpty = info.stored / (math.abs(info.change) * 20)
- writeBuffer(2, 21, "Empty: " .. formatTime(timeToEmpty), COLORS.warning)
- else
- writeBuffer(2, 21, "Stable", COLORS.text)
- end
- -- Control Status
- writeBuffer(2, 23, "Control Status", COLORS.header)
- writeBuffer(2, 24, "Output: " .. (redstoneStatus and "ON" or "OFF"),
- redstoneStatus and COLORS.charging or COLORS.discharging)
- flushBuffer(monitor)
- end
- -- Add this function for debug output
- function debugPrint(message)
- if CONFIG.DEBUG then
- print(message)
- end
- end
- -- Replace the updateRedstone function with this fixed version:
- function updateRedstone(percentage)
- debugPrint(string.format("Current power: %.1f%%", percentage))
- if type(percentage) ~= "number" then
- debugPrint("Invalid percentage value")
- return false
- end
- -- Turn ON redstone when power is LOW
- if percentage <= CONFIG.START_THRESHOLD then
- if not currentRedstoneState then
- debugPrint("Power low - Turning redstone ON")
- currentRedstoneState = true
- redstone.setOutput(CONFIG.REDSTONE_SIDE, true)
- end
- return currentRedstoneState
- -- Turn OFF redstone when power is HIGH
- elseif percentage >= CONFIG.STOP_THRESHOLD then
- if currentRedstoneState then
- debugPrint("Power high - Turning redstone OFF")
- currentRedstoneState = false
- redstone.setOutput(CONFIG.REDSTONE_SIDE, false)
- end
- return currentRedstoneState
- end
- -- Keep current state for values between thresholds
- debugPrint(string.format("Between thresholds - Keeping state: %s", currentRedstoneState and "ON" or "OFF"))
- return currentRedstoneState
- end
- -- Initialize redstone output
- print("Initializing redstone output...")
- currentRedstoneState = CONFIG.DEFAULT_REDSTONE
- redstone.setOutput(CONFIG.REDSTONE_SIDE, currentRedstoneState)
- debugPrint(string.format("Initial redstone state set to: %s", currentRedstoneState and "ON" or "OFF"))
- -- Main loop
- print("Starting Matrix Monitor with Redstone Control")
- local lastUpdate = 0
- local updateThreshold = 0.1 -- Minimum time between updates
- -- In the main loop, modify the redstone handling:
- while true do
- if matrix and monitor then
- local currentTime = os.epoch("local") / 1000
- if currentTime - lastUpdate >= updateThreshold then
- local info = getMatrixInfo()
- if info then
- local redstoneStatus = updateRedstone(info.percentage)
- debugPrint(string.format("Redstone status: %s", redstoneStatus and "ON" or "OFF"))
- display(info, redstoneStatus)
- end
- lastUpdate = currentTime
- end
- else
- -- Try to reconnect peripherals
- matrix = peripheral.wrap(CONFIG.MATRIX_SIDE)
- monitor = peripheral.wrap(CONFIG.MONITOR_SIDE)
- if monitor then
- monitor.setTextScale(CONFIG.TEXT_SCALE)
- monitor.clear()
- monitor.setCursorPos(1, 1)
- monitor.setTextColor(COLORS.warning)
- monitor.write("Reconnecting...")
- end
- end
- os.sleep(CONFIG.UPDATE_RATE)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement