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 set_light_brightness(ch, brightness, serial_port, delay=0): 33 """Turn on light to specified brightness. 34 35 Args: 36 ch: str; light to turn on in ARDUINO_VALID_CH 37 brightness: int value of brightness between 0 and 255. 38 serial_port: object; serial port 39 delay: int; time in seconds 40 """ 41 if brightness < ARDUINO_BRIGHTNESS_MIN: 42 logging.debug('Brightness must be >= %d.', ARDUINO_BRIGHTNESS_MIN) 43 brightness = ARDUINO_BRIGHTNESS_MIN 44 elif brightness > ARDUINO_BRIGHTNESS_MAX: 45 logging.debug('Brightness must be <= %d.', ARDUINO_BRIGHTNESS_MAX) 46 brightness = ARDUINO_BRIGHTNESS_MAX 47 48 cmd = [struct.pack('B', i) for i in [ 49 ARDUINO_LIGHT_START_BYTE, int(ch), brightness]] 50 sensor_fusion_utils.arduino_send_cmd(serial_port, cmd) 51 time.sleep(delay) 52 53 54def lighting_control(lighting_cntl, lighting_ch): 55 """Establish communication with lighting controller. 56 57 lighting_ch is hard wired and must be determined from physical setup. 58 59 First initialize the port and send a test string defined by ARDUINO_TEST_CMD 60 to establish communications. 61 62 Args: 63 lighting_cntl: str to identify 'arduino' controller. 64 lighting_ch: str to identify lighting channel number. 65 Returns: 66 serial port pointer 67 """ 68 69 logging.debug('Controller: %s, ch: %s', lighting_cntl, lighting_ch) 70 if lighting_cntl.lower() == 'arduino': 71 # identify port 72 arduino_serial_port = sensor_fusion_utils.serial_port_def('arduino') 73 74 # send test cmd to Arduino until cmd returns properly 75 sensor_fusion_utils.establish_serial_comm(arduino_serial_port) 76 77 # return serial port 78 return arduino_serial_port 79 80 else: 81 logging.debug('No lighting control: need to control lights manually.') 82 return None 83 84 85def set_lighting_state(arduino_serial_port, lighting_ch, state): 86 """Turn lights ON in test rig. 87 88 Args: 89 arduino_serial_port: serial port object 90 lighting_ch: str for lighting channel 91 state: str 'ON/OFF' 92 """ 93 if state == 'ON': 94 level = 255 95 elif state == 'OFF': 96 level = 0 97 else: 98 raise AssertionError(f'Lighting state not defined correctly: {state}') 99 100 if arduino_serial_port: 101 set_light_brightness(lighting_ch, level, arduino_serial_port, delay=1) 102 else: 103 print(f'Turn {state} lights in rig and hit <ENTER> to continue.') 104 _, _, _ = select.select([sys.stdin], [], [], KEYBOARD_ENTRY_WAIT_TIME) 105 106