-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbuzzer.py
56 lines (48 loc) · 1.33 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
###############################################################################
#
# Copyright (c) 2017-2023 Master AI, Inc.
# ALL RIGHTS RESERVED
#
# Use of this library, in source or binary form, is prohibited without written
# approval from Master AI, Inc.
#
###############################################################################
"""
This module provides a simple helper to use the buzzer.
"""
from auto.asyncio_tools import thread_safe
from auto.capabilities import list_caps, acquire
from auto import IS_VIRTUAL
from car import physics
@thread_safe
def buzz(notes):
"""
Play the given `notes` on the device's buzzer.
"""
global _BUZZER
try:
_BUZZER
except NameError:
caps = list_caps()
if 'Buzzer' not in caps:
raise AttributeError("This device does not have a buzzer.")
_BUZZER = acquire('Buzzer')
_BUZZER.play(notes)
_BUZZER.wait()
def honk(count=2):
"""
Make a car horn ("HONK") sound.
"""
MAX_HONKS = 5
count = min(MAX_HONKS, count)
if IS_VIRTUAL:
_physics_honk(count)
else:
for _ in range(count - 1):
buzz('!T95 O4 G#16 R16') # short honk
buzz('!T95 O4 G#4') # final long honk
def _physics_honk(count):
physics._get_physics().control({
'type': 'honk',
'count': count,
})