Advertisement
Guest User

Untitled

a guest
Dec 16th, 2014
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
CFDG 1.41 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import curses, thread, threading, os
  4. import RPi.GPIO as GPIO
  5.  
  6. def pin_setup():
  7.     global PIN
  8.     GPIO.setmode(GPIO.BCM)
  9.     GPIO.setup(PIN, GPIO.OUT)
  10.     GPIO.output(PIN, GPIO.LOW)
  11.  
  12. def quit(ret):
  13.     curses.nocbreak()
  14.     stdscr.keypad(0)
  15.     curses.echo()
  16.     curses.endwin()
  17.     GPIO.cleanup()
  18.     os._exit(ret)
  19.  
  20. def print_time(t):
  21.     y, x = stdscr.getmaxyx()
  22.     stdscr.clear()
  23.     stdscr.addstr(y / 2, x / 2, "{0}".format(t), curses.A_BOLD)
  24.     stdscr.refresh()
  25.  
  26. def lt():
  27.     global PIN
  28.     global pin_stat
  29.     if pin_stat:
  30.         GPIO.output(PIN, GPIO.LOW)
  31.         pin_stat = False
  32.  
  33. def gt():
  34.     global PIN
  35.     global pin_stat
  36.     if not pin_stat:
  37.         GPIO.output(PIN, GPIO.LOW)
  38.         pin_stat = True
  39.  
  40. def update():
  41.     global sec
  42.     print_time(sec)
  43.     if sec <= 0:
  44.         lt()
  45.     else:
  46.         gt()
  47.  
  48. def read():
  49.     global sec
  50.     global sec_lck
  51.     while 1:
  52.         c = stdscr.getch()
  53.         if c == ord('q') or c == ord('Q'):
  54.             quit(0)
  55.         elif c == curses.KEY_UP:
  56.             sec_lck.acquire()
  57.             try:
  58.                 sec += 1
  59.                 update()
  60.             finally:
  61.                 sec_lck.release()
  62.         else:
  63.             pass
  64.            
  65. def timer():
  66.     global sec
  67.     global sec_lck
  68.     threading.Timer(0.1, timer).start()
  69.     sec_lck.acquire()
  70.     try:
  71.         if not sec <= 0:
  72.             sec -= 1
  73.         update()
  74.     finally:
  75.         sec_lck.release()
  76.  
  77. PIN = 10
  78. pin_stat = False
  79. sec = 0
  80. sec_lck = threading.Lock()
  81.  
  82. pin_setup()
  83.  
  84. stdscr = curses.initscr()
  85. curses.noecho()
  86. curses.cbreak()
  87. stdscr.keypad(1)
  88. curses.curs_set(0)
  89.  
  90. update()
  91.  
  92. read_th = threading.Thread(target = read)
  93.  
  94. read_th.start()
  95. timer()
  96. read_th.join()
  97.  
  98. quit(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement