123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- #!/usr/bin/env python3
- from serial import Serial
- from serial.tools.list_ports import comports as port_list
- from remotes import tv
- 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
- for p in ports_open:
- print("Device: /dev/{0:8} Manufacturer: {1}".format(p.name, p.manufacturer))
- if ser.port == None:
- if p.manufacturer == "Arduino (www.arduino.cc)":
- found = True
- ser.port = "/dev/{0}".format(p.name)
- print("Automagically found an Arduino on port '/dev/{0}'!".format(p.name))
- break # Stop needlessly looping over devices
- else:
- if '/dev/{0}'.format(p.name) == ser.port:
- found = True
- print("Found your Arduino!")
- break # Stop needlessly looping over devices
- # Verify I have found a Arduino
- 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()
- # 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!")
|