87f6da3fa281bca14ef8fb11d6485c9340f92c12
[SaveMySugar/python3-savemysugar.git] / src / measure_ring_distance.py
1 #!/usr/bin/env python3
2 #
3 # measure_ring_distance - measure the time between two rings in the same call
4 #
5 # Copyright (C) 2015  Antonio Ospite <ao2@ao2.it>
6 #
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
20 from savemysugar.cumulative_average import cumulative_average
21 from savemysugar.Modem import Modem
22 import time
23
24
25 class Rings(object):
26     previous_time = -1
27     average_distance = 0
28     min_distance = 10000
29     max_distance = -1
30     count = 0
31
32
33 def on_ring():
34     Rings.count += 1
35
36     if Rings.previous_time == -1:
37         print("First ring")
38         Rings.previous_time = time.time()
39     else:
40         new_time = time.time()
41         distance = new_time - Rings.previous_time
42         Rings.previous_time = new_time
43         Rings.min_distance = min(Rings.min_distance, distance)
44         Rings.max_distance = max(Rings.max_distance, distance)
45         Rings.average_distance = cumulative_average(Rings.average_distance,
46                                                     Rings.count - 1, distance)
47         print()
48         print("Other ring")
49         print("distance: %f" % distance)
50         print("min_distance: %f" % Rings.min_distance)
51         print("max_distance: %f" % Rings.max_distance)
52         print("average_distance: %f" % Rings.average_distance)
53         uncertainty = (Rings.max_distance - Rings.min_distance) / 2.
54         print("uncertainty: %f" % uncertainty)
55
56
57 def measure_ring_distance(ingoing_port, outgoing_port, destination_number):
58     ingoing_modem = Modem(ingoing_port)
59     ingoing_modem.register_callback("RING", on_ring)
60     outgoing_modem = Modem(outgoing_port)
61
62     outgoing_modem.send_command("ATDT" + destination_number)
63
64     try:
65         ingoing_modem.get_response_loop()
66     except KeyboardInterrupt:
67         outgoing_modem.send_command("ATH")
68         outgoing_modem.get_response()
69
70
71 def main():
72     import sys
73     if len(sys.argv) != 4:
74         print("usage: %s" % sys.argv[0],
75               "<ingoing serial port> <outgoing serial port>",
76               "<destination number>")
77         sys.exit(1)
78
79     measure_ring_distance(sys.argv[1], sys.argv[2], sys.argv[3])
80
81
82 if __name__ == "__main__":
83     main()