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 givenDoes 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? -
-
@James-Hofer strangely enough, if you pass a value as
arg, it will become the only argument passed to the callback, instead of theLoRaobject, 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?
-
@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).
-
Update: eventually using globals as jcaron suggested was the solution (by-passing the need for argument passing).
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login