-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbeep.py
More file actions
executable file
·86 lines (68 loc) · 1.79 KB
/
beep.py
File metadata and controls
executable file
·86 lines (68 loc) · 1.79 KB
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
#!/usr/bin/python3
# coding=utf-8
"""
Sends n beeps to a piezo buzzer.
Usage for 5 beeps: beep.py 5
"""
# wait time in seconds -> This is the square wave for the piezo
waitTime = 0.0004
# this is just a count, not a time unit
toneLength = 400
# pause between each tone in seconds
tonePause = 0.5
# for getting arguments
import sys
# check arguments
if len(sys.argv) != 2:
# print("1. argument: " + sys.argv[1])
# print("2. argument: " + sys.argv[2])
print("ERROR. Correct usage for three beeps is: " + sys.argv[0] + " 3")
sys.exit(-1)
# for GPIO pin usage
try:
import RPi.GPIO as GPIO
except RuntimeError:
print("Error importing RPi.GPIO!")
# for sleep
import time
##
## GPIO stuff
##
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM) # use the GPIO names, _not_ the pin numbers on the board
# Raspberry Pi pin configuration:
# pins BCM BOARD
piezoPin = 13 # pin
# GPIO setup
GPIO.setup(piezoPin, GPIO.OUT)
# for signal handling
import signal
import sys
# my signal handler
def sig_handler(_signo, _stack_frame):
# GPIO cleanup
GPIO.cleanup()
print("beep terminated clean.")
sys.exit(0)
# signals to be handled
signal.signal(signal.SIGINT, sig_handler)
signal.signal(signal.SIGHUP, sig_handler)
signal.signal(signal.SIGTERM, sig_handler)
######
###### Beeping
######
for x in range(0, int(sys.argv[1])):
# 10 LOW/HIGH signales generate a kind of square wave
for n in range(0, toneLength):
# Piezo OFF
GPIO.output(piezoPin, GPIO.HIGH)
# wait
time.sleep(waitTime)
# Piezo ON (low active!)
GPIO.output(piezoPin, GPIO.LOW)
# "wait" (generate a square wave for the piezo)
time.sleep(waitTime)
# "wait" (generate a square wave for the piezo)
time.sleep(tonePause)
# GPIO cleanup
GPIO.cleanup()