forked from modmypi/PiModules
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbeep
More file actions
86 lines (74 loc) · 2.75 KB
/
beep
File metadata and controls
86 lines (74 loc) · 2.75 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/env python
# coding: utf-8
###############################################################################
# Python version of the linux beep program to control the UPS PIco's buzzer.
# Copyright (C) 2018 Jimmy DEVEIL <jimmy.deveil@gmail.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
###############################################################################
import smbus
import time
import sys
import getopt
chip_address = 0x6b
freq_address = 0x0e
dur_address = 0x10
version = 'beep-1.0.0 python version for i2c device'
class Beep:
def __repr__(self):
return version
def usage(self):
print """Usage:
beep [-f freq] [-l length] [-r reps] [-d delay] [-D delay] [--verbose | --debug]
beep [Options...] [-n] [--new] [Options...] ...
beep [-h] [--help]
beep [-v] [-V] [--version]"""
def __init__(self, argv):
self.i2c = smbus.SMBus(1)
self.beeps = []
try: opts, args = getopt.getopt(argv, 'f:l:r:d:D:nshvV', ['new', 'help', 'version'])
except:
self.usage()
sys.exit(1)
b = {}
for opt, arg in opts:
if opt in ('-h', '--help'):
self.usage()
sys.exit(0)
elif opt in ('-v', '-V', '--version'):
print self
sys.exit(0)
elif opt in ('-n', '--new'):
self.beeps.append(b)
b = {}
elif opt == '-f':
b['frequency'] = int(float(arg))
elif opt == '-l':
b['duration'] = min(255, int(arg)/10)
elif opt == '-r':
b['repeat'] = int(arg)
elif opt in ('-d', '-D'):
b['delay'] = int(arg)/1000.0
self.beeps.append(b)
def beep(self):
for b in self.beeps:
self._beep(**b)
def _beep(self, duration=10, frequency=750, repeat=1, delay=0):
delay = duration/100.0 + delay
for i in range(repeat):
self.i2c.write_word_data(chip_address, freq_address, frequency)
self.i2c.write_byte_data(chip_address, dur_address, duration)
time.sleep(delay)
if __name__ == '__main__':
Beep(sys.argv[1:]).beep()