lora.callback with arguments
-
Summary:
-
Does the Lora callback accept arguments?
-
Is this alternate solution problematic or present other considerations?
I can not seem to successfully pass arguments as the documentation suggests is possible.
Documentation (link):lora.callback(trigger, [handler=None, arg=None])
Documentation Example (without args):
def lora_cb(lora): ... lora.callback(trigger=(LoRa.RX_PACKET_EVENT | LoRa.TX_PACKET_EVENT), handler=lora_cb)
Tried:
def lora_cb(lora, arg): ... lora.callback(trigger=(LoRa.RX_PACKET_EVENT | LoRa.TX_PACKET_EVENT), handler=lora_cb, arg=tx) TypeError: function takes 2 positional arguments but 1 were given
Does lora.callback accept arguments?
After failing, I stumbled across a workaround for a callback that doesn't accept arguments: solution.TX_CNT = 0 RX_CNT = 0 def lora_cb(lora, TX, RX): events = lora.events() if events & LoRa.RX_PACKET_EVENT: print('recv {}'.format(RX)) if events & LoRa.TX_PACKET_EVENT: print('sent {}'.format(TX)) callback_lambda = lambda x: lora_cb(x, TX_CNT, RX_CNT) lora.callback(trigger=(LoRa.RX_PACKET_EVENT | LoRa.TX_PACKET_EVENT), handler=callback_lambda)
Implementing a callback of a callback does seem to work, but I'm confused as to the implications of this solution.
For example, what if I wanted to return something back up the call chain.
Can someone clarify or point me to some info that I can read up on what is going on?
-
-
Update: eventually using globals as jcaron suggested was the solution (by-passing the need for argument passing).
-
@jcaron Thanks for the reply. Maybe it is scoping, I'll try some more examples and post back.
I'm trying to have a main app loop that is not Lora-related but can respond to Lora events (by send/rcv data).
-
@James-Hofer strangely enough, if you pass a value as
arg
, it will become the only argument passed to the callback, instead of theLoRa
object, rather than in addition to it.But it’s unclear to me what exactly you are trying the achieve. I think you just have a scope issue, maybe simply specifying the variables you need to access as global, or using an enclosing scope, would do what you want?