forked from oldbridge/bluetooth-phone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
telefonoa.py
333 lines (289 loc) · 13.4 KB
/
telefonoa.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# Copyright 2019 by Xabier Zubizarreta.
# All rights reserved.
# This file is released under the "MIT License Agreement".
# More information on this license can be read under https://opensource.org/licenses/MIT
import RPi.GPIO as GPIO
import datetime
import dbus
import dbus.service
import dbus.mainloop.glib
import wave
import alsaaudio
import yaml
from gi.repository import GLib
import time
from threading import Thread
from threading import Event
import queue as Queue
import numpy as np
import struct
import subprocess
import config
import manager
import ringer
class RotaryDial(Thread):
"""
Thread class reading the dialed values and putting them into a thread queue
"""
def __init__(self, ns_pin, number_queue):
Thread.__init__(self)
self.pin = ns_pin
"""
The number_queue is Queue.Queue instance passed in from another thread.
This appears to facilitate interprocess communications
"""
self.number_q = number_queue
GPIO.setup(self.pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
self.value = 0
self.pulse_threshold = 0.2
# self.pulse_threshold = 0.2
self.finish = False
GPIO.add_event_detect(ns_pin, GPIO.FALLING, callback=self.__increment, bouncetime=config.DIAL_BOUNCE_TIME)
def __increment(self, pin_num):
"""
Increment function trigerred each time a falling pulse is detected.
:param pin_num: GPIO pin triggering the event (Can only be self.ns_pin here)
"""
self.value += 1
def run(self):
while not self.finish:
last_value = self.value
time.sleep(self.pulse_threshold)
if last_value != self.value:
pass
elif self.value != 0:
if self.value == 10:
self.number_q.put(0)
else:
self.number_q.put(self.value)
self.value = 0
class Telephone(object):
"""
Main Telephone class containing everything required for the Bluetooth telephone to work.
"""
CHUNK = 1024
def __init__(self, num_pin, receiver_pin, discoverable_pin=None, volume_pin_dict=None):
GPIO.setmode(GPIO.BCM)
self.receiver_pin = receiver_pin
self.number_q = Queue.Queue()
self.discoverable_pin = discoverable_pin # white button to trigger discovery and pairing.
self.discoverable = False
self.has_volume_controller = False
self.stop_audio = False
self.playing_audio = False
self.finish = False
if volume_pin_dict is not None:
self.has_volume_controller = True
self.volume_up_pin = volume_pin_dict['VOLUME_UP_PIN']
self.volume_down_pin = volume_pin_dict['VOLUME_DOWN_PIN']
self.volume_mute_pin = volume_pin_dict['VOLUME_MUTE_PIN']
else:
self.has_volume_controller = False
self.phone_manager = manager.PhoneManager()
self.bt_conn = self.phone_manager.bt_conn
"""Instantiate the thread that monitors the dial"""
self.rotary_dial = RotaryDial(num_pin, self.number_q)
""" instantiate the ringermanager object which exposes the dbus api for the ringer control"""
self.ringer = ringer.RingerManager()
# Load fast_dial numbers
with open("phonebook.yaml", 'r') as stream:
self.phonebook = yaml.safe_load(stream)
print(self.phonebook)
# Discoverability and volume control may not be available of phone model used. If they are then set up listeners
if discoverable_pin is not None:
# Set up the button to make it discoverable by preiously unpaired BT device.
print("Discoverable button available")
GPIO.setup(self.discoverable_pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.add_event_detect(self.discoverable_pin, GPIO.RISING, callback=self.make_discoverable, bouncetime=config.BUTTON_BOUNCE_TIME)
if self.has_volume_controller:
# Set volume up pin
print("Set up volume controls")
GPIO.setup(self.volume_up_pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.add_event_detect(self.volume_up_pin, GPIO.RISING, callback=self.volume_up, bouncetime=config.BUTTON_BOUNCE_TIME)
# Set volume down pin
GPIO.setup(self.volume_down_pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.add_event_detect(self.volume_down_pin, GPIO.RISING, callback=self.volume_down, bouncetime=config.BUTTON_BOUNCE_TIME)
# set up mute toggling function
GPIO.setup(self.volume_mute_pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.add_event_detect(self.volume_mute_pin, GPIO.RISING, callback=self.volume_mute_toggle, bouncetime=config.BUTTON_BOUNCE_TIME)
else:
print("No volume controls available")
# Receiver relevant functions
GPIO.setup(self.receiver_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
if GPIO.input(self.receiver_pin) is GPIO.HIGH:
self.receiver_down = False
else:
self.receiver_down = True
# self.receiver_changed(self.receiver_pin)
print("Initial receiver status = down ? {0}".format(self.receiver_down))
GPIO.add_event_detect(self.receiver_pin, GPIO.BOTH, callback=self.receiver_changed, bouncetime=config.RECEIVER_BOUNCE_TIME)
# Start rotary dial thread
self.rotary_dial.start()
def make_discoverable(self, pin_num):
"""
Set the RPi BT device to discoverable and pairable for 30 seconds. This is used only for pairing
device (e.g. a mobile phone) that has not previously been paired.
param: pin_num - the number of the GPIO pin that triggered the event - not used.
"""
self.bt_conn.make_discoverable(config.DISCOVERABLE_TIMEOUT)
def volume_up(self, pin):
self.phone_manager.volume_up(config.VOLUME_INCREMENT)
print(f"Volume Up: Mic volume = {self.phone_manager.mic_volume}")
def volume_down(self, pin):
self.phone_manager.volume_down(config.VOLUME_INCREMENT)
print(f"Volumne Down: Mic volume = {self.phone_manager.mic_volume}")
def volume_mute_toggle(self, pin):
self.phone_manager.mute_toggle()
print(f"Toggle mute: Current status = {self.phone_manager.muted}")
def nullhandler(self, value):
"""
Used by the phone status dbus service. When passed in twice to the method call,
this makes the method calls asynchronous
"""
pass
def receiver_changed(self, pin_num):
"""
Event triggered when the receiver is hung of lifted.
:param pin_num: GPIO pin triggering the event (Can only be self.receiver_pin here)
:return: None
"""
print("Receiver status changed..")
if GPIO.input(pin_num) is GPIO.HIGH:
print("Receiver Up")
self.receiver_down = False
if self.phone_manager.call_in_progress:
self.phone_manager.answer_call()
else:
# else we're picking the receiver up to begin dialing
# """For debugging the ringer."""
# print("try to ring")
# bus = dbus.SystemBus()
# ringer_service = dbus.Interface(bus.get_object('org.frank', '/'), 'phone.status')
# ringer_service.send_to_ringer(config.RING_START, reply_handler=self.nullhandler, error_handler=self.nullhandler)
self.start_file("/home/pi/bluetooth-phone/dial_tone.wav", loop=True)
else:
print("Receiver Down")
if self.phone_manager.call_in_progress:
print("Hanging up")
self.phone_manager.end_call()
self.receiver_down = True
self.stop_file() # kill thread that might be playing the dial tone.
def start_file(self, filename, loop=False):
"""
Start a thread reproducing an audio file
:param filename: The name of the file to play
:param loop: If the file should be played as a loop (like in the case of the dial tone)
"""
print("Play file : telephone object {0}".format(filename))
self._thread = Thread(target=self.__play_file, args=[filename, loop])
self._thread.start()
self.playing_audio = True
def __play_file(self, filename, loop):
"""
Private function handling the wav file replay
:param filename: The name of the file to play
:param loop: If the file should be played as a loop (like in the case of the dial tone)
"""
self.stop_audio = False
if not loop:
# open a wav format music
f = wave.open(filename, "rb")
# open stream
stream = alsaaudio.PCM(type=alsaaudio.PCM_PLAYBACK, mode=alsaaudio.PCM_NORMAL, device='plughw:1,0')
stream.setchannels(f.getnchannels())
stream.setrate(f.getframerate())
# read data
data = f.readframes(self.CHUNK)
# play stream
while data and not self.stop_audio:
stream.write(data)
data = f.readframes(self.CHUNK)
f.close()
stream = None
else:
# open a wav format music
f = wave.open(filename, "rb")
# open stream
stream = alsaaudio.PCM(type=alsaaudio.PCM_PLAYBACK, mode=alsaaudio.PCM_NORMAL, device='plughw:1,0')
stream.setchannels(f.getnchannels())
stream.setrate(f.getframerate())
# read data
data = f.readframes(self.CHUNK)
# play stream
while loop and not self.stop_audio:
f.rewind()
data = f.readframes(self.CHUNK)
while data and not self.stop_audio:
stream.write(data)
data = f.readframes(self.CHUNK)
f.close()
stream = None
def stop_file(self):
self.stop_audio = True
self.playing_audio = False
print("stopping sound")
def dialing_handler(self):
"""
Main function of the telephone that handles the dialing if the receiver is lifted or hooked.
If only a single digit is dialed (with the handset up) its interpreted as being a speed dial
Number
:return: None
"""
number = ''
while not self.finish:
if not self.receiver_down: # Handling of the dialing when the receiver is lifted
try:
c = self.number_q.get(timeout=5)
# # turn off dial tone as soon as a number is dialed.
# if not number == '' and self.playing_audio:
# self.stop_file()
number += str(c)
except Queue.Empty:
if number is not '':
if len(number) > 1:
print("Dialing: %s" % number)
self.stop_file()
self.phone_manager.call(number)
number = ''
else: # Handling of the dialing for speed dial from phonebook
if self.playing_audio:
self.stop_file()
try:
print("Selected %d" % c)
if c == 9:
print("Turning system off")
self.start_file("/home/pi/bluetooth-phone/turnoff.wav")
time.sleep(6)
subprocess.call("sudo shutdown -h now", shell=True)
elif c <= len(self.phonebook):
print("Shortcut action %d: Automatic dial" % c)
number = self.phonebook[c - 1]['number']
print(number)
time.sleep(4)
self.phone_manager.call(number)
number = ''
except Queue.Empty:
pass
else:
"""
If the receiver is down the must clear the number and the queue constantly because
noise on the dialer pins can cause spirious rising edges when the receiver is lifted or put down.
"""
number = ''
if len(self.number_q.queue) >= 1:
self.number_q.queue.clear()
print("Queue Cleared")
def close(self):
self.rotary_dial.finish = True
self.phone_manager.loop.quit()
GPIO.cleanup()
if __name__ == '__main__':
#create and instance of the telephone
t = Telephone(config.NS_PIN, config.HOERER_PIN, config.DISCOVERABLE_PIN, config.VOLUME_PIN_DICT)
try:
# enter the dialing handler loop
t.dialing_handler()
except KeyboardInterrupt:
print("stopped by keyboard")
pass
t.close()