1
|
|
|
|
2
|
|
|
# Copyright 2019 Virantha N. Ekanayake |
3
|
|
|
# |
4
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License"); |
5
|
|
|
# you may not use this file except in compliance with the License. |
6
|
|
|
# You may obtain a copy of the License at |
7
|
|
|
# |
8
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0 |
9
|
|
|
# |
10
|
|
|
# Unless required by applicable law or agreed to in writing, software |
11
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, |
12
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13
|
|
|
# See the License for the specific language governing permissions and |
14
|
|
|
# limitations under the License. |
15
|
|
|
"""All sound related output devices |
16
|
|
|
""" |
17
|
|
|
from curio import sleep, current_task, spawn # Needed for motor speed ramp |
18
|
|
|
|
19
|
|
|
from enum import Enum, IntEnum |
20
|
|
|
from struct import pack |
21
|
|
|
|
22
|
|
|
from .peripheral import Peripheral |
23
|
|
|
|
24
|
|
|
class DuploSpeaker(Peripheral): |
25
|
|
|
"""Plays one of five preset sounds through the Duplo built-in speaker |
26
|
|
|
|
27
|
|
|
See :class:`sounds` for the list. |
28
|
|
|
|
29
|
|
|
Examples:: |
30
|
|
|
|
31
|
|
|
@attach(DuploSpeaker, name='speaker') |
32
|
|
|
... |
33
|
|
|
await self.speaker.play_sound(DuploSpeaker.sounds.brake) |
34
|
|
|
|
35
|
|
|
Notes: |
36
|
|
|
Uses Mode 1 to play the presets |
37
|
|
|
|
38
|
|
|
""" |
39
|
|
|
_sensor_id = 0x002A |
40
|
|
|
sounds = Enum('sounds', { 'brake': 3, |
41
|
|
|
'station': 5, |
42
|
|
|
'water': 7, |
43
|
|
|
'horn': 9, |
44
|
|
|
'steam': 10, |
45
|
|
|
}) |
46
|
|
|
|
47
|
|
|
async def activate_updates(self): |
48
|
|
|
"""For some reason, even though the speaker is an output device |
49
|
|
|
we need to send a Port Input Format Setup command (0x41) to enable |
50
|
|
|
notifications. Otherwise, none of the sound output commands will play. This function |
51
|
|
|
is called automatically after this sensor is attached. |
52
|
|
|
""" |
53
|
|
|
mode = 1 |
54
|
|
|
b = [0x00, 0x41, self.port, mode, 0x01, 0x00, 0x00, 0x00, 0x01] |
55
|
|
|
await self.send_message('Activate DUPLO Speaker: port {self.port}', b) |
56
|
|
|
|
57
|
|
|
async def play_sound(self, sound): |
58
|
|
|
assert isinstance(sound, self.sounds), 'Can only play sounds that are enums (DuploSpeaker.sounds.brake, etc)' |
59
|
|
|
mode = 1 |
60
|
|
|
self.message_info(f'Playing sound {sound.name}:{sound.value}') |
61
|
|
|
await self.set_output(mode, sound.value) |
62
|
|
|
|