Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- import curses, thread, threading, os
- import RPi.GPIO as GPIO
- def pin_setup():
- global PIN
- GPIO.setmode(GPIO.BCM)
- GPIO.setup(PIN, GPIO.OUT)
- GPIO.output(PIN, GPIO.LOW)
- def quit(ret):
- curses.nocbreak()
- stdscr.keypad(0)
- curses.echo()
- curses.endwin()
- GPIO.cleanup()
- os._exit(ret)
- def print_time(t):
- y, x = stdscr.getmaxyx()
- stdscr.clear()
- stdscr.addstr(y / 2, x / 2, "{0}".format(t), curses.A_BOLD)
- stdscr.refresh()
- def lt():
- global PIN
- global pin_stat
- if pin_stat:
- GPIO.output(PIN, GPIO.LOW)
- pin_stat = False
- def gt():
- global PIN
- global pin_stat
- if not pin_stat:
- GPIO.output(PIN, GPIO.LOW)
- pin_stat = True
- def update():
- global sec
- print_time(sec)
- if sec <= 0:
- lt()
- else:
- gt()
- def read():
- global sec
- global sec_lck
- while 1:
- c = stdscr.getch()
- if c == ord('q') or c == ord('Q'):
- quit(0)
- elif c == curses.KEY_UP:
- sec_lck.acquire()
- try:
- sec += 1
- update()
- finally:
- sec_lck.release()
- else:
- pass
- def timer():
- global sec
- global sec_lck
- threading.Timer(0.1, timer).start()
- sec_lck.acquire()
- try:
- if not sec <= 0:
- sec -= 1
- update()
- finally:
- sec_lck.release()
- PIN = 10
- pin_stat = False
- sec = 0
- sec_lck = threading.Lock()
- pin_setup()
- stdscr = curses.initscr()
- curses.noecho()
- curses.cbreak()
- stdscr.keypad(1)
- curses.curs_set(0)
- update()
- read_th = threading.Thread(target = read)
- read_th.start()
- timer()
- read_th.join()
- quit(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement