EdvartTempest

Untitled

Apr 8th, 2025 (edited)
846
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.36 KB | None | 0 0
  1. --[[
  2.   Wolfe's Mekanism Induction Matrix Monitor для Display Link (Create)
  3.   Поместите компьютер рядом с Induction Port и Display Link, подключённым к Display Board/Sign.
  4.   Настройка: Через файл "config" или аргументы командной строки добавьте:
  5.     side_display = 'left' -- Сторона, где находится Display Link (опционально, для автоопределения)
  6. ]]
  7.  
  8. -- Настройки по умолчанию
  9. options = {
  10.   energy_type = 'FE',
  11.   update_frequency = 1,
  12.   text_scale = 1,
  13. }
  14.  
  15. -- ... (код загрузки конфигурации из файла и аргументов остаётся без изменений) ...
  16.  
  17. -- Автоопределение периферии
  18. for _, side in pairs(peripheral.getNames()) do
  19.   local pType = peripheral.getType(side)
  20.  
  21.   -- Автоопределение Display Link (Create)
  22.   if (pType == 'monitor') and (not options.side_display) then
  23.     options.side_display = side
  24.   end
  25.  
  26.   -- Автоопределение Induction Port (Mekanism)
  27.   if pType == 'inductionPort' and (not options.side_inductor) then
  28.     options.side_inductor = side
  29.   end
  30. end
  31.  
  32. -- Подключение к периферии
  33. displayLink = peripheral.find("monitor")
  34. inductor = peripheral.find("inductionPort")
  35.  
  36. -- Проверка подключения Display Link
  37. if not displayLink then
  38.   print('Error: Display Link not founded!')
  39.   print('Please Check, that:')
  40.   print('1. Monitor connect to computer')
  41.   print('2. Mod CC:C Bridge installed')
  42.   print('3. Config has correctly sides')
  43.   return
  44. end
  45.  
  46. function time (secs)
  47.   -- Prepare value
  48.   secs = math.floor(secs)
  49.  
  50.   -- Days
  51.   local weeks = math.floor(secs / 604800)
  52.   secs = secs - (604800 * weeks)
  53.  
  54.   -- Days
  55.   local days = math.floor(secs / 86400)
  56.   secs = secs - (86400 * days)
  57.  
  58.   -- Hours
  59.   local hours = math.floor(secs / 3600)
  60.   secs = secs - (3600 * hours)
  61.  
  62.   -- Minutes
  63.   local mins = math.floor(secs / 60)
  64.   secs = secs - (60 * mins)
  65.  
  66.   -- If we have more than 72h worth of storage, switch to week, day, hour format
  67.   if weeks > 0 then
  68.     return string.format('%dwk %dd %dh', weeks, days, hours)
  69.   elseif days >= 3 then
  70.     return string.format('%dd %dh', days, hours)
  71.   end
  72.  
  73.   -- Formatting to have trailing zeros on H:MM:SS
  74.   return string.format('%d:%02d:%02d', hours, mins, secs)
  75. end
  76.  
  77. -- Rounds number
  78. function rnd (val, dec)
  79.   local X = math.pow(10, dec)
  80.   return math.floor(val * X) / X
  81. end
  82.  
  83. -- Converts to percentage
  84. function pct (val, dec)
  85.   return rnd(100 * val, dec or 1) .. '%'
  86. end
  87.  
  88. -- Converts to readable power
  89. function pwr (val, dec)
  90.   local pre = ''
  91.   local suf = ''
  92.  
  93.   local is_neg = false
  94.   if val < 0 then
  95.     pre = '-'
  96.     is_neg = true
  97.     val = -val
  98.   end
  99.  
  100.   val = energy_function(val)
  101.  
  102.   if val > 1000 then
  103.     suf = 'k'
  104.     val = val / 1000
  105.   end
  106.  
  107.   if val > 1000 then
  108.     suf = 'M'
  109.     val = val / 1000
  110.   end
  111.  
  112.   if val > 1000 then
  113.     suf = 'G'
  114.     val = val / 1000
  115.   end
  116.  
  117.   if val > 1000 then
  118.     suf = 'T'
  119.     val = val / 1000
  120.   end
  121.  
  122.   return string.format('%s%s%s%s', pre, rnd(val, dec or 1), suf, energy_type)
  123. end
  124.  
  125. -- Checks induction port
  126. function check_connection ()
  127.   return inductor and inductor.getEnergy and inductor.getLastInput
  128. end
  129.  
  130. -- Detects energy type, sets energy function
  131. energy_type = options.energy_type
  132. energy_function = mekanismEnergyHelper[string.format('joulesTo%s', energy_type)]
  133.  
  134. -- Function not found, use default Joules and a stub
  135. if not energy_function then
  136.   energy_type = 'J'
  137.   energy_function = function (val) return val end
  138. end
  139. -- Буфер для текста (остаётся без изменений)
  140. buffer = {}
  141. function queue (text)
  142.   table.insert(buffer, text)
  143. end
  144.  
  145. function queuef (fmt, ...)
  146.   queue(string.format(fmt, ...))
  147. end
  148.  
  149. -- НОВАЯ функция вывода на Display Link
  150. function displayLink_flush ()
  151.   if not displayLink then
  152.     print('Display Link not connect')
  153.     return
  154.   end
  155.  
  156.   -- Очищаем дисплей
  157.   displayLink.clear()
  158.  
  159.   -- Выводим каждую строку из буфера
  160.   for lineNum, lineText in ipairs(buffer) do
  161.     displayLink.setCursorPos(1, lineNum)
  162.     displayLink.write(tostring(lineText))
  163.   end
  164.  
  165.   -- Обязательно обновляем физический дисплей
  166.  
  167.   -- Очищаем буфер
  168.   buffer = {}
  169. end
  170.  
  171. -- ... (функции time(), rnd(), pct(), pwr(), energy_function остаются БЕЗ ИЗМЕНЕНИЙ) ...
  172.  
  173. -- Проверка подключения Induction Port (остаётся)
  174. function check_connection ()
  175.   return inductor and inductor.getEnergy and inductor.getLastInput
  176. end
  177.  
  178. -- Основной цикл работы
  179. while not check_connection() do
  180.   -- Выводим сообщение об ошибке на Display Link
  181.   buffer = {}
  182.   queue('Induction_Port')
  183.   queue('Not finded')
  184.   queue('Check connect...')
  185.   displayLink_flush()
  186.  
  187.   os.sleep(options.update_frequency)
  188.  
  189.   -- Повторная попытка автоопределения
  190.   if not options.side_inductor then
  191.     for _, side in pairs(peripheral.getNames()) do
  192.       if peripheral.getType(side) == 'inductionPort' then
  193.         options.side_inductor = side
  194.         inductor = peripheral.wrap(options.side_inductor)
  195.       end
  196.     end
  197.   end
  198. end
  199.  
  200. -- Основной цикл отображения
  201. while true do
  202.   local status, err = pcall(function ()
  203.     -- Сбор данных (остаётся без изменений)
  204.     queue('Induction Matrix')
  205.     queue('====================')
  206.     queue('')
  207.     queuef('Energy : %s', pwr(inductor.getEnergy()))
  208.     queuef('Max Energy   : %s', pwr(inductor.getMaxEnergy()))
  209.     queuef('Percent   : %s', pct(inductor.getEnergyFilledPercentage()))
  210.     queue('')
  211.     queuef('Input    : %s', pwr(inductor.getLastInput()))
  212.     queuef('Output   : %s', pwr(inductor.getLastOutput()))
  213.     queuef('Max Trasfer: %s/t', pwr(inductor.getTransferCap()))
  214.     queue('')
  215.    
  216.     -- Баланс энергии
  217.     local balance_last = balance or inductor.getEnergy()
  218.     balance = inductor.getEnergy()
  219.     local balance_change = (balance - balance_last) / options.update_frequency
  220.    
  221.     if balance_change < 0 then
  222.       queuef('Change: %s/s', pwr(balance_change))
  223.     else
  224.       queuef('Change: %s/s', pwr(balance_change))
  225.     end
  226.  
  227.     -- Статус зарядки/разрядки
  228.     queue('Status:')
  229.     if balance_change > 0 then
  230.       local remaining_charge = inductor.getMaxEnergy() - inductor.getEnergy()
  231.       local seconds_remaining = remaining_charge / balance_change
  232.       queuef('Charging in %s', time(seconds_remaining))
  233.     elseif balance_change < 0 then
  234.       local remaining_charge = inductor.getEnergy()
  235.       local seconds_remaining = remaining_charge / -balance_change
  236.       queuef('Uncharging in %s', time(seconds_remaining))
  237.     else
  238.       queue('Stable')
  239.     end
  240.   end)
  241.  
  242.   -- Обработка ошибок
  243.   if not status then
  244.     buffer = {}
  245.     queue('Can`t Read')
  246.     queue('Please check Mekanism')
  247.     queue('====================')
  248.     queue(err)
  249.   end
  250.  
  251.   -- Вывод на Display Link
  252.   displayLink_flush()
  253.  
  254.   -- Пауза между обновлениями
  255.   os.sleep(options.update_frequency)
  256. end
Advertisement
Add Comment
Please, Sign In to add comment