Advertisement
kk258966

startup

Apr 6th, 2024 (edited)
647
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.52 KB | None | 0 0
  1. -- Default settings, do not change
  2. local options = {
  3.   -- Unique identifier for this matrix on rednet, required for rednet functionality
  4.   rednet_identifier = '',
  5.  
  6.   -- Energy type being displayed (J, FE)
  7.   energy_type = 'FE',
  8.  
  9.   -- Update frequency, in seconds
  10.   update_frequency = 1,
  11.  
  12.   -- Text scale on the monitor
  13.   text_scale = 1,
  14.  
  15.   -- Output debug data to the computer's internal display
  16.   debug = true,
  17. }
  18.  
  19. --------------------------------------------------
  20. --- Helper functions
  21. --------------------------------------------------
  22.  
  23. --- Holds the current buffer of data being printed
  24. local machine_term = term.current()
  25. local print_buffer = {}
  26.  
  27. --- Writes data to the output monitor buffer
  28. function print_r (text)
  29.   table.insert(print_buffer, text)
  30. end
  31.  
  32. --- Writes formatted data to the output monitor buffer
  33. function print_f (format, ...)
  34.   print_r(string.format(format, ...))
  35. end
  36.  
  37. --- Writes the buffer into the output monitor
  38. function print_flush ()
  39.   if monitor then
  40.     -- Redirects writes to monitor (if any)
  41.     if monitor then
  42.       term.redirect(monitor)
  43.     end
  44.  
  45.     -- Clears terminal
  46.     term.clear()
  47.     term.setCursorPos(1, 1)
  48.  
  49.     -- Writes new data
  50.     print(table.concat(print_buffer or {}, '\n'))
  51.  
  52.     -- Redirects writes back to computer (if using monitor)
  53.     if monitor then
  54.       term.redirect(machine_term)
  55.     end
  56.   end
  57.  
  58.   -- Clears buffer
  59.   print_buffer = {}
  60. end
  61.  
  62. --- Writes debug info to the machine
  63. function debug (...)
  64.   if options.debug then
  65.     print(...)
  66.   end
  67. end
  68.  
  69.  
  70. function print_drawer_info (drawer_info)
  71.   print_r('Cake Production Control')
  72.   print_r('------------------')
  73.   print_r('')
  74.   print_f('Amount : %s', drawer_info.amount)
  75.   print_f('Production : %s', drawer_info.Cake_status)
  76.  
  77.  
  78. end
  79. -- Detects peripherals
  80. monitor = peripheral.find('monitor')
  81.  
  82. if monitor then
  83.   debug('Monitor detected, enabling output!')
  84.   monitor.setTextScale(options.text_scale)
  85. else
  86.   debug('No monitor detected, entering headless mode!')
  87.  
  88.   -- Makes sure we have a connected modem
  89.   if not modem then
  90.     error('No monitor or modem detected, cannot enter headless mode!')
  91.   end
  92. end
  93.  
  94.  
  95.  
  96.  
  97.  
  98. --------------------------------------------------
  99. --- Main runtime
  100. --------------------------------------------------
  101.  
  102. debug('Entering main loop...')
  103.  
  104.  
  105. while true do
  106.   local status, err = pcall(function ()
  107.     -- Attempts to auto-detect missing Drawer
  108.     if not drawer then
  109.       drawer = peripheral.find('functionalstorage:oak_1')
  110.  
  111.       -- Checks if it worked
  112.       if not drawer then
  113.         error('Drawer not connected!')
  114.       end
  115.     end
  116.   local item = drawer.getItemDetail(1) 
  117.   local Cake_status_cal
  118.  
  119.     --- This is our main information
  120.     local drawer_info = {
  121.       amount = item.count,
  122.       Cake_status = Cake_status_cal
  123.      
  124.     }
  125.  
  126.     if item.count < 1000 then
  127.    
  128.         redstone.setOutput("left", false)  
  129.         Cake_status_cal = "Production ongoing"
  130.     else
  131.         redstone.setOutput("left", true)
  132.         Cake_status_cal = "Drawer currently full"
  133.     end
  134.    
  135.  
  136.      
  137.     -- Prints the drawer information
  138.     print_drawer_info(drawer_info)
  139.   end)
  140.  
  141.   -- Checks for errors (might be disconnected)
  142.   if not status then
  143.     -- Clears buffer first
  144.     print_buffer = {}
  145.  
  146.     -- Shows error message
  147.     print_r('Error reading data')
  148.     print_r('Check connections.')
  149.     print_r('------------------')
  150.     print_r(err)
  151.   end
  152.  
  153.   -- Outputs text to screen
  154.   print_flush()
  155.  
  156.   -- Waits for next cycle
  157.   os.sleep(options.update_frequency)
  158. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement