camekommd

graphic

Dec 1st, 2020
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.89 KB | None | 0 0
  1. -----------------------------
  2. -- User Editable Variables --
  3. -----------------------------
  4.  
  5. modem_side = "front"
  6. monitor_side = "back"
  7. modem_channel = 101
  8. refresh_time = 2
  9. monitor_scale = 0.5
  10. show_percentage = false
  11. show_label = true
  12.  
  13. bars_num = 2
  14. label = {
  15. "MFSU 1",
  16. "MFSU 2"
  17. }
  18.  
  19.  
  20. --------------------------------------
  21. -- DO NOT MODIFY ANYTHING FROM HERE --
  22. --------------------------------------
  23.  
  24. -- set peripherals
  25. monitor = peripheral.wrap(monitor_side)
  26. modem = peripheral.wrap(modem_side)
  27. modem.open(modem_channel)
  28. monitor.setTextScale(monitor_scale)
  29.  
  30. -- initalize monitor
  31. monitor.setBackgroundColor(colors.black)
  32. monitor.clear()
  33.  
  34. data_table = {}
  35.  
  36. -- gettable: get data table from modem
  37. function gettable()
  38. local event, modemSide, senderChannel, replyChannel, message, senderDistance = os.pullEvent("modem_message")
  39. data_table = textutils.unserialize(message)
  40. end
  41.  
  42. -- color function
  43. --[[function getcolor(val)
  44. if val < 0.15 then
  45. return colors.red
  46. elseif val < 0.75 then
  47. return colors.orange
  48. elseif val < 1.5 then
  49. return colors.green
  50. else
  51. return colors.magenta
  52. end
  53. end
  54. --]]
  55.  
  56.  
  57. function drawGraph()
  58. width, height = monitor.getSize()
  59. local percentage_line = 0
  60.  
  61. -- calc bar_width
  62. bar_width = math.floor((width-bars_num+1) / bars_num)
  63. print("1:",bar_width)
  64.  
  65. if bars_num == 0 then
  66. return
  67. end
  68.  
  69. -- draw function
  70. for v=0,bars_num-1,1 do
  71. local bar_begin = (v * bar_width) + v + 1
  72. local bar_end = bar_begin + bar_width - 1
  73. print("2:",bar_begin)
  74. print("3:",bar_end)
  75.  
  76. percent = data_table[v+1] / 40000000
  77. percentage = height * percent
  78.  
  79. y = height - math.floor(percentage + 0.5)
  80.  
  81. --[[ percentage_draw
  82. if percentage_line == 1 then
  83. local round_percent = math.floor((percent*100) + 0.5)
  84. local text = round_percent.."%"
  85. local bar_pos = bar_begin + math.floor((bar_width-string.len(text))/2)
  86. monitor.setCursorPos(bar_pos,1)
  87. monitor.setBackgroundColor(colors.black)
  88. monitor.settextColor(colors.white)
  89.  
  90. monitor.write(text)
  91. end
  92. --]]
  93.  
  94. -- draw [i=draw height; j=draw width]
  95. for i=1,height do
  96. for j=bar_begin,bar_end do
  97. local c
  98. if i > y then
  99. c = colors.green
  100. else
  101. c = colors.black
  102. end
  103. monitor.setBackgroundColor(c)
  104. monitor.setCursorPos(j, i)
  105. monitor.write(" ")
  106. end
  107. end
  108.  
  109. -- label_draw
  110. if show_label == true then
  111. local text = label[v+1]
  112. local bar_pos = bar_begin + math.floor((bar_width-string.len(text))/2)
  113. monitor.setCursorPos(bar_pos,height)
  114. monitor.setTextColor(colors.white)
  115. monitor.write(text)
  116. end
  117.  
  118. -- v_how_many_bars
  119. if (v+1) >= bars_num then
  120. return
  121. end
  122. end
  123. end
  124.  
  125. -- while-loop
  126. while true do
  127. getvalue()
  128. drawGraph()
  129. sleep(refresh_time)
  130. end
Add Comment
Please, Sign In to add comment