Advertisement
kaibochan

graph_testing.lua

Feb 24th, 2025 (edited)
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.53 KB | None | 0 0
  1. local gui = require("/apis/gui")
  2.  
  3. function main()
  4.     -- initialize display using above information
  5.     -- receive buffer object which elements can be added to
  6.     local monitor = peripheral.wrap("monitor_0")
  7.     monitor.setTextScale(0.5)
  8.     monitor.clear()
  9.     monitor.setCursorPos(1, 1)
  10.    
  11.     local main_display = gui.initializeDisplay(monitor)
  12.  
  13.     setupDisplays(main_display)  
  14.    
  15.     while true do
  16.         main_display:render()
  17.         gui.doEvents()
  18.     end
  19. end
  20.  
  21. function setupDisplays(display)
  22.     local text_container = gui.Element:new {
  23.         x = 1,
  24.         y = 1,
  25.         width = 16,
  26.         height = display.window.height - 2,
  27.         bg_color = colors.gray,
  28.     }
  29.     display.window:addElement(text_container)
  30.  
  31.     local filler_text = gui.Text:new {
  32.         x = 1,
  33.         y = 1,
  34.         width = text_container.width - 3,
  35.         height = text_container.height - 2,
  36.         bg_color = colors.white,
  37.  
  38.         text = "lorem ipsum dolor sit amet",
  39.         text_color = colors.black,
  40.     }
  41.     text_container:addElement(filler_text)
  42.  
  43.     local scroll_bar_container = gui.Element:new {
  44.         x = filler_text.width + 1,
  45.         y = 1,
  46.         width = 1,
  47.         height = text_container.height - 2,
  48.         bg_color = colors.lightGray,
  49.     }
  50.     text_container:addElement(scroll_bar_container)
  51.  
  52.     local scroll_up_button = gui.Element:new {
  53.         x = 0,
  54.         y = 0,
  55.         width = 1,
  56.         height = 1,
  57.         bg_color = colors.red,
  58.  
  59.         monitor_touch = function(self, e)
  60.             filler_text.scroll_offset = filler_text.scroll_offset - 1
  61.         end,
  62.     }
  63.     scroll_bar_container:addElement(scroll_up_button)
  64.  
  65.     local scroll_down_button = gui.Element:new {
  66.         x = 0,
  67.         y = scroll_bar_container.height - 1,
  68.         width = 1,
  69.         height = 1,
  70.         bg_color = colors.green,
  71.  
  72.         monitor_touch = function(self, e)
  73.             filler_text.scroll_offset = filler_text.scroll_offset + 1
  74.         end,
  75.     }
  76.     scroll_bar_container:addElement(scroll_down_button)
  77.  
  78.     local right_x = text_container.x + text_container.width + 1
  79.     local bar = gui.Bar:new {
  80.         x = right_x,
  81.         y = 1,
  82.         width = display.window.width - right_x - 1,
  83.         height = 2,
  84.         bg_color = colors.white,
  85.         fill_color = colors.green,
  86.         percent_filled = 0.0,
  87.     }
  88.     display.window:addElement(bar)
  89.  
  90.     local function updateBar()
  91.         if bar.percent_filled >= 1 then
  92.             bar.percent_filled = 0.0
  93.         end
  94.  
  95.         local timer_id = os.startTimer(0.1)
  96.         function bar:timer(e)
  97.             if e.id == timer_id then
  98.                 self.percent_filled = self.percent_filled + 0.01
  99.                 updateBar()
  100.             end
  101.         end
  102.     end
  103.  
  104.     updateBar()
  105.  
  106.     local bar_text = gui.Text:new {
  107.         x = 1,
  108.         y = 1,
  109.         width = 8,
  110.         height = 1,
  111.         bg_transparent = true,
  112.         text_color = colors.black,
  113.         text = "bar_text"
  114.     }
  115.     bar:addElement(bar_text)
  116.  
  117.     local data = {
  118.         {
  119.             u = 0,
  120.             v = 1,
  121.         },
  122.         {
  123.             u = 1,
  124.             v = 4,
  125.         },
  126.         {
  127.             u = 2,
  128.             v = 2,
  129.         },
  130.         {
  131.             u = 3,
  132.             v = 1,
  133.         },
  134.         {
  135.             u = 4,
  136.             v = 6,
  137.         },
  138.         {
  139.             u = 5,
  140.             v = 4,
  141.         },
  142.         {
  143.             u = 6,
  144.             v = 0.5,
  145.         },
  146.     }
  147.  
  148.     local data_file = fs.open("data.txt", "w")
  149.     local serial_data = textutils.serialise(data)
  150.     data_file.write(serial_data)
  151.     filler_text:setText(serial_data)
  152.     data_file.close()
  153.  
  154.     local graph = gui.Graph:new {
  155.         x = right_x,
  156.         y = 4,
  157.         width = display.window.width - right_x - 1,
  158.         height = display.window.height - 5,
  159.         bg_color = colors.white,
  160.        
  161.         data_source = "data.txt",
  162.         u_min = -0.4,
  163.         u_max = 7,
  164.         v_min = -1,
  165.         v_max = 6,
  166.         draw_axes = true,
  167.     }
  168.     graph:pullData()
  169.     display.window:addElement(graph)
  170.  
  171.     local u_min = graph.u_min
  172.     local v_min = graph.v_min
  173.     local function updateGraph()
  174.  
  175.         local timer_id = os.startTimer(0.05)
  176.         function graph:timer(e)
  177.             if e.id == timer_id then
  178.                 self.u_min = u_min + 2 * math.sin(os.clock())
  179.                 self.v_min = v_min + 1.3 * math.sin(0.8 * os.clock() + 0.2)
  180.                 updateGraph()
  181.             end
  182.         end
  183.     end
  184.  
  185.     updateGraph()
  186. end
  187.  
  188. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement