Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Introducton to Python breadboarding - Receive Blinks
- #
- #from SetPin import SetPin
- import time
- from MorseCode import MorseCode
- import dataLink
- def receiveblinks(RXpin,blinks=200,duration=.25):
- # Set up waittime until first high is read.
- # Assemble a list of high and low voltage readings
- while True:
- if RXpin.read_pin():
- print('\n1')
- receiveMorse(RXpin)
- else:
- print("*",end="",flush=True)
- time.sleep(duration)
- def receiveMorse(RXpin):
- freq_reading = 1 # per s
- voltage_readings = [1] # Read input from pin before calling this function
- time.sleep(freq_reading)
- num_zeros = 0
- while num_zeros < 8:
- reading = RXpin.read_pin()
- print(reading)
- voltage_readings.append(1 if reading else 0)
- num_zeros = 0 if reading else num_zeros + 1
- time.sleep(freq_reading)
- dataLink.dataLink(convert_readings(voltage_readings))
- def convert_readings(readings):
- # Convert into tuples in the form (voltage, time)
- converted = []
- num_zeros = 0 # consecutive
- num_ones = 0
- idx = 0
- while idx < len(readings):
- voltage = readings[idx]
- if voltage:
- if num_zeros > 0:
- converted.append(0, num_zeros)
- num_zeros = 0
- num_ones += 1
- else:
- if num_ones > 0:
- converted.append(1, num_ones)
- num_ones = 0
- num_zeros += 1
- idx += 1
- return converted
- def decode_morse(readings):
- # Readings is list of voltages taken 1s apart
- # Assuming time unit is 1s
- # Decode readings into dots, dashes, and spaces
- morse_letter = "" # current letter that is being formed
- decoded = ""
- morse_letter = ''
- num_zeros = 0 # consecutive
- num_ones = 0
- idx = 0
- while idx < len(readings):
- voltage = readings[idx]
- if voltage:
- if num_zeros > 0:
- if num_zeros == 7:
- decoded += ' '
- num_zeros = 0
- num_ones += 1
- else:
- if num_ones > 0:
- # Figure out whether on voltage was a dot or dash
- morse_letter += '.' if num_ones == 1 else '-'
- num_ones = 0
- num_zeros += 1
- if num_zeros == 3:
- # Decode letter
- decoded += get_letter(morse_letter)
- morse_letter = ''
- idx += 1
- return decoded
- def get_letter(morse):
- for l, m in MorseCode.items():
- if m == morse:
- return l
- # If didn't find a match for the morse, something went wrong...
- return '*'
- if __name__ == "__main__":
- with SetPin(16,"GPIO_23",direction="RX") as RXpin:
- receiveblinks(RXpin)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement