and another global has been eliminated..

This commit is contained in:
DJ2LS 2023-11-17 10:22:45 +01:00
parent ee709b32ef
commit 4e14eeecb7
3 changed files with 55 additions and 59 deletions

View file

@ -491,19 +491,14 @@ class DATA:
:param repeat_delay: Delay time before sending repeat frame, defaults to 0
:type repeat_delay: int, optional
"""
#print(frame_to_tx[0])
#print(frame_to_tx)
frame_type = FR_TYPE(int.from_bytes(frame_to_tx[0][:1], byteorder="big")).name
self.log.debug("[Modem] enqueue_frame_for_tx", c2_mode=FREEDV_MODE(c2_mode).name, data=frame_to_tx,
type=frame_type)
# Set the TRANSMITTING flag before adding an object to the transmit queue
# TODO This is not that nice, we could improve this somehow
Modem.transmitting = True
modem.MODEM_TRANSMIT_QUEUE.put([c2_mode, copies, repeat_delay, frame_to_tx])
# Wait while transmitting
while Modem.transmitting:
while self.states.is_transmitting:
threading.Event().wait(0.01)
def send_data_to_socket_queue(self, **jsondata):
@ -3342,7 +3337,7 @@ class DATA:
waiting_time = (self.time_list[self.speed_level] * frames_left) + self.duration_sig0_frame + self.channel_busy_timeout + 1
timeout_percent = 100 - (time_left / waiting_time * 100)
#timeout_percent = 0
if timeout_percent >= 75 and not self.states.is_codec2_traffic and not Modem.transmitting:
if timeout_percent >= 75 and not self.states.is_codec2_traffic and not self.states.is_transmitting:
override = True
else:
override = False
@ -3353,7 +3348,7 @@ class DATA:
# better wait some more time because data might be important for us
# reason for this situation can be delays on IRS and ISS, maybe because both had a busy channel condition.
# Nevertheless, we need to keep timeouts short for efficiency
if timeout <= time.time() or modem_error_state and not self.states.is_codec2_traffic and not Modem.transmitting or override:
if timeout <= time.time() or modem_error_state and not self.states.is_codec2_traffic and not self.states.is_transmitting or override:
self.log.warning(
"[Modem] Burst decoding error or timeout",
attempt=self.n_retries_per_burst,
@ -3551,7 +3546,7 @@ class DATA:
# send burst only if channel not busy - but without waiting
# otherwise burst will be dropped
if not ModemParam.channel_busy and not Modem.transmitting:
if not ModemParam.channel_busy and not self.states.is_transmitting:
self.enqueue_frame_for_tx(
frame_to_tx=[fec_frame], c2_mode=codec2.FREEDV_MODE["sig0"].value
)

View file

@ -20,7 +20,7 @@ import codec2
import itertools
import numpy as np
import sounddevice as sd
from global_instances import HamlibParam, Modem
from global_instances import HamlibParam
from static import FRAME_TYPE
import structlog
import tci
@ -34,8 +34,6 @@ TESTMODE = False
RXCHANNEL = ""
TXCHANNEL = ""
Modem.transmitting = False
# Receive only specific modes to reduce CPU load
RECEIVE_SIG0 = True
RECEIVE_SIG1 = False
@ -83,6 +81,10 @@ class RF:
self.tx_delay = config['MODEM']['tx_delay']
self.tuning_range_fmin = config['MODEM']['tuning_range_fmin']
self.tuning_range_fmax = config['MODEM']['tuning_range_fmax']
self.enable_
self.states.set("is_transmitting", False)
self.tci_ip = config['TCI']['tci_ip']
self.tci_port = config['TCI']['tci_port']
@ -389,7 +391,7 @@ class RF:
# Avoid decoding when transmitting to reduce CPU
# TODO Overriding this for testing purposes
# if not Modem.transmitting:
# if not self.states.is_transmitting:
length_x = len(x)
# Avoid buffer overflow by filling only if buffer for
# selected datachannel mode is not full
@ -407,7 +409,7 @@ class RF:
self.event_manager.send_buffer_overflow(self.buffer_overflow_counter)
elif receive:
audiobuffer.push(x)
# end of "not Modem.transmitting" if block
# end of "not self.states.is_transmitting" if block
if not self.modoutqueue or self.mod_out_locked:
data_out48k = np.zeros(frames, dtype=np.int16)
@ -465,7 +467,7 @@ class RF:
else:
return False
Modem.transmitting = True
self.states.set("is_transmitting", True)
# if we're transmitting FreeDATA signals, reset channel busy state
self.states.set("channel_busy", False)
@ -624,7 +626,7 @@ class RF:
self.mod_out_locked = True
self.modem_transmit_queue.task_done()
Modem.transmitting = False
self.states.set("is_transmitting", False)
end_of_transmission = time.time()
transmission_time = end_of_transmission - start_of_transmission
@ -660,7 +662,7 @@ class RF:
alc_level=str(HamlibParam.alc))
def transmit_morse(self, repeats, repeat_delay, frames):
Modem.transmitting = True
self.states.set("is_transmitting", True)
# if we're transmitting FreeDATA signals, reset channel busy state
self.states.set("channel_busy", False)
self.log.debug(
@ -707,7 +709,7 @@ class RF:
self.mod_out_locked = True
self.modem_transmit_queue.task_done()
Modem.transmitting = False
self.states.set("is_transmitting", False)
threading.Event().set()
end_of_transmission = time.time()
@ -850,43 +852,36 @@ class RF:
if nbytes == bytes_per_frame:
print(bytes(bytes_out))
# process commands only if Modem.listen = True
if Modem.listen:
# ignore data channel opener frames for avoiding toggle states
# use case: opener already received, but ack got lost and we are receiving
# an opener again
if mode_name in ["sig1-datac13"] and int.from_bytes(bytes(bytes_out[:1]), "big") in [
FRAME_TYPE.ARQ_SESSION_OPEN.value,
FRAME_TYPE.ARQ_DC_OPEN_W.value,
FRAME_TYPE.ARQ_DC_OPEN_ACK_W.value,
FRAME_TYPE.ARQ_DC_OPEN_N.value,
FRAME_TYPE.ARQ_DC_OPEN_ACK_N.value
]:
print("dropp")
elif int.from_bytes(bytes(bytes_out[:1]), "big") in [
FRAME_TYPE.MESH_BROADCAST.value,
FRAME_TYPE.MESH_SIGNALLING_PING.value,
FRAME_TYPE.MESH_SIGNALLING_PING_ACK.value,
]:
self.log.debug(
"[MDM] [demod_audio] moving data to mesh dispatcher", nbytes=nbytes
)
MESH_RECEIVED_QUEUE.put(bytes(bytes_out))
else:
self.log.debug(
"[MDM] [demod_audio] Pushing received data to received_queue", nbytes=nbytes
)
snr = self.calculate_snr(freedv)
self.modem_received_queue.put([bytes_out, freedv, bytes_per_frame, snr])
self.get_scatter(freedv)
state_buffer = []
else:
self.log.warning(
"[MDM] [demod_audio] received frame but ignored processing",
listen=Modem.listen
# ignore data channel opener frames for avoiding toggle states
# use case: opener already received, but ack got lost and we are receiving
# an opener again
if mode_name in ["sig1-datac13"] and int.from_bytes(bytes(bytes_out[:1]), "big") in [
FRAME_TYPE.ARQ_SESSION_OPEN.value,
FRAME_TYPE.ARQ_DC_OPEN_W.value,
FRAME_TYPE.ARQ_DC_OPEN_ACK_W.value,
FRAME_TYPE.ARQ_DC_OPEN_N.value,
FRAME_TYPE.ARQ_DC_OPEN_ACK_N.value
]:
print("dropp")
elif int.from_bytes(bytes(bytes_out[:1]), "big") in [
FRAME_TYPE.MESH_BROADCAST.value,
FRAME_TYPE.MESH_SIGNALLING_PING.value,
FRAME_TYPE.MESH_SIGNALLING_PING_ACK.value,
]:
self.log.debug(
"[MDM] [demod_audio] moving data to mesh dispatcher", nbytes=nbytes
)
MESH_RECEIVED_QUEUE.put(bytes(bytes_out))
else:
self.log.debug(
"[MDM] [demod_audio] Pushing received data to received_queue", nbytes=nbytes
)
snr = self.calculate_snr(freedv)
self.modem_received_queue.put([bytes_out, freedv, bytes_per_frame, snr])
self.get_scatter(freedv)
state_buffer = []
except Exception as e:
self.log.warning("[MDM] [demod_audio] Stream not active anymore", e=e)
@ -1309,7 +1304,7 @@ class RF:
threading.Event().wait(0.1)
HamlibParam.hamlib_status = self.radio.get_status()
threading.Event().wait(0.1)
if Modem.transmitting:
if self.states.is_transmitting:
HamlibParam.alc = self.radio.get_alc()
threading.Event().wait(0.1)
# HamlibParam.hamlib_rf = self.radio.get_level()
@ -1351,7 +1346,7 @@ class RF:
# Therefore we are setting it to 100 so it will be highlighted
# Have to do this when we are not transmitting so our
# own sending data will not affect this too much
if not Modem.transmitting:
if not self.states.is_transmitting:
dfft[dfft > avg + 15] = 100
# Calculate audio dbfs
@ -1407,12 +1402,11 @@ class RF:
range_start = range[0]
range_end = range[1]
# define the area, we are detecting busy state
#dfft = dfft[120:176] if Modem.low_bandwidth_mode else dfft[65:231]
slotdfft = dfft[range_start:range_end]
# Check for signals higher than average by checking for "100"
# If we have a signal, increment our channel_busy delay counter
# so we have a smoother state toggle
if np.sum(slotdfft[slotdfft > avg + 15]) >= 200 and not Modem.transmitting:
if np.sum(slotdfft[slotdfft > avg + 15]) >= 200 and not self.states.is_transmitting:
addDelay=True
self.states.channel_busy_slot[slot] = True
else:

View file

@ -2,8 +2,15 @@ import time
import ujson as json
class STATES:
def __init__(self, statequeue):
# state related settings
self.statequeue = statequeue
self.newstate = None
self.last = time.time()
# modem related states
# not every state is needed to publish, yet
# TODO can we reduce them?
self.channel_busy = False
self.channel_busy_slot = [False, False, False, False, False]
self.is_codec2_traffic = False
@ -11,9 +18,9 @@ class STATES:
self.is_beacon_running = False
self.is_arq_state = False
self.is_arq_session = False
self.is_transmitting = False
self.arq_session_state = 'disconnected'
self.audio_dbfs = 0
self.last = time.time()
def set(self, key, value):
setattr(self, key, value)