1#!/usr/bin/env python
2#
3#   Copyright 2017 - The Android Open Source Project
4#
5#   Licensed under the Apache License, Version 2.0 (the "License");
6#   you may not use this file except in compliance with the License.
7#   You may obtain a copy of the License at
8#
9#       http://www.apache.org/licenses/LICENSE-2.0
10#
11#   Unless required by applicable law or agreed to in writing, software
12#   distributed under the License is distributed on an "AS IS" BASIS,
13#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14#   See the License for the specific language governing permissions and
15#   limitations under the License.
16import time
17import enum
18from acts.controllers.relay_lib.generic_relay_device import GenericRelayDevice
19from acts.controllers.relay_lib.relay import SynchronizeRelays
20from acts.controllers.relay_lib.errors import RelayConfigError
21from acts.controllers.relay_lib.helpers import validate_key
22
23PAIRING_MODE_WAIT_TIME = 5.2
24MISSING_RELAY_MSG = 'Relay config for FuguRemote "%s" missing relay "%s".'
25
26
27class Buttons(enum.Enum):
28    HOME = 'Home'
29    BACK = 'Back'
30    PLAY_PAUSE = 'Play'
31
32
33class FuguRemote(GenericRelayDevice):
34    """A Nexus Player (Fugu) Remote.
35
36    Wraps the button presses, as well as the special features like pairing.
37    """
38
39    def __init__(self, config, relay_rig):
40        GenericRelayDevice.__init__(self, config, relay_rig)
41
42        self.mac_address = validate_key('mac_address', config, str,
43                                        'FuguRemote')
44
45        for button in Buttons:
46            self.ensure_config_contains_relay(button.value)
47
48    def ensure_config_contains_relay(self, relay_name):
49        """Throws an error if the relay does not exist."""
50        if relay_name not in self.relays:
51            raise RelayConfigError(MISSING_RELAY_MSG % (self.name, relay_name))
52
53    def setup(self):
54        """Sets all relays to their default state (off)."""
55        GenericRelayDevice.setup(self)
56        # If the Fugu remote does have a power relay attached, turn it on.
57        power = 'Power'
58        if power in self.relays:
59            self.relays[power].set_nc()
60
61    def clean_up(self):
62        """Sets all relays to their default state (off)."""
63        GenericRelayDevice.clean_up(self)
64
65    def enter_pairing_mode(self):
66        """Enters pairing mode. Blocks the thread until pairing mode is set.
67
68        Holds down the 'Home' and 'Back' buttons for a little over 5 seconds.
69        """
70        with SynchronizeRelays():
71            self.hold_down(Buttons.HOME.value)
72            self.hold_down(Buttons.BACK.value)
73
74        time.sleep(PAIRING_MODE_WAIT_TIME)
75
76        with SynchronizeRelays():
77            self.release(Buttons.HOME.value)
78            self.release(Buttons.BACK.value)
79
80    def press_play_pause(self):
81        """Briefly presses the Play/Pause button."""
82        self.press(Buttons.PLAY_PAUSE.value)
83
84    def press_home(self):
85        """Briefly presses the Home button."""
86        self.press(Buttons.HOME.value)
87
88    def press_back(self):
89        """Briefly presses the Back button."""
90        self.press(Buttons.BACK.value)
91