-
Notifications
You must be signed in to change notification settings - Fork 0
/
buzzer.py
executable file
·169 lines (144 loc) · 4.47 KB
/
buzzer.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
#!/usr/bin/python
#-*- coding: latin-1 -*-
import RPi.GPIO as GPIO
import atexit
import time
class Buzzer (object):
"""Object to operate the piezo buzzer.
This module can either give normal beeps for warning or acknowledgement
purposes or, as a special treat, do meesages in Morse code.
"""
# The Morse alphabet
CODE = {" ": ' ',
"'": '.----.',
'(': '-.--.-',
')': '-.--.-',
',': '--..--',
'-': '-....-',
'.': '.-.-.-',
'/': '-..-.',
'0': '-----',
'1': '.----',
'2': '..---',
'3': '...--',
'4': '....-',
'5': '.....',
'6': '-....',
'7': '--...',
'8': '---..',
'9': '----.',
':': '---...',
';': '-.-.-.',
'?': '..--..',
'A': '.-',
'B': '-...',
'C': '-.-.',
'D': '-..',
'E': '.',
'F': '..-.',
'G': '--.',
'H': '....',
'I': '..',
'J': '.---',
'K': '-.-',
'L': '.-..',
'M': '--',
'N': '-.',
'O': '---',
'P': '.--.',
'Q': '--.-',
'R': '.-.',
'S': '...',
'T': '-',
'U': '..-',
'V': '...-',
'W': '.--',
'X': '-..-',
'Y': '-.--',
'Z': '--..',
'_': '..--.-'}
DURATION_UNIT = 0.025
def __init__ (self, buzzer_pin):
"""Create the Buzzer object.
:param pin: GPIO pin (BCM notation) the buzzer is connected to
"""
self.buzzer_pin = buzzer_pin
GPIO.setmode (GPIO.BCM)
GPIO.setup (self.buzzer_pin, GPIO.OUT)
atexit.register (self.cleanup)
def cleanup (self):
"""Free GPIO pin on object destruction"""
GPIO.cleanup (self.buzzer_pin)
def _dot (self):
"""Output a dot"""
GPIO.output (self.buzzer_pin, 1)
time.sleep (Buzzer.DURATION_UNIT)
GPIO.output (self.buzzer_pin, 0)
time.sleep (Buzzer.DURATION_UNIT)
#print ".",
def _dash (self):
"""Output a dash"""
GPIO.output (self.buzzer_pin, 1)
time.sleep (3 * Buzzer.DURATION_UNIT)
GPIO.output (self.buzzer_pin, 0)
time.sleep (Buzzer.DURATION_UNIT)
#print "_",
def _gap (self):
"""Wait for the duration between two letters"""
time.sleep (3 * Buzzer.DURATION_UNIT)
#print "/",
def _space (self):
"""Wait for the duration of a space
Actually the space duration is 7 units. However the gap
delimiter of 3 units will also be sent, so here we are
only allowing for 4 additional units waiting time."""
time.sleep (4 * Buzzer.DURATION_UNIT)
#print " ",
def MorseSend (self, msg):
"""Send a message as Morse code to the buzzer
:param msg: String to be sent (default: none)
"""
for letter in msg:
try:
for symbol in Buzzer.CODE[letter.upper ()]:
if symbol == '-':
self._dash ()
elif symbol == '.':
self._dot ()
elif symbol == ' ':
self._space ()
self._gap ()
except KeyError:
# ignore unknown symbols
pass
def AcknowledgeBeepShort (self, num = 1):
"""Send a very short beep as acknowledgement sound.
:param num: number of beeps (default: 1)
"""
while num > 0:
GPIO.output (self.buzzer_pin, 1)
time.sleep (0.0005)
GPIO.output (self.buzzer_pin, 0)
num -= 1
if num > 0:
time.sleep (0.1)
def AcknowledgeBeepLong (self):
"""Send a longer beep as acknowledgement sound."""
GPIO.output (self.buzzer_pin, 1)
time.sleep (0.15)
GPIO.output (self.buzzer_pin, 0)
if __name__ == '__main__':
"""Just testing"""
buzzer = Buzzer (4)
buzzer.MorseSend ("SOS ")
buzzer.MorseSend ("SOS ")
buzzer.MorseSend ("SOS ")
buzzer.MorseSend (" ")
buzzer.MorseSend (" ")
buzzer.MorseSend (" ")
buzzer.AcknowledgeBeepLong ()
buzzer.MorseSend (" ")
buzzer.MorseSend (" ")
buzzer.MorseSend (" ")
buzzer.MorseSend (" ")
buzzer.AcknowledgeBeepShort ()