Welcome to the Pycom forum Forum
    • \[\[global:header.categories\]\]
    • \[\[global:header.recent\]\]
    • \[\[global:header.tags\]\]
    • \[\[global:header.popular\]\]
    • Login

    Explore Pybytes | Official Documentation | Report a Firmware Bug/Issue | GitHub

    lora.callback with arguments

    Scheduled Pinned Locked Moved LoPy
    4 Posts 2 Posters 55 Views 2 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • J Offline
      James Hofer
      last edited by

      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?

      J 1 Reply Last reply Reply Quote 0
      • J Offline
        jcaron @James Hofer
        last edited by

        @James-Hofer strangely enough, if you pass a value as arg, it will become the only argument passed to the callback, instead of the LoRa 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?

        1 Reply Last reply Reply Quote 0
        • J Offline
          James Hofer
          last edited by

          @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).

          1 Reply Last reply Reply Quote 0
          • J Offline
            James Hofer
            last edited by

            Update: eventually using globals as jcaron suggested was the solution (by-passing the need for argument passing).

            1 Reply Last reply Reply Quote 1

            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
            • 1 / 1
            • First post
              Last post

            Pycom on Twitter

            Pycom