GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

bricknil.sensor.sound.DuploSpeaker.play_sound()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 5
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nop 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
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