1# Copyright 2014 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 15import time 16 17import its.caps 18import its.device 19import its.objects 20 21 22def main(): 23 """Test if image and motion sensor events are in the same time domain. 24 """ 25 26 with its.device.ItsSession() as cam: 27 props = cam.get_camera_properties() 28 props = cam.override_with_hidden_physical_camera_props(props) 29 30 # Only run test if the appropriate caps are claimed. 31 its.caps.skip_unless(its.caps.sensor_fusion(props) and 32 its.caps.backward_compatible(props)) 33 34 # Get the timestamp of a captured image. 35 if its.caps.manual_sensor(props): 36 req, fmt = its.objects.get_fastest_manual_capture_settings(props) 37 else: 38 req, fmt = its.objects.get_fastest_auto_capture_settings(props) 39 cap = cam.do_capture(req, fmt) 40 ts_image0 = cap['metadata']['android.sensor.timestamp'] 41 42 # Get the timestamps of motion events. 43 print 'Reading sensor measurements' 44 sensors = cam.get_sensors() 45 cam.start_sensor_events() 46 time.sleep(2.0) 47 events = cam.get_sensor_events() 48 ts_sensor_first = {} 49 ts_sensor_last = {} 50 for sensor, existing in sensors.iteritems(): 51 # Vibrator doesn't generate outputs: b/142653973 52 if existing and sensor != 'vibrator': 53 assert events[sensor], '%s sensor has no events!' % sensor 54 ts_sensor_first[sensor] = events[sensor][0]['time'] 55 ts_sensor_last[sensor] = events[sensor][-1]['time'] 56 57 # Get the timestamp of another image. 58 cap = cam.do_capture(req, fmt) 59 ts_image1 = cap['metadata']['android.sensor.timestamp'] 60 61 print 'Image timestamps:', ts_image0, ts_image1 62 63 # The motion timestamps must be between the two image timestamps. 64 for sensor, existing in sensors.iteritems(): 65 if existing and sensor != 'vibrator': 66 print '%s timestamps: %d %d' % (sensor, ts_sensor_first[sensor], 67 ts_sensor_last[sensor]) 68 assert ts_image0 < ts_sensor_first[sensor] < ts_image1 69 assert ts_image0 < ts_sensor_last[sensor] < ts_image1 70 71if __name__ == '__main__': 72 main() 73 74