#4 Change Python to "Call Response" detection

Terbuka
dibuka 3 tahun lalu oleh david · 3 komentar

Currently my Python code will assume the Arduino sent the code without errors. This needs to change.

A small Todo list: (Our goal is to power the TV on, then navigate to Pluto TV)

  • Have Python wait till it receives "Ready!" then send power on.
  • Have Python interpret it's "Response" from any command. (Either an "Error" or "Ok" which is equal to "Ready!")

Some things I need to think about:

  • If we get an "Error" response how do we handle that? (What do we do?)
  • The TV power on command takes a bit of time (While our code will get a "Ok" response back when it's done sending, the TV will still be "warming up")
Currently my Python code will assume the Arduino sent the code without errors. This needs to change. A small Todo list: (Our goal is to power the TV on, then navigate to Pluto TV) - [x] Have Python wait till it receives "Ready!" then send power on. - [ ] Have Python interpret it's "Response" from any command. (Either an "Error" or "Ok" which is equal to "Ready!") Some things I need to think about: * If we get an "Error" response how do we handle that? (What do we do?) * The TV power on command takes a bit of time (While our code will get a "Ok" response back when it's done sending, the TV will still be "warming up")
David Thielemann komentar 3 tahun lalu
Pemilik

Hmm all this will almost look like some twisted/state machine code. (Actually for some "navigation" I think I will possibly make some kind of assistant either class or just a function)

Proof of concept of the Navigation class

class Navigate:
    """ A collection of orders/commands to send """
    def __init__(self):
        """ Initialize with no orders """
        self.orders = []
    
    def add_order(self, order, delay=2):
        """ Adds a new order to the end of the list of orders """
        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 response in ("Ready!", "Ok"):
                sendCode(self.orders[0]["order"])
                sleep(self.orders[0]["delay"])
                del self.orders[0]

# Demos adding some commands
pluto = Navigate()
pluto.add_order("power", 6) # Power on takes longer so we wait X time
pluto.add_order("home")
pluto.add_order("home")

# Then our "reactor" ... I mean our input/output loop
while (ser.is_open):
    line = ser.readline().decode().strip("\n").strip("\r")
    if line: # Pass anything that we get to our navigate class
        pluto.perform(line)

Hmm all this will almost look like some twisted/state machine code. (Actually for some "navigation" I think I will possibly make some kind of assistant either class or just a function) Proof of concept of the Navigation class ```py class Navigate: """ A collection of orders/commands to send """ def __init__(self): """ Initialize with no orders """ self.orders = [] def add_order(self, order, delay=2): """ Adds a new order to the end of the list of orders """ 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 response in ("Ready!", "Ok"): sendCode(self.orders[0]["order"]) sleep(self.orders[0]["delay"]) del self.orders[0] # Demos adding some commands pluto = Navigate() pluto.add_order("power", 6) # Power on takes longer so we wait X time pluto.add_order("home") pluto.add_order("home") # Then our "reactor" ... I mean our input/output loop while (ser.is_open): line = ser.readline().decode().strip("\n").strip("\r") if line: # Pass anything that we get to our navigate class pluto.perform(line) ```
David Thielemann komentar 3 tahun lalu
Pemilik

Ok I just reworked this a little in [d471e9fe4d]

Now we essentially use this to "do" something.

I've also added it so we just call the script with say pluto or church and it will automatically turn the TV on, then go to the selected thing for that. (Pluto TV for pluto, YouTube for church)

I also include a test routine to test if we get any more sent 1 command but the TV saw 2 commands. (Basically it turns the TV on then moves around in a circle 4 times then turns the TV back off)

Ok I just reworked this a little in [d471e9fe4d] Now we essentially use this to "do" something. I've also added it so we just call the script with say pluto or church and it will automatically turn the TV on, then go to the selected thing for that. (Pluto TV for pluto, YouTube for church) I also include a test routine to test if we get any more sent 1 command but the TV saw 2 commands. (Basically it turns the TV on then moves around in a circle 4 times then turns the TV back off)
David Thielemann komentar 3 tahun lalu
Pemilik

We still don't interpret an error message, we only detect and use "Ready!" and "Ok" from our response.

I guess I could just have it exit() if we see Error in the response.

We still don't interpret an error message, we only detect and use "Ready!" and "Ok" from our response. I guess I could just have it `exit()` if we see Error in the response.
Masuk untuk bergabung dalam percakapan ini.
Tidak ada penerima
1 Peserta
Memuat...
Batal
Simpan
Belum ada konten.