Skip to content

Commit

Permalink
#118: Close radio when no not used anymore
Browse files Browse the repository at this point in the history
  • Loading branch information
ataffanel committed Oct 6, 2020
1 parent 57ea1a5 commit 12f2a49
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion cflib/crtp/radiodriver.py
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 """
Expand Down

0 comments on commit 12f2a49

Please sign in to comment.