xref: /aosp_15_r20/cts/apps/CameraITS/utils/lighting_control_utils.py (revision b7c941bb3fa97aba169d73cee0bed2de8ac964bf)
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 struct
19import time
20import sensor_fusion_utils
21
22# Constants for Arduino
23ARDUINO_BRIGHTNESS_MAX = 255
24ARDUINO_BRIGHTNESS_MIN = 0
25ARDUINO_LIGHT_START_BYTE = 254
26
27
28def _toggle_screen_state(device, desired_state):
29  """Trigger device power key to toggle screen."""
30  current_state = 'ON'
31  if desired_state == 'ON':
32    current_state = 'OFF'
33  output = device.adb.shell('dumpsys display | grep mScreenState=')
34  output_val = str(output.decode('utf-8')).strip()
35  if current_state in output_val:
36    device.adb.shell(['input', 'keyevent', 'KEYCODE_POWER'])
37
38
39def turn_off_device_screen(device):
40  """Turn off a device screen via power key if screen is on."""
41  _toggle_screen_state(device, 'OFF')
42
43
44def turn_on_device_screen(device):
45  """Turn on a device screen via power key if screen is on."""
46  _toggle_screen_state(device, 'ON')
47
48
49def set_light_brightness(ch, brightness, serial_port, delay=0):
50  """Turn on light to specified brightness.
51
52  Args:
53    ch: str; light to turn on in ARDUINO_VALID_CH
54    brightness: int value of brightness between 0 and 255.
55    serial_port: object; serial port
56    delay: int; time in seconds
57  """
58  if brightness < ARDUINO_BRIGHTNESS_MIN:
59    logging.debug('Brightness must be >= %d.', ARDUINO_BRIGHTNESS_MIN)
60    brightness = ARDUINO_BRIGHTNESS_MIN
61  elif brightness > ARDUINO_BRIGHTNESS_MAX:
62    logging.debug('Brightness must be <= %d.', ARDUINO_BRIGHTNESS_MAX)
63    brightness = ARDUINO_BRIGHTNESS_MAX
64
65  cmd = [struct.pack('B', i) for i in [
66      ARDUINO_LIGHT_START_BYTE, int(ch), brightness]]
67  sensor_fusion_utils.arduino_send_cmd(serial_port, cmd)
68  time.sleep(delay)
69
70
71def lighting_control(lighting_cntl, lighting_ch):
72  """Establish communication with lighting controller.
73
74  lighting_ch is hard wired and must be determined from physical setup.
75
76  First initialize the port and send a test string defined by ARDUINO_TEST_CMD
77  to establish communications.
78
79  Args:
80    lighting_cntl: str to identify 'arduino' controller.
81    lighting_ch: str to identify lighting channel number.
82  Returns:
83    serial port pointer
84  """
85
86  logging.debug('Controller: %s, ch: %s', lighting_cntl, lighting_ch)
87  if lighting_cntl.lower() == 'arduino':
88    # identify port
89    arduino_serial_port = sensor_fusion_utils.serial_port_def('arduino')
90
91    # send test cmd to Arduino until cmd returns properly
92    sensor_fusion_utils.establish_serial_comm(arduino_serial_port)
93
94    # return serial port
95    return arduino_serial_port
96
97  else:
98    logging.debug('No lighting control: need to control lights manually.')
99    return None
100
101
102def set_lighting_state(arduino_serial_port, lighting_ch, state):
103  """Turn lights ON in test rig.
104
105  Args:
106    arduino_serial_port: serial port object
107    lighting_ch: str for lighting channel
108    state: str 'ON/OFF'
109  """
110  if state == 'ON':
111    level = 255
112  elif state == 'OFF':
113    level = 0
114  else:
115    raise AssertionError(f'Lighting state not defined correctly: {state}')
116
117  if arduino_serial_port:
118    set_light_brightness(lighting_ch, level, arduino_serial_port, delay=1)
119  else:
120    print(f'Turn {state} lights in rig and hit <ENTER> to continue.')
121    input('')
122
123