Home | History | Annotate | Download | only in relay_lib
      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.
     16 import time
     17 import enum
     18 from acts.controllers.relay_lib.generic_relay_device import GenericRelayDevice
     19 from acts.controllers.relay_lib.relay import SynchronizeRelays
     20 from acts.controllers.relay_lib.errors import RelayConfigError
     21 from acts.controllers.relay_lib.helpers import validate_key
     22 
     23 # Necessary timeout inbetween commands
     24 CMD_TIMEOUT = 1.2
     25 # Pairing mode activation wait time
     26 PAIRING_MODE_WAIT_TIME = 4.5
     27 SINGLE_ACTION_SHORT_WAIT_TIME = 0.6
     28 SINGLE_ACTION_LONG_WAIT_TIME = 2.0
     29 MISSING_RELAY_MSG = 'Relay config for Three button  "%s" missing relay "%s".'
     30 
     31 
     32 class Buttons(enum.Enum):
     33     ACTION = 'Action'
     34     NEXT = 'Next'
     35     PREVIOUS = 'Previous'
     36 
     37 
     38 class SingleButtonDongle(GenericRelayDevice):
     39     """A Bluetooth dongle with one generic button Normally action.
     40 
     41     Wraps the button presses, as well as the special features like pairing.
     42     """
     43 
     44     def __init__(self, config, relay_rig):
     45         GenericRelayDevice.__init__(self, config, relay_rig)
     46 
     47         self.mac_address = validate_key('mac_address', config, str,
     48                                         'SingleButtonDongle')
     49 
     50         self.ensure_config_contains_relay(Buttons.ACTION.value)
     51 
     52     def ensure_config_contains_relay(self, relay_name):
     53         """Throws an error if the relay does not exist."""
     54         if relay_name not in self.relays:
     55             raise RelayConfigError(MISSING_RELAY_MSG % (self.name, relay_name))
     56 
     57     def setup(self):
     58         """Sets all relays to their default state (off)."""
     59         GenericRelayDevice.setup(self)
     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 'ACTION' buttons for PAIRING_MODE_WAIT_TIME seconds.
     69         """
     70         self.relays[Buttons.ACTION.value].set_nc_for(
     71             seconds=PAIRING_MODE_WAIT_TIME)
     72 
     73     def press_play_pause(self):
     74         """Briefly presses the Action button."""
     75         self.relays[Buttons.ACTION.value].set_nc_for(
     76             seconds=SINGLE_ACTION_SHORT_WAIT_TIME)
     77 
     78     def press_vr_mode(self):
     79         """Long press the Action button."""
     80         self.relays[Buttons.ACTION.value].set_nc_for(
     81             seconds=SINGLE_ACTION_LONG_WAIT_TIME)
     82 
     83 
     84 class ThreeButtonDongle(GenericRelayDevice):
     85     """A Bluetooth dongle with three generic buttons Normally action, next, and
     86      previous.
     87 
     88     Wraps the button presses, as well as the special features like pairing.
     89     """
     90 
     91     def __init__(self, config, relay_rig):
     92         GenericRelayDevice.__init__(self, config, relay_rig)
     93 
     94         self.mac_address = validate_key('mac_address', config, str,
     95                                         'ThreeButtonDongle')
     96 
     97         for button in Buttons:
     98             self.ensure_config_contains_relay(button.value)
     99 
    100     def ensure_config_contains_relay(self, relay_name):
    101         """Throws an error if the relay does not exist."""
    102         if relay_name not in self.relays:
    103             raise RelayConfigError(MISSING_RELAY_MSG % (self.name, relay_name))
    104 
    105     def setup(self):
    106         """Sets all relays to their default state (off)."""
    107         GenericRelayDevice.setup(self)
    108 
    109     def clean_up(self):
    110         """Sets all relays to their default state (off)."""
    111         GenericRelayDevice.clean_up(self)
    112 
    113     def enter_pairing_mode(self):
    114         """Enters pairing mode. Blocks the thread until pairing mode is set.
    115 
    116         Holds down the 'ACTION' buttons for a little over 5 seconds.
    117         """
    118         self.relays[Buttons.ACTION.value].set_nc_for(
    119             seconds=PAIRING_MODE_WAIT_TIME)
    120 
    121     def press_play_pause(self):
    122         """Briefly presses the Action button."""
    123         self.relays[Buttons.ACTION.value].set_nc_for(
    124             seconds=SINGLE_ACTION_SHORT_WAIT_TIME)
    125         time.sleep(CMD_TIMEOUT)
    126 
    127     def press_vr_mode(self):
    128         """Long press the Action button."""
    129         self.relays[Buttons.ACTION.value].set_nc_for(
    130             seconds=SINGLE_ACTION_LONG_WAIT_TIME)
    131         time.sleep(CMD_TIMEOUT)
    132 
    133     def press_next(self):
    134         """Briefly presses the Next button."""
    135         self.relays[Buttons.NEXT.value].set_nc_for(
    136             seconds=SINGLE_ACTION_SHORT_WAIT_TIME)
    137         time.sleep(CMD_TIMEOUT)
    138 
    139     def press_previous(self):
    140         """Briefly presses the Previous button."""
    141         self.relays[Buttons.PREVIOUS.value].set_nc_for(
    142             seconds=SINGLE_ACTION_SHORT_WAIT_TIME)
    143         time.sleep(CMD_TIMEOUT)
    144