1#!/usr/bin/env python3 2# 3# Copyright 2018 - 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 17import enum 18import time 19 20from acts.controllers.relay_lib.devices.bluetooth_relay_device import BluetoothRelayDevice 21 22SHORT_PRESS_WAIT_TIME = 0.5 23MEDIUM_PRESS_WAIT_TIME = 3.0 24LONG_PRESS_WAIT_TIME = 4.5 25WAIT_FOR_EFFECT_TIME = 1 26 27 28class Buttons(enum.Enum): 29 NEXT = 'Next' 30 PREVIOUS = "Previous" 31 PLAY_PAUSE = 'Play_pause' 32 VOLUME_UP = "Volume_up" 33 VOLUME_DOWN = "Volume_down" 34 35 36class EarstudioReceiver(BluetoothRelayDevice): 37 38 def __init__(self, config, relay_rig): 39 BluetoothRelayDevice.__init__(self, config, relay_rig) 40 self._ensure_config_contains_relays(button.value for button in Buttons) 41 42 def power_on(self): 43 """Power on the Earstudio device. 44 45 BLUE LED blinks once when power is on. "power-on sound" plays when it is 46 on. Automatically connects to a device that has been connected before. 47 GREEN LED blinks once every 3 seconds after the "connection sound." 48 Enters Discoverable Mode/Paring Mode when there is no device that has 49 been connected before. GREEN LED blinks twice every 0.5 seconds. 50 """ 51 self.relays[Buttons.PLAY_PAUSE.value].set_nc_for(MEDIUM_PRESS_WAIT_TIME) 52 time.sleep(WAIT_FOR_EFFECT_TIME) 53 54 def power_off(self): 55 """Power off the Earstudio device. 56 57 RED LED blinks once right before power off. "power-off sound" plays when 58 it is off. 59 """ 60 self.relays[Buttons.PLAY_PAUSE.value].set_nc_for(LONG_PRESS_WAIT_TIME) 61 time.sleep(WAIT_FOR_EFFECT_TIME) 62 63 def press_play_pause(self): 64 """Toggle audio play state. 65 66 GREEN LED slowly blinks once every 3 seconds during Bluetooth/USB 67 playback. 68 """ 69 self.relays[Buttons.PLAY_PAUSE.value].set_nc_for(SHORT_PRESS_WAIT_TIME) 70 time.sleep(WAIT_FOR_EFFECT_TIME) 71 72 def press_accept_call(self): 73 """Receive incoming call. 74 75 BLUE LED slowly blinks once every 3 seconds 76 "Call-receiving sound" when received. 77 """ 78 self.relays[Buttons.PLAY_PAUSE.value].set_nc_for(SHORT_PRESS_WAIT_TIME) 79 time.sleep(WAIT_FOR_EFFECT_TIME) 80 81 def press_reject_call(self): 82 """Reject incoming call. 83 84 "Call-rejection sound" when refused. 85 """ 86 self.relays[Buttons.PLAY_PAUSE.value].set_nc_for(MEDIUM_PRESS_WAIT_TIME) 87 time.sleep(WAIT_FOR_EFFECT_TIME) 88 89 def press_end_call(self): 90 """End ongoing call. 91 92 "Call-end sound" when ended. 93 """ 94 self.relays[Buttons.PLAY_PAUSE.value].set_nc_for(SHORT_PRESS_WAIT_TIME) 95 time.sleep(WAIT_FOR_EFFECT_TIME) 96 97 def press_next(self): 98 """Skip to the next track.""" 99 self.relays[Buttons.NEXT.value].set_nc_for(SHORT_PRESS_WAIT_TIME) 100 time.sleep(WAIT_FOR_EFFECT_TIME) 101 102 def toggle_ambient_mode(self): 103 """Turn ambient mode on/off. 104 105 Only available during playback. 106 To use it, you must set 'Ambient Shortcut Key' to 'on' in the EarStudio 107 app. 108 """ 109 self.relays[Buttons.NEXT.value].set_nc_for(MEDIUM_PRESS_WAIT_TIME) 110 time.sleep(WAIT_FOR_EFFECT_TIME) 111 112 def press_previous(self): 113 """Rewind to beginning of current or previous track.""" 114 self.relays[Buttons.PREVIOUS.value].set_nc_for(SHORT_PRESS_WAIT_TIME) 115 time.sleep(WAIT_FOR_EFFECT_TIME) 116 117 def enter_pairing_mode(self): 118 """Enter BlueTooth pairing mode. 119 120 GREEN LED blinks twice every 0.5 seconds after "enter paring-mode 121 sound." Disconnects from the current connected device when entering 122 this mode. 123 """ 124 self.relays[Buttons.PREVIOUS.value].set_nc_for(MEDIUM_PRESS_WAIT_TIME) 125 time.sleep(WAIT_FOR_EFFECT_TIME) 126 127 def press_volume_up(self, press_duration=SHORT_PRESS_WAIT_TIME): 128 """Turn up the volume. 129 130 Volume increases by 0.5dB for each press. 131 Press&holding the button increases the volume consistently up to 6dB. 132 Args: 133 press_duration (int|float): how long to hold button for. 134 """ 135 self.relays[Buttons.VOLUME_UP.value].set_nc_for(press_duration) 136 time.sleep(WAIT_FOR_EFFECT_TIME) 137 138 def press_volume_down(self, press_duration=SHORT_PRESS_WAIT_TIME): 139 """Turn down the volume. 140 141 Volume decreases by 0.5dB for each press. 142 Press&hold the button decreases the volume consistently down to -60dB. 143 Pressing the button at the minimum volume turns to a mute level. 144 Args: 145 press_duration (int|float): how long to hold button for. 146 """ 147 self.relays[Buttons.VOLUME_DOWN.value].set_nc_for(press_duration) 148 time.sleep(WAIT_FOR_EFFECT_TIME) 149