From 12f2a49427e6342459065410d0fef154abe04a37 Mon Sep 17 00:00:00 2001 From: Arnaud Taffanel Date: Tue, 6 Oct 2020 15:16:16 +0200 Subject: [PATCH] #118: Close radio when no not used anymore --- cflib/crtp/radiodriver.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/cflib/crtp/radiodriver.py b/cflib/crtp/radiodriver.py index 220acadfa..fc1683588 100644 --- a/cflib/crtp/radiodriver.py +++ b/cflib/crtp/radiodriver.py @@ -145,6 +145,7 @@ class _SharedRadio(Thread): def __init__(self, devid: int): Thread.__init__(self) self._radio = Crazyradio(devid=devid) + self._devid = devid self.version = self._radio.version self._cmd_queue = Queue() # type: Queue[Tuple[int, _RadioCommands, Any]] # noqa @@ -172,7 +173,11 @@ def run(self): if command[1] == _RadioCommands.STOP: with self._lock: - pass + del self._rsp_queues[command[0]] + if len(self._rsp_queues) == 0: + self._radio.close() + _RadioManager.remove(self._devid) + return elif command[1] == _RadioCommands.SEND_PACKET: channel, address, datarate, data = command[2] self._radio.set_channel(channel) @@ -214,6 +219,11 @@ def open(devid: int) -> _SharedRadioInstance: return shared_radio.open_instance() + @staticmethod + def remove(devid: int): + with _RadioManager._lock: + _RadioManager._radios[devid] = None + class RadioDriver(CRTPDriver): """ Crazyradio link driver """