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 its.device
16import its.objects
17import its.caps
18import time
19
20def 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        req, fmt = its.objects.get_fastest_manual_capture_settings(props)
32        cap = cam.do_capture(req, fmt)
33        ts_image0 = cap['metadata']['android.sensor.timestamp']
34
35        # Get the timestamps of motion events.
36        print "Reading sensor measurements"
37        cam.start_sensor_events()
38        time.sleep(0.5)
39        events = cam.get_sensor_events()
40        assert(len(events["gyro"]) > 0)
41        assert(len(events["accel"]) > 0)
42        assert(len(events["mag"]) > 0)
43        ts_gyro0 = events["gyro"][0]["time"]
44        ts_gyro1 = events["gyro"][-1]["time"]
45        ts_accel0 = events["accel"][0]["time"]
46        ts_accel1 = events["accel"][-1]["time"]
47        ts_mag0 = events["mag"][0]["time"]
48        ts_mag1 = events["mag"][-1]["time"]
49
50        # Get the timestamp of another image.
51        cap = cam.do_capture(req, fmt)
52        ts_image1 = cap['metadata']['android.sensor.timestamp']
53
54        print "Image timestamps:", ts_image0, ts_image1
55        print "Gyro timestamps:", ts_gyro0, ts_gyro1
56        print "Accel timestamps:", ts_accel0, ts_accel1
57        print "Mag timestamps:", ts_mag0, ts_mag1
58
59        # The motion timestamps must be between the two image timestamps.
60        assert ts_image0 < min(ts_gyro0, ts_accel0, ts_mag0) < ts_image1
61        assert ts_image0 < max(ts_gyro1, ts_accel1, ts_mag1) < ts_image1
62
63if __name__ == '__main__':
64    main()
65
66