command.py 899 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. #!/usr/bin/env python3
  2. from serial import Serial
  3. # Use port_listing to print out the avalible ports
  4. # from serial.tools.list_ports import main as port_listing
  5. # port_listing()
  6. from remotes import tv
  7. from pprint import pprint
  8. ser = Serial(timeout=1)
  9. ser.baudrate = 9600
  10. ser.port = '/dev/ttyACM0'
  11. def send_code(ky):
  12. if ky in tv:
  13. msg = "0x{0},{1},1\n".format(tv[ky], tv["_config"]["size"])
  14. ser.write(msg.encode())
  15. print("Sent {0}".format(msg.encode()))
  16. else:
  17. print("Invalid key")
  18. ser.open()
  19. print("Opened!")
  20. connect_t = 0
  21. while ser.is_open:
  22. line = ser.readline().decode().strip("\n").strip("\r")
  23. if line != "":
  24. pprint(line)
  25. if connect_t == 6:
  26. send_code('power')
  27. elif connect_t == 18:
  28. send_code('power')
  29. elif connect_t == 30:
  30. ser.close()
  31. print("Closed!")
  32. connect_t += 1 # About 1 every second