mirror of
https://github.com/DJ2LS/FreeDATA
synced 2024-05-14 08:04:33 +00:00
first working TNC
This commit is contained in:
parent
42c5ee5665
commit
0a655d5b5e
1 changed files with 45 additions and 43 deletions
78
modem.py
78
modem.py
|
@ -159,10 +159,19 @@ class RF():
|
||||||
while len(self.streambuffer) > 0:
|
while len(self.streambuffer) > 0:
|
||||||
time.sleep(0.01)
|
time.sleep(0.01)
|
||||||
if len(self.streambuffer) > 0:
|
if len(self.streambuffer) > 0:
|
||||||
# print(self.streambuffer)
|
|
||||||
self.audio_writing_to_stream = True
|
self.audio_writing_to_stream = True
|
||||||
audio = audioop.ratecv(self.streambuffer,2,1,static.MODEM_SAMPLE_RATE, static.AUDIO_SAMPLE_RATE_TX, None)
|
print("es geht los...")
|
||||||
self.stream_tx.write(audio[0])
|
|
||||||
|
#audio = audioop.ratecv(self.streambuffer,2,1,static.MODEM_SAMPLE_RATE, static.AUDIO_SAMPLE_RATE_TX, None)
|
||||||
|
#self.stream_tx.write(audio[0])
|
||||||
|
|
||||||
|
self.streambuffer = bytes(self.streambuffer)
|
||||||
|
#print(type(self.streambuffer))
|
||||||
|
#print(self.streambuffer)
|
||||||
|
|
||||||
|
# we need t wait a little bit until the buffer is filled. If we are not waiting, we are sending empty data
|
||||||
|
time.sleep(0.1)
|
||||||
|
self.stream_tx.write(self.streambuffer)
|
||||||
self.streambuffer = bytes()
|
self.streambuffer = bytes()
|
||||||
#static.CHANNEL_STATE = state_before_transmit
|
#static.CHANNEL_STATE = state_before_transmit
|
||||||
self.audio_writing_to_stream = False
|
self.audio_writing_to_stream = False
|
||||||
|
@ -205,6 +214,9 @@ class RF():
|
||||||
self.streambuffer += bytes(mod_out)
|
self.streambuffer += bytes(mod_out)
|
||||||
self.streambuffer += bytes(mod_out_postamble)
|
self.streambuffer += bytes(mod_out_postamble)
|
||||||
|
|
||||||
|
converted_audio = audioop.ratecv(self.streambuffer,2,1,static.MODEM_SAMPLE_RATE, static.AUDIO_SAMPLE_RATE_TX, None)
|
||||||
|
self.streambuffer = bytes(converted_audio[0])
|
||||||
|
|
||||||
# -------------- transmit audio
|
# -------------- transmit audio
|
||||||
#logging.debug("SENDING SIGNALLING FRAME " + str(data_out))
|
#logging.debug("SENDING SIGNALLING FRAME " + str(data_out))
|
||||||
|
|
||||||
|
@ -234,7 +246,6 @@ class RF():
|
||||||
static.ARQ_DATA_CHANNEL_LAST_RECEIVED = int(time.time()) # we need to update our timeout timestamp
|
static.ARQ_DATA_CHANNEL_LAST_RECEIVED = int(time.time()) # we need to update our timeout timestamp
|
||||||
static.ARQ_START_OF_BURST = int(time.time()) # we need to update our timeout timestamp
|
static.ARQ_START_OF_BURST = int(time.time()) # we need to update our timeout timestamp
|
||||||
|
|
||||||
|
|
||||||
self.my_rig.set_ptt(self.hamlib_ptt_type, 1)
|
self.my_rig.set_ptt(self.hamlib_ptt_type, 1)
|
||||||
|
|
||||||
state_before_transmit = static.CHANNEL_STATE
|
state_before_transmit = static.CHANNEL_STATE
|
||||||
|
@ -260,15 +271,15 @@ class RF():
|
||||||
mod_out_postamble = ctypes.c_short * n_tx_postamble_modem_samples # *2 #1760 for mode 10,11,12 #4000 for mode 9
|
mod_out_postamble = ctypes.c_short * n_tx_postamble_modem_samples # *2 #1760 for mode 10,11,12 #4000 for mode 9
|
||||||
mod_out_postamble = mod_out_postamble()
|
mod_out_postamble = mod_out_postamble()
|
||||||
|
|
||||||
|
|
||||||
|
self.streambuffer = bytearray()
|
||||||
|
|
||||||
self.c_lib.freedv_rawdatapreambletx(freedv, mod_out_preamble)
|
self.c_lib.freedv_rawdatapreambletx(freedv, mod_out_preamble)
|
||||||
|
self.streambuffer += bytes(mod_out_preamble)
|
||||||
|
|
||||||
txbuffer = bytearray()
|
if not static.ARQ_RPT_RECEIVED:
|
||||||
txbuffer += bytes(mod_out_preamble)
|
|
||||||
|
|
||||||
|
|
||||||
if static.ARQ_RPT_RECEIVED == False:
|
|
||||||
for n in range(0, static.ARQ_TX_N_FRAMES_PER_BURST):
|
for n in range(0, static.ARQ_TX_N_FRAMES_PER_BURST):
|
||||||
|
|
||||||
# ---------------------------BUILD ARQ BURST ---------------------------------------------------------------------
|
# ---------------------------BUILD ARQ BURST ---------------------------------------------------------------------
|
||||||
frame_type = 10 + n + 1 # static.ARQ_TX_N_FRAMES_PER_BURST
|
frame_type = 10 + n + 1 # static.ARQ_TX_N_FRAMES_PER_BURST
|
||||||
frame_type = bytes([frame_type])
|
frame_type = bytes([frame_type])
|
||||||
|
@ -299,17 +310,13 @@ class RF():
|
||||||
data = (ctypes.c_ubyte * static.FREEDV_DATA_BYTES_PER_FRAME).from_buffer_copy(buffer)
|
data = (ctypes.c_ubyte * static.FREEDV_DATA_BYTES_PER_FRAME).from_buffer_copy(buffer)
|
||||||
|
|
||||||
self.c_lib.freedv_rawdatatx(freedv, mod_out, data) # modulate DATA and safe it into mod_out pointer
|
self.c_lib.freedv_rawdatatx(freedv, mod_out, data) # modulate DATA and safe it into mod_out pointer
|
||||||
self.c_lib.freedv_rawdatapostambletx(freedv, mod_out_postamble)
|
self.streambuffer += bytes(mod_out)
|
||||||
|
|
||||||
txbuffer += bytes(mod_out_preamble)
|
|
||||||
txbuffer += bytes(mod_out)
|
|
||||||
txbuffer += bytes(mod_out_postamble)
|
|
||||||
|
|
||||||
elif static.ARQ_RPT_RECEIVED == True:
|
elif static.ARQ_RPT_RECEIVED:
|
||||||
|
|
||||||
for n in range(0, len(static.ARQ_RPT_FRAMES)):
|
for n in range(0, len(static.ARQ_RPT_FRAMES)):
|
||||||
|
|
||||||
|
|
||||||
missing_frame = int.from_bytes(static.ARQ_RPT_FRAMES[n], "big")
|
missing_frame = int.from_bytes(static.ARQ_RPT_FRAMES[n], "big")
|
||||||
|
|
||||||
# ---------------------------BUILD ARQ BURST ---------------------------------------------------------------------
|
# ---------------------------BUILD ARQ BURST ---------------------------------------------------------------------
|
||||||
|
@ -333,7 +340,6 @@ class RF():
|
||||||
static.MYCALLSIGN_CRC8 + \
|
static.MYCALLSIGN_CRC8 + \
|
||||||
payload_data
|
payload_data
|
||||||
|
|
||||||
|
|
||||||
buffer = bytearray(static.FREEDV_DATA_PAYLOAD_PER_FRAME) # create TX buffer
|
buffer = bytearray(static.FREEDV_DATA_PAYLOAD_PER_FRAME) # create TX buffer
|
||||||
buffer[:len(arqframe)] = arqframe # set buffersize to length of data which will be send
|
buffer[:len(arqframe)] = arqframe # set buffersize to length of data which will be send
|
||||||
|
|
||||||
|
@ -344,30 +350,28 @@ class RF():
|
||||||
data = (ctypes.c_ubyte * static.FREEDV_DATA_BYTES_PER_FRAME).from_buffer_copy(buffer)
|
data = (ctypes.c_ubyte * static.FREEDV_DATA_BYTES_PER_FRAME).from_buffer_copy(buffer)
|
||||||
|
|
||||||
self.c_lib.freedv_rawdatatx(freedv, mod_out, data) # modulate DATA and safe it into mod_out pointer
|
self.c_lib.freedv_rawdatatx(freedv, mod_out, data) # modulate DATA and safe it into mod_out pointer
|
||||||
|
self.streambuffer += bytes(mod_out)
|
||||||
|
|
||||||
|
|
||||||
self.c_lib.freedv_rawdatapostambletx(freedv, mod_out_postamble)
|
self.c_lib.freedv_rawdatapostambletx(freedv, mod_out_postamble)
|
||||||
|
self.streambuffer += bytes(mod_out_postamble)
|
||||||
|
|
||||||
txbuffer += bytes(mod_out)
|
converted_audio = audioop.ratecv(self.streambuffer,2,1,static.MODEM_SAMPLE_RATE, static.AUDIO_SAMPLE_RATE_TX, None)
|
||||||
#txbuffer += bytes(mod_out_postamble)
|
self.streambuffer = bytes(converted_audio[0])
|
||||||
|
|
||||||
|
|
||||||
|
print(len(self.streambuffer))
|
||||||
|
|
||||||
# -------------- transmit audio
|
# -------------- transmit audio
|
||||||
txbuffer += bytes(mod_out_postamble)
|
|
||||||
|
|
||||||
|
|
||||||
# self.stream_tx.write(bytes(txbuffer))
|
|
||||||
self.streambuffer = bytes()
|
|
||||||
self.streambuffer = bytes(txbuffer)
|
|
||||||
|
|
||||||
|
|
||||||
self.ptt_and_wait(True)
|
self.ptt_and_wait(True)
|
||||||
self.audio_writing_to_stream = True
|
self.audio_writing_to_stream = True
|
||||||
|
|
||||||
|
|
||||||
# wait until audio has been processed
|
# wait until audio has been processed
|
||||||
while self.audio_writing_to_stream == True:
|
while self.audio_writing_to_stream:
|
||||||
time.sleep(0.01)
|
time.sleep(0.01)
|
||||||
static.CHANNEL_STATE = 'SENDING_DATA'
|
static.CHANNEL_STATE = 'SENDING_DATA'
|
||||||
#print("sending data...")
|
|
||||||
|
|
||||||
static.CHANNEL_STATE = 'RECEIVING_SIGNALLING'
|
static.CHANNEL_STATE = 'RECEIVING_SIGNALLING'
|
||||||
|
|
||||||
|
@ -395,7 +399,8 @@ class RF():
|
||||||
|
|
||||||
self.c_lib.freedv_set_frames_per_burst(freedv, 0)
|
self.c_lib.freedv_set_frames_per_burst(freedv, 0)
|
||||||
else:
|
else:
|
||||||
pass
|
#pass
|
||||||
|
self.c_lib.freedv_set_frames_per_burst(freedv, 0)
|
||||||
|
|
||||||
bytes_out = (ctypes.c_ubyte * bytes_per_frame)
|
bytes_out = (ctypes.c_ubyte * bytes_per_frame)
|
||||||
bytes_out = bytes_out() # get pointer to bytes_out
|
bytes_out = bytes_out() # get pointer to bytes_out
|
||||||
|
@ -444,18 +449,15 @@ class RF():
|
||||||
|
|
||||||
static.AUDIO_RMS = audioop.rms(data_in, 2)
|
static.AUDIO_RMS = audioop.rms(data_in, 2)
|
||||||
nbytes = self.c_lib.freedv_rawdatarx(freedv, bytes_out, data_in) # demodulate audio
|
nbytes = self.c_lib.freedv_rawdatarx(freedv, bytes_out, data_in) # demodulate audio
|
||||||
# logging.debug(self.c_lib.freedv_get_rx_status(freedv))
|
|
||||||
print("listening-" + str(mode) + " - " + "nin: " + str(nin) + " - " + str(self.c_lib.freedv_get_rx_status(freedv)))
|
print("listening-" + str(mode) + " - " + "nin: " + str(nin) + " - " + str(self.c_lib.freedv_get_rx_status(freedv)))
|
||||||
##print(static.CHANNEL_STATE)
|
|
||||||
|
|
||||||
# -------------STUCK IN SYNC DETECTOR
|
# -------------STUCK IN SYNC DETECTOR
|
||||||
stuck_in_sync_counter += 1
|
stuck_in_sync_counter += 1
|
||||||
if self.c_lib.freedv_get_rx_status(freedv) == 10:
|
if self.c_lib.freedv_get_rx_status(freedv) == 10:
|
||||||
stuck_in_sync_10_counter += 1
|
stuck_in_sync_10_counter += 1
|
||||||
if mode != 14:
|
if mode != 14:
|
||||||
self.c_lib.freedv_set_sync(freedv, 0)
|
#self.c_lib.freedv_set_sync(freedv, 0)
|
||||||
|
|
||||||
|
|
||||||
logging.warning("MODEM | SYNC 10 TRIGGER | M:" + str(mode) + " | " + str(static.CHANNEL_STATE))
|
logging.warning("MODEM | SYNC 10 TRIGGER | M:" + str(mode) + " | " + str(static.CHANNEL_STATE))
|
||||||
|
|
||||||
if stuck_in_sync_counter == 33 and self.c_lib.freedv_get_rx_status(freedv) == 10:
|
if stuck_in_sync_counter == 33 and self.c_lib.freedv_get_rx_status(freedv) == 10:
|
||||||
|
@ -476,6 +478,7 @@ class RF():
|
||||||
# bytes_out[1:2] == callsign check for signalling frames, bytes_out[6:7] == callsign check for data frames, bytes_out[1:2] == b'\x01' --> broadcasts like CQ
|
# bytes_out[1:2] == callsign check for signalling frames, bytes_out[6:7] == callsign check for data frames, bytes_out[1:2] == b'\x01' --> broadcasts like CQ
|
||||||
# we could also create an own function, which returns True. In this case we could add callsign blacklists and so on
|
# we could also create an own function, which returns True. In this case we could add callsign blacklists and so on
|
||||||
if nbytes == bytes_per_frame and bytes(bytes_out[1:2]) == static.MYCALLSIGN_CRC8 or bytes(bytes_out[6:7]) == static.MYCALLSIGN_CRC8 or bytes(bytes_out[1:2]) == b'\x01':
|
if nbytes == bytes_per_frame and bytes(bytes_out[1:2]) == static.MYCALLSIGN_CRC8 or bytes(bytes_out[6:7]) == static.MYCALLSIGN_CRC8 or bytes(bytes_out[1:2]) == b'\x01':
|
||||||
|
|
||||||
self.calculate_ber(freedv)
|
self.calculate_ber(freedv)
|
||||||
self.calculate_snr(freedv)
|
self.calculate_snr(freedv)
|
||||||
|
|
||||||
|
@ -595,10 +598,9 @@ class RF():
|
||||||
bytes_out = (ctypes.c_ubyte * bytes_per_frame)
|
bytes_out = (ctypes.c_ubyte * bytes_per_frame)
|
||||||
bytes_out = bytes_out() # get pointer to bytes_out
|
bytes_out = bytes_out() # get pointer to bytes_out
|
||||||
|
|
||||||
if mode == 14:
|
#if mode == 14:
|
||||||
print("mmmmmh")
|
# bytes_out = (ctypes.c_ubyte * bytes_per_frame)
|
||||||
bytes_out = (ctypes.c_ubyte * bytes_per_frame)
|
# bytes_out = bytes_out() # get pointer to bytes_out
|
||||||
bytes_out = bytes_out() # get pointer to bytes_out
|
|
||||||
|
|
||||||
# self.c_lib.freedv_set_sync(freedv, 0)
|
# self.c_lib.freedv_set_sync(freedv, 0)
|
||||||
# self.stream_rx.read(static.AUDIO_FRAMES_PER_BUFFER, exception_on_overflow=False)
|
# self.stream_rx.read(static.AUDIO_FRAMES_PER_BUFFER, exception_on_overflow=False)
|
||||||
|
|
Loading…
Reference in a new issue