Advertisement
kk258966

Power_control

Apr 13th, 2024 (edited)
810
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.89 KB | None | 0 0
  1. -- Default settings, do not change
  2. local options = {
  3.   -- Unique identifier for this matrix on rednet, required for rednet functionality
  4.   rednet_identifier = '',
  5.  
  6.   -- Energy type being displayed (J, FE)
  7.   energy_type = 'FE',
  8.  
  9.   -- Update frequency, in seconds
  10.   update_frequency = 1,
  11.  
  12.   -- Text scale on the monitor
  13.   text_scale = 1,
  14.  
  15.   -- Output debug data to the computer's internal display
  16.   debug = true,
  17. }
  18.  
  19. --------------------------------------------------
  20. --- Internal variables, DO NOT CHANGE
  21. --------------------------------------------------
  22.  
  23. --- This will be used as the installer source (Pastebin)
  24. local INSTALLER_ID = 'LMdUZY4Z'
  25.  
  26. --- Supported energy suffixes
  27. local energy_suffixes = { 'k', 'M', 'G', 'T', 'P' }
  28.  
  29. --- Supported time periods when converting seconds
  30. local time_periods = {
  31.   { 'weeks', 604800 },
  32.   { 'days', 86400 },
  33.   { 'hours', 3600 },
  34.   { 'minutes', 60 },
  35.   { 'seconds', 1 },
  36. }
  37.  
  38. --- This is our Induction Matrix, we'll auto-detect it later
  39. local cube = nil
  40.  
  41. --- This is our Monitor, we'll auto-detect it later
  42. local monitor = nil
  43.  
  44. --------------------------------------------------
  45. --- Helper functions
  46. --------------------------------------------------
  47.  
  48. --- Holds the current buffer of data being printed
  49. local machine_term = term.current()
  50. local print_buffer = {}
  51.  
  52. --- Writes data to the output monitor buffer
  53. function print_r (text)
  54.   table.insert(print_buffer, text)
  55. end
  56.  
  57. --- Writes formatted data to the output monitor buffer
  58. function print_f (format, ...)
  59.   print_r(string.format(format, ...))
  60. end
  61.  
  62. --- Writes the buffer into the output monitor
  63. function print_flush ()
  64.   if monitor then
  65.     -- Redirects writes to monitor (if any)
  66.     if monitor then
  67.       term.redirect(monitor)
  68.     end
  69.  
  70.     -- Clears terminal
  71.     term.clear()
  72.     term.setCursorPos(1, 1)
  73.  
  74.     -- Writes new data
  75.     print(table.concat(print_buffer or {}, '\n'))
  76.  
  77.     -- Redirects writes back to computer (if using monitor)
  78.     if monitor then
  79.       term.redirect(machine_term)
  80.     end
  81.   end
  82.  
  83.   -- Clears buffer
  84.   print_buffer = {}
  85. end
  86.  
  87. --- Writes debug info to the machine
  88. function debug (...)
  89.   if options.debug then
  90.     print(...)
  91.   end
  92. end
  93.  
  94. --- Rounds a number with N decimals
  95. function round_decimal (number, decimals)
  96.   local multiplier = math.pow(10, decimals or 0)
  97.   return math.floor(number * multiplier) / multiplier
  98. end
  99.  
  100. --- Rounds a percentage (0..1) to a number of decimals
  101. function round_percentage (number, decimals)
  102.   return ('%s%%'):format(round_decimal(100 * number, decimals or 1))
  103. end
  104.  
  105. function print_cube_info (cube_info)
  106.   print_r('Energy Production Control')
  107.   print_r('------------------')
  108.   print_r('')
  109.   print_f('Power : %s', energy_string(cube_info.energy_stored))
  110.   print_f('Limit : %s', energy_string(cube_info.energy_capacity))
  111.   print_f('Charge: %s', round_percentage(cube_info.energy_percentage))
  112.   print_r('')
  113.   print_f('Input : %s/t', energy_string(cube_info.io_input))
  114.   print_f('Output: %s/t', energy_string(cube_info.io_output))
  115.   print_f('Max IO: %s/t', energy_string(cube_info.io_capacity))
  116.   print_f('Stat: %s/t', cube_info.gen_stats)
  117.   print_r('')
  118.  
  119.  
  120. end
  121. -- Detects peripherals
  122. monitor = peripheral.find('monitor')
  123.  
  124. if monitor then
  125.   debug('Monitor detected, enabling output!')
  126.   monitor.setTextScale(options.text_scale)
  127. else
  128.   debug('No monitor detected, entering headless mode!')
  129.  
  130.  
  131. end
  132.  
  133.  
  134.  
  135.  
  136.  
  137. --------------------------------------------------
  138. --- Main runtime
  139. --------------------------------------------------
  140.  
  141. debug('Entering main loop...')
  142. --- This will be updated after every energy collection, it is used to calculate how much power is actually being added/removed from the system
  143. local energy_stored_previous = nil
  144.  
  145. while true do
  146.   local status, err = pcall(function ()
  147.     -- Attempts to auto-detect missing Cube
  148.     if not Cube then
  149.       Cube = peripheral.find('mekanism:ultimate_energy_cube')
  150.  
  151.       -- Checks if it worked
  152.       if not Cube then
  153.         error('Cube not connected!')
  154.       end
  155.     end
  156.    
  157.      --- Save status
  158.     local charge_status = 1
  159.  
  160.     --- This is our main information
  161.     local cube_info = {
  162.       energy_stored = cube.getEnergy(),
  163.       energy_capacity = cube.getMaxEnergy(),
  164.       energy_percentage = cube.getEnergyFilledPercentage(),
  165.       io_input = cube.getLastInput(),
  166.       io_output = cube.getLastOutput(),
  167.       io_capacity = cube.getTransferCap(),
  168.       gen_stats = gen_stats_input
  169.     }
  170.    
  171.      if charge_status == 1 then
  172.         if energy_capacity < 50 then
  173.             redstone.setOutput("right", false)
  174.             gen_stats_input = "Charging ..."
  175.             charge_status = 2
  176.             end
  177.         else
  178.             if energy_capacity > 90 then
  179.             redstone.setOutput("right", true)
  180.             gen_stats_input = "Idle"
  181.             charge_status = 1
  182.             end
  183.         end
  184.        
  185.        
  186.  
  187.      if not energy_stored_previous then
  188.       energy_stored_previous = cube_info.energy_stored
  189.     end
  190.    
  191.     -- Calculates power changes and adds them to our information
  192.     cube_info.change_interval = options.update_frequency
  193.     cube_info.change_amount = cube_info.energy_stored - energy_stored_previous
  194.     cube_info.change_amount_per_second = cube_info.change_amount / options.update_frequency
  195.    
  196.    
  197.     -- General stats
  198.     cube_info.is_charging = cube_info.change_amount > 0
  199.     cube_info.is_discharging = cube_info.change_amount < 0
  200.  
  201.     -- Sets the new "previous" value
  202.     energy_stored_previous = cube_info.energy_stored
  203.  
  204.      
  205.    -- Prints the matrix information
  206.     print_cube_infoinfo(cube_info)
  207.   end)
  208.  
  209.   -- Checks for errors (might be disconnected)
  210.   if not status then
  211.     -- Clears buffer first
  212.     print_buffer = {}
  213.  
  214.     -- Shows error message
  215.     print_r('Error reading data')
  216.     print_r('Check connections.')
  217.     print_r('------------------')
  218.     print_r(err)
  219.   end
  220.  
  221.   -- Outputs text to screen
  222.   print_flush()
  223.  
  224.   -- Waits for next cycle
  225.   os.sleep(options.update_frequency)
  226. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement