104 lines
4.1 KiB
Python
104 lines
4.1 KiB
Python
#CW Decoder for Raspberry Pi Pico
|
|
#Written using MicroPython
|
|
#Written by Stuart Griffiths M0SUD in 2021
|
|
#Status: Initial start to determine whether dits and dahs can be determined
|
|
|
|
#Physical connections:
|
|
#GP26 to mono audio jack hi
|
|
#0V to mono audio jack GND
|
|
|
|
#Setup functions
|
|
import machine #to read pins
|
|
import utime #to add delays
|
|
|
|
#Program to Run
|
|
decoder()
|
|
|
|
#Objects
|
|
|
|
#Functions
|
|
def decoder():
|
|
#Overall program doe decoding
|
|
#set ADC pin
|
|
adc_pin = 0 #set to pin GP26
|
|
adc = machine.ADC(adc_pin)
|
|
#set squelch tolerance
|
|
tolerance = float(5)
|
|
#Do a reading
|
|
squelch = noise(adc) #establish squelch level
|
|
#check for data to read
|
|
while True: #loop forever checking for a high signal.
|
|
data_value_to_check = adc.read_u16() #read data
|
|
if data_value_to_check >= (squelch + tolerance): #are we beating squelch?
|
|
#go do some readings
|
|
data_to_decode = data_in(adc, squelch, tolerance)
|
|
decoded_data = decode (data_to_decode)
|
|
#no need for else as this will loop ADC reading to a check
|
|
|
|
def noise(adc):
|
|
#Establish a squelch point to ensure noise does not give false positives
|
|
#determine squelch point
|
|
noise1 = adc.read_u16()
|
|
utime.sleep_ms(250) #sleep for 250 ms, selected at random
|
|
noise2 = adc.read_u16()
|
|
utime.sleep_ms(250) #sleep for 250 ms, selected at random
|
|
noise3 = adc.read_u16()
|
|
utime.sleep_ms(250) #sleep for 250 ms, selected at random
|
|
noise4 = adc.read_u16()
|
|
#determine average noise level
|
|
noise_sum = noise1 + noise2 + noise3 + noise4
|
|
squelch_level = noise_sum/4 #average determined
|
|
return squelch_level
|
|
|
|
def data_in(adc, squelch, tolerance):
|
|
#Activated when squelch is passed
|
|
#Read data in, keep reading until hit a 0 for a specific period of time
|
|
#Remember, already measured a single 'high'
|
|
data_value_to_check = adc.read_u16() #read data
|
|
if data_value_to_check < (squelch + tolerance): #are we beating squelch?
|
|
#we have a glitch
|
|
break #go back to the hunt for a signal
|
|
#set up a method of recording number of samples while HIGH
|
|
high_in = "HH" #records the 2 highs which got us into the function and through glitch test
|
|
#need to create a string of highs until a low
|
|
#then add string to a list
|
|
#if the low is longer than a string of highs then suggests new character
|
|
charactor = [] #define the list
|
|
while i < 100: #arbitary number for long low. i = number of lows in a string?
|
|
#need to keep looping until transmission is over
|
|
data_value_to_check = adc.read_u16()
|
|
if data_value_to_check >= (squelch + tolerance): #Is it high?
|
|
high_in = high_in + "H" #add a high to the list
|
|
low_in = "H"
|
|
elif data_value_to_check < (squelch + tolerance) and low_in[0] == "H":
|
|
#If it is the first low then write string high_in to list charactor
|
|
charactor.append(high_in)
|
|
low_in = "L" #write first L to ensure this loop is avoided for next low
|
|
high_in = "" #reset the high in string
|
|
i= i+1 #for the loop exit
|
|
else:
|
|
#add another low to the string
|
|
low_in = low_in + "L"
|
|
#already reset the high in the first low after a high
|
|
#Now we have recorded a message. Time to decode!
|
|
return (charactor)
|
|
|
|
def decode(data_to_decode):
|
|
#Parse through a list, detemining dits, dahs and new words
|
|
#go through the list to find lengths of highs and lows
|
|
for i in range(len(data_to_decode)):
|
|
data_check = data_to_decode[i] #read the list item
|
|
if data_check[0] = 'H':
|
|
#if high, replace the list value with length of H.
|
|
data_to_decode[i] = "?H" + len(data_check)
|
|
elif data_check[0] = 'L':
|
|
#if low, replace the list value with length of L.
|
|
data_to_decode[i] = "?L" + len(data_check)
|
|
else:
|
|
break #break as there is an error in the list
|
|
#now we know the lengths of the high and low signals, time to decode
|
|
#pass through again to establish limits
|
|
#how to calculate a dit and a dah - comparative with some elemement
|
|
#of change allowable for straight keys
|
|
return (words)
|