Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
input timer
#6
First, these don't seem to really be "wake words" if you have to process them. They're more like guard words as a check. But it looks to me like both the wake and the control are coming in the same input channel.

I can think of a couple of approaches.

One is write it multithreaded. Have one thread get the input (which can block), but then put the info into a queue for the other thread to read. The other thread can timeout if the queue doesn't give any information in the time.

A perhaps simpler way is to loop over input, but then just check a timer for when the input arrived. If it's been too long since the last input, only process it as a wake word. If the time has been short, process as control. The disadvantage is that you can't report that you're transitioning to "asleep", because you're blocking on the input.

import time

WAIT_TIME = 5  # max wait time in seconds
WAKE_WORDS = ["Hello", "Mornin", "Howdy", "Hi"]

last_input = 0
state = "asleep"
while True:
    text = input()
    input_time = time.time()

    if input_time - last_input > WAIT_TIME:
        state = "asleep"
    last_input = input_time

    if state == "asleep":
        if text in WAKE_WORDS:
            print("Hello, what can I do for you?")
            state = "awake"
        else:
            print("  debug: wake word not found while asleep. ")
    elif state == "awake":
        print(f"I am processing {text} now")
Output:
$ python3 timerloop.py not wake word debug: wake word not found while asleep. Hi Hello, what can I do for you? dothis I am processing dothis now dothis # typed after waiting 5 seconds debug: wake word not found while asleep.
Reply


Messages In This Thread
input timer - by Nickd12 - Nov-18-2020, 11:29 PM
RE: input timer - by bowlofred - Nov-19-2020, 01:09 AM
RE: input timer - by Nickd12 - Nov-19-2020, 01:20 AM
RE: input timer - by jefsummers - Nov-19-2020, 02:26 AM
RE: input timer - by Nickd12 - Nov-19-2020, 02:51 AM
RE: input timer - by bowlofred - Nov-19-2020, 04:48 AM
RE: input timer - by Nickd12 - Nov-19-2020, 10:03 PM
RE: input timer - by bowlofred - Nov-19-2020, 10:15 PM
RE: input timer - by JarredAwesome - Nov-19-2020, 10:08 PM
RE: input timer - by Nickd12 - Nov-23-2020, 09:50 PM
RE: input timer - by Nickd12 - Dec-07-2020, 02:24 AM
RE: input timer - by bowlofred - Dec-07-2020, 02:51 AM
RE: input timer - by Nickd12 - Dec-07-2020, 08:05 PM
RE: input timer - by Nickd12 - Dec-08-2020, 01:55 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  email timer/rss feed timer ndiniz 1 2,168 Feb-02-2021, 07:18 PM
Last Post: nilamo
  input timer Nickd12 0 1,735 Nov-18-2020, 12:31 AM
Last Post: Nickd12

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020