control.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #!/usr/bin/python3
  2. import curses
  3. import time
  4. from .common import *
  5. pin_map = {
  6. 'w': fwd_pin,
  7. 'a': left_pin,
  8. 's': back_pin,
  9. 'd': right_pin
  10. }
  11. all_pins = list(pin_map.values())
  12. all_pins.append(hard_pin)
  13. def main(stdscr):
  14. current_milli_time = lambda: int(round(time.time() * 1000))
  15. stdscr.nodelay(1)
  16. lastcommand = current_milli_time()
  17. while True:
  18. # get keyboard input, returns -1 if none available
  19. c = stdscr.getch()
  20. if c != -1:
  21. # print numeric value
  22. stdscr.addstr(str(c) + ' ')
  23. stdscr.refresh()
  24. # return curser to start position
  25. stdscr.move(0, 0)
  26. char = chr(c)
  27. if char.lower() in pin_map:
  28. GPIO.output(pin_map[char.lower()], GPIO.HIGH)
  29. if char.isupper():
  30. GPIO.output(hard_pin, GPIO.HIGH)
  31. elif c == ord('q'):
  32. return
  33. lastcommand = current_milli_time()
  34. elif current_milli_time() - lastcommand > 100:
  35. GPIO.output(all_pins, GPIO.LOW)
  36. GPIO.cleanup()
  37. if __name__ == '__main__':
  38. curses.wrapper(main)