1#!/usr/bin/env python3
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 enum
17import time
18
19from acts.controllers.relay_lib.devices.bluetooth_relay_device import BluetoothRelayDevice
20
21# Necessary timeout in between commands
22CMD_TIMEOUT = 1.2
23# Pairing mode activation wait time
24PAIRING_MODE_WAIT_TIME = 4.5
25SINGLE_ACTION_SHORT_WAIT_TIME = 0.6
26SINGLE_ACTION_LONG_WAIT_TIME = 2.0
27MISSING_RELAY_MSG = 'Relay config for Three button "%s" missing relay "%s".'
28
29
30class Buttons(enum.Enum):
31    ACTION = 'Action'
32    NEXT = 'Next'
33    PREVIOUS = 'Previous'
34
35
36class SingleButtonDongle(BluetoothRelayDevice):
37    """A Bluetooth dongle with one generic button Normally action.
38
39    Wraps the button presses, as well as the special features like pairing.
40    """
41
42    def __init__(self, config, relay_rig):
43        BluetoothRelayDevice.__init__(self, config, relay_rig)
44        self._ensure_config_contains_relay(Buttons.ACTION.value)
45
46    def enter_pairing_mode(self):
47        """Enters pairing mode. Blocks the thread until pairing mode is set.
48
49        Holds down the 'ACTION' buttons for PAIRING_MODE_WAIT_TIME seconds.
50        """
51        self.relays[Buttons.ACTION.value].set_nc_for(
52            seconds=PAIRING_MODE_WAIT_TIME)
53
54    def press_play_pause(self):
55        """Briefly presses the Action button."""
56        self.relays[Buttons.ACTION.value].set_nc_for(
57            seconds=SINGLE_ACTION_SHORT_WAIT_TIME)
58
59    def press_vr_mode(self):
60        """Long press the Action button."""
61        self.relays[Buttons.ACTION.value].set_nc_for(
62            seconds=SINGLE_ACTION_LONG_WAIT_TIME)
63
64    def setup(self):
65        """Sets all relays to their default state (off)."""
66        BluetoothRelayDevice.setup(self)
67
68    def clean_up(self):
69        """Sets all relays to their default state (off)."""
70        BluetoothRelayDevice.clean_up(self)
71
72
73class ThreeButtonDongle(BluetoothRelayDevice):
74    """A Bluetooth dongle with three generic buttons Normally action, next, and
75     previous.
76
77    Wraps the button presses, as well as the special features like pairing.
78    """
79
80    def __init__(self, config, relay_rig):
81        BluetoothRelayDevice.__init__(self, config, relay_rig)
82        self._ensure_config_contains_relays(button.value for button in Buttons)
83
84    def setup(self):
85        """Sets all relays to their default state (off)."""
86        BluetoothRelayDevice.setup(self)
87
88    def clean_up(self):
89        """Sets all relays to their default state (off)."""
90        BluetoothRelayDevice.clean_up(self)
91
92    def enter_pairing_mode(self):
93        """Enters pairing mode. Blocks the thread until pairing mode is set.
94
95        Holds down the 'ACTION' buttons for a little over 5 seconds.
96        """
97        self.relays[Buttons.ACTION.value].set_nc_for(
98            seconds=PAIRING_MODE_WAIT_TIME)
99
100    def press_play_pause(self):
101        """Briefly presses the Action button."""
102        self.relays[Buttons.ACTION.value].set_nc_for(
103            seconds=SINGLE_ACTION_SHORT_WAIT_TIME)
104        time.sleep(CMD_TIMEOUT)
105
106    def press_vr_mode(self):
107        """Long press the Action button."""
108        self.relays[Buttons.ACTION.value].set_nc_for(
109            seconds=SINGLE_ACTION_LONG_WAIT_TIME)
110        time.sleep(CMD_TIMEOUT)
111
112    def press_next(self):
113        """Briefly presses the Next button."""
114        self.relays[Buttons.NEXT.value].set_nc_for(
115            seconds=SINGLE_ACTION_SHORT_WAIT_TIME)
116        time.sleep(CMD_TIMEOUT)
117
118    def press_previous(self):
119        """Briefly presses the Previous button."""
120        self.relays[Buttons.PREVIOUS.value].set_nc_for(
121            seconds=SINGLE_ACTION_SHORT_WAIT_TIME)
122        time.sleep(CMD_TIMEOUT)
123