Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Outputs on/off signal based on analog input
- local AUTHOR = 'nxuul'
- local TITLE = 'analogwatch'
- local VERSION = '0.2'
- --------------------------------------------------------------------------------
- -- Static vars
- ANALOG_INPUT_SIDE = 'top'
- OUTPUT_SIDE = 'bottom'
- -- Values to enable and disable output at
- ANALOG_CUTOFF_VAL = 10 -- 1 to 15
- -- Setup colors
- local HEADER_BG_COLOR, HEADER_FG_COLOR
- if term.isColor() then
- HEADER_BG_COLOR = colors.blue
- HEADER_FG_COLOR = colors.white
- else
- local HEADER_BG_COLOR = colors.white
- local HEADER_FG_COLOR = colors.black
- end
- --------------------------------------------------------------------------------
- -- Nifty/Neat functions
- function round(num, idp)
- return tonumber(string.format("%." .. (idp or 0) .. 'f', num))
- end
- --------------------------------------------------------------------------------
- --GUI Stuff
- function lAndRAligned(cWindow, left, right)
- local maxx, maxy = term.getSize()
- local padding = maxx - (string.len(left) + string.len(right))
- return left .. string.rep(' ', padding) .. right
- end
- function setupGUI()
- local windows = {}
- local cx, cy = term.current().getSize()
- windows.root = window.create(
- term.current(),
- 1, 1,
- cx, cy
- )
- local rx, ry = windows.root.getSize()
- windows.log = window.create(
- windows.root,
- 1, 2,
- rx, ry - 2
- )
- windows.root.setBackgroundColor(HEADER_BG_COLOR)
- windows.root.setTextColor(HEADER_FG_COLOR)
- windows.root.clear()
- windows.root.setCursorPos(1, 1)
- windows.root.write(lAndRAligned(
- windows.root,
- TITLE, 'v' .. VERSION
- ))
- windows.root.setCursorPos(1, ry)
- windows.root.write(lAndRAligned(
- windows.root,
- 'Ctrl-T to quit', 'By: ' .. AUTHOR
- ))
- local lx, ly = windows.log.getSize()
- windows.log.clear()
- windows.log.setCursorPos(1, ly)
- windows.log.write('Starting analogwatch...')
- return windows
- end
- function appendLog(logWindow, line)
- local lwx, lwy = logWindow.getSize()
- logWindow.scroll(1)
- logWindow.setCursorPos(1, lwy)
- logWindow.write(line)
- end
- --------------------------------------------------------------------------------
- -- Main
- local windows = setupGUI()
- function loop()
- if rs.getAnalogInput(ANALOG_INPUT_SIDE) > ANALOG_CUTOFF_VAL then
- appendLog(windows.log, 'VAL: ' .. rs.getAnalogInput(ANALOG_INPUT_SIDE) .. ', Output HIGH')
- rs.setOutput(OUTPUT_SIDE, true)
- appendLog(windows.log, 'Waiting for VAL <= 0 before continuing...')
- while rs.getAnalogInput(ANALOG_INPUT_SIDE) ~= 0 do
- appendLog(windows.log, 'VAL: ' .. rs.getAnalogInput(ANALOG_INPUT_SIDE) .. ', Waiting...')
- os.pullEvent('redstone')
- end
- return
- else
- appendLog(windows.log, 'VAL: ' .. rs.getAnalogInput(ANALOG_INPUT_SIDE) .. ', Output LOW')
- rs.setOutput(OUTPUT_SIDE, false)
- end
- os.pullEvent('redstone')
- end
- while true do loop() end
Add Comment
Please, Sign In to add comment