aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore1
-rw-r--r--dial.py23
-rw-r--r--events.py64
-rw-r--r--hangup.py25
-rwxr-xr-xmain.py26
-rw-r--r--phone.py16
6 files changed, 119 insertions, 36 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..0d20b64
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+*.pyc
diff --git a/dial.py b/dial.py
index c5f556f..1d08a05 100644
--- a/dial.py
+++ b/dial.py
@@ -3,14 +3,17 @@ import time
import RPi.GPIO as GPIO
import threading
+import events
+
BUTTON_GPIO = 25
REST_TIME = 0.3 # seconds
DIAL_RESET_TIME = 5 # seconds
class DialThread(threading.Thread):
- def __init__(self, queue):
+ def __init__(self, queue, pressed):
threading.Thread.__init__(self, args=(), kwargs=None)
self.queue = queue
+ self.pressed = pressed
self.daemon = True
GPIO.setmode(GPIO.BCM)
@@ -52,18 +55,9 @@ class DialManager:
def _load_sequences(self):
self.sequences = {
- 0: "operator",
- 1: {
- 2: "test_multiple"
- },
- 2: "weather",
- 3: "horrorscope",
- 4: "",
- 5: "",
- 6: "",
- 7: "fortune",
- 8: "",
- 9: ""
+ 0: events.OperatorEvent(self),
+ 4: {1: {1: events.DirectoryEvent(self)}},
+ 7: events.FortuneEvent(self),
}
print(self.sequences)
@@ -90,3 +84,6 @@ class DialManager:
return sequences[digit]
return recursive_find(self.sequence, self.sequences)
+ def clear_sequence(self):
+ self.sequence = []
+
diff --git a/events.py b/events.py
new file mode 100644
index 0000000..77480b9
--- /dev/null
+++ b/events.py
@@ -0,0 +1,64 @@
+import subprocess
+from datetime import datetime
+
+class Event():
+ def __init__(self, dial_manager):
+ self.dial_manager = dial_manager
+
+ def get_name(self):
+ return "generic event"
+
+ def get_text(self):
+ return "event"
+
+ def speak(self):
+ text = self.get_text()
+ print("speaking:", text)
+ subprocess.run(["espeak", text])
+
+class FortuneEvent(Event):
+ def __init__(self, dial_manager):
+ super().__init__(dial_manager)
+
+ def get_name(self):
+ return "fortune"
+
+ def get_text(self):
+ res = subprocess.run(["fortune", "-s"], stdout=subprocess.PIPE)
+ return res.stdout.decode("utf-8")
+
+class DirectoryEvent(Event):
+ def __init__(self, dial_manager):
+ super().__init__(dial_manager)
+
+ def get_name(self):
+ return "directory"
+
+ def get_text(self):
+ def recursive_build(prefix, sequences):
+ sequence_list = []
+ for key in sequences.keys():
+ sequence = prefix+" "+str(key)
+ if isinstance(sequences[key], dict):
+ sequence_list += recursive_build(sequence, sequences[key])
+ else:
+ sequence_list.append((sequence, sequences[key]))
+ return sequence_list
+ sequences = self.dial_manager.sequences
+ text = ""
+ for sequence, event in recursive_build("", sequences):
+ text += f"Dial {sequence} for {event.get_name()}. "
+ return text
+
+
+class OperatorEvent(Event):
+ def __init__(self, dial_manager):
+ super().__init__(dial_manager)
+
+ def get_name(self):
+ return "operator"
+
+ def get_text(self):
+ now = datetime.now()
+ return f"Right now, it is {now.hour} {now.minute}. Dial 4 1 1 for directory."
+
diff --git a/hangup.py b/hangup.py
new file mode 100644
index 0000000..2bfb1a7
--- /dev/null
+++ b/hangup.py
@@ -0,0 +1,25 @@
+import RPi.GPIO as GPIO
+import threading
+import time
+
+BUTTON_GPIO = 24
+
+class HangUpThread(threading.Thread):
+ def __init__(self, phone_held, dial_manager):
+ threading.Thread.__init__(self, args=(), kwargs=None)
+ self.phone_held = phone_held
+ self.dial_manager = dial_manager
+ self.daemon = True
+
+ GPIO.setmode(GPIO.BCM)
+ GPIO.setup(BUTTON_GPIO, GPIO.IN, pull_up_down=GPIO.PUD_UP)
+
+ def run(self):
+ while True:
+ if not GPIO.input(BUTTON_GPIO):
+ self.phone_held.clear()
+ self.dial_manager.clear_sequence()
+ else:
+ self.phone_held.set()
+ time.sleep(0.1)
+
diff --git a/main.py b/main.py
index ec3d0b6..ef51809 100755
--- a/main.py
+++ b/main.py
@@ -1,24 +1,36 @@
#!/usr/bin/env python3
from dial import DialManager, DialThread
-from phone import PhoneManager
+from hangup import HangUpThread
from time import sleep
from queue import Queue
+from threading import Event
if __name__ == "__main__":
queue = Queue()
- dial_thread = DialThread(queue)
- print("starting thread")
- dial_thread.start()
+ phone_held = Event()
+
+ # start phone as on hook
+ phone_held.clear()
+ dial_thread = DialThread(queue, phone_held)
+ print("starting dial thread")
+ dial_thread.start()
dial_manager = DialManager()
- phone_manager = PhoneManager()
+
+ hang_up_thread = HangUpThread(phone_held, dial_manager)
+ print("starting hang up thread")
+ hang_up_thread.start()
print("main loop")
while True:
- dialed = queue.get(block=True)
+ phone_held.wait()
+ try:
+ dialed = queue.get(block=True, timeout=0.1)
+ except:
+ continue
response = dial_manager.dial(dialed)
if response is not None:
- phone_manager.handle_event(response)
+ response.speak()
diff --git a/phone.py b/phone.py
deleted file mode 100644
index 59b9cfb..0000000
--- a/phone.py
+++ /dev/null
@@ -1,16 +0,0 @@
-import subprocess
-
-class PhoneManager:
- def handle_event(self, event):
- print("handling event", event)
- if event == "fortune":
- res = subprocess.run(["fortune"], stdout=subprocess.PIPE)
- output = res.stdout.decode("utf-8")
- self.speak(output)
- else:
- self.speak(event)
-
-
- def speak(self, text):
- print("speaking:", text)
- subprocess.run(["espeak", text])