|  | @@ -3,30 +3,15 @@
 | 
	
		
			
				|  |  |  from serial import Serial
 | 
	
		
			
				|  |  |  from serial.tools.list_ports import comports as port_list
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -from remotes import tv
 | 
	
		
			
				|  |  | +from remotes import tv, movie_box
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  from pprint import pprint
 | 
	
		
			
				|  |  |  from time import sleep
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -from sys import argv
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  |  ser = Serial(timeout=1)
 | 
	
		
			
				|  |  |  ser.baudrate = 9600
 | 
	
		
			
				|  |  |  #ser.port = '/dev/ttyACM0'
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -# Chooses one of the Navigate objects to perform
 | 
	
		
			
				|  |  | -# Via an argument
 | 
	
		
			
				|  |  | -if len(argv) <= 1:
 | 
	
		
			
				|  |  | -    print("Usage: {0} <action> (Where action is either pluto, test, or church)".format(argv[0]))
 | 
	
		
			
				|  |  | -    exit()
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -action = argv[1].lower().strip()
 | 
	
		
			
				|  |  | -#pprint(action)
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -if action not in ("pluto", "test", "church"):
 | 
	
		
			
				|  |  | -    print("Usage: {0} <action> (Where action is either pluto, test, or church)".format(argv[0]))
 | 
	
		
			
				|  |  | -    exit()
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  |  # Find the first Arduino we can find (Or verify the port we got is valid)
 | 
	
		
			
				|  |  |  ports_open = port_list(False)
 | 
	
		
			
				|  |  |  found = False
 | 
	
	
		
			
				|  | @@ -49,111 +34,27 @@ if not found:
 | 
	
		
			
				|  |  |      raise TypeError("Device '{0}' not found!".format(ser.port))
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  # Attempts to send the requested code
 | 
	
		
			
				|  |  | -def send_code(ky):
 | 
	
		
			
				|  |  | -    if ky in tv:
 | 
	
		
			
				|  |  | -        msg = "0x{0},{1},1\n".format(tv[ky], tv["_config"]["size"])
 | 
	
		
			
				|  |  | -        ser.write(msg.encode())
 | 
	
		
			
				|  |  | -        #print("< Sent {0} {1}".format(ky, msg.encode()))
 | 
	
		
			
				|  |  | -        print("< Sent {0}".format(ky))
 | 
	
		
			
				|  |  | -    else:
 | 
	
		
			
				|  |  | -        print("Invalid key")
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -class Navigate():
 | 
	
		
			
				|  |  | -    """ A collection of orders/commands to be sent """
 | 
	
		
			
				|  |  | -    def __init__(self):
 | 
	
		
			
				|  |  | -        """ Initialize with no orders """
 | 
	
		
			
				|  |  | -        self.orders = []
 | 
	
		
			
				|  |  | -    
 | 
	
		
			
				|  |  | -    def add_order(self, order, delay=1, repeat=1):
 | 
	
		
			
				|  |  | -        """ Adds a new order to the end of the list of orders
 | 
	
		
			
				|  |  | -            Given:
 | 
	
		
			
				|  |  | -                Order
 | 
	
		
			
				|  |  | -                Delay in seconds
 | 
	
		
			
				|  |  | -                If wanted you can issue the command multiple times (Que it up in a single add_order)
 | 
	
		
			
				|  |  | -        """
 | 
	
		
			
				|  |  | -        for x in range(0, repeat):
 | 
	
		
			
				|  |  | -            self.orders.append({"order": order, "delay": delay})
 | 
	
		
			
				|  |  | -    
 | 
	
		
			
				|  |  | -    def perform(self, response):
 | 
	
		
			
				|  |  | -        """ Sends the code then waits, executes the first then removes it """
 | 
	
		
			
				|  |  | -        if len(self.orders) > 0:
 | 
	
		
			
				|  |  | -            if self.orders[0]["order"] == "wait":
 | 
	
		
			
				|  |  | -                print("  Wait {0}".format(self.orders[0]["delay"]))
 | 
	
		
			
				|  |  | -                sleep(self.orders[0]["delay"])
 | 
	
		
			
				|  |  | -                del self.orders[0]
 | 
	
		
			
				|  |  | -            elif response in ("Ready!", "Ok"):
 | 
	
		
			
				|  |  | -                send_code(self.orders[0]["order"])
 | 
	
		
			
				|  |  | -                sleep(self.orders[0]["delay"])
 | 
	
		
			
				|  |  | -                del self.orders[0]
 | 
	
		
			
				|  |  | -                #print("There are {0} remaining orders".format(len(self.orders)))
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -# Make some built in orders
 | 
	
		
			
				|  |  | -def goto_pluto():
 | 
	
		
			
				|  |  | -    """ This will go to Pluto TV from the TV off
 | 
	
		
			
				|  |  | -    """
 | 
	
		
			
				|  |  | -    pluto = Navigate()
 | 
	
		
			
				|  |  | -    pluto.add_order("power", delay=7)
 | 
	
		
			
				|  |  | -    pluto.add_order("mute")
 | 
	
		
			
				|  |  | -    pluto.add_order("home", repeat=2)
 | 
	
		
			
				|  |  | -    pluto.add_order("right", repeat=3)
 | 
	
		
			
				|  |  | -    pluto.add_order("down", repeat=8)
 | 
	
		
			
				|  |  | -    pluto.add_order("ok")
 | 
	
		
			
				|  |  | -    return pluto
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -def test_system():
 | 
	
		
			
				|  |  | -    """ This will test the basic movements from the TV off
 | 
	
		
			
				|  |  | -    """
 | 
	
		
			
				|  |  | -    test = Navigate()
 | 
	
		
			
				|  |  | -    test.add_order("power", delay=7)
 | 
	
		
			
				|  |  | -    test.add_order("mute")
 | 
	
		
			
				|  |  | -    test.add_order("home", repeat=2)
 | 
	
		
			
				|  |  | -    test.add_order("right")
 | 
	
		
			
				|  |  | -    for _ in range(0, 4):
 | 
	
		
			
				|  |  | -        test.add_order("right", repeat=2)
 | 
	
		
			
				|  |  | -        test.add_order("down", repeat=2)
 | 
	
		
			
				|  |  | -        test.add_order("left", repeat=2)
 | 
	
		
			
				|  |  | -        test.add_order("up", repeat=2)
 | 
	
		
			
				|  |  | -    test.add_order("left")
 | 
	
		
			
				|  |  | -    test.add_order("power")
 | 
	
		
			
				|  |  | -    return test
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -def goto_church():
 | 
	
		
			
				|  |  | -    """ This will go to the YouTube channel for our church, from the TV off
 | 
	
		
			
				|  |  | -    """
 | 
	
		
			
				|  |  | -    church = Navigate()
 | 
	
		
			
				|  |  | -    church.add_order("power", delay=7)
 | 
	
		
			
				|  |  | -    church.add_order("mute")
 | 
	
		
			
				|  |  | -    church.add_order("home", repeat=2)
 | 
	
		
			
				|  |  | -    church.add_order("right", repeat=3)
 | 
	
		
			
				|  |  | -    church.add_order("down", repeat=5)
 | 
	
		
			
				|  |  | -    church.add_order("ok", delay=7)
 | 
	
		
			
				|  |  | -    church.add_order("left")
 | 
	
		
			
				|  |  | -    church.add_order("up")
 | 
	
		
			
				|  |  | -    church.add_order("right")
 | 
	
		
			
				|  |  | -    church.add_order("ok", delay=3)
 | 
	
		
			
				|  |  | -    church.add_order("ok", delay=2)
 | 
	
		
			
				|  |  | -    church.add_order("mute")
 | 
	
		
			
				|  |  | -    return church
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -if action == "test":
 | 
	
		
			
				|  |  | -    nav = test_system()
 | 
	
		
			
				|  |  | -elif action == "pluto":
 | 
	
		
			
				|  |  | -    nav = goto_pluto()
 | 
	
		
			
				|  |  | -elif action == "church":
 | 
	
		
			
				|  |  | -    nav = goto_church()
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | +def send_code(ky, target):
 | 
	
		
			
				|  |  | +    if target == "tv":
 | 
	
		
			
				|  |  | +        if ky in tv:
 | 
	
		
			
				|  |  | +            msg = "0x{0},{1},1\n".format(tv[ky], tv["_config"]["size"])
 | 
	
		
			
				|  |  | +            ser.write(msg.encode())
 | 
	
		
			
				|  |  | +            #print("< Sent {0} {1}".format(ky, msg.encode()))
 | 
	
		
			
				|  |  | +            print("< Sent {0}".format(ky))
 | 
	
		
			
				|  |  | +        else:
 | 
	
		
			
				|  |  | +            print("Invalid key")
 | 
	
		
			
				|  |  | +    elif target == "mb":
 | 
	
		
			
				|  |  | +        if ky in movie_box:
 | 
	
		
			
				|  |  | +            msg = "0x{0},{1},0\n".format(movie_box[ky], movie_box["_config"]["size"])
 | 
	
		
			
				|  |  | +            ser.write(msg.encode())
 | 
	
		
			
				|  |  | +            #print("< Sent {0} {1}".format(ky, msg.encode()))
 | 
	
		
			
				|  |  | +            print("< Sent {0}".format(ky))
 | 
	
		
			
				|  |  | +        else:
 | 
	
		
			
				|  |  | +            print("Invalid key")
 | 
	
		
			
				|  |  |  # Ok we are ready to actually open the Serial port
 | 
	
		
			
				|  |  |  ser.open()
 | 
	
		
			
				|  |  |  print("Opened!")
 | 
	
		
			
				|  |  | -while ser.is_open: # While the connection is open
 | 
	
		
			
				|  |  | -    line = ser.readline().decode().strip("\n").strip("\r") # Attempt to read a line
 | 
	
		
			
				|  |  | -    if line != "": # If there is a line Pretty Print it to the screen
 | 
	
		
			
				|  |  | -        print("> {0}".format(line))
 | 
	
		
			
				|  |  | -        #pluto.perform(line)
 | 
	
		
			
				|  |  | -        #test.perform(line)
 | 
	
		
			
				|  |  | -        nav.perform(line)
 | 
	
		
			
				|  |  | -    #if len(pluto.orders) <= 0:
 | 
	
		
			
				|  |  | -    #if len(test.orders) <= 0:
 | 
	
		
			
				|  |  | -    if len(nav.orders) <= 0:
 | 
	
		
			
				|  |  | -        ser.close()
 | 
	
		
			
				|  |  | -        print("Closed!")
 | 
	
		
			
				|  |  | +#while ser.is_open: # While the connection is open
 | 
	
		
			
				|  |  | +#    line = ser.readline().decode().strip("\n").strip("\r") # Attempt to read a line
 | 
	
		
			
				|  |  | +#    if line != "": # If there is a line Pretty Print it to the screen
 | 
	
		
			
				|  |  | +#        print("> {0}".format(line))
 |