1# Copyright 2021 The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#      http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14"""Utility functions for sensor_fusion hardware rig."""
15
16
17import logging
18import select
19import struct
20import sys
21import time
22import sensor_fusion_utils
23
24# Constants for Arduino
25ARDUINO_BRIGHTNESS_MAX = 255
26ARDUINO_BRIGHTNESS_MIN = 0
27ARDUINO_LIGHT_START_BYTE = 254
28
29KEYBOARD_ENTRY_WAIT_TIME = 20  # seconds to wait for keyboard entry
30
31
32def _toggle_screen_state(device, desired_state):
33  """Trigger device power key to toggle screen."""
34  current_state = 'ON'
35  if desired_state == 'ON':
36    current_state = 'OFF'
37  output = device.adb.shell('dumpsys display | grep mScreenState=')
38  output_val = str(output.decode('utf-8')).strip()
39  if current_state in output_val:
40    device.adb.shell(['input', 'keyevent', 'KEYCODE_POWER'])
41
42
43def turn_off_device_screen(device):
44  """Turn off a device screen via power key if screen is on."""
45  _toggle_screen_state(device, 'OFF')
46
47
48def turn_on_device_screen(device):
49  """Turn on a device screen via power key if screen is on."""
50  _toggle_screen_state(device, 'ON')
51
52
53def set_light_brightness(ch, brightness, serial_port, delay=0):
54  """Turn on light to specified brightness.
55
56  Args:
57    ch: str; light to turn on in ARDUINO_VALID_CH
58    brightness: int value of brightness between 0 and 255.
59    serial_port: object; serial port
60    delay: int; time in seconds
61  """
62  if brightness < ARDUINO_BRIGHTNESS_MIN:
63    logging.debug('Brightness must be >= %d.', ARDUINO_BRIGHTNESS_MIN)
64    brightness = ARDUINO_BRIGHTNESS_MIN
65  elif brightness > ARDUINO_BRIGHTNESS_MAX:
66    logging.debug('Brightness must be <= %d.', ARDUINO_BRIGHTNESS_MAX)
67    brightness = ARDUINO_BRIGHTNESS_MAX
68
69  cmd = [struct.pack('B', i) for i in [
70      ARDUINO_LIGHT_START_BYTE, int(ch), brightness]]
71  sensor_fusion_utils.arduino_send_cmd(serial_port, cmd)
72  time.sleep(delay)
73
74
75def lighting_control(lighting_cntl, lighting_ch):
76  """Establish communication with lighting controller.
77
78  lighting_ch is hard wired and must be determined from physical setup.
79
80  First initialize the port and send a test string defined by ARDUINO_TEST_CMD
81  to establish communications.
82
83  Args:
84    lighting_cntl: str to identify 'arduino' controller.
85    lighting_ch: str to identify lighting channel number.
86  Returns:
87    serial port pointer
88  """
89
90  logging.debug('Controller: %s, ch: %s', lighting_cntl, lighting_ch)
91  if lighting_cntl.lower() == 'arduino':
92    # identify port
93    arduino_serial_port = sensor_fusion_utils.serial_port_def('arduino')
94
95    # send test cmd to Arduino until cmd returns properly
96    sensor_fusion_utils.establish_serial_comm(arduino_serial_port)
97
98    # return serial port
99    return arduino_serial_port
100
101  else:
102    logging.debug('No lighting control: need to control lights manually.')
103    return None
104
105
106def set_lighting_state(arduino_serial_port, lighting_ch, state):
107  """Turn lights ON in test rig.
108
109  Args:
110    arduino_serial_port: serial port object
111    lighting_ch: str for lighting channel
112    state: str 'ON/OFF'
113  """
114  if state == 'ON':
115    level = 255
116  elif state == 'OFF':
117    level = 0
118  else:
119    raise AssertionError(f'Lighting state not defined correctly: {state}')
120
121  if arduino_serial_port:
122    set_light_brightness(lighting_ch, level, arduino_serial_port, delay=1)
123  else:
124    print(f'Turn {state} lights in rig and hit <ENTER> to continue.')
125    _, _, _ = select.select([sys.stdin], [], [], KEYBOARD_ENTRY_WAIT_TIME)
126
127