Last active
June 4, 2020 16:59
-
-
Save aallan/b6d60d457decff8ce24fd3d7f48efbf6 to your computer and use it in GitHub Desktop.
A Python script managing a Google Voice Assistant living inside a rotary dial phone. Full write up at https://medium.com/@aallan/a-retro-rotary-phone-powered-by-aiy-projects-and-the-raspberry-pi-e516b3ff1528.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# | |
# Full write up at https://medium.com/@aallan/a-retro-rotary-phone-powered-by-aiy-projects-and-the-raspberry-pi-e516b3ff1528. | |
import logging | |
import sys | |
import threading | |
import RPi.GPIO as GPIO | |
import pygame | |
import aiy.assistant.auth_helpers | |
import aiy.voicehat | |
from google.assistant.library import Assistant | |
from google.assistant.library.event import EventType | |
logging.basicConfig( | |
level=logging.INFO, | |
format="[%(asctime)s] %(levelname)s:%(name)s:%(message)s" | |
) | |
class MyAssistant(object): | |
def __init__(self): | |
self._task = threading.Thread(target=self._run_task) | |
self._can_start_conversation = False | |
self._assistant = None | |
self._hook_is_down = 1; | |
self._hook_up = aiy._drivers._button.Button(channel=4,polarity=GPIO.RISING) | |
self._hook_up.on_press(self._handset_lifted) | |
self._hook_down = aiy._drivers._button.Button(channel=17,polarity=GPIO.FALLING) | |
self._hook_down.on_press(self._handset_replaced) | |
pygame.mixer.init() | |
pygame.mixer.music.load("/home/pi/Dial_Tone.wav") | |
def start(self): | |
self._task.start() | |
def _run_task(self): | |
credentials = aiy.assistant.auth_helpers.get_assistant_credentials() | |
with Assistant(credentials) as assistant: | |
self._assistant = assistant | |
for event in assistant.start(): | |
self._process_event(event) | |
def _process_event(self, event): | |
status_ui = aiy.voicehat.get_status_ui() | |
if event.type == EventType.ON_START_FINISHED: | |
status_ui.status('ready') | |
self._can_start_conversation = True | |
aiy.voicehat.get_button().on_press(self._on_button_pressed) | |
if sys.stdout.isatty(): | |
print('Press the button, then speak. Press Ctrl+C to quit...') | |
elif event.type == EventType.ON_CONVERSATION_TURN_STARTED: | |
self._can_start_conversation = False | |
status_ui.status('listening') | |
elif event.type == EventType.ON_END_OF_UTTERANCE: | |
status_ui.status('thinking') | |
elif event.type == EventType.ON_CONVERSATION_TURN_FINISHED: | |
status_ui.status('ready') | |
self._can_start_conversation = True | |
aiy.audio.play_wave("/home/pi/Hang_Up.wav") | |
if self._hook_is_down == 0: | |
pygame.mixer.music.play(loops=-1) | |
elif event.type == EventType.ON_ASSISTANT_ERROR and event.args and event.args['is_fatal']: | |
sys.exit(1) | |
def _handset_replaced(self): | |
print('Hook down') | |
self._hook_is_down = 1 | |
self._assistant.stop_conversation() | |
pygame.mixer.music.stop() | |
pygame.mixer.music.rewind() | |
def _handset_lifted(self): | |
print('Hook up') | |
self._hook_is_down = 0 | |
pygame.mixer.music.play(loops=-1) | |
def _on_button_pressed(self): | |
if self._hook_is_down == 0: | |
if self._can_start_conversation: | |
pygame.mixer.music.stop() | |
pygame.mixer.music.rewind() | |
aiy.audio.say('Operator?') | |
self._assistant.start_conversation() | |
def main(): | |
MyAssistant().start() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment