symbols_distances.gnuplot: use more informative titles
[SaveMySugar/python3-savemysugar.git] / src / savemysugar / CallDistanceTransceiver.py
index 1e378d2..ecc0992 100755 (executable)
@@ -17,6 +17,8 @@
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
+import logging
+import time
 
 # This hack allows importing local modules also when
 # __name__ == "__main__"
@@ -27,9 +29,6 @@ except SystemError:
     from MorseDistanceModulator import MorseDistanceModulator
     from MorseTranslator import MorseTranslator
 
-import logging
-import time
-
 
 def log_symbol(distance, symbol, extra_info=""):
     logging.info("distance: %.2f Received \"%s\"%s", distance, symbol,
@@ -60,7 +59,7 @@ class CallDistanceTransceiver(object):
     """
 
     def __init__(self, modem,
-                 call_setup_time_min=7, call_setup_time_max=15,
+                 call_setup_time_min=7, call_setup_time_max=16.5,
                  ring_time_min=4.8, ring_time_max=5.2,
                  add_inter_call_distance=True):
         """Encode the Morse symbols using the distance between calls.
@@ -99,7 +98,7 @@ class CallDistanceTransceiver(object):
             # ring time makes them happy.
             inter_symbol_distance = ring_time_max
         else:
-            inter_symbol_distance = 0
+            inter_symbol_distance = 0.0
 
         self.modulator = MorseDistanceModulator(call_setup_time_min,
                                                 call_setup_time_max,
@@ -232,11 +231,25 @@ class CallDistanceTransceiver(object):
         # Dial, then wait self.call_setup_time_max to make sure the receiver
         # gets at least one RING, and then hangup and sleep the time needed to
         # transmit a symbol.
+        time_before = time.time()
         self.modem.send_command("ATDT" + destination_number + ";")
         time.sleep(self.call_setup_time_max)
         self.modem.send_command("ATH")
         self.modem.get_response()
-        time.sleep(sleep_time)
+        time_after = time.time()
+
+        # Account for possible delays in order to be as adherent as
+        # possible to the nominal total symbol transmission distance.
+        delay = (time_after - time_before) - self.call_setup_time_max
+        logging.debug("Delay %.2f", delay)
+
+        remaining_sleep_time = sleep_time - delay
+        if remaining_sleep_time < 0:
+            remaining_sleep_time = 0
+
+        logging.debug("Should sleep %.2f. Will sleep %.2f", sleep_time,
+                      remaining_sleep_time)
+        time.sleep(remaining_sleep_time)
 
     def transmit(self, message, destination_number):
         morse_message = self.translator.text_to_morse(message)
@@ -283,7 +296,12 @@ def test_transmit_receive():
         def __init__(self):
             self.ring_count = 0
 
-            # Take trasmission times from a transceiver
+            # A transceiver will be used to get the symbol distance to fake in
+            # get_response, but it will only be assigned _after_ DummyModem has
+            # been instantiated.
+            #
+            # This placeholder here avoids an attribute-defined-outside-init
+            # warning from pylint.
             self.transceiver = None
 
             random.seed(None)