Advertisement
ksoltan

morseReceiver.py

Feb 22nd, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.41 KB | None | 0 0
  1. # Introducton to Python breadboarding - Receive Blinks
  2. #
  3. #from SetPin import SetPin
  4. import time
  5. from MorseCode import MorseCode
  6. import dataLink
  7. def receiveblinks(RXpin,blinks=200,duration=.25):
  8.     # Set up waittime until first high is read.
  9.     # Assemble a list of high and low voltage readings
  10.     while True:
  11.         if RXpin.read_pin():
  12.             print('\n1')
  13.             receiveMorse(RXpin)
  14.         else:
  15.             print("*",end="",flush=True)
  16.             time.sleep(duration)
  17.  
  18. def receiveMorse(RXpin):
  19.     freq_reading = 1 # per s
  20.     voltage_readings = [1] # Read input from pin before calling this function
  21.     time.sleep(freq_reading)
  22.     num_zeros = 0
  23.     while num_zeros < 8:
  24.         reading = RXpin.read_pin()
  25.         print(reading)
  26.         voltage_readings.append(1 if reading else 0)
  27.         num_zeros = 0 if reading else num_zeros + 1
  28.         time.sleep(freq_reading)
  29.     dataLink.dataLink(convert_readings(voltage_readings))
  30.  
  31. def convert_readings(readings):
  32.     # Convert into tuples in the form (voltage, time)
  33.     converted = []
  34.     num_zeros = 0 # consecutive
  35.     num_ones = 0
  36.     idx = 0
  37.     while idx < len(readings):
  38.         voltage = readings[idx]
  39.         if voltage:
  40.             if num_zeros > 0:
  41.                 converted.append(0, num_zeros)
  42.                 num_zeros = 0
  43.             num_ones += 1
  44.         else:
  45.             if num_ones > 0:
  46.                 converted.append(1, num_ones)
  47.                 num_ones = 0
  48.            
  49.             num_zeros += 1
  50.         idx += 1
  51.     return converted
  52.  
  53. def decode_morse(readings):
  54.     # Readings is list of voltages taken 1s apart
  55.     # Assuming time unit is 1s
  56.     # Decode readings into dots, dashes, and spaces
  57.     morse_letter = "" # current letter that is being formed
  58.     decoded = ""
  59.     morse_letter = ''
  60.     num_zeros = 0 # consecutive
  61.     num_ones = 0
  62.     idx = 0
  63.     while idx < len(readings):
  64.         voltage = readings[idx]
  65.         if voltage:
  66.             if num_zeros > 0:
  67.                 if num_zeros == 7:
  68.                     decoded += ' '
  69.                 num_zeros = 0
  70.             num_ones += 1
  71.         else:
  72.             if num_ones > 0:
  73.             # Figure out whether on voltage was a dot or dash
  74.                 morse_letter += '.' if num_ones == 1 else '-'
  75.                 num_ones = 0
  76.            
  77.             num_zeros += 1
  78.             if num_zeros == 3:
  79.                 # Decode letter
  80.                 decoded += get_letter(morse_letter)
  81.                 morse_letter = ''
  82.  
  83.         idx += 1
  84.     return decoded
  85.  
  86. def get_letter(morse):
  87.     for l, m in MorseCode.items():
  88.         if m == morse:
  89.             return l
  90.     # If didn't find a match for the morse, something went wrong...
  91.     return '*'
  92.  
  93. if __name__ == "__main__":
  94.     with SetPin(16,"GPIO_23",direction="RX") as RXpin:
  95.         receiveblinks(RXpin)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement