Mbed Host Tests
wait_us_auto.py
Go to the documentation of this file.
1"""
2mbed SDK
3Copyright (c) 2011-2013 ARM Limited
4
5Licensed under the Apache License, Version 2.0 (the "License");
6you may not use this file except in compliance with the License.
7You may obtain a copy of the License at
8
9 http://www.apache.org/licenses/LICENSE-2.0
10
11Unless required by applicable law or agreed to in writing, software
12distributed under the License is distributed on an "AS IS" BASIS,
13WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14See the License for the specific language governing permissions and
15limitations under the License.
16"""
17
18from time import time
19from mbed_host_tests import BaseHostTest
20
21
22class WaitusTest(BaseHostTest):
23 """ This test is reading single characters from stdio
24 and measures time between their occurrences.
25 """
26 __result = None
27 DEVIATION = 0.10 # +/-10%
28 ticks = []
29
30 def _callback_exit(self, key, value, timeout):
31 self.notify_complete()
32
33 def _callback_tick(self, key, value, timestamp):
34 """ {{tick;%d}}} """
35 self.log("tick! " + str(timestamp))
36 self.ticks.append((key, value, timestamp))
37
38 def setup(self):
39 self.register_callback('exit', self._callback_exit_callback_exit)
40 self.register_callback('tick', self._callback_tick_callback_tick)
41
42 def result(self):
43 def sub_timestamps(t1, t2):
44 delta = t1 - t2
45 deviation = abs(delta - 1.0)
46 #return True if delta > 0 and deviation <= self.DEVIATION else False
47 return deviation <= self.DEVIATION
48
49 # Check if time between ticks was accurate
50 if self.ticks:
51 # If any ticks were recorded
52 timestamps = [timestamp for _, _, timestamp in self.ticks]
53 self.log(str(timestamps))
54 m = map(sub_timestamps, timestamps[1:], timestamps[:-1])
55 self.log(str(m))
56 self.__result = all(m)
57 else:
58 self.__result = False
59 return self.__result
60
61 def teardown(self):
62 pass