mah17

sorter.lua

Apr 21st, 2015
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.82 KB | None | 0 0
  1. -- Outputs on/off signal based on analog input
  2.  
  3. local AUTHOR    = 'nxuul'
  4. local TITLE     = 'analogwatch'
  5. local VERSION   = '0.1'
  6.  
  7. --------------------------------------------------------------------------------
  8. -- Static vars
  9.  
  10. ANALOG_INPUT_SIDE = 'top'
  11. OUTPUT_SIDE = 'bottom'
  12.  
  13. -- Values to enable and disable output at
  14. ANALOG_LOW_VAL = 0 -- 0 to 14
  15. ANALOG_HIGH_VAL = 12 -- 1 to 15
  16.  
  17. -- Setup colors
  18. local HEADER_BG_COLOR
  19. local HEADER_FG_COLOR
  20.  
  21. if term.isColor() then
  22.   HEADER_BG_COLOR = colors.blue
  23.   HEADER_FG_COLOR = colors.white
  24. else
  25.   HEADER_BG_COLOR = colors.white
  26.   HEADER_FG_COLOR = colors.black
  27. end
  28.  
  29. local args = {...}
  30.  
  31. --------------------------------------------------------------------------------
  32. -- Nifty/Neat functions
  33. function round(num, idp)
  34.     return tonumber(string.format("%." .. (idp or 0) .. 'f', num))
  35. end
  36.  
  37. --------------------------------------------------------------------------------
  38. --GUI Stuff
  39. function lAndRAligned(cWindow, left, right)
  40.   local maxx, maxy = term.getSize()
  41.   local padding = maxx - (string.len(left) + string.len(right))
  42.   return left .. string.rep(' ', padding) .. right
  43. end
  44.  
  45. function setupGUI()
  46.   local windows = {}
  47.   local cx, cy = term.current().getSize()
  48.   windows.root = window.create(
  49.     term.current(),
  50.     1, 1,
  51.     cx, cy
  52.   )
  53.  
  54.   local rx, ry = windows.root.getSize()
  55.   windows.log = window.create(
  56.     windows.root,
  57.     1, 2,
  58.     rx, ry - 2
  59.   )
  60.  
  61.   windows.root.setBackgroundColor(HEADER_BG_COLOR)
  62.   windows.root.setTextColor(HEADER_FG_COLOR)
  63.   windows.root.clear()
  64.  
  65.   windows.root.setCursorPos(1, 1)
  66.   windows.root.write(lAndRAligned(
  67.     windows.root,
  68.     TITLE, 'v' .. VERSION
  69.   ))
  70.   windows.root.setCursorPos(1, ry)
  71.   windows.root.write(lAndRAligned(
  72.     windows.root,
  73.     'Ctrl-T to quit', 'By: ' .. AUTHOR
  74.   ))
  75.  
  76.   local lx, ly = windows.log.getSize()
  77.   windows.log.clear()
  78.   windows.log.setCursorPos(1, ly)
  79.   windows.log.write('Starting analogwatch...')
  80.  
  81.   return windows
  82. end
  83.  
  84. function appendLog(logWindow, line)
  85.   local lwx, lwy = logWindow.getSize()
  86.   logWindow.scroll(1)
  87.   logWindow.setCursorPos(1, lwy)
  88.   logWindow.write(line)
  89. end
  90.  
  91. --------------------------------------------------------------------------------
  92. -- Main
  93.  
  94. local windows = setupGUI()
  95.  
  96. while true do
  97.     os.pullEvent('redstone')
  98.     if rs.getAnalogInput(ANALOG_INPUT_SIDE) >= ANALOG_LOW_VAL then
  99.         while rs.getAnalogInput(ANALOG_INPUT_SIDE) < ANALOG_HIGH_VAL do
  100.             appendLog(windows.log, 'VAL: ' .. rs.getAnalogInput(ANALOG_INPUT_SIDE) .. ', Output HIGH')
  101.             rs.setOutput(OUTPUT_SIDE, true)
  102.             os.pullEvent('redstone')
  103.         end
  104.         rs.setOutput(OUTPUT_SIDE, false)
  105.         while rs.getAnalogInput(ANALOG_INPUT_SIDE) > ANALOG_LOW_VAL do
  106.             appendLog(windows.log, 'VAL: ' .. rs.getAnalogInput(ANALOG_INPUT_SIDE) .. ', Output LOW')
  107.             os.pullEvent('redstone')
  108.         end
  109.     end
  110. end
Advertisement
Add Comment
Please, Sign In to add comment