command.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #!/usr/bin/env python3
  2. from serial import Serial
  3. from serial.tools.list_ports import comports as port_list
  4. from remotes import tv, movie_box
  5. from pprint import pprint
  6. from time import sleep
  7. ser = Serial(timeout=1)
  8. ser.baudrate = 9600
  9. #ser.port = '/dev/ttyACM0'
  10. # Find the first Arduino we can find (Or verify the port we got is valid)
  11. ports_open = port_list(False)
  12. found = False
  13. for p in ports_open:
  14. print("Device: /dev/{0:8} Manufacturer: {1}".format(p.name, p.manufacturer))
  15. if ser.port == None:
  16. if p.manufacturer == "Arduino (www.arduino.cc)":
  17. found = True
  18. ser.port = "/dev/{0}".format(p.name)
  19. print("Automagically found an Arduino on port '/dev/{0}'!".format(p.name))
  20. break # Stop needlessly looping over devices
  21. else:
  22. if '/dev/{0}'.format(p.name) == ser.port:
  23. found = True
  24. print("Found your Arduino!")
  25. break # Stop needlessly looping over devices
  26. # Verify I have found a Arduino
  27. if not found:
  28. raise TypeError("Device '{0}' not found!".format(ser.port))
  29. # Attempts to send the requested code
  30. def send_code(ky, target):
  31. if target == "tv":
  32. if ky in tv:
  33. msg = "0x{0},{1},1\n".format(tv[ky], tv["_config"]["size"])
  34. ser.write(msg.encode())
  35. #print("< Sent {0} {1}".format(ky, msg.encode()))
  36. print("< Sent {0}".format(ky))
  37. else:
  38. print("Invalid key")
  39. elif target == "mb":
  40. if ky in movie_box:
  41. msg = "0x{0},{1},0\n".format(movie_box[ky], movie_box["_config"]["size"])
  42. ser.write(msg.encode())
  43. #print("< Sent {0} {1}".format(ky, msg.encode()))
  44. print("< Sent {0}".format(ky))
  45. else:
  46. print("Invalid key")
  47. # Ok we are ready to actually open the Serial port
  48. ser.open()
  49. print("Opened!")
  50. #while ser.is_open: # While the connection is open
  51. # line = ser.readline().decode().strip("\n").strip("\r") # Attempt to read a line
  52. # if line != "": # If there is a line Pretty Print it to the screen
  53. # print("> {0}".format(line))