Guest User

Untitled

a guest
Nov 19th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. import os
  2. from time import sleep
  3. import sys
  4.  
  5. import serial
  6.  
  7. commands = {
  8. 'on': b'0',
  9. 'volume_down': b'1',
  10. 'volume_up': b'2',
  11. 'mute': b'3',
  12. 'channel_dvd': b'4',
  13. 'channel_tv': b'5',
  14. 'channel_game': b'6',
  15. 'channel_mp3': b'7',
  16. }
  17.  
  18.  
  19. def figure_out_serial():
  20. devs = os.listdir('/dev/')
  21. try:
  22. device_name = [file_ for file_ in devs if 'ttyUSB' in file_][0]
  23. except IndexError:
  24. print("USB serial not found")
  25. return
  26. return '/dev/' + device_name
  27.  
  28.  
  29. def main():
  30. key = sys.argv[1]
  31. if not key:
  32. print('key was not specified. The command was not sent.')
  33. return
  34.  
  35. serial_name = figure_out_serial()
  36. print('Serial name: {}'.format(serial_name))
  37.  
  38. ser = serial.Serial(serial_name)
  39.  
  40. command = commands[key]
  41. ser.write(command)
  42. print('Command "{}" was sent to Hi-Fi system.'.format(key))
  43. sleep(1)
  44. ser.close()
  45.  
  46. main()
Add Comment
Please, Sign In to add comment