From 0dbe03669d2ef3ccc4a644603232cd750a7b5e60 Mon Sep 17 00:00:00 2001 From: DJ2LS <75909252+DJ2LS@users.noreply.github.com> Date: Thu, 21 Jan 2021 08:33:45 +0100 Subject: [PATCH] added ARQ ACK TX function --- arq.py | 2 +- modem.py | 56 ++++++++++++++++++++++++++++++++++++++++++++----------- static.py | 2 +- 3 files changed, 47 insertions(+), 13 deletions(-) diff --git a/arq.py b/arq.py index 430f570a..01a3ba22 100644 --- a/arq.py +++ b/arq.py @@ -71,7 +71,7 @@ def data_received(data_in): #TRANSMIT ACK FRAME ----------------------------------------------- logging.info("TX | SENDING ARQ BURST ACK [" + str(data_in[1:3]) +"]") - modem.transmit(7,ack_buffer) + modem.transmit_arq_ack(ack_buffer) print(ack_buffer) # ------------------------------------------------------------ diff --git a/modem.py b/modem.py index eb905d0c..be116cc4 100644 --- a/modem.py +++ b/modem.py @@ -94,12 +94,7 @@ class RF(): crc = ctypes.c_ushort(self.c_lib.freedv_gen_crc16(bytes(buffer), payload_per_frame)) # generate CRC16 crc = crc.value.to_bytes(2, byteorder='big') # convert crc to 2 byte hex string buffer += crc # append crc16 to buffer - - - - - - + data = (ctypes.c_ubyte * bytes_per_frame).from_buffer_copy(buffer) self.c_lib.freedv_rawdatapreambletx(freedv, mod_out_preamble) self.c_lib.freedv_rawdatatx(freedv,mod_out,data) # modulate DATA and safe it into mod_out pointer @@ -117,12 +112,52 @@ class RF(): self.stream_tx.write(audio[0]) +#-------------------------------------------------------------------------------------------------------- + def transmit_arq_ack(self,ack_buffer): + self.c_lib.freedv_open.restype = ctypes.POINTER(ctypes.c_ubyte) + freedv = self.c_lib.freedv_open(static.FREEDV_SIGNALLING_MODE) + bytes_per_frame = int(self.c_lib.freedv_get_bits_per_modem_frame(freedv)/8) + payload_per_frame = bytes_per_frame -2 + n_nom_modem_samples = self.c_lib.freedv_get_n_nom_modem_samples(freedv) + n_tx_modem_samples = self.c_lib.freedv_get_n_tx_modem_samples(freedv)*2 #get n_tx_modem_samples which defines the size of the modulation object + + mod_out = ctypes.c_short * n_tx_modem_samples + mod_out = mod_out() + #mod_out_preamble = ctypes.c_short * n_tx_modem_samples #1760 for mode 10,11,12 #4000 for mode 9 + #mod_out_preamble = mod_out_preamble() + + buffer = bytearray(payload_per_frame) # use this if CRC16 checksum is required ( DATA1-3) + buffer[:len(ack_buffer)] = ack_buffer # set buffersize to length of data which will be send + + crc = ctypes.c_ushort(self.c_lib.freedv_gen_crc16(bytes(buffer), payload_per_frame)) # generate CRC16 + crc = crc.value.to_bytes(2, byteorder='big') # convert crc to 2 byte hex string + buffer += crc # append crc16 to buffer + print(bytes(buffer)) + data = (ctypes.c_ubyte * bytes_per_frame).from_buffer_copy(buffer) + #self.c_lib.freedv_rawdatapreambletx(freedv, mod_out_preamble) + self.c_lib.freedv_rawdatatx(freedv,mod_out,data) # modulate DATA and safe it into mod_out pointer + + # -------------- preamble area + # WE NEED TO ADJUST IT FOR SINGLE TRANSMISSION + if static.FREEDV_SIGNALLING_MODE == 7: + modulation = bytes(mod_out) + txbuffer = modulation[:len(modulation)] + modulation # double transmission in one audio burst + else: + txbuffer = bytearray() + #txbuffer += bytes(mod_out_preamble) + txbuffer += bytes(mod_out) + + + # -------------- audio sample rate conversion + audio = audioop.ratecv(txbuffer,2,1,static.MODEM_SAMPLE_RATE, static.AUDIO_SAMPLE_RATE_TX, static.TX_SAMPLE_STATE) + + # -------------- transmit audio + self.stream_tx.write(audio[0]) + #-------------------------------------------------------------------------------------------------------- # GET ARQ BURST FRAME VOM BUFFER AND MODULATE IT def transmit_arq_burst(self): - static.ARQ_STATE = 'SENDING_DATA' - - + static.ARQ_STATE = 'SENDING_ACK' self.c_lib.freedv_open.restype = ctypes.POINTER(ctypes.c_ubyte) freedv = self.c_lib.freedv_open(static.FREEDV_DATA_MODE) @@ -175,7 +210,7 @@ class RF(): self.stream_tx.write(audio[0]) static.ARQ_STATE = 'IDLE' - +#-------------------------------------------------------------------------------------------------------- def receive(self,data_mode,signalling_mode): print("RECEIVING FOR DATA MODE: " + str(data_mode)) @@ -227,7 +262,6 @@ class RF(): print("MODE: " + str(data_mode) + " DATA: " + str(bytes(data_bytes_out))) - while static.ARQ_STATE == 'IDLE' or static.ARQ_STATE == 'RECEIVING_ACK': time.sleep(0.01) diff --git a/static.py b/static.py index a586d0b0..9f8a76f6 100644 --- a/static.py +++ b/static.py @@ -10,7 +10,7 @@ Created on Wed Dec 23 11:13:57 2020 # FreeDV Defaults FREEDV_DATA_MODE = 12 -FREEDV_SIGNALLING_MODE = 12 +FREEDV_SIGNALLING_MODE = 7 FREEDV_DATA_BYTES_PER_FRAME = 0 FREEDV_DATA_PAYLOAD_PER_FRAME = 0