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