Nov-19-2020, 04:48 AM
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.
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.