Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Create monitor in online emulator
- --periphemu.create("left", "monitor")
- -- Find attached monitor
- local monitor = peripheral.find("monitor")
- -- Find attached modem
- local modem = peripheral.find("modem") or error("No modem attached", 0)
- -- Set monitor scaling (0.5 superior)
- monitor.setTextScale(0.5)
- -- Listening Channel
- modem.open(1)
- -- Get the monitor size
- local width, height = monitor.getSize()
- -- Use the monitor sizing to calculate chart size
- 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
- local chartWidth = chartArea[3] - chartArea[1]
- local yScale = (chartArea[4] - chartArea[2]) / 100
- -- Element Size (approx 9px each looks good) subject to change
- local elementSize = 9
- print(chartWidth)
- -- Calculate array size
- local arraySize = math.floor(chartWidth / elementSize)
- -- Basic Variables
- local secondArray = {}
- local tenSecondArray = {}
- local counter = 0
- -- Function to clear the monitor
- function clearMonitor()
- -- Clear Screen by painting it black
- paintutils.drawFilledBox(1, 1, width, height, colors.black)
- end
- -- Function to prep the monitor
- function prepMonitor()
- -- Start Drawing on monitor
- term.redirect(monitor)
- clearMonitor()
- end
- -- Function to initialise arrays with 0
- function prepArray()
- for i=1, arraySize do
- table.insert(secondArray, 0)
- table.insert(tenSecondArray, 0)
- end
- end
- -- Function to update parent arrays, updating average value of item
- function updateParentArray()
- -- Last (most recent) item = average
- tenSecondArray[arraySize] = (tenSecondArray[arraySize] + secondArray[arraySize]) / 2
- end
- -- Function to draw main chaty body (white borders, and markers)
- function drawChartArea()
- -- Lower marker (reactor turns on at 50% or lower, so 50%)
- local actualY = ((1 - 0.5) * 100 * yScale) + chartArea[2]
- paintutils.drawLine(chartArea[1], actualY, chartArea[3], actualY, colors.red)
- -- Higher marker (reactor turns off at 00% or above, so 80%)
- local actualY = ((1 - 0.8) * 100 * yScale) + chartArea[2]
- paintutils.drawLine(chartArea[1], actualY, chartArea[3], actualY, colors.green)
- -- white borders
- paintutils.drawLine(chartArea[1], chartArea[2], chartArea[1], chartArea[4], colors.white)
- paintutils.drawLine(chartArea[1], chartArea[4], chartArea[3], chartArea[4], colors.white)
- end
- -- Function to draw chart content
- function chartArray(array)
- -- Clear chart to redraw everything
- clearMonitor()
- -- Draw chart border, etc.
- drawChartArea()
- -- For every item in the array (ignore first item)
- for i=2, arraySize do
- -- Calculate the X and Y position
- local actualX = ((i-1) * elementSize) + chartArea[1]
- local actualY = ((1 - array[i]) * 100 * yScale) + chartArea[2]
- -- Calculate the previous Y positon
- local prevY = ((1 - array[i-1]) * 100 * yScale) + chartArea[2]
- -- Y axis is flipped
- -- If going down then red else green
- col = prevY < actualY and colors.red or colors.green
- -- Draw a line between current point and previous point
- paintutils.drawLine(actualX, actualY, actualX - elementSize, prevY, col)
- -- Draw a pixel marker at the previous positon to display in front of the line
- paintutils.drawPixel(actualX - elementSize, prevY, colors.white)
- end
- end
- -- Set up
- prepArray()
- prepMonitor()
- -- Keep repeating
- while true do
- local event, side, channel, replyChannel, message, distance
- repeat
- event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message")
- until channel == 1
- -- CHANGE ME
- table.remove(secondArray, 1)
- table.insert(secondArray, message)
- --
- --print(message)
- -- Update bigger arrays
- updateParentArray()
- -- If the counter is 10, then shift the 10 Seconds Interval Array
- if counter % 10 == 0 then
- table.remove(tenSecondArray, 1)
- table.insert(tenSecondArray, secondArray[arraySize])
- end
- -- Chart the arrays
- --chartArray(tenSecondArray)
- chartArray(secondArray)
- -- Increment counter
- counter = counter + 1
- -- Sleep 1s
- sleep(1)
- end
Advertisement
Add Comment
Please, Sign In to add comment