Advertisement
THEdarkkman

CC Mekanism Induction Battery Monitoring V2

May 10th, 2024
1,074
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.67 KB | Software | 0 0
  1. -- Terminal preparation
  2. term.clear()
  3. term.setCursorPos(1,1)
  4.  
  5. -- Monitor preparation
  6. local monitor = peripheral.find("monitor")
  7. if monitor then
  8.     monitor.setBackgroundColour(colors.gray)
  9.     monitor.clear()
  10.     monitor.setCursorPos(1,1)
  11. end
  12.  
  13. -- Configuration
  14. local config = {
  15.     isTestingModeEnabled = false
  16. }
  17. if config.isTestingModeEnabled then
  18.     print("Testing mode is enabled.")
  19. end
  20.  
  21. -- Variable declaration
  22. inductionPort = nil
  23. local energy
  24. local energySuffix
  25. local progressBar
  26. local monitorColor = colors.gray
  27. local pos1
  28. local pos2
  29.  
  30. -- Functions
  31.  
  32. -- Print text serialised
  33. local function printText(text)
  34.     print(textutils.serialise(text))
  35. end
  36.  
  37. -- Convert Joules to RF(Redstone Flux)
  38. local function convertToRF(amntEnergy)
  39.     return math.floor(amntEnergy / 2.5)
  40. end
  41.  
  42. -- Find suffix and convert value
  43. local function formatEnergyValue(val)
  44.     val = val + 1       -- bugProof
  45.     local suffix
  46.     local energy = 1    -- bugProof
  47.     if val < 1000 then
  48.         suffix = ''
  49.     elseif val < 1000000 then
  50.         energy = val / 1000
  51.         suffix = 'k'
  52.     elseif val < 1000000000 then
  53.         energy = val / 1000000
  54.         suffix = 'M'
  55.     elseif val < 1000000000000 then
  56.         energy = val / 1000000000
  57.         suffix = 'G'
  58.     else
  59.         energy = val / 1000000000000
  60.         suffix = 'T'
  61.     end
  62.     return suffix, energy
  63. end
  64.  
  65. -- Round a number to a specific number of decimal places
  66. local function roundToDecimal(number, decimalPlaces)
  67.     local powerOf10 = 10 ^ decimalPlaces
  68.     return math.floor(number * powerOf10 + 0.5) / powerOf10
  69. end
  70.  
  71. -- Monitor draw region
  72. local function drawRegion(pos1, pos2, monitorColor, monitor)
  73.     --[[
  74.     - pos1          Top-left coordinates
  75.     - pos2          Bottom-right corner coordinates
  76.     - monitorColor  Color you want to draw with
  77.     - monitor       Monitor you want to draw to
  78.     ]]
  79.     local x1, y1 = pos1[1], pos1[2]     -- Top-left corner coordinates
  80.     local x2, y2 = pos2[1], pos2[2]     -- Bottom-right corner coordinates
  81.     local numSpaces = y2 - y1 + 1       -- Calculate the number of blank spaces to write
  82.     if monitor and config.isTestingModeEnabled then
  83.         printText("Monitor is working")
  84.         printText("Color: " .. monitorColor)
  85.         printText("Drawing region at x1:" .. x1 .. " x2:"..x2.." y1:"..y1.." y2:"..y2)
  86.         printText("Drawed region is "..numSpaces.. " pixels long")
  87.     end
  88.     monitor.setBackgroundColour(monitorColor)  -- Set the color for the draw
  89.  
  90.     -- Loop through each line of the region
  91.     for y = y1, y2 do
  92.         -- Loop through each column of the region
  93.         for x = x1, x2 do
  94.             monitor.setCursorPos(x,y)
  95.             monitor.write(" ")
  96.         end
  97.     end
  98.     -- Reset the background color to the default
  99.     monitor.setBackgroundColour(colors.gray)
  100. end
  101.  
  102. -- Check for induction port in peripheral
  103. local function isInductionPort()
  104.     local name = "inductionPort"
  105.     for _, side in pairs(peripheral.getNames()) do
  106.         if name == peripheral.getType(side) then
  107.             printText("Found " .. name .. " on " .. side)
  108.             return peripheral.wrap(side)
  109.         end
  110.     end
  111.     print("There is no "..name .." in the peripheral list")
  112.     return false
  113. end
  114.  
  115. local function getCurrentEnergy(inductionPort)
  116.     local suffix, energy = formatEnergyValue(convertToRF(inductionPort.getEnergy()))
  117.     energy = roundToDecimal(energy, 2)
  118.     return suffix, energy
  119. end
  120.  
  121. local function getMaxEnergy(inductionPort)            
  122.     local suffix, energy = formatEnergyValue(convertToRF(inductionPort.getMaxEnergy()))
  123.     energy = roundToDecimal(energy, 1)
  124.     return suffix, energy
  125. end
  126.  
  127. local function getLastInput()
  128.     local suffix, energy = formatEnergyValue(convertToRF(inductionPort.getLastInput()))
  129.     energy = roundToDecimal(energy, 0)
  130.     return suffix, energy
  131. end
  132.  
  133. local function getLastOutput()
  134.     local suffix, energy  = formatEnergyValue(convertToRF(inductionPort.getLastOutput()))
  135.     energy = roundToDecimal(energy, 0)
  136.     return suffix, energy
  137. end
  138.  
  139. local function drawBar()
  140.     local monitorColor = colors.black
  141.     local pos1 = {1, 18}
  142.     local pos2 = {29, 19}
  143.     drawRegion(pos1, pos2, monitorColor, monitor)    
  144. end
  145.  
  146. local function drawProgressBar()
  147.     monitorColor = colors.red
  148.     progressBar = roundToDecimal(inductionPort.getEnergyFilledPercentage() * 28 + 1, 1)
  149.     pos1 = {1, 18}
  150.     pos2 = {progressBar, 19}
  151.     drawRegion(pos1, pos2, monitorColor, monitor)    
  152. end
  153.  
  154. -- Monitor updating
  155. -- Refresh and update the monitor each time the function is called
  156. local function monitorUpdateValue()
  157.     local energy
  158.     local energySuffix
  159.     local maxEnergy
  160.     local maxEnergySuffix
  161.     -- Title
  162.     monitor.setCursorPos(1,1)
  163.     monitor.write("INDUCTION BATTERY")
  164.     monitor.setCursorPos(1,2)
  165.     monitor.write("-----------------")
  166.     -- Stored energy
  167.     monitor.setCursorPos(1,3)
  168.     monitor.write("Energy")
  169.     monitor.setCursorPos(10,3)
  170.     monitor.write(":")
  171.     monitor.setCursorPos(11,3)
  172.     energySuffix, energy = getCurrentEnergy(inductionPort)
  173.     maxEnergySuffix, maxEnergy = getMaxEnergy(inductionPort)
  174.     monitor.write(energy ..energySuffix .."RF / " .. maxEnergy ..maxEnergySuffix .."RF")
  175.     -- Input
  176.     monitor.setCursorPos(1,4)
  177.     monitor.write("Input")
  178.     monitor.setCursorPos(10,4)
  179.     monitor.write(":")
  180.     monitor.setCursorPos(11,4)
  181.     energySuffix, energy = getLastInput()
  182.     monitor.write(energy .. energySuffix .. "RF")
  183.     -- Output
  184.     monitor.setCursorPos(1,5)
  185.     monitor.write("Output")
  186.     monitor.setCursorPos(10,5)
  187.     monitor.write(":")
  188.     monitor.setCursorPos(11,5)
  189.     energySuffix, energy = getLastOutput()
  190.     monitor.write(energy .. energySuffix .. "RF")
  191. end
  192. -----------------
  193.  
  194. -- Code
  195.  
  196. -- Assign the induction port object
  197. inductionPort = isInductionPort()
  198.  
  199. -- Testing
  200. if config.isTestingModeEnabled then
  201.     -- Test and printing
  202.     printText("Testing print:")
  203.     printText("color is " .. monitorColor)
  204.     printText(inductionPort.getEnergyFilledPercentage())
  205. end
  206. -- check if inductionPort is present
  207. if not inductionPort then -- if not, stop the program
  208.     print("There is no induction port, aborting.")
  209.     return
  210. else
  211.     -- Program start here
  212.    
  213.     -- Draw the initial bar outside the loop
  214.     drawBar()
  215.  
  216.     while true do
  217.         -- Main loop --
  218.  
  219.         -- Check if a monitor is present
  220.         if monitor then
  221.             -- Call monitoring
  222.             monitorUpdateValue()
  223.  
  224.             -- Draw the progression bar
  225.             drawProgressBar()
  226.         else
  227.             printText("No monitor is present, aborting")
  228.             return false
  229.         end
  230.  
  231.         os.sleep(1)
  232.     end
  233. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement