mah17

analogwatch.lua

Dec 24th, 2015
83
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.2'
  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_CUTOFF_VAL = 10 -- 1 to 15
  15.  
  16. -- Setup colors
  17. local HEADER_BG_COLOR, HEADER_FG_COLOR
  18. if term.isColor() then
  19.     HEADER_BG_COLOR = colors.blue
  20.     HEADER_FG_COLOR = colors.white
  21. else
  22.     local HEADER_BG_COLOR = colors.white
  23.     local HEADER_FG_COLOR = colors.black
  24. end
  25.  
  26. --------------------------------------------------------------------------------
  27. -- Nifty/Neat functions
  28. function round(num, idp)
  29.     return tonumber(string.format("%." .. (idp or 0) .. 'f', num))
  30. end
  31.  
  32. --------------------------------------------------------------------------------
  33. --GUI Stuff
  34. function lAndRAligned(cWindow, left, right)
  35.     local maxx, maxy = term.getSize()
  36.     local padding = maxx - (string.len(left) + string.len(right))
  37.     return left .. string.rep(' ', padding) .. right
  38. end
  39.  
  40. function setupGUI()
  41.     local windows = {}
  42.     local cx, cy = term.current().getSize()
  43.     windows.root = window.create(
  44.         term.current(),
  45.         1, 1,
  46.         cx, cy
  47.     )
  48.  
  49.     local rx, ry = windows.root.getSize()
  50.     windows.log = window.create(
  51.         windows.root,
  52.         1, 2,
  53.         rx, ry - 2
  54.     )
  55.  
  56.     windows.root.setBackgroundColor(HEADER_BG_COLOR)
  57.     windows.root.setTextColor(HEADER_FG_COLOR)
  58.     windows.root.clear()
  59.  
  60.     windows.root.setCursorPos(1, 1)
  61.     windows.root.write(lAndRAligned(
  62.         windows.root,
  63.         TITLE, 'v' .. VERSION
  64.     ))
  65.     windows.root.setCursorPos(1, ry)
  66.     windows.root.write(lAndRAligned(
  67.         windows.root,
  68.         'Ctrl-T to quit', 'By: ' .. AUTHOR
  69.     ))
  70.  
  71.     local lx, ly = windows.log.getSize()
  72.     windows.log.clear()
  73.     windows.log.setCursorPos(1, ly)
  74.     windows.log.write('Starting analogwatch...')
  75.  
  76.     return windows
  77. end
  78.  
  79. function appendLog(logWindow, line)
  80.     local lwx, lwy = logWindow.getSize()
  81.     logWindow.scroll(1)
  82.     logWindow.setCursorPos(1, lwy)
  83.     logWindow.write(line)
  84. end
  85.  
  86. --------------------------------------------------------------------------------
  87. -- Main
  88.  
  89. local windows = setupGUI()
  90.  
  91. function loop()
  92.     if rs.getAnalogInput(ANALOG_INPUT_SIDE) > ANALOG_CUTOFF_VAL then
  93.         appendLog(windows.log, 'VAL: ' .. rs.getAnalogInput(ANALOG_INPUT_SIDE) .. ', Output HIGH')
  94.         rs.setOutput(OUTPUT_SIDE, true)
  95.         appendLog(windows.log, 'Waiting for VAL <= 0 before continuing...')
  96.  
  97.         while rs.getAnalogInput(ANALOG_INPUT_SIDE) ~= 0 do
  98.             appendLog(windows.log, 'VAL: ' .. rs.getAnalogInput(ANALOG_INPUT_SIDE) .. ', Waiting...')
  99.             os.pullEvent('redstone')
  100.         end
  101.         return
  102.  
  103.     else
  104.         appendLog(windows.log, 'VAL: ' .. rs.getAnalogInput(ANALOG_INPUT_SIDE) .. ', Output LOW')
  105.         rs.setOutput(OUTPUT_SIDE, false)
  106.     end
  107.     os.pullEvent('redstone')
  108. end
  109.  
  110. while true do loop() end
Add Comment
Please, Sign In to add comment