Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- Wolfe's Mekanism Induction Matrix Monitor для Display Link (Create)
- Поместите компьютер рядом с Induction Port и Display Link, подключённым к Display Board/Sign.
- Настройка: Через файл "config" или аргументы командной строки добавьте:
- side_display = 'left' -- Сторона, где находится Display Link (опционально, для автоопределения)
- ]]
- -- Настройки по умолчанию
- options = {
- energy_type = 'FE',
- update_frequency = 1,
- text_scale = 1,
- }
- -- ... (код загрузки конфигурации из файла и аргументов остаётся без изменений) ...
- -- Автоопределение периферии
- for _, side in pairs(peripheral.getNames()) do
- local pType = peripheral.getType(side)
- -- Автоопределение Display Link (Create)
- if (pType == 'monitor') and (not options.side_display) then
- options.side_display = side
- end
- -- Автоопределение Induction Port (Mekanism)
- if pType == 'inductionPort' and (not options.side_inductor) then
- options.side_inductor = side
- end
- end
- -- Подключение к периферии
- displayLink = peripheral.find("monitor")
- inductor = peripheral.find("inductionPort")
- -- Проверка подключения Display Link
- if not displayLink then
- print('Error: Display Link not founded!')
- print('Please Check, that:')
- print('1. Monitor connect to computer')
- print('2. Mod CC:C Bridge installed')
- print('3. Config has correctly sides')
- return
- end
- function time (secs)
- -- Prepare value
- secs = math.floor(secs)
- -- Days
- local weeks = math.floor(secs / 604800)
- secs = secs - (604800 * weeks)
- -- Days
- local days = math.floor(secs / 86400)
- secs = secs - (86400 * days)
- -- Hours
- local hours = math.floor(secs / 3600)
- secs = secs - (3600 * hours)
- -- Minutes
- local mins = math.floor(secs / 60)
- secs = secs - (60 * mins)
- -- If we have more than 72h worth of storage, switch to week, day, hour format
- if weeks > 0 then
- return string.format('%dwk %dd %dh', weeks, days, hours)
- elseif days >= 3 then
- return string.format('%dd %dh', days, hours)
- end
- -- Formatting to have trailing zeros on H:MM:SS
- return string.format('%d:%02d:%02d', hours, mins, secs)
- end
- -- Rounds number
- function rnd (val, dec)
- local X = math.pow(10, dec)
- return math.floor(val * X) / X
- end
- -- Converts to percentage
- function pct (val, dec)
- return rnd(100 * val, dec or 1) .. '%'
- end
- -- Converts to readable power
- function pwr (val, dec)
- local pre = ''
- local suf = ''
- local is_neg = false
- if val < 0 then
- pre = '-'
- is_neg = true
- val = -val
- end
- val = energy_function(val)
- if val > 1000 then
- suf = 'k'
- val = val / 1000
- end
- if val > 1000 then
- suf = 'M'
- val = val / 1000
- end
- if val > 1000 then
- suf = 'G'
- val = val / 1000
- end
- if val > 1000 then
- suf = 'T'
- val = val / 1000
- end
- return string.format('%s%s%s%s', pre, rnd(val, dec or 1), suf, energy_type)
- end
- -- Checks induction port
- function check_connection ()
- return inductor and inductor.getEnergy and inductor.getLastInput
- end
- -- Detects energy type, sets energy function
- energy_type = options.energy_type
- energy_function = mekanismEnergyHelper[string.format('joulesTo%s', energy_type)]
- -- Function not found, use default Joules and a stub
- if not energy_function then
- energy_type = 'J'
- energy_function = function (val) return val end
- end
- -- Буфер для текста (остаётся без изменений)
- buffer = {}
- function queue (text)
- table.insert(buffer, text)
- end
- function queuef (fmt, ...)
- queue(string.format(fmt, ...))
- end
- -- НОВАЯ функция вывода на Display Link
- function displayLink_flush ()
- if not displayLink then
- print('Display Link not connect')
- return
- end
- -- Очищаем дисплей
- displayLink.clear()
- -- Выводим каждую строку из буфера
- for lineNum, lineText in ipairs(buffer) do
- displayLink.setCursorPos(1, lineNum)
- displayLink.write(tostring(lineText))
- end
- -- Обязательно обновляем физический дисплей
- -- Очищаем буфер
- buffer = {}
- end
- -- ... (функции time(), rnd(), pct(), pwr(), energy_function остаются БЕЗ ИЗМЕНЕНИЙ) ...
- -- Проверка подключения Induction Port (остаётся)
- function check_connection ()
- return inductor and inductor.getEnergy and inductor.getLastInput
- end
- -- Основной цикл работы
- while not check_connection() do
- -- Выводим сообщение об ошибке на Display Link
- buffer = {}
- queue('Induction_Port')
- queue('Not finded')
- queue('Check connect...')
- displayLink_flush()
- os.sleep(options.update_frequency)
- -- Повторная попытка автоопределения
- if not options.side_inductor then
- for _, side in pairs(peripheral.getNames()) do
- if peripheral.getType(side) == 'inductionPort' then
- options.side_inductor = side
- inductor = peripheral.wrap(options.side_inductor)
- end
- end
- end
- end
- -- Основной цикл отображения
- while true do
- local status, err = pcall(function ()
- -- Сбор данных (остаётся без изменений)
- queue('Induction Matrix')
- queue('====================')
- queue('')
- queuef('Energy : %s', pwr(inductor.getEnergy()))
- queuef('Max Energy : %s', pwr(inductor.getMaxEnergy()))
- queuef('Percent : %s', pct(inductor.getEnergyFilledPercentage()))
- queue('')
- queuef('Input : %s', pwr(inductor.getLastInput()))
- queuef('Output : %s', pwr(inductor.getLastOutput()))
- queuef('Max Trasfer: %s/t', pwr(inductor.getTransferCap()))
- queue('')
- -- Баланс энергии
- local balance_last = balance or inductor.getEnergy()
- balance = inductor.getEnergy()
- local balance_change = (balance - balance_last) / options.update_frequency
- if balance_change < 0 then
- queuef('Change: %s/s', pwr(balance_change))
- else
- queuef('Change: %s/s', pwr(balance_change))
- end
- -- Статус зарядки/разрядки
- queue('Status:')
- if balance_change > 0 then
- local remaining_charge = inductor.getMaxEnergy() - inductor.getEnergy()
- local seconds_remaining = remaining_charge / balance_change
- queuef('Charging in %s', time(seconds_remaining))
- elseif balance_change < 0 then
- local remaining_charge = inductor.getEnergy()
- local seconds_remaining = remaining_charge / -balance_change
- queuef('Uncharging in %s', time(seconds_remaining))
- else
- queue('Stable')
- end
- end)
- -- Обработка ошибок
- if not status then
- buffer = {}
- queue('Can`t Read')
- queue('Please check Mekanism')
- queue('====================')
- queue(err)
- end
- -- Вывод на Display Link
- displayLink_flush()
- -- Пауза между обновлениями
- os.sleep(options.update_frequency)
- end
Advertisement
Add Comment
Please, Sign In to add comment