dynamicXblue

CC Energy Chart

Aug 19th, 2025 (edited)
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.34 KB | None | 0 0
  1. -- Create monitor in online emulator
  2. --periphemu.create("left", "monitor")
  3.  
  4. -- Find attached monitor
  5. local monitor = peripheral.find("monitor")
  6. -- Find attached modem
  7. local modem = peripheral.find("modem") or error("No modem attached", 0)
  8.  
  9. -- Set monitor scaling (0.5 superior)
  10. monitor.setTextScale(0.5)
  11.  
  12. -- Listening Channel
  13. modem.open(1)
  14.  
  15. -- Get the monitor size
  16. local width, height = monitor.getSize()
  17.  
  18.  
  19. -- Use the monitor sizing to calculate chart size
  20. local chartArea = {math.floor((0.25 * width) + 1), math.floor((0.05 *  height) + 1), math.floor((0.95 * width) + 1), math.floor((0.95 *  height) + 1)} -- x1,y1, x2,y2
  21. local chartWidth = chartArea[3] - chartArea[1]
  22. local yScale = (chartArea[4] - chartArea[2]) / 100
  23.  
  24. -- Element Size (approx 9px each looks good) subject to change
  25. local elementSize = 9
  26. print(chartWidth)
  27. -- Calculate array size
  28. local arraySize = math.floor(chartWidth / elementSize)
  29.  
  30. -- Basic Variables
  31. local secondArray = {}
  32. local tenSecondArray = {}
  33. local counter = 0
  34.  
  35. -- Function to clear the monitor
  36. function clearMonitor()
  37.     -- Clear Screen by painting it black
  38.     paintutils.drawFilledBox(1, 1, width, height, colors.black)
  39. end
  40.  
  41. -- Function to prep the monitor
  42. function prepMonitor()
  43.     -- Start Drawing on monitor
  44.     term.redirect(monitor)
  45.  
  46.     clearMonitor()
  47. end
  48.  
  49. -- Function to initialise arrays with 0
  50. function prepArray()
  51.     for i=1, arraySize do
  52.         table.insert(secondArray, 0)
  53.         table.insert(tenSecondArray, 0)
  54.     end
  55. end
  56.  
  57. -- Function to update parent arrays, updating average value of item
  58. function updateParentArray()
  59.     -- Last (most recent) item = average
  60.     tenSecondArray[arraySize] = (tenSecondArray[arraySize] + secondArray[arraySize]) / 2
  61. end
  62.  
  63. -- Function to draw main chaty body (white borders, and markers)
  64. function drawChartArea()
  65.     -- Lower marker (reactor turns on at 50% or lower, so 50%)
  66.     local actualY = ((1 - 0.5) * 100 * yScale) + chartArea[2]
  67.     paintutils.drawLine(chartArea[1], actualY, chartArea[3], actualY, colors.red)
  68.    
  69.     -- Higher marker (reactor turns off at 00% or above, so 80%)
  70.     local actualY = ((1 - 0.8) * 100 * yScale) + chartArea[2]
  71.     paintutils.drawLine(chartArea[1], actualY, chartArea[3], actualY, colors.green)
  72.  
  73.     -- white borders
  74.     paintutils.drawLine(chartArea[1], chartArea[2], chartArea[1], chartArea[4], colors.white)
  75.     paintutils.drawLine(chartArea[1], chartArea[4], chartArea[3], chartArea[4], colors.white)
  76. end
  77.  
  78. -- Function to draw chart content
  79. function chartArray(array)
  80.    
  81.     -- Clear chart to redraw everything
  82.     clearMonitor()
  83.  
  84.     -- Draw chart border, etc.
  85.     drawChartArea()
  86.    
  87.     -- For every item in the array (ignore first item)
  88.     for i=2, arraySize do
  89.  
  90.         -- Calculate the X and Y position
  91.         local actualX = ((i-1) * elementSize) + chartArea[1]
  92.         local actualY = ((1 - array[i]) * 100 * yScale) + chartArea[2]
  93.  
  94.         -- Calculate the previous Y positon
  95.         local prevY = ((1 - array[i-1]) * 100 * yScale) + chartArea[2]
  96.  
  97.         -- Y axis is flipped
  98.         -- If going down then red else green
  99.         col = prevY < actualY and colors.red or colors.green      
  100.  
  101.         -- Draw a line between current point and previous point
  102.         paintutils.drawLine(actualX, actualY, actualX - elementSize, prevY, col)
  103.        
  104.         -- Draw a pixel marker at the previous positon to display in front of the line
  105.         paintutils.drawPixel(actualX - elementSize, prevY, colors.white)
  106.     end
  107.  
  108.    
  109. end
  110.  
  111. -- Set up
  112. prepArray()
  113. prepMonitor()
  114.  
  115. -- Keep repeating
  116. while true do
  117.  
  118.     local event, side, channel, replyChannel, message, distance
  119.     repeat
  120.         event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message")
  121.     until channel == 1
  122.  
  123.     -- CHANGE ME
  124.     table.remove(secondArray, 1)
  125.     table.insert(secondArray, message)
  126.     --
  127.    
  128.     --print(message)
  129.  
  130.     -- Update bigger arrays
  131.     updateParentArray()
  132.    
  133.     -- If the counter is 10, then shift the 10 Seconds Interval Array
  134.     if counter % 10 == 0 then
  135.         table.remove(tenSecondArray, 1)
  136.         table.insert(tenSecondArray, secondArray[arraySize])
  137.     end
  138.  
  139.     -- Chart the arrays
  140.     --chartArray(tenSecondArray)
  141.     chartArray(secondArray)
  142.    
  143.     -- Increment counter
  144.     counter = counter + 1
  145.     -- Sleep 1s
  146.     sleep(1)
  147. end
  148.  
  149.  
  150.  
Advertisement
Add Comment
Please, Sign In to add comment